次の方法で共有


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.

Comments

  • Anonymous
    November 13, 2013
    What about IPv6 return types? Lookign at the API it states a byte[20] array for the return which means that it should be able to return an IPv6 address - can you give an example of a client connecting over IPv6 and returning a valid address and also how you detect from the Address Family that the address is IPv6..

  • Anonymous
    April 01, 2014
    I accomplish this on my Server 2012 and Server 2008 Terminal servers throught he event viewer Open event viewer Applications and Services Logs Microsoft Windows TerminalServices–TSAppSvr–TSVIP.  

  • Anonymous
    April 10, 2015
    I want to clarify my previous tip.   I have two RDS servers.    The user who got the VIP in question could have logged on to and gotten a VIP from either server.   In order to find out who owns what VIP, it is conceivable that I would have to do the following on both of my servers.

  1.  Open event viewer.  
  2.  Applications and Services Logs
  3.  Microsoft
  4.  Windows
  5.  TerminalServices–TSAppSvr–TSVIP.
  6.  Scroll through the events until you see the VIP being assigned.  The message will include the user's ID.