Problem with Structures using FieldOffset -- BUG?

  • Thread starter Danny Mavromatis
  • Start date
D

Danny Mavromatis

I'm trying to set up a structure using unions (fieldoffset) and I'm running
into a problem. When I try to setup a union of data array or
message type, I get the following error:

System.TypeLoadException: Could not load type 'Test.Message' from assembly
'Test, Version=1.0.2228.19435, Culture=neutral, PublicKeyToken=null' because
it contains an object field at offset 1 that is incorrectly aligned or
overlapped by a non-object field.

WHY?

<StructLayout(LayoutKind.Explicit)> _
Public Structure R_OMNI_LINK_MESSAGE
<FieldOffset(0)> Public MessageLength As Byte
' ....... THIS ONE DOESN'T WORK!!.... Throws Error
<FieldOffset(1), MarshalAs(UnmanagedType.ByValArray, SizeConst:=255)> Public
Data() As Byte
'...............................................................................
<FieldOffset(1)> Public MessageType As Byte
<FieldOffset(2)> Public NAME_DATA As olmNAME_DATA ' 8bit / 16bit
<FieldOffset(2)> Public EVENT_LOG_DATA As olmEVENT_LOG_DATA
<FieldOffset(2)> Public COMMAND_MESSAGE As olmCOMMAND_MESSAGE
<FieldOffset(2)> Public SET_TIME As olmSET_TIME
<FieldOffset(2)> Public SYSTEM_STATUS As olmSYSTEM_STATUS
<FieldOffset(2)> Public REQUEST_ZONE_STATUS As olmREQUEST_ZONE_STATUS
<FieldOffset(2)> Public REQUEST_UNIT_STATUS As olmREQUEST_UNIT_STATUS '8bit
/ 16bit
<FieldOffset(2)> Public REQUEST_AUXILIARY_STATUS As
olmREQUEST_AUXILIARY_STATUS
<FieldOffset(2)> Public REQUEST_THERMOSTAT_STATUS As
olmREQUEST_THERMOSTAT_STATUS
<FieldOffset(2)> Public LOGIN As olmLOGIN
<FieldOffset(2)> Public REQUEST_SECURITY_CODE_VALIDATION As
olmREQUEST_SECURITY_CODE_VALIDATION
<FieldOffset(2)> Public SECURITY_CODE_VALIDATION As
olmSECURITY_CODE_VALIDATION
End Structure
 
D

Danny Mavromatis

Quick update... If I change the StructLayout to
<StructLayout(LayoutKind.Auto)>, the code compiles without errors... haven't
check to see if it works as intended... anyone see any problems with using
Auto?

Thanks,
Danny
 
D

Danny Mavromatis

More updates: Changing it to Auto isn't what I need since the fieldoffsets,
don't seem to be doing anything with Auto.
 
M

Mattias Sjögren

System.TypeLoadException: Could not load type 'Test.Message' from assembly
'Test, Version=1.0.2228.19435, Culture=neutral, PublicKeyToken=null' because
it contains an object field at offset 1 that is incorrectly aligned or
overlapped by a non-object field.

WHY?

The error message pretty much tells you why. You can't have a
reference type (such as a byte array) overlap a value type.


Mattias
 
D

Danny Mavromatis

Yes, I'm trying to do unions, but, it freaks out when I want to have a union
with a fixed amount of data (255 bytes) and the data broken out by
structures.

So, I'd like to be able to to look at the data as the complete byte(255)
array or by name (defined by the structures). It can be done in VC.NET but
not in VB.NET???

The problem is that it thinks it overlapping when I compile, but it
shouldn't care, it's either one view or another (of the same data)... do I
need to use the overload method to achieve this?

Danny
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top