title: Interop: C++ COM client with C# COM server byte array parameter - is array copied in/out or accessed directly?
body: <p>We have a C#/.Net COM server (COM-visible assembly) with the following interface &amp; class declarations:</p>
  
  <pre><code>[ComVisible(true)]
  [Guid( "..." )]
  [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  public interface ITest
  {
      void TestMethod( [In] int nNumElements, 
                       [In, Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0)] byte [] bArray );
  }
  
  [ComVisible(true)]
  [Guid( "..." )]
  [ClassInterface( ClassInterfaceType.None )]
  [ProgId( "CSCOMServer.CSTest" )]
  public class CSTest : ITest
  {
      public void TestMethod( int nNumElements, byte [] bArray )
      {
          for (int i = 0; i &lt; bArray.Length; i++)
          {
              bArray [i] = (byte)i;
          }
      }
  }
  </code></pre>
  
  <p>The resulting IDL/typelib is:  </p>
  
  <pre><code>...
  interface ITest : IUnknown {
      HRESULT _stdcall TestMethod(
                      [in] long nNumElements, 
                      [in, out] unsigned char* bArray);
  };
  
  ...
  coclass CSTest {
      interface _Object;
      [default] interface ITest;
  };
  </code></pre>
  
  <p>The goal is to be able to utilize this from a C++ COM client, which works fine using the following:</p>
  
  <pre><code>int iSize = 10;
  ITest *test;
  byte *buf  = (byte*)CoTaskMemAlloc( iSize * sizeof(byte) );
  CoCreateInstance( CLSID_CSTest, NULL, CLSCTX_INPROC_SERVER, IID_ITest, (void **) &amp;test );
  test-&gt;TestMethod( iSize, buf );
  </code></pre>
  
  <p>The buffer is allocated by C++ and passed to C# for population.
  All works well and upon the completion of TestMethod() the C++ array (buf) contains correct values set by the C# method.</p>
  
  <p>The question is about efficiency:<br>
  Does the Interop wrapper perform array copying during marshaling ("In" phase) and then back ("Out" phase) OR does the C# method operate directly on the passed in memory (which may be pinned)?</p>