开发者

How to get the string from an address specified by a long in VB

In vba, There is an address held by a long type which points to a null-terminated string, but I can't find a开发者_如何学Python way to get the string from this address:

long str_address = ...
string str = ?

Would you please shed some light on this?


I use CopyMemory this way:

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


If for some reason you ended up with a pointer to an ANSII zero-terminated string in VBA, you need to copy the contents to a string:

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))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜