Search code examples
.netcpinvokeunions

PInvoke returns C type with union


How would I P/Invoke a C function which returns a union'ed struct?


Solution

  • You would need to use a StructLayout of explicit and the FieldOffset attribute.

    An example of usage:

    <StructLayout(LayoutKind.Explicit, Size:=4)> _
       Public Structure DWord
          <FieldOffset(0)> Public Value As Int32
          <FieldOffset(0)> Public High As Int16
          <FieldOffset(2)> Public Low As Int16
       End Structure