title: Marshal between C# byte array and C++ byte array
body: <p>I have a C# <code>array&lt;System::Byte&gt;</code> and I want that to translate to a C++ <code>byte*</code>. How can I do that? I am using C++/CLR because it lets me use managed/unmanaged code in the same project. I'm basically writing a DLL and making a few methods that can be called via C#, but that contain unmanaged C++ code. </p>
  
  <p>So basically, my C++/CLR method header is this:</p>
  
  <pre><code>void testMethod(array&lt;Byte&gt;^ bytesFromCSharp);
  </code></pre>
  
  <p>and inside of that <code>testMethod</code> I would like to translate the <code>bytesFromCSharp</code> into a <code>byte*</code> which can be used by other unmanaged C++ code. I malloced the <code>byte*</code> array and wrote a for loop to copy byte by byte but it feels like there should be a better solution. </p>
  
  <p><strong>edit: Example of Hans' technique from his answer below:</strong></p>
  
  <pre><code>//C#
  byte[] myBytes = {1,2,3,4};
  
  //C++/CLI
  void testMethod(array&lt;byte&gt;^ myBytes) {
      pin_ptr&lt;byte&gt; thePtr = &amp;myBytes[0];
      byte* bPtr = thePtr;
      nativeCode(bPtr);
      ...
  }
  </code></pre>