如何从VB中的long指定的地址中获取字符串

时间:2010-10-13 08:18:57

标签: vb.net vba vb6 vbscript

在vba中,有一个long类型的地址,它指向一个以null结尾的字符串,但我找不到从这个地址获取字符串的方法:

long str_address = ...
string str = ?

请您详细说明一下吗?

2 个答案:

答案 0 :(得分:3)

如果由于某种原因你最终在VBA中找到了指向ANSII零终止字符串的指针,则需要将内容复制到字符串中:

Private Declare Function SysAllocStringByteLen Lib "oleaut32.dll" (ByVal m_pBase As Long, ByVal l As Long) As String
Private Declare Function lstrlen Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As Long) As Long

...
dim s as string    
s = SysAllocStringByteLen(str_address, lstrlen(str_address))

答案 1 :(得分:3)

我以这种方式使用CopyMemory

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function lstrlenA Lib "kernel32" (ByVal lpString As Long) As Long

Private Function pvToString(ByVal lPtr As Long) As String
    If lPtr <> 0 Then
        pvToString = String(lstrlenA(lPtr), 0)
        Call CopyMemory(ByVal pvToString, ByVal lPtr, Len(pvToString))
    End If
End Function