CopyMemory and UDTs

I am trying to use CopyMemory to copy data from a User-Type into a byte array for transmission using the Winsock Control. Then copy back from the byte array to the Same UserType.

The problem I'm having is my UDT contains a Byte Array too, and on copying the data back into the UDT sets all the other fields correctly but the byte array contains the whole byte array that was received.

Any one any ideas whats going on!

More points available for any good answers.

Cheers
Gordon
LVL 3
GordonpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mcriderCommented:
Post a snippet of your code...

Cheers!
0
GordonpAuthor Commented:
Public Type tMessage
    Addressee As Byte
    Sender As Byte
    MessageLength As Integer
    Data() As Byte
End Type

'Send out Data on Winsock
Public Sub Send(Message As tMessage)
    Dim bydata() As Byte

    ReDim bydata(LenB(Message))
    CopyMemory bydata(0), Message, LenB(Message)
    sckConnect.SendData bydata
End Sub

'Winsock Data Arrival Event
Private Sub sckConnect_DataArrival(ByVal bytesTotal As Long)
   
Dim bydata() As Byte
sckConnect.GetData bydata, vbByte + vbArray, bytesTotal
   
Dim RxMessage As tMessage
CopyMemory RxMessage, bydata(0), LenB(RxMessage)  

'Here Byte array in RxMessage contains all of bydata, but other fields set correctly
       
End Sub


0
charlescopeCommented:
Add DataStartPos to the UDT  then when you are reading the byte array  have the starting position equal to that.

Public Type tMessage
    Addressee As Byte
    Sender As Byte
    MessageLength As Integer
    DataStartPos as Long
    Data() As Byte
End Type
Then when you are using the byte array later in your code do something like this
 For lngCounter = DatastartPos to WhatEver
Mystring = data(lngcounter)
next lngcounter
So the code not great but the Ideal is good. Remember the data() is a array just disregard the bad data. good luck and hope this helps
0
corvanderlindenCommented:
I think your problem is that the Data member of your Type is in fact a pointer to a byte array, and not the byte array itself.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GordonpAuthor Commented:
thanks folks,
points to corvanderlinden.

I'm now copying Data() seperately to the rest of the structure and its working fine.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.