title: How do I write a custom marshaler which allows data to flow from native to managed?
body: <p>In attempting to write a custom marshaler related to this question (<a href="https://stackoverflow.com/questions/18491000/p-invoke-from-c-to-c-sharp-withound-knowing-size-of-array">P/Invoke from C to C# without knowing size of array</a>), I have come across something I cannot understand. This is the first ever custom marshaler that I have written so no doubt I'm missing something obvious due to my ignorance.</p>
  
  <p>Here's my C# code:</p>
  
  <pre><code>using System;
  using System.Runtime.InteropServices;
  using System.Text;
  
  namespace CustomMarshaler
  {
      public class MyCustomMarshaler : ICustomMarshaler
      {
          static MyCustomMarshaler static_instance;
  
          public IntPtr MarshalManagedToNative(object managedObj)
          {
              if (managedObj == null)
                  return IntPtr.Zero;
              if (!(managedObj is int[]))
                  throw new MarshalDirectiveException("VariableLengthArrayMarshaler must be used on an int array.");
  
              int[] arr = (int[])managedObj;
              int size = sizeof(int) + arr.Length * sizeof(int);
              IntPtr pNativeData = Marshal.AllocHGlobal(size);
              Marshal.WriteInt32(pNativeData, arr.Length);
              Marshal.Copy(arr, 0, pNativeData + sizeof(int), arr.Length);
              return pNativeData;
          }
  
          public object MarshalNativeToManaged(IntPtr pNativeData)
          {
              int len = Marshal.ReadInt32(pNativeData);
              int[] arr = new int[len];
              Marshal.Copy(pNativeData + sizeof(int), arr, 0, len);
              return arr;
          }
  
          public void CleanUpNativeData(IntPtr pNativeData)
          {
              Marshal.FreeHGlobal(pNativeData);
          }
  
          public void CleanUpManagedData(object managedObj)
          {
          }
  
          public int GetNativeDataSize()
          {
              return -1;
          }
  
          public static ICustomMarshaler GetInstance(string cookie)
          {
              if (static_instance == null)
              {
                  return static_instance = new MyCustomMarshaler();
              }
              return static_instance;
          }
      }
      class Program
      {
          [DllImport(@"MyLib.dll")]
          private static extern void Foo(
              [In, Out, MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MyCustomMarshaler))]
              int[] arr
          );
  
          static void Main(string[] args)
          {
              int[] colorTable = new int[] { 1, 2, 3, 6, 12 };
              Foo(colorTable);
              foreach (int value in colorTable)
                  Console.WriteLine(value);
          }
      }
  }
  </code></pre>
  
  <p>On the other side is a trivial native DLL, written in Delphi as it happens.</p>
  
  <pre class="lang-pascal prettyprint-override"><code>library MyLib;
  
  procedure Foo(P: PInteger); stdcall;
  var
    i, len: Integer;
  begin
    len := P^;
    Writeln(len);
    for i := 1 to len do begin
      inc(P);
      Writeln(P^);
      inc(P^);
    end;
  end;
  
  exports
    Foo;
  
  begin
  end.
  </code></pre>
  
  <p>The idea is that the array is passed to the DLL which then prints out the length field, and the values of the array. The native code also increments each value of the array by 1.</p>
  
  <p>So, I expect to see this output:</p>
  
  <pre>
  5
  1
  2
  3
  6
  12
  2
  3
  4
  7
  13
  </pre>
  
  <p>But unfortunately I see this output:</p>
  
  <pre>
  5
  1
  2
  3
  6
  12
  1
  2
  3
  6
  12
  </pre>
  
  <p>Under the debugger I can see that <code>MarshalNativeToManaged</code> is executing, and that the values that it returns have been incremented. But these incremented values don't find there way back into the object that is passed to <code>Foo</code>.</p>
  
  <p>What do I need to do to fix this?</p>