title: Marshalling a pointer of struct in C++ to an array in C#
body: <p>I have the following C++ structs:</p>
  
  <pre><code>typedef struct Point
  {
      int x;
      int y;
  } Point;
  
  
  typedef struct IMTResult
  {
      int    numberOfPoints;
      Point* vect_intima;
      Point* vect_media;
      Point* vect_adventitia;
  } IMTResult;
  </code></pre>
  
  <p>Where <code>numberOfPoints</code> is the length of each pointer of <code>Point</code></p>
  
  <p>And this C++ function:</p>
  
  <pre><code>bool setSecondPoint( int x, int y, IMTResult* result );
  </code></pre>
  
  <p>How can I marshall this IMTResult struct in C#?
  I tried:</p>
  
  <pre><code>public struct IMTResult
  {
      public int numberOfPoints;
      public IntPtr vect_intima;
      public IntPtr vect_media;
      public IntPtr vect_adventitia;
  }
  </code></pre>
  
  <p>And tried to manage myself the three vectors using:</p>
  
  <pre><code>[DllImport("MyDll.dll", CallingConvention = CallingConvention.Cdecl)]
  public static extern bool setSecondPoint(int x, int y, [MarshalAs(UnmanagedType.Struct)] ref IMTResult result);
  
  public bool dllSetSecondPoint(int x, int y, ref IMTResult result)
  {
      bool res = setSecondPoint(x, y, ref result);
  
      int structSize = Marshal.SizeOf(typeof(Point));
      Point vect = new Point();
      IntPtr ptr = result.vect_media;
      for (int i = 0; i &lt; result.numberOfPoints; i++)
      {
          Point vect = (Point) Marshal.PtrToStructure(ptr, typeof(Point));
          ptr = (IntPtr)((int)ptr + structSize);
      }
  
      return res;
  }
  </code></pre>
  
  <p>But <code>vect</code> always results in a vector where <code>x</code> and <code>y</code> are equal to -1
  . I also tried to marshal as attribute each of these vectors unsuccessfully.
  Could anyone help me?</p>