How can I tell if a user is running in a terminal services environment?

Posted on 2005-04-28
Last Modified: 2008-01-09
Hi all,

Today, our software product ran into a problem with terminal services environments.  The problem is this

1) We make each customer register their account with us.
3) We do want multiple terminal services logins on one server to have to register each account with us.
3) We do NOT want multiple logins on one computer to have to register each account with us.

So, the problem arises in determining if a user is acting within a terminal services environment or not. do I figure that out?

I know of an ugly hack that should work well.  It's comparing what is returned from 2 different API calls.  GetWindowsDirectory and GetSystemWindowsDirectory.

On a terminal services environment, GetWindowsDirectory would return something like C:\Documents and Settings\[username]\Windows\.  And GetSystemWindowsDirectory would return C:\Windows.

On a multiple login environment (such as one computer that has multiple logins, but not through terminal services), GetWindowsDirectory and GetSystemWindowsDirectory would both return C:\Windows.

I don't like this fix too well.  It should work, but it's more of a hack than a true solution.   So is there a nice API call I can make to see if I'm truly in a terminal services environment?

Question by:helix400
    LVL 7

    Expert Comment

    Are you just using TS or is Citrix also involved?  Regardless, you should be able to do this fairly easily in a number of ways.  Check the SESSIONNAME and CLIENTNAME environment variables on your TS box for a start.  If %CLIENTNAME%==%COMPUTERNAME% ...  TS admins have been dealing with this for years in order to prevent domain logon scripts from running on terminal servers, so you may have better luck poking around from that angle.  Good luck!

    Author Comment

    Thanks for the interesting tips!  I often pride myself in finding obscure answers using Google, but this one was too difficult.  You've given me many areas to investigate.  I'll check them out tomorrow at work.  

    As for TS and Citrix...we may run into both.  I ask this question because our software product is growing in use, and we are starting to run into terminal server setups that are giving us headaches.  Is there anything special I'll need to know for Citrix that can't be found through Google searching?
    LVL 8

    Accepted Solution

    Stick this in a module somewhere:-

    Private Declare Function GetSystemMetrics Lib "user32.dll" (ByVal nIndex As Long) As Long
    Private Const SM_REMOTESESSION As Long = &H1000

    Public Property Get InRemoteSession() As Boolean
    InRemoteSession = GetSystemMetrics(SM_REMOTESESSION) <> 0
    End Property

    Now when you want to know if a remote session is running:-

    If InRemoteSession Then
      ' Do something different
    End If


    Author Comment

    I finally got to test this out, it worked flawlessly.  Thank you!

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Suggested Solutions

    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now