Link to home
Start Free TrialLog in
Avatar of Gordonp
GordonpFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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
Avatar of mcrider
mcrider

Post a snippet of your code...

Cheers!
Avatar of Gordonp

ASKER

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


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
ASKER CERTIFIED SOLUTION
Avatar of corvanderlinden
corvanderlinden

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gordonp

ASKER

thanks folks,
points to corvanderlinden.

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