Results 1 to 4 of 4

Thread: Retrieving data from a pointer

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2001
    Location
    tehran
    Posts
    4

    Red face Retrieving data from a pointer

    how can I retrieve some data that contains a string from a pointer ?
    I'm using win api in vb , the function returns (long) actually a pointer to some text , how can I get the text in vb ?

    thanks

  2. #2
    Fanatic Member crispin's Avatar
    Join Date
    Aug 2000
    Location
    2 clicks west of a Quirkafleeg...Cornwall, England
    Posts
    754
    Code:
    Option Explicit
    Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long)
    Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
    
    Private Sub Command1_Click()
    Dim s$
    Dim lpStr&
        s = "HELLO WORLD"
        lpStr = StrPtr(s)
        MsgBox StringFromPointer(lpStr)
    End Sub
    
    Private Function StringFromPointer(lpStr As Long) As String
       Dim bBuffer() As Byte
       Dim nLen&
       If lpStr Then
          nLen = lstrlenW(lpStr) * 2
          If nLen Then
             ReDim bBuffer(0 To (nLen - 1)) As Byte
             CopyMem bBuffer(0), ByVal lpStr, nLen
             StringFromPointer = bBuffer
          End If
       End If
    End Function
    Crispin
    VB6 ENT SP5
    VB.NET
    W2K ADV SVR SP3
    WWW.BLOCKSOFT.CO.UK

    [Microsoft Basic: 1976-2001, RIP]

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2001
    Location
    tehran
    Posts
    4

    Smile done

    thanks for the info , that helped alot !
    you know ,how can I manage binary data too ?
    for ex. storing a long integer into byte array,and then get a pointer to the array !
    thanks

  4. #4
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    This will show you how to stick a long into a byte array, and get a long out of it (most of it is just reporting info so you can see how it works, and that it does work):
    VB Code:
    1. 'I prefer declaring the sub like this, since it uses addresses anyway.
    2. 'then I give it strptr(stringvar), varptr(numericvar), or objptr(objectvar).  never seen that last one used before though
    3. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal lpDest As Long, ByVal lpSource As Long, ByVal nBytes As Long)
    VB Code:
    1. 'somewhere in the prog
    2.    Dim lngX As Long, lngArrayPtr As Long, i As Long
    3.    Dim abyteX(0 To 3) As Byte
    4.    'give a value you can see all the bytes of easily
    5.    lngX = &H4030201
    6.    Debug.Print "lngX ="; lngX
    7.    'this returns the start address of the array
    8.    lngArrayPtr = VarPtr(abyteX(0))
    9.    'copy the long var into the array
    10.    CopyMemory lngArrayPtr, VarPtr(lngX), Len(lngX)
    11.    For i = 0 To 3
    12.       'show each byte's value (just a test so you see it works)
    13.       Debug.Print "abytex("; i; ") ="; abyteX(i)
    14.    Next i
    15.    'clear var
    16.    lngX = 0
    17.    Debug.Print "lngX ="; lngX
    18.    'copy array back to var
    19.    CopyMemory VarPtr(lngX), lngArrayPtr, Len(lngX)
    20.    Debug.Print "copied from array lngX ="; lngX
    Last edited by Kaverin; Jul 9th, 2001 at 04:47 AM.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width