In a previous post, Joacim Andersson advised me to use the CopyMemory API routine to copy a UDT to a ByteArray in order to send the data via WinSock. This works great.

However, it appears that when a UDT is declared as a mixed set of data types, the CopyMemory routine doesn't keep the integrity. For example
Code:
'In Module

Declare Sub CopyMemory _
Lib "kernel32" Alias "RtlMoveMemory" ( _
Destination As Any, _
Source As Any, _
ByVal Length As Long)

Type typeInMsg
    Size as Long
    Function As Byte
    TID As Long
    Seq As Long
    Msg(255) as Byte
End Type

'In form
Dim InMsg as typeInMsg
Dim bArr() as byte
Dim Buf() as byte

'Load up the OutMsg UDT with values.

InMsg.Function = 5
InMsg.TID = 20562
InMsg.Seq = 1
Buf() = StrConv("Test", vbFromUnicode)
For i = 0 To UBound(Buf())
    InMsg.Msg(i) = Buf(i)
Next i

InMsg.Size = Len(InMsg)

'Copy UDT to Byte Array
ReDim Preserve bArr(1 To Len(InMsg)) As Byte    
CopyMemory bArr(1), InMsg, Len(InMsg)
If tcpClient.State = sckConnected Then tcpClient.SendData bArr

'The Byte string output should be
'Size     F TID       Seq
'13 1 0 0 5 82 80 0 0 1 0 0 0 {Start of Msg text}

'However, my Byte Array reads
'Size     Func    TID       Seq
'13 1 0 0 5 0 0 0 82 80 0 0 1 0 0 0 {Start of Msg text}
It appears that CopyMemory has turned my Byte into a 4 byte Long Integer which of course throws out my byte array alignment.

Does anyone know why this happens? If so, what's the way around it?

Appreciate any advice
Regards
Adrian