Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Struct def TypeLoadException

Status
Not open for further replies.

Erakis

Programmer
Jan 10, 2003
26
0
0
CA
Hi,

What wrong with my struct declaration ?
When I run my program, I got a TypeLoadException.

Code:
[StructLayout(LayoutKind.Explicit, CharSet=CharSet.Ansi, Size=16)] 
struct InfoRegister 
{ 
   [FieldOffset(0)] 
   public uint TargetIdBitField; 

   [FieldOffset(4)] 
   [MarshalAs(UnmanagedType.ByValArray, SizeConst = 9) ] 
   public byte[] Label; 

   [FieldOffset(13)] 
   public byte Mode; 

   [FieldOffset(14)] 
   public byte Index; 

   [FieldOffset(15)] 
   public byte CS; 

   [FieldOffset(0)] 
   [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16) ] 
   public ushort[] StructInUShortArray; 
}

Thanks
 
My first guess would be that you aren't allocating any space for your arrays.

My second guess is that your ushort array is overlaying offset 0. C# does not have unions.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Hi,

I cannot pre-allocate space for my array because C-Sharp doesn't allow default constructor (empty) on struct object.

I already test other types of union using C-Sharp and it work well.

Here it is an exemple :
Code:
public struct SpecialFloat
{
   [FieldOffset(0)] public float FloatValue;

   [FieldOffset(0)] public ushort UShortValue1;
   [FieldOffset(2)] public ushort UShortValue2;

   [FieldOffset(0)] public byte ByteValue0;
   [FieldOffset(1)] public byte ByteValue1;
   [FieldOffset(2)] public byte ByteValue2;
   [FieldOffset(3)] public byte ByteValue3;
}

This pattern works perfectly...
So how can I make InfoRegister struct to works ?

Thanks
 
You might have to do something ugly like this:
Code:
   [FieldOffset(4)]
   public byte Label1;
   [FieldOffset(5)]
   public byte Label2;
   [FieldOffset(6)]
   public byte Label3;
    :
    :
   [FieldOffset(12)]
   public byte Label9;
Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Hi,

Ouch ! Ungly as you said, not really usefull :(
 
There are three problems with your struct (or better name it union):

1. StructInUShortArray is a "object" on 4 bytes which points to an array of ushorts so it cannot overlap a value type. If you put two of this kind in a same union you will see that it works, but if you put one value type with one array it won't.

2. You miss an (ArraySubType = UnmanagedType.U2) name parameter in the MarshallAs attribute for StructInUShortArray.

3. The SizeConst member does not reflect the syze in bytes (see the MSDN help), but:
"Indicates the number of elements in the fixed-length array or the number of characters (not bytes) in a string to import"

Now, take the above into account, and rewrite your struct and I am sure you will get it right.

Side Note: I admit, the error message from the TypeLoadException is kind of dumb and missleading, but "There are spots even in the sun", isn't it.

HTH,



s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
Hi,

Thanks for you help, it is really appreciated :)
Here is my struct again :
Code:
[StructLayout(LayoutKind.Explicit, CharSet=CharSet.Ansi, Size=16)] 
struct InfoRegister 
{ 
   [FieldOffset(0)] 
   public uint TargetIdBitField; 

   [FieldOffset(4)] 
   [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I1, SizeConst = 9) ] 
   public byte[] Label; 

   [FieldOffset(13)] 
   public byte Mode; 

   [FieldOffset(14)] 
   public byte Index; 

   [FieldOffset(15)] 
   public byte CS; 

   [FieldOffset(0)] 
   [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U2, SizeConst = 8) ] 
   public ushort[] StructInUShortArray; 
}

I did change for point 2 and 3 but for you tips #1 I understand the problem but I don't know how to solve it.

I cannot do that, this is not what I need.
Code:
   [FieldOffset(4)]
   public byte Label1;
   [FieldOffset(5)]
   public byte Label2;
   [FieldOffset(6)]
   public byte Label3;
    :
    :
   [FieldOffset(12)]
   public byte Label9;

This struct need to be filled by a DLL (unmanaged) function that required a ushort[] pointer.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top