Share this page 

Detect if running in remote session (Windows)Tag(s): Environment


[Windows only]
To detect if an application is running in a Remote Desktop session, you can call the Windows API GetSystemMetrics. The GetSystemMetrics() function will return a non-zero value with a SM_REMOTESESSION parameter value if the application is associated with a client terminal session.

It is useful to detect if running in a remote session to optimize visual effects or colors.

In Java, you need to use JNI (Java Native Interface) to call this Windows native API. Native Call is an Open Source project which provide an easy way to that.

http://johannburkard.de/software/nativecall/

All you need is 2 jars (nativecall-0,4,1.jar and nativeloader-200505172341.jar) plus 1 DLL (NativeCall.dll) in your classpath.

When running from Eclipse, the DLL should be in the bin directory of your application.

import java.io.IOException;
import com.eaio.nativecall.IntCall;
import com.eaio.nativecall.NativeCall;

public class WindowsUtils {

   public static final int SM_REMOTESESSION = 4096;  // remote session

   private WindowsUtils() {}

   public static boolean isRemote() throws SecurityException, UnsatisfiedLinkError,
      UnsupportedOperationException, IOException
   {
      NativeCall.init();
      IntCall ic = null;
      try {
         ic = new IntCall("user32", "GetSystemMetrics");
         int rc = ic.executeCall(new Integer(SM_REMOTESESSION));
         return (rc gt; 0);
      }
      finally {
         if (ic != null) ic.destroy();
      }
   }

   public static void main(String ... args) throws Exception {
      System.out.println(WindowsUtils.isRemote());
   }
}
An easy way is to check the Windows environment variable sessionname. The value of this environment variable will be 'Console' for a normal, local session. For an Remote Desktop session it will contain the phrase 'RDP'.
public static boolean isRemoteDesktopSession() {
   System.getenv("sessionname").toLowerCase().startsWith("rdp");
}
Note that environment varialbe values are read at the JVM startup. So if the JVM process was started by a console session, but then accessed by an RDP session, further calls to System.getenv("sessionname") still return 'Console'

See this HowTo to detect a Citrix session.