Re: unmanaged structure layout error
John Warner <[email protected]> Wed, 11 Nov 2009 18:01:27 -0500
| Newsgroups | gmane.comp.windows.devel.dotnet.advanced |
|---|---|
| Organization | House |
| Message-ID | <0e7801ca6322$e6c57aa0$b4506fe0$@com> |
Well, I warned you I was speaking from ignorance ... John Warner > -----Original Message----- > From: Discussion of advanced .NET topics. [mailto:ADVANCED- > [email protected]] On Behalf Of Mike Andrews > Sent: Wednesday, November 11, 2009 3:11 PM > To: [email protected] > Subject: Re: [ADVANCED-DOTNET] unmanaged structure layout error > > That does not work for structs as you can only do that on a static member. > > On Wed, Nov 11, 2009 at 1:54 PM, John Warner <[email protected]> wrote: > > > Private System.String _name = New String(" "c, 70) > > > > > > > > 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:46 PM > > > To: [email protected] > > > Subject: Re: [ADVANCED-DOTNET] unmanaged structure layout error > > > > > > 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 > > > > =================================== > > 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