4

Under normal circumstances, a VB.NET application of mine can check the ClientName environmental variable to get the name of the workstation the user is connecting from.

So when WorkstationX RDPs into ServerA:

  • ComputerName=ServerA
  • ClientName=WorkstationX

That works fine.

If I right-click on the application and choose Run As Administrator, the ClientName variable is not set.

Does anyone know of a way of easily getting the workstation name of the client connected to the terminal server, even when the application is launched via "Run As Administrator"?

Kevin Fairchild
  • 10,891
  • 6
  • 33
  • 52

2 Answers2

4

If you don't want to deal with the P/Invoke business yourself, you can use Cassia, which wraps it for you:

New Cassia.TerminalServicesManager().CurrentSession.ClientName
Dan Ports
  • 1,421
  • 9
  • 7
  • 1
    I installed a Cassia NuGet package and it is (still) much more reliable at returning the client name that the CLIENTNAME environment variable. – Guru Josh Dec 22 '21 at 12:44
2

The terminal services API might be your answer. I use it in a VB.NET app to log off user sessions on a given server, and to retrieve the names of the workstations connected to those sessions. The code I have has a lot of p-invoke stuff and is rather unwieldy, but I can post it if you want.

Chris Tybur
  • 1,612
  • 2
  • 23
  • 38