Wednesday, December 17, 2008

[vb6] Get hold of string pointers

Some API functions return pointers to strings (char*) allocated by that function. This is all very kind, except that you cannot do anything with that pointer with plain Basic. Use the lstrcpyn and lstrlen API functions to copy a string and to learn the string length, respectively. The pCharToString function shown below wraps the two string functions. For example, suppose an API function has returned you a pointer to a string. You can then call the pCharToString function with the string pointer as the parameter. The return is the copied string.
    Private Declare Function lstrcpyn& Lib "kernel32" Alias _
"lstrcpynA" (ByVal Recipient As String, ByVal pSourceString _
As Long, ByVal iMaxLength As Long)
Private Declare Function lstrlen& Lib "kernel32" Alias _
"lstrlenA" (ByVal lpString As Long)

Public Function pCharToString(ByVal Address As Long) As String
Dim r As Long
r = lstrlen(Address)
pCharToString = String(r, Chr(32))
r = lstrcpyn(pCharToString, Address, r + 1)
End Function

No comments:

Post a Comment