我不是COM或C ++ / C#封送专家,可以在这种情况下使用一些帮助:
原生代码:
typedef struct _Foo {
FooType a; // enum
WCHAR b[16];
WCHAR c[16];
BOOL d;
ULONG size;
} Foo;
typedef struct _Bar {
GUID a;
WCHAR b[16];
WCHAR c[16];
BOOL d;
} Bar;
typedef struct _Baz {
FILETIME a;
FILETIME b;
ULONG c;
ULONG d;
GUID e;
} Baz;
typedef struct _FooBarBaz
{
SomeType type; // enum
[switch_is(type)] union
{
[case(SomeType.A)]
Foo a;
[case(SomeType.B)]
Bar b;
[case(SomeType.C)]
Baz b;
} data;
} FooBarBaz;
托管代码:
[StructLayout(LayoutKind.Sequential)]
internal struct Foo
{
public FooType a;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public char[] b;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public char[] c;
[MarshalAs(UnmanagedType.Bool)]
public bool d;
[MarshalAs(UnmanagedType.U4)]
public uint e;
}
[StructLayout(LayoutKind.Sequential)]
internal struct Bar
{
public Guid a;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public char[] b;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public char[] c;
[MarshalAs(UnmanagedType.Bool)]
public bool d;
}
[StructLayout(LayoutKind.Sequential)]
internal struct Baz
{
public ComTypes.FILETIME a;
public ComTypes.FILETIME b;
[MarshalAs(UnmanagedType.U4)]
public uint c;
[MarshalAs(UnmanagedType.U4)]
public uint d;
public Guid e;
}
internal struct FooBarBaz
{
public SomeType Type;
// ??????
}
我不确定如何将本机结构FooBarBaz转换/编组为托管代码。这里有任何提示。
答案 0 :(得分:3)
可以找到关于翻译的宝贵资源here IMO,读它值得每一页:)
关于FooBarZ,您正在寻找[FieldOffset(NBR)],这是翻译联合的方式。
查看the translation in pinvoke的STRRET struct以获取有关如何使用它的示例。