OpenDevice(byref handle as long ,byref name as string) as integer
Declare Function OpenDevice Lib "xxx.dll" (Byref handle As Long, Byval name As String) As Long 或Declare Function OpenDevice Lib "xxx.dll" (Byref handle As Long, Byref name As Byte) As Long 后者的调用:Dim bytName(255) As Byte, lngHandle As Long, rv As LonglngHandle = 0rv = OpenDevice(lngHandle, bytName(0))
用3楼的第2种声明和用法应该是可以的。原始声明是Unicode类型的函数,所以不要把它声明为String类型,否则VB会自动做UA/AU转换,导致DLL函数工作不正常。VB中的字符串本身就是Unicode编码的,所以你不需要转换,直接赋值给一个Byte数组就可以了。比如: s = "IAMSOSAD" b = s & vbNullChar '加个NULL表示C字符串里的结尾' Call OpenDevice(lngHandle, b(0))
补充:8楼的示例代码,要加上如下声明, Dim s As String Dim b() As Byte
也可以用StrPtr来传地址:用这个声明: Declare Function OpenDevice Lib "xxx.dll" (ByRef handle As Long, ByRef name As Byte) As Long这么调用: Dim s As String
s = "DeviceName" & vbNullChar OpenDevice lngHandle, ByVal StrPtr(s)
int WINAPI OpenDevice(HANDLE *hDevice, WCHAR *name); Private Declare Function OpenDevice Lib "xxx.dll" (ByRef hDevice As Long, ByVal name As Long) As LongPrivate Sub Form_Load() Dim hDevice As Long, name As String, ret As Long name = Space(255) ret = OpenDevice(hDevice, StrPtr(name))name = Left(name, InStr(name, Chr(0)) - 1) Debug.Print name, Len(name) End Sub
10楼的声明应该改成这样的:Declare Function OpenDevice Lib "xxx.dll" (ByRef handle As Long, ByVal lpStrName As Long) As Long
Declare Function OpenDevice Lib "xxx.dll" (Byref handle As Long, Byval name As String) As Long 或Declare Function OpenDevice Lib "xxx.dll" (Byref handle As Long, Byref name As Byte) As Long
后者的调用:Dim bytName(255) As Byte, lngHandle As Long, rv As LonglngHandle = 0rv = OpenDevice(lngHandle, bytName(0))
按你的意思需要再转换成字节数组吗?如果是的话又该怎么转化呢?
s = "IAMSOSAD"
b = s & vbNullChar '加个NULL表示C字符串里的结尾'
Call OpenDevice(lngHandle, b(0))
Dim b() As Byte
Declare Function OpenDevice Lib "xxx.dll" (ByRef handle As Long, ByRef name As Byte) As Long这么调用:
Dim s As String
s = "DeviceName" & vbNullChar
OpenDevice lngHandle, ByVal StrPtr(s)
Dim hDevice As Long, name As String, ret As Long
name = Space(255)
ret = OpenDevice(hDevice, StrPtr(name))name = Left(name, InStr(name, Chr(0)) - 1)
Debug.Print name, Len(name)
End Sub
恩 急得要命啊 没法连续回复了 呵呵这个函数是不是应该传个DeviceName进去,而不是由API函数返回一个DeviceName,所以是不是应该10楼的那种用法更科学一些啊?