Click to See Complete Forum and Search --> : Retrieving data from a pointer
sepul6006
Jul 5th, 2001, 02:55 AM
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
crispin
Jul 5th, 2001, 03:09 AM
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
sepul6006
Jul 8th, 2001, 08:44 AM
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
Kaverin
Jul 9th, 2001, 04:41 AM
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):
'I prefer declaring the sub like this, since it uses addresses anyway.
'then I give it strptr(stringvar), varptr(numericvar), or objptr(objectvar). never seen that last one used before though
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal lpDest As Long, ByVal lpSource As Long, ByVal nBytes As Long)
'somewhere in the prog
Dim lngX As Long, lngArrayPtr As Long, i As Long
Dim abyteX(0 To 3) As Byte
'give a value you can see all the bytes of easily
lngX = &H4030201
Debug.Print "lngX ="; lngX
'this returns the start address of the array
lngArrayPtr = VarPtr(abyteX(0))
'copy the long var into the array
CopyMemory lngArrayPtr, VarPtr(lngX), Len(lngX)
For i = 0 To 3
'show each byte's value (just a test so you see it works)
Debug.Print "abytex("; i; ") ="; abyteX(i)
Next i
'clear var
lngX = 0
Debug.Print "lngX ="; lngX
'copy array back to var
CopyMemory VarPtr(lngX), lngArrayPtr, Len(lngX)
Debug.Print "copied from array lngX ="; lngX
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.