title: Marshal C++ struct array into C#
body: <p>I have the following struct in C++:</p>
  
  <pre><code>#define MAXCHARS 15
  
  typedef struct 
  {
      char data[MAXCHARS];
      int prob[MAXCHARS];
  } LPRData;
  </code></pre>
  
  <p>And a function that I'm p/invoking into to get an array of 3 of these structures:</p>
  
  <pre><code>void GetData(LPRData *data);
  </code></pre>
  
  <p>In C++ I would just do something like this:</p>
  
  <pre><code>LPRData *Results;
  Results = (LPRData *)malloc(MAXRESULTS*sizeof(LPRData));
  GetData( Results );
  </code></pre>
  
  <p>And it would work just fine, but in C# I can't seem to get it to work.
  I've created a C# struct like this:</p>
  
  <pre><code>public struct LPRData
  {
  
      /// char[15]
      [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 15)]
      public string data;
  
      /// int[15]
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 15)]
      public int[] prob;
  }
  </code></pre>
  
  <p>And if I initialize an array of 3 of those (and all their sub-arrays) and pass it into this:</p>
  
  <pre><code>GetData(LPRData[] data);
  </code></pre>
  
  <p>It returns with success, but the data in the LPRData array has not changed.</p>
  
  <p>I've even tried to create a raw byte array the size of 3 LPRData's and pass that into a function prototype like this:</p>
  
  <p>GetData(byte[] data);</p>
  
  <p>But in that case I will get the "data" string from the very first LPRData structure, but nothing after it, including the "prob" array from the same LPRData.</p>
  
  <p>Any ideas of how to properly handle this?</p>