gpt4 book ai didi

c - WTS_CLIENT_ADDRESS 地址无法正确打印

转载 作者:可可西里 更新时间:2023-11-01 11:48:38 26 4
gpt4 key购买 nike

我正在尝试获取登录到计算机的所有 RDP 连接的 IP 地址。我正在使用 WTSEnumerateSessions 和 WTSQuerySessionInformation 来检索指向包含地址的 WTS_CLIENT_ADDRESS 结构的指针。这是代码的一部分:

WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE,
SessionNo,
WTSClientAddress,
&ppBuffer,
&pBytesReturned);

address = (WTS_CLIENT_ADDRESS *)ppBuffer;

if (AF_INET == address->AddressFamily)
{
printf("Client Address : %s\n", (LPSTR)address->Address);
}
else if (AF_INET6 == address->AddressFamily)
{
//print IPV6
}

我可以获得 AddressFamily 并且值很好,但是当我尝试打印 Address 时出现乱码。 MSDN 说该值是一个空终止字符串,但它不会像它应该的那样打印。这是链接:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa383857(v=vs.85).aspx

我觉得我在这里遗漏了一些东西。

最佳答案

您没有考虑 WTS_INFO_CLASS 中的以下注释文档:

WTSClientAddress
The network type and network address of the client. For more information, see WTS_CLIENT_ADDRESS.

The IP address is offset by two bytes from the start of the Address member of the WTS_CLIENT_ADDRESS structure.

另外,不管文档怎么说,IPv4 地址不是以 null 结尾的字符串!它实际上是 IPv4 地址的原始 4 字节(就像 IPv6 地址是原始字节一样)。

试试这个:

WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE,
SessionNo,
WTSClientAddress,
&ppBuffer,
&pBytesReturned);

address = (WTS_CLIENT_ADDRESS *)ppBuffer;

if (AF_INET == address->AddressFamily)
{
printf("Client Address : %d.%d.%d.%d\n",
address->Address[2], address->Address[3], address->Address[4], address->Address[5]);
}
else if (AF_INET6 == address->AddressFamily)
{
//print IPV6
}

关于c - WTS_CLIENT_ADDRESS 地址无法正确打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36751066/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com