Help with TypeLoadException (marshaling unmanaged code)

Jun 30 2007 2:53 PM
Hi.
I am trying to write a wrapper for the standard VC1 decoder, and I need to resolve a "TypeLoadException"
The decoder comes an an executable which I've turned into a .dll. This decoder has about a ton of structures, most of the containing other structures, arrays of structures, and unions of structures.

I need help converting the following to managed code:
The unmanaged structure is this:

typedef struct
{
vc1_eBlkType eBlkType; /** Block type */
FLAG Coded; /** Non zero AC coefficients for Intra,
non zero AC/DC for Inter */
union
{
vc1_sBlkIntra sIntra; /** Intra block state information */
vc1_sBlkInter sInter; /** Inter block state information */
} u; /** Intra/Inter union */
} vc1_sBlk;


As you can see it contains an enumerator and a union of another type of struct.
I've taken the unmanaged vc1_sBlkInter structure
typedef struct
{
vc1_NumZeroCoef NZC; /** NUMZERO and NUMCOEF (excludes DC) */
HWD16 DC; /** Quantized DC for prediction */
HWD16 ACTop[7]; /** Quantized AC top row for prediction */
HWD16 ACLeft[7]; /** Quantized AC left column for prediction */
HWD16 SmoothRows[16]; /** Bottom two rows kept for overlap smoothing */
} vc1_sBlkIntra;


And turned it into a managed structure :
[StructLayout(LayoutKind.Sequential)]
public unsafe struct vc1_sBlkIntra
{
ushort NZC; /** NUMZERO and NUMCOEF (excludes DC) */
short DC; /** Quantized DC for prediction */

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 7)] public short[] ACTop; /** Quantized AC top row for prediction */

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 7)] public short[] ACLeft; /** Quantized AC left column for prediction */
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] public short[] moothRows; /** Bottom two rows kept for overlap smoothing */
};


Now, I am trying to turn vc1_sBlk into a managed structure as well.
The last attempt is this
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi)]
public unsafe struct vc1_sBlk
{
[FieldOffset(0), MarshalAs(UnmanagedType.I4)]
public vc1_eBlkType eBlkType; /** Block type */
[FieldOffset(4), MarshalAs(UnmanagedType.AsAny)]
public byte Coded; /** Non zero AC coefficients for Intra, non zero AC/DC for Inter */
[FieldOffset(5), MarshalAs(UnmanagedType.Struct, SizeConst = 64)] public vc1_sBlkIntra sIntra; /** Intra block state information */
[FieldOffset(5), MarshalAs(UnmanagedType.Struct, SizeConst = 64)] public vc1_sBlkIntra sInter; /** Inter block state information */
} ;

But I have pretty much tried every kind of marshalling mentioned online, but I keep getting this error:
"Could not load type 'vc1_sBlk'...because it contains an object field at offset 5 that is incorrectly aligned or overlapped by a non-object field."
I am really stuck here and I can't find an answer to this anywhere.
ANY help would be greatly appreciated.

Tom

Answers (3)