Home All Groups Group Topic Archive Search About

Get a string from a pointer?

Author
17 Oct 2005 1:42 PM
jameshamilton777
If I have a variable lngPointer that contains an address to a string
(using StrPtr), then how can I get back the string from that pointer??

I'm sure the answer is obvious, but it alludes me.

Author
17 Oct 2005 1:47 PM
jameshamilton777
OK, I found a solution on this group:

Hi,
You may use this function to get a string from a long pointer to a null

terminated string.


Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" ( _
    lpString As Any) As Long


Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" ( _
    lpString1 As Any, _
    lpString2 As Any) As Long


Public Function StringFromPointer(ByVal lp As Long) As String
    Dim sz As Long
    if lp <> 0 then
      sz = lstrlen(lp)
      StringFromPointer = String$(sz + 1, Chr$(0))
      lstrcpy ByVal StringFromPointer, ByVal lp
      StringFromPointer = Left$(StringFromPointer, sz)
    end if
End Function


regards,
alejandro lapeyre
Author
17 Oct 2005 2:30 PM
Mike D Sutton
> If I have a variable lngPointer that contains an address to a string
> (using StrPtr), then how can I get back the string from that pointer??
>
> I'm sure the answer is obvious, but it alludes me.

Grab this method which includes the ability to copy ASCII or wide (Unicode)
strings:
http://groups.google.co.uk/group/microsoft.public.vb.winapi/msg/fab77880865c95e5
Hope this helps,

    Mike


- Microsoft Visual Basic MVP -
E-Mail: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Author
17 Oct 2005 3:18 PM
Tony Proctor
....or try this set James which copes with ANSI or Unicode, and counted or
null-terminated formats (i.e. 4 combinations)

Public Declare Function lstrlenA Lib "kernel32" ( _
    ByVal lpString As Long) As Long

Public Declare Function SysAllocStringByteLen Lib "oleaut32" ( _
    ByVal ansistr As Long, ByVal BLen As Long) As String

Public Declare Function SysAllocString Lib "oleaut32" ( _
    ByVal olestr As Long) As Long

Public Declare Function SysAllocStringLen Lib "oleaut32" ( _
    ByVal olestr As Long, ByVal WLen As Long) As Long

Public Function sANSIPtrToVBString(lStrptr As Long, lNChars As Long) As
String
' Convert 'pointer to Ansi string' to a 'VB string value'
    sANSIPtrToVBString = SysAllocStringByteLen(lStrptr, lNChars)
End Function

Public Function sANSIPtrZToVBString(lStrptr As Long) As String
' Convert 'pointer to null-terminated Ansi string' to a 'VB string value'
    sANSIPtrZToVBString = sANSIPtrToVBString(lStrptr, lstrlenA(lStrptr))
End Function

Public Function sUniPtrToVBString(ByVal lStrptr As Long, lNChars As Long) As
String
' Convert 'pointer to Unicode string' to a 'VB string value'
    sUniPtrToVBString = vbNullString
    lStrptr = SysAllocStringLen(lStrptr, lNChars)
    CopyMemory ByVal VarPtr(sUniPtrToVBString), ByVal VarPtr(lStrptr), 4
End Function

Public Function sUniPtrZToVBString(ByVal lStrptr As Long) As String
' Convert 'pointer to (wide-)null-terminated Unicode string' to a 'VB string
value'
    sUniPtrZToVBString = vbNullString
    lStrptr = SysAllocString(lStrptr)
    CopyMemory ByVal VarPtr(sUniPtrZToVBString), ByVal VarPtr(lStrptr), 4
End Function

        Tony Proctor

<jameshamilton***@hotmail.com> wrote in message
Show quoteHide quote
news:1129556533.477863.208120@g44g2000cwa.googlegroups.com...
> If I have a variable lngPointer that contains an address to a string
> (using StrPtr), then how can I get back the string from that pointer??
>
> I'm sure the answer is obvious, but it alludes me.
>
Author
18 Oct 2005 8:43 AM
J French
On 17 Oct 2005 06:42:13 -0700, jameshamilton***@hotmail.com wrote:

>If I have a variable lngPointer that contains an address to a string
>(using StrPtr), then how can I get back the string from that pointer??
>
>I'm sure the answer is obvious, but it alludes me.

If you really have the StrPtr of a VB String then here is a dirty
little trick for getting a copy of the String

Warning: do not move the string although as you will see you can
'modify' it in-place

Private Declare Sub CopyMemory _
        Lib "kernel32" _
        Alias "RtlMoveMemory" _
        (pDst As Any, _
         pSrc As Any, _
         ByVal ByteLen As Long)


Private Sub Command1_Click()
   Dim S$
   S$ = "This Is a Test"
   MsgBox StrFromPtr(StrPtr(S$)) + vbCrLf + S$
End Sub

Private Function StrFromPtr(pStr&) As String
   Dim S$, Nix&
   ' Make VarPtr(S$) contain pStr
   Call CopyMemory(ByVal VarPtr(S$), pStr&, 4)
   StrFromPtr = S$
   Mid$(S$, 1, 1) = "X"   ' modify in-place
   ' Make VarPtr(S$) contain 0&
   Call CopyMemory(ByVal VarPtr(S$), Nix, 4)

End Function