title: how to modify c# array in c++ dll
body: <p>I've got an unmanaged C++ dll with a C-style exported function I'm trying to use from within C# code. The function is supposed to take arrays as arguments and modify them so I can use the results in C#.</p>
  <p>So I figured out how to do this with float arrays. When my C function looks as follows...</p>
  <pre><code>__declspec( dllexport ) float testArrayFlt( const float *f, int arraySize, float *fCopy )
  {
      float sum = 0.0f;
  
      for( int i = 0; i &lt; arraySize; i++ )
      {
          sum += f[i];
          fCopy[i] = f[i];
      }
  
      return sum;
  }
  </code></pre>
  <p>...then I have to declare as follows in C#:</p>
  <pre><code>[DllImport( lib )]
  public static extern float testArrayFlt( float[] f, int arraySize, float[] fCopy );
  </code></pre>
  <p>Now, when I use it like this, everything works well:</p>
  <pre><code>float[] flt = new float[10];
  float[] fltCopy = new float[flt.Length];
  for( int i = 0; i &lt; flt.Length; i++ )
      flt[i] = i;
  
  float f = testArrayFlt( flt, flt.Length, fltCopy );
  
  Console.WriteLine( &quot;flt sum: &quot; + f );
  for( int i = 0; i &lt; fltCopy.Length; i++ )
      Console.WriteLine( &quot;flt #&quot; + i + &quot;: &quot; + fltCopy[i] );
  </code></pre>
  <p>The output I get from that is:</p>
  <blockquote>
  <p>flt sum: 45</p>
  <p>flt #0: 0</p>
  <p>flt #1: 1</p>
  <p>flt #2: 2</p>
  <p>flt #3: 3</p>
  <p>flt #4: 4</p>
  <p>flt #5: 5</p>
  <p>flt #6: 6</p>
  <p>flt #7: 7</p>
  <p>flt #8: 8</p>
  <p>flt #9: 9</p>
  </blockquote>
  <p>Now, when I try to do this with a Vector type, which is composed of three floats, only half of it works. Passing and reading the data, as well as returning results works, but not the modification of the passed array. In more detail: I got a C function as follows:</p>
  <pre><code>__declspec(dllexport) glm::vec3 testArrayVec3( const glm::vec3 *points, int arraySize, glm::vec3 *pointsCopy )
  {
      glm::vec3 sum( 0.0f );
  
      for( int i = 0; i &lt; arraySize; i++ )
      {
          sum += points[i];
          pointsCopy[i] = points[i];
      }
  
      return sum;
  }
  </code></pre>
  <p>where glm::vec3 is basically just a struct of three floats. In C#, I define a counterpart</p>
  <pre><code>struct Vec3f
  {
      public float X;
      public float Y;
      public float Z;
  
      public override string ToString()
      {
          return &quot;[&quot; + this.X + &quot;, &quot; + this.Y + &quot;, &quot; + this.Z + &quot;]&quot;;
      }
  };
  </code></pre>
  <p>...declare the dll import as...</p>
  <pre><code>[DllImport( lib )]
  public static extern Vec3f testArrayVec3( Vec3f[] points, int arraySize, Vec3f[] pointsCopy );
  </code></pre>
  <p>...and use it as follows:</p>
  <pre><code>Vec3f[] points = new Vec3f[10];
  Vec3f[] pointsCopy = new Vec3f[points.Length];
  for( int i = 0; i &lt; points.Length; i++ )
      points[i].X = points[i].Y = points[i].Z = i;
  
  Vec3f p = testArrayVec3( points, points.Length, pointsCopy );
  
  Console.WriteLine( &quot;vec sum: &quot; + p );
  for( int i = 0; i &lt; pointsCopy.Length; i++ )
      Console.WriteLine( &quot;vec #&quot; + i + &quot;: &quot; + pointsCopy[i] );
  </code></pre>
  <p>Now I get the following output:</p>
  <blockquote>
  <p>vec sum: [45, 45, 45]</p>
  <p>vec #0: [0, 0, 0]</p>
  <p>vec #1: [0, 0, 0]</p>
  <p>vec #2: [0, 0, 0]</p>
  <p>vec #3: [0, 0, 0]</p>
  <p>vec #4: [0, 0, 0]</p>
  <p>vec #5: [0, 0, 0]</p>
  <p>vec #6: [0, 0, 0]</p>
  <p>vec #7: [0, 0, 0]</p>
  <p>vec #8: [0, 0, 0]</p>
  <p>vec #9: [0, 0, 0]</p>
  </blockquote>
  <p>What confuses me is, that it can't be <em>that</em> far from being right, since calculation and returning of the sum does works. It just seems like the second array isn't passed properly by reference, but I can't find out how to do that. Also, in the float-version it works just like that. I also checked in the dll, the glm::vec3 assignment <em>does</em> work, so pointsCopy actually <em>does</em> contain the correct data after the loop, but it doesn't arrive on the C#-side.</p>
  <p>Any ideas?</p>