title: How to marshal an array of structure In C#?
body: <p>I have to call a C++ dll in C#. And the header of the dll is as following(simplified):</p>
  <p>//Header of C++</p>
  <pre><code>struct vector
  {
      float x;
      float y;
  
      vector()
      {}
  
      vector(float x0, float y0)
      {
          x = x0;
          y = y0;
      }
  };
  
  struct unmanaged_struct
  {
      int int_var;
      float float_var;
      char* chars_var;
      vector vector_var;
  
      unmanaged_struct(int i, float f, char* ch, float vec_x, float vec_y) 
      {
          int_var = i;
          float_var = f;
          chars_var = ch;
          vector_var = vector(vec_x, vec_y);
      }
  };
  </code></pre>
  <p>// this function is used to output all the variable values of the struct instance</p>
  <pre><code>extern &quot;C&quot; __declspec( dllexport )  void unmanagedstruct_summary(unmanaged_struct* us_list, int length);
  </code></pre>
  <p>And I defined following class in C#</p>
  <p>//CSharp</p>
  <pre><code>[StructLayout(LayoutKind.Sequential)]
  public class Vector
  {
      public float x;
      public float y;
  
      public Vector(float f1, float f2)
      {
          x = f1;
          y = f2;
      }
  }
  
  [StructLayout(LayoutKind.Sequential)]
  public class UnmanagedStruct 
  { 
      public int int_var;
      public float float_var;
      public string char_var;
      public Vector vector_var;
  
      public UnmanagedStruct(int i, float f, string s, Vector vec)
      {
          this.int_var = i;
          this.float_var = f;
          this.char_var = s;
          this.vector_var = vec;
      }
  }
  
  class UnmanagedDllCallTest
  {
      [DllImport(&quot;unmanageddll.dll&quot;, EntryPoint = &quot;unmanagedstruct_summary&quot;)]
      public  static extern void unmanagedstruct_summary([Out]UnmanagedStruct[] usList, int length);    
  
    static void Main(string[] args)
      {
      
          UnmanagedStruct[] usList = new UnmanagedStruct[1];
          usList[0] = new UnmanagedStruct(1, 1.0f, &quot;aa&quot;, new Vector(10, 1));       
          usList[1] = new UnmanagedStruct(2, 2.0f, &quot;ba&quot;, new Vector(20, 2));  
          UnmanagedDllCallTest.unmanagedstruct_summary(usList, 2);
  }
  </code></pre>
  <p>And the output is as following:</p>
  <blockquote>
  <p>unmanaged_struct summary:</p>
  <p>0</p>
  <p>1.12104e-044</p>
  <p>Unhandled Exception:
  System.AccessViolationException:
  Attempted to read or write  protected
  memory. This is often an indication
  that other memory is corrupt.    at
  callunmanageddll.UnmanagedDllCallTest.unmanagedstruct_summary(UnmanagedStr
  uct[] usList, Int32 length)    at
  callunmanageddll.Program.Main(String[]
  args) in c:\users\dynaturtle\docume
  nts\visual studio
  2010\Projects\callunmanageddll\callunmanageddll\Program.cs:lin
  e 68</p>
  </blockquote>
  <p>The C++ dll is OK as I have written test in C++ and the function works well. I have read <a href="https://stackoverflow.com/questions/729514/how-to-marshall-array-of-structs-in-c">this thread</a> but it seems the solution didn't work in my case. Any suggestions? Thanks in advance!</p>