对于 pinvoke 调用,我需要制作一个相当于以下内容的结构:
typedef struct _Somenativestruct {
PCWSTR filename;
DWORD count;
DWORD anothercount;
AnEnumWithByteSize info;
union {
Structwithoneintfield Progress;
Anotherstructwithoneintfield Result;
};
} Somenativestruct , *PSomenativestruct ;
由于它在结构中有一个联合,因此我必须将其设为 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;
}