Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2005-04-28
Medium Priority
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.  So...how 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
  • 2

Expert Comment

ID: 13891882
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

ID: 13892450
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?

Accepted Solution

anthonywjones66 earned 1000 total points
ID: 13892871
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

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

564 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