2013-07-19 63 views
0

的工會C#結構我不是在COM或C++/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轉換/編組爲託管代碼。任何提示將不勝感激。

回答

3

關於翻譯一件珍貴的ressource可以發現here IMO,閱讀它值得:)

關於FooBarZ,你正在尋找每一個單頁[FieldOffset(NBR),這是翻譯工會的方式。

查看STRRET structthe translation in pinvoke關於如何使用它的示例。

+0

良好的資源,謝謝你的鏈接。 – raney