windowswinapiremote-accessremote-desktopterminal-services

WTSClientInfo returns NULL on Windows 10 OS Over RDP Connection


I am remotely connecting to Windows 10 Client OS through RDP client. When I collected WTSIsRemoteSession and WTSClientProtocolType using WTSQuerySessionInformationW() I correctly received that it was a remote session and protocol was RDP. However, when I tried to collect Client info as follows, the condition for AF_INET fails and Client Device Id is also blank. Need to know what I may be missing here. I noticed that all of the PWTSCLIENTA data was null. Strange thing was that, the query function itself did not fail.Following is my code.

if (WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientInfo, &pData, &bytesReturned)){
            PWTSCLIENTA address = (PWTSCLIENTA)pData;
            printf("\tWTSQuerySessionInformationW - session %d - %s returned \"%p\"\n", WTS_CURRENT_SESSION, "WTSClientInfo", pData);
            if (AF_INET == address->ClientAddressFamily)
            {
                printf("\n\tClient Address : %d.%d.%d.%d\n", address->ClientAddress[2], address->ClientAddress[3], address->ClientAddress[4], address->ClientAddress[5]);
            }
            printf("\tClient DeviceId : %s\n\n", address->DeviceId);
    }

Solution

  • You are calling WTSQuerySessionInformationW(), but you are casting pData to PWTSCLIENTA when you need to cast it to PWTSCLIENTW instead. As such, you are accessing the ClientAddressFamily, ClientAddress, and DeviceId fields using the wrong byte offsets.

    Also, make sure you handle the possibility of the ClientAddressFamily being AF_INET6 instead of AF_INET.

    Try something more like this:

    PWTSCLIENTW pData;
    if (WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientInfo, (LPWSTR*)&pData, &bytesReturned)){
        printf("\tWTSQuerySessionInformationW - session %d - %s returned \"%p\"\n", WTS_CURRENT_SESSION, "WTSClientInfo", pData);
        char ipAddress[46];
        if (inet_ntop(pData->ClientAddressFamily,pData->ClientAddress), ipAddress, sizeof(ipAddress)))
        {
            printf("\n\tClient Address : %s\n", ipAddress);
        }
        printf("\tClient DeviceId : %ls\n\n", pData->DeviceId);
        WTSFreeMemory(pData);
    }