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

Marshalling: unmanaged strcture containing array into C#

P: 2
Hi,
I have some C union which are mapped on C# as below

Expand|Select|Wrap|Line Numbers
  1. [StructLayout(LayoutKind.Explicit, Pack = 4)]
  2.     public struct service_choice_u
  3.     {
  4.         [FieldOffset(0)]
  5.         public RPM_RESPONSE m_stRPM_Response;        
  6.         [FieldOffset(0)]
  7.         public PRIVATE_TRANSFER_REQUEST pt_request;
  8.     }
  9.  
Expand|Select|Wrap|Line Numbers
  1. //Structure to note
  2.     [StructLayout(LayoutKind.Explicit, Pack = 4)]
  3.     public struct PRIVATE_TRANSFER_REQUEST
  4.     {
  5.         [FieldOffset(0)]
  6.         public UInt32 m_u32VendorID;
  7.         [FieldOffset(4)]
  8.         public UInt32 m_u32ServiceNumber;
  9.         [FieldOffset(8)]
  10.         public UInt32 m_u32DataLength;       
  11. //See offset
  12.         [FieldOffset(40)]
  13.         [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U1, SizeConst = StackConstants.MAX_PT_SERVICE_PARAMETER_BYTE_LENGTH)]
  14.         public byte[] m_ai8ServiceParameters;
  15.         [FieldOffset(140)]
  16.         public byte m_bIsConfirmedRequest;
  17.         [FieldOffset(141)]
  18.         public byte m_bIsSerParaPresent;
  19.     }
  20.  
  21.  
  22.     [StructLayout(LayoutKind.Explicit, Pack = 4)]
  23.     public struct RPM_RESPONSE
  24.     {
  25.         [FieldOffset(0)]
  26.         public byte m_i8ArrayIndexPresent;
  27.         [FieldOffset(4)]
  28.         public int m_i32ErrorClass;
  29.         [FieldOffset(8)]
  30.         public int m_i32ErrorCode;
  31.         [FieldOffset(12)]
  32.         public int m_i32ObjectType;
  33.         [FieldOffset(16)]
  34.         public int m_i32ObjectInstance;
  35.         [FieldOffset(20)]
  36.         public int m_i32ObjectProperty;
  37.         [FieldOffset(24)]
  38.         public uint m_u32ArrayIndex;
  39.         [FieldOffset(28)]
  40.         public int m_eAccessType;
  41.         [FieldOffset(32)]
  42.         public int m_i32StringLen;
  43.         [FieldOffset(36)]
  44.         public int m_pi8PropValue;
  45.         [FieldOffset(40)]
  46.         [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U1, SizeConst = 96)]
  47.         public byte[] bacnet_property_value_structure;
  48.         [FieldOffset(136)]
  49.         public int m_pstNext;
  50.     }
  51.  
  52.  

But when PRIVATE_TRANSFER_REQUEST structure is change to following, it is generating Type load exception
Expand|Select|Wrap|Line Numbers
  1. //Structure to note
  2.     [StructLayout(LayoutKind.Explicit, Pack = 4)]
  3.     public struct PRIVATE_TRANSFER_REQUEST
  4.     {
  5.         [FieldOffset(0)]
  6.         public UInt32 m_u32VendorID;
  7.         [FieldOffset(4)]
  8.         public UInt32 m_u32ServiceNumber;
  9.         [FieldOffset(8)]
  10.         public UInt32 m_u32DataLength;       
  11. //See offset
  12.         [FieldOffset(12)]
  13.         [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U1, SizeConst = StackConstants.MAX_PT_SERVICE_PARAMETER_BYTE_LENGTH)]
  14.         public byte[] m_ai8ServiceParameters;
  15.         [FieldOffset(112)]
  16.         public byte m_bIsConfirmedRequest;
  17.         [FieldOffset(113)]
  18.         public byte m_bIsSerParaPresent;
  19.     }

Exception:
An unhandled exception of type 'System.TypeLoadException' occurred in Unknown Module.

Additional information: Could not load type 'xxxx.BACnetStackIntegration.Structures.service_ch oice_u' from assembly 'xxxx.BACnetStackIntegration, Version=1.0.11.0, Culture=neutral, PublicKeyToken=null' because it contains an object field at offset 0 that is incorrectly aligned or overlapped by a non-object field.
Aug 12 '14 #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.