Search code examples
.net-corepinvokemarshalling

struct field marshaling failed on linux


I'm using following struct for data marshaling with PInvoke

[StructLayout(LayoutKind.Sequential)]
struct Data
{
  int id;
  IntPtr state;
  object obj;
}

And all works fine on Windows, but on Linux I'm getting the error:

Cannot marshal field 'obj' of type 'Data': Invalid managed/unmanaged type combination (Marshaling to and from COM interface pointers isn't supported).

Why? Is it possible to disable marshaling for the specific field ?


Solution

  • Digging the CLR sources I've found that the issue is really Linux specific, and tied to COM interop feature, which is windows-only.

    So I've fixed the problem using IntPtr for obj (as @Simonare suggested) and GCHandle.