Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / May 2007

Tip: Looking for answers? Try searching our database.

TypeLoadException struct problems

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nuno Magalhaes - 28 May 2007 22:29 GMT
Hello,

When I use the following structure at the Load event I get a type load
exception.

[StructLayout(LayoutKind.Explicit)]
public struct TempStruct
{
  [FieldOffset(0)]
  public int a;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
  [FieldOffset(0)]
  public string b;
}

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

What is this problem?
Does anyone know a workaround or a fix to this problem?

Thanks.
Miroslav Stampar [MCSD.NET / Security+] - 29 May 2007 00:12 GMT
try:

[FieldOffset(0)]
public int a;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
[FieldOffset(4)]
public string b;

problem is literally what it says: "it contains an object field at
offset 0 that is incorrectly aligned or overlapped by a non-object
field. ".

integer is 32-bit long (4 bytes), so using FieldOffset(0) for field b
instead of FieldOffset(4) causes address overlapping of string field b
with integer field a.

HTH :)
Marc Gravell - 29 May 2007 07:37 GMT
For reference, in this type of union with a value-type and
reference-type overlapping, a google hunt showed some suggestions e.g.
using an sbyte*.

I have not idea if this will help.

Marc
Christof Nordiek - 29 May 2007 10:57 GMT
> Hello,
>
[quoted text clipped - 10 lines]
>   public string b;
> }

I suppose, you use this type only for P\Invoke.
Since the fields occupy the same space, you only have to marshal one of the
values. You could try to make the Marshalling conversion yourself
before/after the call.

Maybe it's feasable to use different structs and two imports pair function;
one for int and one for string.

HTH

Christof

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.