在C#中使用in_addr

时间:2009-08-24 18:05:00

标签: c# struct pinvoke ip-address in-addr

我正在尝试使用P / Invoke与本机DLL进行交互,但它需要in_addr struct参数。我一直看到很多不同的定义,但最好用哪个?

另外,如何将C#IPAddress对象转换为in_addr结构?

2 个答案:

答案 0 :(得分:5)

如果有人有兴趣,这里是完整的代码。它能够将IPAddress对象解压缩到in_addr结构并再返回。

[StructLayout(LayoutKind.Sequential)]
public struct in_addr {
    public Anonymous1 S_un;

    [StructLayoutAttribute(LayoutKind.Explicit)]
    public struct Anonymous1 {
        [FieldOffsetAttribute(0)]
        public Anonymous2 S_un_b;

        [FieldOffsetAttribute(0)]
        public Anonymous3 S_un_w;

        [FieldOffsetAttribute(0)]
        public uint S_addr;
    }

    [StructLayoutAttribute(LayoutKind.Sequential)]
    public struct Anonymous2 {
        public byte s_b1;
        public byte s_b2;
        public byte s_b3;
        public byte s_b4;
    }

    [StructLayoutAttribute(LayoutKind.Sequential)]
    public struct Anonymous3 {
        public ushort s_w1;
        public ushort s_w2;
    }

    public in_addr(IPAddress address) : this(address.GetAddressBytes()) { }

    public in_addr(byte[] address) {
        // Set this first, otherwise it wipes out the other fields
        S_un.S_un_w = new Anonymous3();

        S_un.S_addr = (uint)BitConverter.ToInt32(address, 0);

        S_un.S_un_b.s_b1 = address[0];
        S_un.S_un_b.s_b2 = address[1];
        S_un.S_un_b.s_b3 = address[2];
        S_un.S_un_b.s_b4 = address[3];
    }

    /// <summary>
    /// Unpacks an in_addr struct to an IPAddress object
    /// </summary>
    /// <returns></returns>
    public IPAddress ToIPAddress() {
        byte[] bytes = new[] {
            S_un.S_un_b.s_b1,
            S_un.S_un_b.s_b2,
            S_un.S_un_b.s_b3,
            S_un.S_un_b.s_b4
        };

        return new IPAddress(bytes);
    }

}

和JaredPar一样,我仍然不知道如何处理Anonymous3,但它并不重要,因为它无论如何都无法设置。由于它们都具有相同的FieldOffset,因此设置一个字段会清除所有其他字段。它似乎有用,所以我不太担心它。

答案 1 :(得分:2)

尝试以下

[StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct in_addr {

    /// Anonymous1
    public Anonymous1 S_un;
}

[StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Explicit)]
public struct Anonymous1 {

    /// Anonymous2
    [System.Runtime.InteropServices.FieldOffsetAttribute(0)]
    public Anonymous2 S_un_b;

    /// Anonymous3
    [System.Runtime.InteropServices.FieldOffsetAttribute(0)]
    public Anonymous3 S_un_w;

    /// u_long->unsigned int
    [System.Runtime.InteropServices.FieldOffsetAttribute(0)]
    public uint S_addr;
}

[StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct Anonymous2 {

    /// u_char->unsigned char
    public byte s_b1;

    /// u_char->unsigned char
    public byte s_b2;

    /// u_char->unsigned char
    public byte s_b3;

    /// u_char->unsigned char
    public byte s_b4;
}

[StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct Anonymous3 {

    /// u_short->unsigned short
    public ushort s_w1;

    /// u_short->unsigned short
    public ushort s_w2;
}

使用PInvoke Interop Assistant生成

编辑清理匿名名称。