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()); } }
public static boolean isRemoteDesktopSession() { System.getenv("sessionname").toLowerCase().startsWith("rdp"); }
See this HowTo to detect a Citrix session.