title: Marshalling Complex structures between C#/C++
body: <p>I'm trying to populate an array of structures from C++ and pass the result back to C#.</p>
  
  <p>I thought maybe creating a struct with an array of structures maybe the way forward as most examples I have come across use structures(but passing basic types). I have tried the following but no luck so far.</p>
  
  <p>Found an example at: <a href="http://limbioliong.wordpress.com/2011/08/20/passing-a-pointer-to-a-structure-from-c-to-c-part-2/?relatedposts_exclude=542" rel="nofollow">http://limbioliong.wordpress.com/2011/08/20/passing-a-pointer-to-a-structure-from-c-to-c-part-2/?relatedposts_exclude=542</a></p>
  
  <p>I have the following in C#</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Runtime.InteropServices;
  namespace CSharpDLLCall
  {
    class Program
    {
  
  [StructLayout(LayoutKind.Sequential,Pack=8)]
  public struct Struct1
  {
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
      public double[] d1;
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
      public double[] d2;
  }
  
  [StructLayout(LayoutKind.Sequential)]
  public struct TestStructOuter
  {
       public int length;
       public IntPtr embedded;
  }
  
   static void Main(string[] args)
   {
      Program program = new Program();
      program.demoArrayOfStructs();
   }
  
  public void demoArrayOfStructs() 
  {
      TestStructOuter outer = new TestStructOuter();
      testStructOuter.embedded = new Struct1[10];
      outer.length = 10;
      outer.embedded = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Struct1)) * 10);
      Marshal.StructureToPtr(outer, outer.embedded, false);
      testAPI2(ref outer);
      outer = (TestStructOuter)(Marshal.PtrToStructure(outer.embedded, typeof(TestStructOuter)));
      Marshal.FreeHGlobal(outer.embedded);
   }
  
  [DllImport(@"C:\CPP_Projects\DLL\DLLSample\Release\DLLSample.dll")]
  static extern void testAPI2(IntPtr pTestStructOuter);
  }
  }
  </code></pre>
  
  <p>In C++ in the header i have</p>
  
  <pre><code>#ifdef DLLSAMPLE_EXPORTS
  #define DLLSAMPLE_API __declspec(dllexport)
  #else
  #define DLLSAMPLE_API __declspec(dllimport)
  #endif
  
  #include &lt;iostream&gt;
  using namespace std;
  
  #pragma pack(1)
  struct struct1
  {
      double d1[];
      double d2[];
  };
  
  struct TestStructOuter
  {
      struct1* embedded;
  };
  
  extern "C"
  {   
      DLLSAMPLE_API void __stdcall testAPI2(TestStructOuter* pTestStructOuter);
  }
  </code></pre>
  
  <p>In the cpp I have:</p>
  
  <pre><code>#include "stdafx.h"
  #include "DLLSample.h"
  
  __declspec(dllexport) void __stdcall testAPI2(TestStructOuter* pTestStructOuter)
  {
      // not sure that this is necessary
      // for (int i = 0; i &lt; 10 ; ++i)
      // {    
      //     pTestStructOuter-&gt;embedded = new struct1;        
      // }
  
      for (int i = 0; i &lt; 10 ; ++i)
      {
              struct1 s1;
  
              for (int idx = 0; idx &lt; 10; ++idx)
              {
                      s1.d1[i] = i+0.5;
                      s1.d2[i] = i+0.5;
              }
  
              pTestStructOuter-&gt;embedded[0] = s1;
      }
  }
  </code></pre>
  
  <p>This doesn't seem to work the error i get:
  The parameter is incorrect.(Exception from HRESULT:0x80070057 (E_INVALIDARG))</p>
  
  <p>Which probably means that its not recognizing the array of structures. Any ideas how I can do this? Thanks.</p>