Re: unmanaged structure layout error

Mike Andrews <[email protected]> Wed, 11 Nov 2009 13:54:34 -0600
Newsgroups gmane.comp.windows.devel.dotnet.advanced
Message-ID <[email protected]>
I just tried something different and it seemed to work.

I remove the LayoutKind.Explicit and specified LayoutKind.Sequential.  I
then removed the FieldOffset attribute and that seemd to fix the problem.

I would still like to know why having explicit field offsets causes an
exception when the type is loaded.  It really doesn't make any sense to me.

On Wed, Nov 11, 2009 at 1:46 PM, Mike Andrews
<[email protected]>wrote:

> Oh, ok.  I get it.
>
> I kinda thought that might be it as well except that in .NET there is no
> specific size for a string.  That's what I thought the MarshalAs attribute
> was for; that I'm saying that the field starts and byte index 64 and
> continues for 70 bytes.  That puts it at byte 134 for the next offest.  The
> previous offset is 64 - 133.  I've tried specifying 133 and 135 or the
> offset for the _address1 but nothing seems to work.
>
>   On Wed, Nov 11, 2009 at 1:38 PM, John Warner <[email protected]> wrote:
>
>> Please don't mistake my question for knowledge! Just given the error
>> message it seems to me the problem is in the last three fields and I
>> suspect really the last two. String_name must be going further than offset
>> 134.
>>
>> But again, make no mistake my question here is not a show of knowledge
>> just a guess from the peanut gallery (if you are old enough to know that
>> one)
>>
>> John Warner
>>
>>
>> > -----Original Message-----
>> > From: Discussion of advanced .NET topics. [mailto:ADVANCED-
>> > [email protected]] On Behalf Of Mike Andrews
>> > Sent: Wednesday, November 11, 2009 2:35 PM
>> > To: [email protected]
>>  > Subject: Re: [ADVANCED-DOTNET] unmanaged structure layout error
>> >
>> > I'm not sure I understand.
>> > I assumed that by placing [MarshalAs(UnmanagedType.ByValTStr, SizeConst
>> > =
>> > 70)] over the field that it would read the next 70 bytes as a string.
>> >
>> > But that still doesn't explain why the runtime is crying when it tries
>> to
>> > load the type.
>> >
>> > On Wed, Nov 11, 2009 at 1:26 PM, John Warner <[email protected]> wrote:
>> >
>> > > Does the string class need more space than the data it contains?
>> > >
>> > > String_name
>> > >
>> > > John Warner
>> > >
>> > >
>> > > > -----Original Message-----
>> > > > From: Discussion of advanced .NET topics. [mailto:ADVANCED-
>> > > > [email protected]] On Behalf Of Mike Andrews
>> > > > Sent: Wednesday, November 11, 2009 2:04 PM
>> > > > To: [email protected]
>> > > > Subject: [ADVANCED-DOTNET] unmanaged structure layout error
>> > > >
>> > > > I'm having a problem with an unmanaged structure.
>> > > >
>> > > > I'm getting this error message when the system tries to load the
>> type:
>> > > >
>> > > > *Could not load type '_S' from assembly '...' because it contains an
>> > > object
>> > > > field at offset 134 that is incorrectly aligned or overlapped by a
>> > > > non-object field.*
>> > > >
>> > > > For the life of me I cannot figure this out why this is failing.
>> > > > I've set the byte packing to 1 which means that there is no padding
>> > > between
>> > > > bytes.  I've also explicitly specified each field offset.
>> > > >
>> > > > Below is the structure that I have declared.  It's only a partial
>> > > structure
>> > > > and I removed all the properties for brevity.  Also there's more
>> members
>> > > but
>> > > > the last field is where the error is occurring.  Also, if I remove
>> the
>> > > last
>> > > > field it works.  When I add it back it fails.
>> > > >
>> > > > Any suggestions?
>> > > >
>> > > > Thanks,
>> > > > Mike
>> > > >
>> > > > [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi, Pack =
>> 1)]
>> > > > private struct _S {
>> > > >     [FieldOffset(0)]
>> > > >     private System.Double _height;
>> > > >     [FieldOffset(8)]
>> > > >     private System.Double _width;
>> > > >     [FieldOffset(16)]
>> > > >     private System.Double _depth;
>> > > >     [FieldOffset(24)]
>> > > >     private System.Double _a;
>> > > >     [FieldOffset(32)]
>> > > >     [MarshalAs(UnmanagedType.ByValArray, SizeConst = 24)]
>> > > >     private System.Byte[] _u1;
>> > > >     [FieldOffset(56)]
>> > > >     private System.UInt16 _number;
>> > > >     [FieldOffset(58)]
>> > > >     private System.UInt16 _d;
>> > > >     [FieldOffset(60)]
>> > > >     [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
>> > > >     private System.Byte[] _u2;
>> > > >     [FieldOffset(64)]
>> > > >     [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 70)]
>> > > >     private System.String _name;
>> > > >     [FieldOffset(134)]
>> > > >     [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)]
>> > > >     private System.String _address1;
>> > > >
>> > > > ===================================
>> > > > View archives and manage your subscription(s) at
>> > > > http://peach.ease.lsoft.com/archives
>> > >
>> > > ===================================
>> > > View archives and manage your subscription(s) at
>> > > http://peach.ease.lsoft.com/archives
>> > >
>> >
>> > ===================================
>> > View archives and manage your subscription(s) at
>> > http://peach.ease.lsoft.com/archives
>>
>> ===================================
>> View archives and manage your subscription(s) at
>> http://peach.ease.lsoft.com/archives
>>
>
>

===================================
View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives