Private Type AAABBB aa As Integer bb As String * 10 End Type Private Declare Function ABCD Lib "AAA" (ByVal AAA As AAABBB) As Long Private Sub Form_Load() Dim a As AAABBB MsgBox Len(a) ABCD (a) MsgBox a.bb
End Sub
多谢大侠! 关于指针的,可能是我没说清楚,不好意思。 按您上面的例子。如果api传入的是AAABBB型的指针,在vb中为一个long,比如说是p 即 Private Declare Function ABCD Lib "AAA" (ByVal p As long) As Long 那么我在这个函数中应该如何做就能简单的引用p所指结构中的元素。 由于函数中要反复使用p,即对p所指结构做修改后还要将p作为参数调用其他api,所以,最好不用copymemory的方法。 请指教!
我研究过几个API的调用,比如SendMessage,由于lParam As Any,所以可以指定任何类型 用以上的
1) use len or lenb 2) type AAAA .... end typedim a( 0 to ... ) /接收数组 dim b( 0 to 0 ) as AAAAcopymemory b(0), a(0), len( b(0) )------------所以,最好不用copymemory的方法。是不可能的。
上面是如何把接收到的数据转化为自己的结构,这是读内存的。 读取字符串 Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As _ Any, source As Any, ByVal bytes As Long) Private Declare Function lstrlenA Lib "kernel32" (ByVal lpString As Long) As _ Long Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As _ Long' retrieve a string at a given address ' if LENGTH < 0 the string is considered to be null-terminated ' and the function determines its lengthFunction StringFromAddr(ByVal address As Long, ByVal length As Long, _ Optional ByVal isUnicode As Boolean) As String ' determine the length, if necessary If length < 0 Then If isUnicode Then length = lstrlenW(address) Else length = lstrlenA(address) End If End If ' copy the characters StringFromAddr = Space$(length) If isUnicode Then CopyMemory ByVal StrPtr(StringFromAddr), ByVal address, length * 2 Else CopyMemory ByVal StringFromAddr, ByVal address, length End If End Function-------------------------------------- 读取字节,整数,长整数 Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As _ Any, source As Any, ByVal bytes As Long)' read a byte from memoryFunction PeekB(ByVal address As Long) As Byte CopyMemory PeekB, ByVal address, 1 End Function' read an integer from memoryFunction PeekI(ByVal address As Long) As Integer CopyMemory PeekI, ByVal address, 2 End Function' read a Long value from memoryFunction PeekL(ByVal address As Long) As Long CopyMemory PeekL, ByVal address, 4 End Function
aa As Integer
bb As String * 10
End Type
Private Declare Function ABCD Lib "AAA" (ByVal AAA As AAABBB) As Long
Private Sub Form_Load()
Dim a As AAABBB
MsgBox Len(a)
ABCD (a)
MsgBox a.bb
End Sub
关于指针的,可能是我没说清楚,不好意思。
按您上面的例子。如果api传入的是AAABBB型的指针,在vb中为一个long,比如说是p
即 Private Declare Function ABCD Lib "AAA" (ByVal p As long) As Long
那么我在这个函数中应该如何做就能简单的引用p所指结构中的元素。
由于函数中要反复使用p,即对p所指结构做修改后还要将p作为参数调用其他api,所以,最好不用copymemory的方法。
请指教!
用以上的
2)
type AAAA
....
end typedim a( 0 to ... ) /接收数组
dim b( 0 to 0 ) as AAAAcopymemory b(0), a(0), len( b(0) )------------所以,最好不用copymemory的方法。是不可能的。
读取字符串
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As _
Any, source As Any, ByVal bytes As Long)
Private Declare Function lstrlenA Lib "kernel32" (ByVal lpString As Long) As _
Long
Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As _
Long' retrieve a string at a given address
' if LENGTH < 0 the string is considered to be null-terminated
' and the function determines its lengthFunction StringFromAddr(ByVal address As Long, ByVal length As Long, _
Optional ByVal isUnicode As Boolean) As String
' determine the length, if necessary
If length < 0 Then
If isUnicode Then
length = lstrlenW(address)
Else
length = lstrlenA(address)
End If
End If
' copy the characters
StringFromAddr = Space$(length)
If isUnicode Then
CopyMemory ByVal StrPtr(StringFromAddr), ByVal address, length * 2
Else
CopyMemory ByVal StringFromAddr, ByVal address, length
End If
End Function--------------------------------------
读取字节,整数,长整数
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As _
Any, source As Any, ByVal bytes As Long)' read a byte from memoryFunction PeekB(ByVal address As Long) As Byte
CopyMemory PeekB, ByVal address, 1
End Function' read an integer from memoryFunction PeekI(ByVal address As Long) As Integer
CopyMemory PeekI, ByVal address, 2
End Function' read a Long value from memoryFunction PeekL(ByVal address As Long) As Long
CopyMemory PeekL, ByVal address, 4
End Function