views:

65

answers:

2

In vba, There is an address held by a long type which points to a null-terminated string, but I can't find a way to get the string from this address:

long str_address = ...
string str = ?

Would you please shed some light on this?

+2  A: 

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))
GSerg
+2  A: 

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
wqw