title: What is the difference between C# marshaled struct pointers?
body: <p>Ok, this is working:</p>
  
  <pre><code>[StructLayout(LayoutKind.Explicit, Size = 28)]
  public unsafe struct HandleProxy
  {
      [FieldOffset(0), MarshalAs(UnmanagedType.I4)]
      public JSValueType _ValueType; // JSValueType is an enum
  
      [FieldOffset(4), MarshalAs(UnmanagedType.I4)]
      public Int32 _ManagedObjectID; 
  
      [FieldOffset(8)]
      public void* _NativeEngineProxy;
  
      [FieldOffset(16), MarshalAs(UnmanagedType.I4)]
      public Int32 _EngineID;
  
      [FieldOffset(20)]
      public void* _Handle;
  }
  
  [DllImport("Proxy")]
  public static extern void DisposeHandleProxy(HandleProxy* handle);
  </code></pre>
  
  <p>... and this does not ...</p>
  
  <pre><code>[StructLayout(LayoutKind.Explicit, Size = 20)]
  public unsafe struct ValueProxy
  {
      [FieldOffset(0), MarshalAs(UnmanagedType.I4)]
      public JSValueType _ValueType; // 32-bit type value.
  
      [FieldOffset(4), MarshalAs(UnmanagedType.Bool)]
      public bool _Boolean;
  
      [FieldOffset(4), MarshalAs(UnmanagedType.I4)]
      public Int32 _Integer;
  
      [FieldOffset(4)]
      public double _Number;
  
      [FieldOffset(12)]
      public void* _String;
  }
  
  [DllImport("Proxy")]
  public static extern void DisposeValueProxy(ValueProxy* valueProxy);
  </code></pre>
  
  <p>So, what's the difference? I'm missing something. A call to "DisposeValueProxy() gives the following error:</p>
  
  <p>"Cannot marshal 'parameter #1': Pointers cannot reference marshaled structures.  Use ByRef instead."</p>
  
  <p>(and yes, I can simply use IntPtr/void* instead of "ValueProxy*" but that's not my point).</p>
  
  <p>A call to "DisposeHandleProxy()" works fine.</p>
  
  <p>Let's see if someone can figure this one out. ;)</p>