打印机dll的VB.NET调用 - VB6的问题ANY [英] VB.NET call of printer dll - Problem with VB6 ANY
问题描述
大家好,
我在VB.NET(VS 2013)中控制ID卡打印机(HiTi CS-200e)时遇到问题。
它带有一个dllPavoApi.dll(似乎是一个非托管的dll)。
参考打印机SDK文档我有使用
Hi everyone,
I'm have problems to control a ID-Card Printer (HiTi CS-200e) in VB.NET (VS 2013).
It comes with a dll "PavoApi.dll" (seems to be an unmanaged dll).
Referring to the printers SDK docs I have to use
DWORD __stdcall PAVO_GetDeviceInfo(char* szPrinter, DWORD dwInfoType, BYTE *lpInfoData, DWORD *lpdwDataLen);
获取不同的返回值。
dwInfoType 确定,在lpInfoData中返回什么信息
1用于序列号(在这种情况下,lpInfoData中的数据类型是'char array')
6打印数量卡片(返回DWORD)
lpdwDataLen返回lpInfoData的大小。
我试图打电话这样的功能
to get different return values.
dwInfoType determines, what information is returned in lpInfoData
1 for serial number (data type in lpInfoData is 'char array' in this case)
6 for number of printed cards (a DWORD is returned)
lpdwDataLen returns the size of lpInfoData.
I tried to call the function this way
<DllImport("PavoApi.dll")> _
Public Shared Function PAVO_GetDeviceInfo(ByVal szPrinter As String, ByVal dwInfoType As Integer, ByRef lpInfoData As IntPtr, ByRef lpdwDataLen As Integer) As Integer
End Function
Public Function GetDeviceInfo(ByVal PrinterName As String) As Integer
Dim resPtr As IntPtr
Dim resLen As Integer
PAVO_GetDeviceInfo(PrinterName, 1, resPtr, resLen)
Return 0 'TODO: return something serious
End Function
VB6样本代码对lpInfoData使用Any。
The VB6 samplecode used "Any" for lpInfoData.
Public Declare Function PAVO_GetDeviceInfo Lib "PavoApi.DLL" (ByVal szPrinter As String, ByVal dwInfoType As Long, lpInfoData As Any, ByRef lpdwDataLen As Long) As Long
我试图将对象用于lpInfoData,但收到了错误。
但我怎么能访问resPtr上的信息?
I tried to use Object for lpInfoData, but recieved errors.
But how can I access the Information at resPtr?
推荐答案
在VB6中,函数以这种方式调用
In VB6 the function is called this way
Private Sub CommandGetDeviceInfo_Click()
Dim dwError As Long
Dim dwInfoType As Long
Dim dwSize As Long
Dim byteBuf(64) As Byte
Dim longBuf(16) As Long
Dim szString As String
Dim dwCardPos As Long
Dim szCardPosDesc As String
Dim szPrinter As String
Select Case g_nConnectType
Case 0
szPrinter = Printer.DeviceName
Case 1
szPrinter = TextIP.Text
End Select
dwInfoType = ComboDeviceInfo.ListIndex
If dwInfoType = 0 Then
dwSize = 64
dwError = PAVO_GetDeviceInfo(szPrinter, PAVO_DEVINFO_MFG_SERIAL, byteBuf(0), dwSize)
szString = StrConv(byteBuf, vbUnicode)
MsgBox "Serial = " & szString, vbOKOnly, "Get Device Info"
ElseIf dwInfoType = 1 Then
dwSize = 64
dwError = PAVO_GetDeviceInfo(szPrinter, PAVO_DEVINFO_MODEL_NAME, byteBuf(0), dwSize)
szString = StrConv(byteBuf, vbUnicode)
MsgBox "Model = " & szString, vbOKOnly, "Get Device Info"
ElseIf dwInfoType = 2 Then
dwSize = 64
dwError = PAVO_GetDeviceInfo(szPrinter, PAVO_DEVINFO_FIRMWARE_VERSION, byteBuf(0), dwSize)
szString = StrConv(byteBuf, vbUnicode)
MsgBox "FW version = " & szString, vbOKOnly, "Get Device Info"
ElseIf dwInfoType = 3 Then
dwSize = 64
dwError = PAVO_GetDeviceInfo(szPrinter, PAVO_DEVINFO_RIBBON_INFO, longBuf(0), dwSize)
MsgBox "Ribbon Type = " & vbLf & vbLf & longBuf(0) & "Remain ribbon = " & longBuf(1), vbOKOnly, "Get Device Info"
ElseIf dwInfoType = 4 Then
dwSize = 64
dwError = PAVO_GetDeviceInfo(szPrinter, PAVO_DEVINFO_PRINT_COUNT, longBuf(0), dwSize)
MsgBox "Printed card = " & longBuf(0), vbOKOnly, "Get Device Info"
ElseIf dwInfoType = 5 Then
dwSize = 4
dwError = PAVO_GetDeviceInfo(szPrinter, PAVO_DEVINFO_CARD_POSITION, dwCardPos, dwSize)
Select Case dwCardPos
Case 0
szCardPosDesc = "Out of printer"
Case 1
szCardPosDesc = "Start printing position"
Case 2
szCardPosDesc = "Mag out position"
Case 3
szCardPosDesc = "Mag in position"
Case 4
szCardPosDesc = "Contact encoder position"
Case 5
szCardPosDesc = "Contactless encoder position"
Case 6
szCardPosDesc = "Flipper position"
Case 7
szCardPosDesc = "Card jam"
Case 8
szCardPosDesc = "Standby position"
End Select
MsgBox "Card position = " & dwCardPos & vbLf & vbLf & szCardPosDesc, vbOKOnly, "Get Device Info"
End If
End Sub
从VB6代码示例中,您看起来有三种不同的返回类型:
- 一个字符串;
- 一个整数;
- 一个整数数组;
From the VB6 code sample, it looks like you have three different return types:
- A string;
- An integer;
- An array of integers;
' Integer:
<DllImport("PavoApi.dll")> _
Public Shared Function PAVO_GetDeviceInfo(ByVal szPrinter As String, ByVal dwInfoType As Integer, ByRef lpInfoData As Integer, ByRef lpdwDataLen As Integer) As Integer
End Function
Public Function GetDeviceInfoAsInteger(ByVal printerName As String, ByVal infoType As Integer) As Integer
Dim resultLength As Integer = 4
Dim result As Integer
Dim returnCode As Integer = PAVO_GetDeviceInfo(printerName, infoType, result, resultLength)
' TODO: Check the return code
Return result
End Function
' String:
<DllImport("PavoApi.dll")> _
Public Shared Function PAVO_GetDeviceInfo(ByVal szPrinter As String, ByVal dwInfoType As Integer, ByVal lpInfoData As StringBuilder, ByRef lpdwDataLen As Integer) As Integer
End Function
Public Function GetDeviceInfoAsString(ByVal printerName As String, ByVal infoType As Integer) As String
Dim resultLength As Integer = 64
Dim result As New StringBuilder(resultLength)
Dim returnCode As Integer = PAVO_GetDeviceInfo(printerName, infoType, result, resultLength)
' TODO: Check the return code
Return result.ToString()
End Function
' Integer array:
<DllImport("PavoApi.dll")> _
Public Shared Function PAVO_GetDeviceInfo(ByVal szPrinter As String, ByVal dwInfoType As Integer, <In, Out> ByVal lpInfoData As Integer(), ByRef lpdwDataLen As Integer) As Integer
End Function
Public Function GetDeviceInfoAsIntegerArray(ByVal printerName As String, ByVal infoType As Integer, ByVal resultLength As Integer) As Integer()
Dim result As Integer() = New Integer(resultLength - 1) {}
Dim returnCode As Integer = PAVO_GetDeviceInfo(printerName, infoType, result, resultLength)
' TODO: Check the return code
Return result
End Function
这篇关于打印机dll的VB.NET调用 - VB6的问题ANY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!