title: How do I convert this to c# (Marshalling)
body: <p>I have these declarations (DLL) and tried to convert it in C# so i can call the functions from the DLL.</p>
  
  <p>Same for struct1 to struct3  </p>
  
  <pre><code>typedef struct1  
  {  
      int num;  
      char chars[25];  
      short shrt;  
      union  
      {  
           struct4 objstruct4;  
      }  
  }  
  
  typedef struct
  {        
      Long Length;    
      short Type;    
      union    
      {
         struct1 objStruct1;      
         struct2 objStruct2;      
         struct3 objStruct3;    
      }Data;  
  } Msg;
  </code></pre>
  
  <p>In C#, i converted these...same for struct1 to struct3</p>
  
  <pre><code>[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
  public struct struct1  
  {
      [MarshalAs(UnmanagedType.I4)]
      public Int32 num;  
  
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = size + 1)]
      public string chars;  
  
      [MarshalAs(UnmanagedType.I2)]
      public short shrt;  
  
      [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
      public struct struct4
      {                
          [MarshalAs(UnmanagedType.Struct)]
          public ...
          ...
      }
  
  [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi)]
  protected struct Msg
  {
      [FieldOffset(0)]
      [MarshalAs(UnmanagedType.I4)]
      public int Length;
  
      [FieldOffset(4)]
      [MarshalAs(UnmanagedType.I2)]
      public short Type;
  
      [FieldOffset(6)]
      [MarshalAs(UnmanagedType.Struct)]
      public Data MsgData;  
  }
  
  [StructLayout(LayoutKind.Explicit, Pack = 1, CharSet = CharSet.Ansi)]
  public struct Data
  {
      [FieldOffset(0)]
      [MarshalAs(UnmanagedType.Struct)]
      public struct1 objStruct1;      
  
      [FieldOffset(0)]
      [MarshalAs(UnmanagedType.Struct)]
      public struct2 objStruct2;      
  
      [FieldOffset(0)]
      [MarshalAs(UnmanagedType.Struct)]
      public struct3 objStruct3;    
  </code></pre>
  
  <p>}</p>
  
  <p>Problem is when I tried to call a function from the DLL and passed the struct MSG as REF,
  some member variables of the inner structs/union (struct1 to struct3) don't have values.
  Its like they are rumbled inside the memory...</p>
  
  <p>But when I remove the struct1 or struct2 in struct MSG, all of the member variables inside the remaining inner structs/union were successfully retrieved.</p>
  
  <p>Can I ask your advice if my conversion is correct or did i missed something...
  Or is there a better way to convert this or you have answers why this problem occur.
  One thing that I suspect is the size of the struct, ANSI or Unicode, and the arrangement of the variables, structs.</p>
  
  <p>Please advise - thanks.</p>
  
  <p>sample 2:<br>
      ////////////////  </p>
  
  <pre><code>[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  public struct struct1  
  {
      [MarshalAs(UnmanagedType.I4)]
      public Int32 num1;  
  
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = size + 1)]
      public string chars;
  }
  
  [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  public struct struct2 
  {
      [MarshalAs(UnmanagedType.I4)]
      public Int32 num2;  
  }
  
  [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  public struct struct3 
  {
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = size + 1)]
      public string chars;
  
      [MarshalAs(UnmanagedType.I4)]
      public Int32 num3;  
  }
  
  [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  public struct struct4
  {
      [MarshalAs(UnmanagedType.I4)]
      public Int32 num4;  
  
      [MarshalAs(UnmanagedType.Struct)]
      public Data DataMsg;
  
      [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi)]
      public struct Data
      {
        //[FieldOffSet(0)]
        public struct1 str1;
  
        //[FieldOffSet(0)]
        public struct2 str2;
  
        //[FieldOffSet(0)]
        public struct3 str3;
      }
  }
  ////////////////
  </code></pre>