title: C# call C++ DLL passing pointer-to-pointer argument
body: <p>Could you guys please help me solve the following issue?
  I have a C++ function dll, and it will be called by another C# application.
  One of the functions I needed is as follow:</p>
  
  <pre><code>struct DataStruct
  {
      unsigned char* data;
      int len;
  };
  
  DLLAPI int API_ReadFile(const wchar_t* filename, DataStruct** outData);
  </code></pre>
  
  <p>I wrote the following code in C#:</p>
  
  <pre><code>class CS_DataStruct
  {
      public byte[] data;
      public int len;
  }
  
  [DllImport("ReadFile.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
  private static extern int API_ReadFile([MarshalAs(UnmanagedType.LPWStr)]string filename, ref CS_DataStruct data);
  </code></pre>
  
  <p>Unfortunately, the above code is not working... I guess that is due to the C++ func takes a pointer-to-pointer of DataStruct, while I just passed a reference of CS_DataStruct in.</p>
  
  <p>May I know how can I pass a pointer-to-pointer to the C++ func? If it is not possible, is there any workaround? (the C++ API is fixed, so changing API to pointer is not possible)</p>
  
  <p><strong>Edit:</strong> 
  Memory of DataStruct will be allocated by c++ function. Before that, I have no idea how large the data array should be. 
  (Thanks for the comments below)</p>