title: How to marshal nested structure?
body: <p>I have three C++ structures that I need to send as parameters when calling a DLL method from C#.
  I simplified them as the following:</p>
  
  <pre><code>typedef struct
  {
      int data1;
      int data2;
  } A;
  
  typedef struct
  {
      int numStructA;
      A *pStructA;
      int moreData;
  } B;
  
  typedef struct
  {
      TCHAR *pStr;
      B structB;
  } C;
  </code></pre>
  
  <p>The following is the C++ function I need to call from C#:</p>
  
  <pre><code>int func(C *pStructC, int numStructC);
  </code></pre>
  
  <p>In my C# code, I define the structs as the following:</p>
  
  <pre><code>[StructLayout(LayoutKind.Sequential, Pack = 1)]
  private struct A
  {
      public int data1;
      public int data2;
  }
  
  [StructLayout(LayoutKind.Sequential, Pack = 1)]
  private struct B
  {
      public int numStuctA;
      public IntPtr structA;
      public int moreData;
  }
  
  [StructLayout(LayoutKind.Sequential, Pack = 1)]
  private struct C
  {
      [MarshalAs(UnmanagedType.LPTStr)]
      public string Str;
      [MarshalAs(UnmanagedType.Struct)]
      public B structB;
  }
  
  [DllImport("somedll.dll")]
  private static extern int func(IntPtr pStructC, int size);
  </code></pre>
  
  <p>I tried to marshal the nested structures. However, the C++ function does not receive the data correctly. So far only the data that comes before the nested structure is sent correctly to the C++ function.
  How can I marshal the nested structures so the C++ method receives the correct data? Thanks in advance.</p>