I am trying to write a C++ DLL that would run on VMWare server and would return the client (terminal user) Name and IP Address.
I am using WTSQuerySessionInformation to fetch the ClientName & IP Address. Is
WTSQuerySessionInformation
the right function, or is there a different way to solve this problem?
What I have tried:
ClientName:
The problem with ClientName is that
WTSQuerySessionInformation
returns the IP address separate by hyphen (like 10-0-156-234) instead of the Client Name. Could you please help me understand if I have done anything wrong here?
LPTSTR szClientName = NULL;
DWORD dwSize = 0;
String^ cliName = String::Empty;
if (WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, TSClientName, &szClientName, &dwSize))
{
cliName = gcnew String(szClientName, 0, dwSize);
}
return cliName;
IP Address:
I tried to bypass the above issue by fetching the IP Address and then the hostname and I was successful too when I ran the code from within the organization's network.
But when I login from home to the company's VPN and try the same, it gives me a NAT:ted IP which obviously doesn't map to a Computer Name. Is there any way I can find the "real" IP Address connected to the server. Or if that's not possible, if I can find the computer name, that works fine for me.
LPTSTR ppBuffer = NULL;
DWORD pBytesReturned = 0;
PWTS_CLIENT_ADDRESS pWTSCA = NULL;
WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientAddress, &ppBuffer, &pBytesReturned);
pWTSCA = (PWTS_CLIENT_ADDRESS)ppBuffer;
String^ addrStr = String::Empty;
for (int i = 2; i < 6; i++)
{
addrStr += Convert::ToString(pWTSCA->Address[i]);
if (i != 5)
addrStr += ".";
}