使用联合的C ++ struct转换为C#struct

时间:2016-11-24 03:03:12

标签: c# c++ struct mfc

我在MFC(C ++)中有源代码,并且有一些带有联合的复杂结构。我很难将它们转换为C#代码。 有in6_addr结构。

    typedef struct in6_addr {
    union {
        UCHAR       Byte[16];
        USHORT      Word[8];
    } u;
} IN6_ADDR, *PIN6_ADDR, FAR *LPIN6_ADDR;

in_addr struct

typedef struct in_addr {
        union {
                struct { UCHAR s_b1,s_b2,s_b3,s_b4; } S_un_b;
                struct { USHORT s_w1,s_w2; } S_un_w;
                ULONG S_addr;
        } S_un;
#define s_addr  S_un.S_addr /* can be used for most tcp & ip code */
#define s_host  S_un.S_un_b.s_b2    // host on imp
#define s_net   S_un.S_un_b.s_b1    // network
#define s_imp   S_un.S_un_w.s_w2    // imp
#define s_impno S_un.S_un_b.s_b4    // imp #
#define s_lh    S_un.S_un_b.s_b3    // logical host
} IN_ADDR, *PIN_ADDR, FAR *LPIN_ADDR;

由Microsoft定义。使用这些结构有另一个结构_TUNNELINFO

typedef struct _TUNNELINFO
{
    union {
        IN6_ADDR    In6Addr;
        IN_ADDR       In4Addr;
    } U_Addr;

#define TIAddr4     U_Addr.In4Addr
#define TIAddr6     U_Addr.In6Addr

    IN_ADDR        LocalAddr;
    IN_ADDR        RemoteAddr;

    USHORT        LocalPort;
    USHORT        RemotePort;
    UCHAR        TunnelType;

    UCHAR        VrType;
    USHORT       vrID;
    UCHAR        PrefixLength;

    ULONG        Flag;
    ULONG        GoodTransmits;
    ULONG        GoodReceives;
} TUNNELINFO, *PTUNNELINFO;

我使用pinvoke.net查找in6_addrin_addr C#定义。有我的_TUNNELINFO C#结构。但这似乎不对。

public struct _TUNNELINFO
        {    
           [StructLayout(LayoutKind.Explicit)]
           public struct U_Addr
            {
                [FieldOffset(0)]
                public in6_addr In6Addr;
                [FieldOffset(0)]
                public in_addr In4Addr;
            }
            public U_Addr u_Addr;

            public in_addr LocalAddr;
            public in_addr RemoteAddr;

            public ushort LocalPort;
            public ushort RemotePort;
            public byte TunnelType;

            public byte VrType;
            public ushort vrID;
            public byte PrefixLength;

            public uint Flag;
            public uint GoodTransmits;
            public uint GoodReceives;
        };

有没有人可以帮忙解决这个问题? 谢谢!

0 个答案:

没有答案