在 vb.net 中解压 _WTS_CLIENT_ADDRESS.Address(从终端服务客户端检索 IP 地址)
我有以下结构:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码就快完成了。 我同意你的观点,MSDN 对于该字节数组内部的内容并不是很明确,但是你可以这样做:
前两个字节似乎没有被使用,但在 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 :
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.
真正的答案可以在这里找到。 http://www. tech-archive.net/Archive/Windows/microsoft.public.windows.terminal_services/2007-03/msg00474.html
The real answer can be found here. http://www.tech-archive.net/Archive/Windows/microsoft.public.windows.terminal_services/2007-03/msg00474.html