Finding Your Windows Terminal Server Virtual IP Address

My name is Jeff Lambert.  I work as part of the Microsoft Developer Support team here at Microsoft support.

My team supports customers who are using the technologies and tools that are part of the Microsoft Windows SDK to write desktop applications.

I normally handle networking cases so when a customer wanted to find out the Windows Terminal Server virtual IP address the client was assigned, I took the case.

Windows Terminal Server virtual IP addresses are treated just like a regular network adapter that has multiple IP addresses assigned. There isn’t any difference at the networking level. A machine with 5 client sessions will have 6 addresses, one for the terminal server itself and one for each client.

We can use the Windows Terminal Server APIs to discover the virtual IP addresses assigned to ours and, if we are an administrator, other sessions as well.

The API, WTSQuerySessionInformation() can be used to query 30 different types of information, and one of them is the virtual IP address assigned to a session. Using the short sample below we can find the virtual IP address our session is using.

// VirtualIP.cpp : Defines the entry point for the console application.

 #pragma comment(lib,"Wtsapi32.lib") // Library for WTS functions

 #include "stdafx.h"
#include "windows.h"
#include "Wtsapi32.h" 

int _tmain(int argc, _TCHAR* argv[])
{

       PWTS_SESSION_ADDRESS wAddr = NULL;
       DWORD dwBytes = 0;             

       // Use WTS_CURRENT_SERVER_HANDLE and
       // WTS_CURRENT_SESSION to get just our virtual address

       if(WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE,
              WTS_CURRENT_SESSION, WTSSessionAddressV4,
              (LPWSTR*)&wAddr, &dwBytes))
       {

              //wAddr->AddressFamily is always 2 (AF_INET)
              printf("Family: %d\n",wAddr->AddressFamily);
              printf("Address: %d.%d.%d.%d\n",wAddr->Address[2],
              wAddr->Address[3],wAddr->Address[4],wAddr->Address[5]);
       } else
       {
              printf("WTSQuerySessionInformation failed. GLE=%d\n",GetLastError());
       }     
       return 0;

}

The sample uses predefined handles for the local terminal server and session. Combining WTSQuerySessionInformation() with the APIs WTSOpenServer() and WTSEnumerateSessions() we can find the virtual IP assigned to any session of any terminal server.

For more information on Windows Terminal Services IP virtual IP addresses see this blog.