在 C# 中使用 in_addr

发布于 2024-08-03 05:30:28 字数 165 浏览 6 评论 0原文

我正在尝试使用 P/Invoke 与本机 DLL 进行交互,但它需要 in_addr 结构参数。我不断看到它的许多不同类型的定义,但哪种定义最好?

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

I'm trying to interact with a native DLL using P/Invoke, but it requires an in_addr struct parameter. I keep seeing many different kinds of definitions for it, but which is the best to use?

Also, how can I convert a C# IPAddress object to an in_addr struct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过度放纵 2024-08-10 05:30:28

如果有人感兴趣,这里是完整的代码。它能够将 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,因此设置一个字段会清除所有其他字段。不过,它似乎有效,所以我不太担心。

In case anyone is interested, here is the full code. It's capable of unpacking an IPAddress object to an in_addr struct and back again.

[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);
    }

}

Like JaredPar, I still don't know what to do with Anonymous3, but it doesn't really matter because it can't be set anyway. Since they all have the same FieldOffset, setting one field clears all of the others. It appears to work, though, so I'm not too worried about it.

抹茶夏天i‖ 2024-08-10 05:30:28

尝试使用以下

[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

编辑 清理了匿名名称。

Try the following

[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;
}

Generated with PInvoke Interop Assistant

EDIT Cleaned up the anonymous names.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文