Click here to Skip to main content
15,880,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C++
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?

C++
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.

C++
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 += ".";
}
Posted
Updated 25-Apr-18 4:34am
Comments
Richard MacCutchan 25-Apr-18 3:53am    
That most likely means that the IP address does not resolve to a name. You might get more details from the WTSClientInfo option.
vivek2414 25-Apr-18 10:36am    
Thank you Richard. I have tried that but I get the same output. Here is the code:

if (WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientInfo, &ppBuffer, &dwSize))
{
    pWtsClientInfo = (PWTSCLIENTW)ppBuffer;
    for (int i = 0; i < 22; i++ )
        clientInfo += Convert::ToString(pWtsClientInfo->ClientName[i]);
}
else
{
    return "Failed";
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900