如果没有放入函数,CopyMemory会崩溃Excel

时间:2018-05-29 03:52:29

标签: vba copymemory

我在尝试使用CopyMemory时遇到了一个奇怪的问题。我有一段代码,是从Bytecomb复制的,只有在我把它放入函数时才有效:

我把它放在开头:

Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal ByteLen As Long)

工作版:

Sub StringTest()
    Dim str1 As String
    Dim pstr1 As LongPtr

    str1 = "PowerVB"
    Debug.Print "Memory  : 0x"; Mem_ReadHex(pstr1 - 4, LenB(str1) + 6)

End Sub

Public Function Mem_ReadHex(ByVal Ptr As LongPtr, ByVal Length As Long) As String
    Dim bBuffer() As Byte, strBytes() As String, i As Long, ub As Long, b As Byte
    ub = Length - 1
    ReDim bBuffer(ub)
    ReDim strBytes(ub)
    CopyMemory bBuffer(0), ByVal Ptr, Length
    For i = 0 To ub
        b = bBuffer(i)
        strBytes(i) = IIf(b < 16, "0", "") & Hex$(b)
    Next
    Mem_ReadHex = Join(strBytes, "")
End Function

这个程序完全打印出内存中字符串的整个布局(前4个字节表示长度,后跟字符串内容,然后是2个字节的null):

Memory  : 0x0E00000050006F00770065007200560042000000

如果我将函数放入sub:

,它会崩溃
Sub StringTest()
    Dim str1 As String
    Dim str2 As String
    Dim pstr1 As LongPtr

    str1 = "PowerVB"
    CopyMemory pstr1, ByVal VarPtr(str1), 8

    Dim bBuffer() As Byte, strBytes() As String
    ub = LenB(str1) + 5
    ReDim bBuffer(ub)
    ReDim strBytes(ub)
    CopyMemory bBuffer(0), ByVal pstr1 - 4, LenB(str1) + 6 'extra 4 bytes for string length, and 2 bytes of null characters
    For i = 0 To ub
        b = bBuffer(i)
        strBytes(i) = IIf(b < 16, "0", "") & Hex$(b) 'for 2 bytes, if the value < 16, then it only consists of one byte
    Next i
    Debug.Print Join(strBytes, "")
End Sub

我不明白。这两个版本的区别是什么?

1 个答案:

答案 0 :(得分:1)

好的,我找到了修复:

CopyMemory bBuffer(0), ByVal pstr1 - 4, ub + 1

因为CopyMemory必须以long作为第三个参数,所以我不能使用LenB(str1) + 6,因为它可能是一个整数。