By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,556 Members | 1,609 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,556 IT Pros & Developers. It's quick & easy.

Problem with Structures using FieldOffset -- BUG?

P: n/a
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


Feb 9 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

"Danny Mavromatis" <ne**@mavromatis.com> wrote in message
news:11*************@corp.supernews.com...
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

Feb 9 '06 #2

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

"Danny Mavromatis" <ne**@mavromatis.com> wrote in message
news:11*************@corp.supernews.com...
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

"Danny Mavromatis" <ne**@mavromatis.com> wrote in message
news:11*************@corp.supernews.com...
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


Feb 9 '06 #3

P: n/a
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 9 '06 #4

P: n/a
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
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:eX**************@TK2MSFTNGP09.phx.gbl...
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Feb 9 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.