By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

Help with TypeLoadException (marshaling unmanaged code)

P: 1
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:

Expand|Select|Wrap|Line Numbers
  1. typedef struct
  2. {
  3. vc1_eBlkType eBlkType; /** Block type */
  4. FLAG Coded; /** Non zero AC coefficients for Intra,
  5. non zero AC/DC for Inter */
  6. union
  7. {
  8. vc1_sBlkIntra sIntra; /** Intra block state information */
  9. vc1_sBlkInter sInter; /** Inter block state information */
  10. } u; /** Intra/Inter union */
  11. } 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
Expand|Select|Wrap|Line Numbers
  1. typedef struct
  2. {
  3. vc1_NumZeroCoef NZC; /** NUMZERO and NUMCOEF (excludes DC) */
  4. HWD16 DC; /** Quantized DC for prediction */
  5. HWD16 ACTop[7]; /** Quantized AC top row for prediction */
  6. HWD16 ACLeft[7]; /** Quantized AC left column for prediction */
  7. HWD16 SmoothRows[16]; /** Bottom two rows kept for overlap smoothing */
  8. } vc1_sBlkIntra;
And turned it into a managed structure :
Expand|Select|Wrap|Line Numbers
  1. [StructLayout(LayoutKind.Sequential)]
  2. public unsafe struct vc1_sBlkIntra
  3. {
  4. ushort NZC; /** NUMZERO and NUMCOEF (excludes DC) */
  5. short DC; /** Quantized DC for prediction */
  6.  
  7. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 7)] public short[] ACTop; /** Quantized AC top row for prediction */
  8.  
  9. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 7)] public short[] ACLeft; /** Quantized AC left column for prediction */
  10. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] public short[] moothRows; /** Bottom two rows kept for overlap smoothing */
  11. };
Now, I am trying to turn vc1_sBlk into a managed structure as well.
The last attempt is this
Expand|Select|Wrap|Line Numbers
  1. [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi)]
  2. public unsafe struct vc1_sBlk
  3. {
  4. [FieldOffset(0), MarshalAs(UnmanagedType.I4)]
  5. public vc1_eBlkType eBlkType; /** Block type */
  6. [FieldOffset(4), MarshalAs(UnmanagedType.AsAny)]
  7. public byte Coded; /** Non zero AC coefficients for Intra, non zero AC/DC for Inter */
  8. [FieldOffset(5), MarshalAs(UnmanagedType.Struct, SizeConst = 64)] public vc1_sBlkIntra sIntra; /** Intra block state information */
  9. [FieldOffset(5), MarshalAs(UnmanagedType.Struct, SizeConst = 64)] public vc1_sBlkIntra sInter; /** Inter block state information */
  10. } ;
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
Jun 30 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.