|
-
Jul 5th, 2001, 02:55 AM
#1
Thread Starter
New Member
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
-
Jul 5th, 2001, 03:09 AM
#2
Fanatic Member
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]
-
Jul 8th, 2001, 08:44 AM
#3
Thread Starter
New Member
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
-
Jul 9th, 2001, 04:41 AM
#4
Fanatic Member
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:
'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)
VB Code:
'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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|