title: How to return array of struct from C++ dll to C#
body: <p>I need to call a function in dll and return an array of structures. I do not know the size of the array in advance. How can this be done? The error <code>can not marshal 'returns value' invalid managed / unmanaged</code></p>
  
  <p>Code in C#:</p>
  
  <pre><code>[DllImport("CppDll"]
  public static extern ResultOfStrategy[] MyCppFunc(int countO, Data[] dataO, int countF, Data[] dataF);
  </code></pre>
  
  <p>in C++:</p>
  
  <pre><code>extern "C" _declspec(dllexport) ResultOfStrategy* WINAPI MyCppFunc(int countO, MYDATA * dataO, int countF, MYDATA * dataF)
  {
      return Optimization(countO, dataO, countF, dataF);
  }
  </code></pre>
  
  <p>Return array of struct :</p>
  
  <pre><code>struct ResultOfStrategy
  {
  bool isGood;
  double allProfit;
  double CAGR;
  double DD;
  int countDeals;
  double allProfitF;
  double CAGRF;
  double DDF;
  int countDealsF;
  Param Fast;
  Param Slow;
  Param Stop;
  Param Tp;
  newStop stloss;
  };
  </code></pre>