title: Marshal C++ int array to C#
body: <p>I would like to marshal an array of ints from C++ to C#. I have an unmanaged C++ dll which contains:</p>
  
  <pre><code>DLL_EXPORT int* fnwrapper_intarr()
  {
      int* test = new int[3];
  
      test[0] = 1;
      test[1] = 2;
      test[2] = 3;
  
      return test;
  }
  </code></pre>
  
  <p>with declaration in header <code>extern "C" DLL_EXPORT int* fnwrapper_intarr();</code></p>
  
  <p>I am then using pinvoke to marshal it into C#:</p>
  
  <pre><code>[DllImport("wrapper_demo_d.dll")]
  [return: MarshalAs(UnmanagedType.SafeArray)]
  public static extern int[] fnwrapper_intarr();
  </code></pre>
  
  <p>And I use the function like so:</p>
  
  <pre><code>int[] test = fnwrapper_intarr();
  </code></pre>
  
  <p>However, during program execution I get the following error: <code>SafeArray cannot be marshaled to this array type because it has either nonzero lower bounds or more than one dimension.</code></p>
  
  <p><strong>What array type should I be using? Or is there a bettery way of marshalling arrays or vectors of integers?</strong></p>