|
-
Jan 16th, 2002, 07:26 PM
#1
Thread Starter
Member
Really need help for copy memory..
I want to move data from a byte array to some kind of udt. I used copy memory to do it. But got some prob, really can't figure out why the result is like this:
Here is the code and the result:
Code:
Private Type MyUdt1
count As Byte 'length of udt
c(4) As Byte
d As Byte
End Type
Private Type MyUdt2
count As Byte 'length of udt
a As Byte
b() As Byte
End Type
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Private Sub Command1_Click()
Dim arr(6) As Byte
Dim udt1 As MyUdt1
Dim udt2 As MyUdt2
arr(0) = 5
arr(1) = 1
arr(2) = 2
arr(3) = 3
arr(4) = 4
arr(5) = 5
CopyMemory udt1, arr(0), 6
Debug.Print udt1.c(2) '=3,ok
Debug.Print udt1.d '=0, why??? (it should be 5)
ReDim udt2.b(0 To 2)
CopyMemory ByVal udt2, arr(0), 6
Debug.Print udt2.a '=1, ok
Debug.Print udt2.b(0) 'script out of range.???
End Sub
please help me out. Your answer would be greatly appreciated.!!!
-
Jan 17th, 2002, 11:09 AM
#2
Thread Starter
Member
-
Jan 17th, 2002, 11:56 AM
#3
Frenzied Member
Re: Really need help for copy memory..
Originally posted by jwm
I want to move data from a byte array to some kind of udt. I used copy memory to do it. But got some prob, really can't figure out why the result is like this:
Code:
CopyMemory udt1, arr(0), 6
Debug.Print udt1.c(2) '=3,ok
Debug.Print udt1.d '=0, why??? (it should be 5)
A byte array is defined as vbArray + vbByte. Therefore, there is an extra byte between c(3) and d.
Code:
ReDim udt2.b(0 To 2)
CopyMemory ByVal udt2, arr(0), 6
Debug.Print udt2.a '=1, ok
Debug.Print udt2.b(0) 'script out of range.???
End Sub
I can't say for sure what is happening here. I never use variable length variables in a UDT. For example, if you declare a string in a UDT, 4 bytes are allocated to hold a pointer to the actual string. If you declare a string as String * 8, 16 bytes are allocated to hold the string.
Here is the output from some testing that I did some time ago. The UDT and its type defines are:
Code:
Private Type dclLogRec
UserId As String * 8
PassWd As String * 8
UserOK As Integer
End Type
Dim Login As dclLogRec ' the IPC structure
The code in the DataArrival sub to get the received data is:
Code:
ReDim bTBuff(bytesTotal)
tcpClient.GetData bTBuff, vbArray + vbByte, bytesTotal
CopyMemory Login, bTBuff(0), Len(Login)
Debug.Print output is:
Code:
Login VarPtr=1689292
Login.Userid VarPtr=1242488, StrPtr=1552148
Login.PassWd VarPtr=1242488, StrPtr=1625324
Login.UserOK VarPtr=1689324
Login Len = 18
Login LenB = 34
99 99 111 100 101 114 32 32 98 121 116 101 109 101 32 32 2 0
The last line of numbers shows the value of each byte in the UDT.
-
Jan 17th, 2002, 12:23 PM
#4
Thread Starter
Member
Thanks lot.
I got some idea.
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
|