pinvoke 呼び出しの場合、次のように相当する構造体を作成する必要があります。
typedef struct _Somenativestruct {
PCWSTR filename;
DWORD count;
DWORD anothercount;
AnEnumWithByteSize info;
union {
Structwithoneintfield Progress;
Anotherstructwithoneintfield Result;
};
} Somenativestruct , *PSomenativestruct ;
構造体に 1 つの共用体があるため、layout.explicit にする必要がありますが、問題は PCWSTR であるファイル名に対して行うオフセットです。32 ビットのポインターなので、オフセットを 4 にできますか? では、以下は正しいでしょうか?
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode)]
public struct Mynet40struct
{
[FieldOffset(0)]
private [MarshalAs(UnmanagedType.LPWStr)] string filename;
[FieldOffset(4)]
private int count;
[FieldOffset(8)]
private int anothercount;
[FieldOffset(12)]
AnEnumWithByteSize info;
[FieldOffset(13)]
StructWithOneIntField progress;
[FieldOffset(13)]
AnotherStructWithOneIntField result;
}