在 vb.net 中解压 _WTS_CLIENT_ADDRESS.Address(从终端服务客户端检索 IP 地址)

发布于 2024-07-09 01:50:35 字数 1054 浏览 9 评论 0原文

我有以下结构:

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure _WTS_CLIENT_ADDRESS
        Public AddressFamily As Integer
        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=20)> _
        Public Address() As Byte
    End Structure

由以下调用填充:

        Dim _ClientIPAddress As New _WTS_CLIENT_ADDRESS
        Dim rtnPtr As IntPtr
        Dim rtncount As Int32

        NativeMethods.WTSQuerySessionInformation(CInt(NativeMethods.WTS_CURRENT_SERVER_HANDLE), NativeMethods.WTS_CURRENT_SESSION, NativeMethods.WTS_INFO_CLASS.WTSClientAddress, rtnPtr, rtncount)
        '_ClientIPAddress()
        _ClientIPAddress = _
            CType(System.Runtime.InteropServices.Marshal.PtrToStructure(rtnPtr, GetType(_WTS_CLIENT_ADDRESS)), _WTS_CLIENT_ADDRESS)

正在填充地址字节数组,但我不知道如何将其转换为有用的字符串或整数值。 MDSN 文档很少: http://msdn.microsoft .com/en-us/library/aa383857(VS.85).aspx

I have the following structure:

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure _WTS_CLIENT_ADDRESS
        Public AddressFamily As Integer
        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=20)> _
        Public Address() As Byte
    End Structure

Which is populated by the following call:

        Dim _ClientIPAddress As New _WTS_CLIENT_ADDRESS
        Dim rtnPtr As IntPtr
        Dim rtncount As Int32

        NativeMethods.WTSQuerySessionInformation(CInt(NativeMethods.WTS_CURRENT_SERVER_HANDLE), NativeMethods.WTS_CURRENT_SESSION, NativeMethods.WTS_INFO_CLASS.WTSClientAddress, rtnPtr, rtncount)
        '_ClientIPAddress()
        _ClientIPAddress = _
            CType(System.Runtime.InteropServices.Marshal.PtrToStructure(rtnPtr, GetType(_WTS_CLIENT_ADDRESS)), _WTS_CLIENT_ADDRESS)

The address byte array is being populated, but I have no idea how to convert it into a useful string or integer values. The MDSN documentation is sparse: http://msdn.microsoft.com/en-us/library/aa383857(VS.85).aspx

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

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

发布评论

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

评论(2

情深如许 2024-07-16 01:50:35

您的代码就快完成了。 我同意你的观点,MSDN 对于该字节数组内部的内容并不是很明确,但是你可以这样做:

IPAddress address = new IPAddress(_ClientIPAddress.Address.Skip(2).Take(4).ToArray());

前两个字节似乎没有被使用,但在 AF_INET (即 IPv4 或 2)的情况下接下来的四个字节是客户端的 IPv4 地址。

您可能还想确保您的代码能够正确处理 IPv6 (AF_INET6),或者处理 AF_INET6 是一个可能值的事实。 对于此协议,您可能需要读取 16 个字节而不是 4 个字节。

You're almost there with your code. I agree with you, the MSDN is not quite explicit on what's inside that byte array, but here's what you can do :

IPAddress address = new IPAddress(_ClientIPAddress.Address.Skip(2).Take(4).ToArray());

The first two bytes do not seem to be used, but in the case of AF_INET (which is IPv4, or 2) the next four bytes are the IPv4 address of the client.

You might also want to make sure that your code will handle IPv6 (AF_INET6) properly, or handle the fact that AF_INET6 is a likely value. You'll probably need to read 16 bytes instead of 4 for this protocol.

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