100分,谁能解释一下CopyMemory是怎么识别字节数组指针的?
本帖最后由 liucqa 于 2013-04-08 01:42:54 编辑 我在Excel 2007 VBA里面写如下代码:
Sub TestBSTRRef()
'替换BSTR变量指向字符串的指针,从而修改BSTR变量所指的字符串
Dim b(13) As Byte, vpstr As Long, vpbyte As Long, spstr As Long, str$
b(0) = 8
b(1) = 0
b(2) = 0
b(3) = 0
b(4) = 66
b(5) = 0
b(6) = 66
b(7) = 0
b(8) = 65
b(9) = 0
b(10) = 65
b(11) = 0
b(12) = 0
b(13) = 0
str = "MNP"
CopyMemory ByVal VarPtr(str),VarPtr(b(4)), 4 '把b(4)的地址,拷贝到BSTR变量里面,当作新的指针
MsgBox "长度是:" & Len(str) & vbCrLf & "字符串是" & str '输出长度4,字符串BBAA
End Sub
CopyMemory ByVal VarPtr(str),VarPtr(b(4)), 4
Dim _Temp As Long
_Temp = VarPtr(b(4))
CopyMemory ByVal VarPtr(str),_Temp, 4