title: Return byte array from C++ DLL to C#
body: <p>I'm implementing a C++ DLL that needs to read/write data to the serial line. 
  The usage of this DLL is in a C# application.
  Currently I can't manage to read data from the C# application while i'm using the C++ read code (without the C# wrapper the read function works properly).</p>
  
  <p>C++ code:</p>
  
  <pre><code>extern "C" __declspec(dllexport) int Read(void *Buffer, unsigned int MaxNbBytes, unsigned int TimeOut_ms)
  {
      return uart.Read(Buffer, MaxNbBytes, TimeOut_ms);
  }
  </code></pre>
  
  <p>C# Code</p>
  
  <pre><code>[DllImport("RS232LIB.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
  public static extern int Read(out byte[] bytesRead, int maxNbBytes, int timeOutMs);
  
  var bytes = new byte[4];
  Read(out bytes, 4, 10);
  </code></pre>
  
  <p>After running those line i keeps getting the <code>System.AccessViolationException</code>.
  How can I solve this issue?</p>
  
  <p>Remark: I can't use the C# Serial class. My C++ serial function works well.</p>
  
  <p>The <code>uart.Read(void *Buffer, unsigned int MaxNbBytes, unsigned int TimeOut_ms)</code> reference:</p>
  
  <blockquote>
    <p>\Buffer : array of bytes read from the serial device<br>
    \MaxNbBytes : maximum allowed number of bytes read<br>
    \TimeOut_ms : delay of timeout before giving up the reading</p>
  </blockquote>