VB Union with byte array problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

To all,

I’m trying to make a union of an 8 byte array with a set of header values in
order to facilitate the concatenation of the header and byte data into a
packet for transmission, but the structure that I’ve created triggers the
following exception when I try to instantiate it:

System.TypeLoadException was unhandled
Could not load type ‘PacketHeaderStruct’ ... because it contains an
object field at offset 0 that is incorrectly aligned or overlapped by
a non-object field.

The structure:

Const HDR_SIZE as Integer = 8

<StructLayout(LayoutKind.Explicit)> _
Private Structure PacketHeaderStruct
<FieldOffset(0), VBFixedArray(HDR_SIZE-1)> Public hdrBytes() as Byte
<FieldOffset(0)> Public dataType as Short
<FieldOffset(2)> Public version as Byte
<FieldOffset(3)> Public location as Byte
<FieldOffset(4)> Public sequenceNumber as Byte

‘I don’t know if these 3 declarations are necessary…
‘
<FieldOffset(5)> Public reserved1 as Byte
<FieldOffset(6)> Public reserved2 as Byte
<FieldOffset(7)> Public reserved3 as Byte

Public Sub Initialize()
ReDim hdrBytes(HDR_SIZE-1)
End Sub
End Structure

The method that triggers the exception:

Public Function MakePacket( byVal dType as Short, _
byVal version as Byte, byVal location as Byte, _
byRef dataIn() as Byte ) as Byte()

Dim hdr as New PacketHeaderStruct
hdr.Initialize()

hdr.version = version
hdr.location = location
 
Minor correction to MakePacket(): The 2nd Array.Copy statement

Array.Copy(hdr.hdrBytes, 0, packet, HDR_SIZE, dataIn.Length)

should be

Array.Copy(dataIn, 0, packet, HDR_SIZE, dataIn.Length)
 
I noticed that hdrBytes and dataType both have a field offset of 0. That
could be your problem.
 
That is by design. For instance, to concatenate byte values into a short or
extract bytes from a short, I have previously created a structure like

<StructLayout(LayoutKind.Explicit)> _
Private Structure I2Struct
<FieldOffset(0)> Public ival as Short
<FieldOffset(0)> Public b0 as Byte
<FieldOffset(1)> Public b1 as Byte
End Structure

which is equivalent to a union in C or C++. Then, after creating an instance
such as

Dim i2 as New I2Struct

I can set i2.ival and recover its bytes as a=i2.b0 and c=I2.b1

Alternatively, given the two consecutive bytes a & b from some data stream,
I can easily concatenate them into a short by setting i2.b0 = a, i2.b1=b and
recovering i2.ival.

My difficulty is in getting this to work with an array.

Thanks!
 
It was pointed out to me that I was trying to overlap a value type with an
object reference. Oh, well some things were much easier in Visual Studio 6,
and some things were much easier. Enough complaining... here’s the solution
that I came up with using a different mechanism. If anyone has a more elegant
solution, I’d love to see it. My thanks to those who took the time to respond
to my post.

<StructLayout(LayoutKind.Explicit)> _
Private Structure PacketHeaderStruct
<FieldOffset(0)> Public dataType as Short
<FieldOffset(2)> Public version as Byte
<FieldOffset(3)> Public location as Byte
<FieldOffset(4)> Public sequenceNumber as Byte
<FieldOffset(5)> Public reserved1 as Byte
<FieldOffset(6)> Public reserved2 as Byte
<FieldOffset(7)> Public reserved3 as Byte
End Structure

Public Function MakePacket( byVal dType as Short, _
byVal version as Byte, byVal location as Byte, _
byRef dataIn() as Byte ) as Byte()
‘
‘Return packet, or Nothing on error
‘
Dim hdr as New PacketHeaderStruct

hdr.version = version
hdr.location = location
 
Back
Top