[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

TypeLoadException struct problems

Nuno Magalhaes

5/28/2007 9:30:00 PM

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.

3 Answers

Miroslav Stampar [MCSD.NET / Security+]

5/28/2007 11:12:00 PM

0

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

5/29/2007 6:37:00 AM

0

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

5/29/2007 9:58:00 AM

0

"Nuno Magalhaes" <nunommagalhaes@hotmail.com> schrieb im Newsbeitrag
news:1180387780.512892.33240@g4g2000hsf.googlegroups.com...
> 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;
> }
>

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