title: How do I marshall a pointer to a pointer of an array of structures?
body: <p>My C declarations are as follows:</p>
  <pre><code>int myData(uint myHandle, tchar *dataName, long *Time, uint *maxData, DATASTRUCT **data);
  
  typedef struct {
    byte Rel;
    __int64 Time;
    char Validated;
    unsigned char Data[1];
  } DATASTRUCT;
  </code></pre>
  <p>My C# declarations are as follows:</p>
  <pre><code>[DllImport(&quot;myData.dll&quot;, EntryPoint = &quot;myData&quot;)]
  public static extern int myData(uint myHandle, [MarshalAs(UnmanagedType.LPTStr)] string dataName, out long Time, out uint maxData, ref DATASTRUCT[] data);
  
  [StructLayout(LayoutKind.Sequential, Pack = 1)]
  public struct DATASTRUCT
  {
      public sbyte Rel;
      public long Time;
      public byte Validated;
      public double Data;
  }
  </code></pre>
  <p>I then call the managed function as follows:</p>
  <pre><code>string dataToShow = &quot;description&quot;;
  long Time;
  uint maxData; // How many structs will be returned, i.e., how much data is available?
  uint myHandle = 1;
  
  DATASTRUCT[] dataInformation = new DATASTRUCT[3]; // Doesn't it matter what I specify as the array size?
  
  myData(myHandle, dataToShow, out Time, out maxData, ref dataInformation);
  </code></pre>
  <p>Upon execution the above function will return successfully with only one structure even though there are 3 to return. Why is this so?</p>
  <p>Additional information; I have tried passing the pointer to a pointer of an array of structs the following ways:</p>
  <ul>
  <li><code>ref DATASTRUCT[] data;</code> // It works, but it only returns one struct</li>
  <li><code>[Out, MarshalAs(UnmanagedType.LPArray)] DATASTRUCT[] data;</code> // returns the number of defined structs with garbage</li>
  </ul>
  <p>As I understand it I might need to do some manual marshalling using <code>IntPtr</code>, I do not know how to implement this however, so any advice would be appreciated.</p>