Tuesday, February 15, 2005
« Fun with Regions - Region Generator Tool... | Main | Working at Microsoft »

Frequently, when developing applications we go out of our way to be considerate to end users.  We pop up nice dialogs, use friendly colors, provide keyboard shortcuts as necessary, sometimes we even write help documentation.  But how often do we go out of our way to consider those that run our applications over a terminal session?

Sure, it's cool to have the fancy dialog that fades in and out, but over a terminal session that can be horrendously slow.  As a courtesy, our applications should check to see if they are running in a terminal session environment and, if so, degrade the graphics output gracefully.  But how do we check?  It's amazingly simple.  Dipping down to the GetSystemMetrics() function in the Win32 API is the key.

[DllImport(”user32.dll”)]
public static extern int GetSystemMetrics(int nIndex);

public const int SM_REMOTESESSION = 0x1000;

public static bool IsTerminalSession() {
   return ( 0 != GetSystemMetrics(SM_REMOTESESSION) );
}

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.

Interestingly, the vast majority of the GetSystemMetrics() functionality was ported over to the .NET Framework to the System.Windows.Forms.SystemInformation class, but the SM_REMOTESESSION is missing (unless I'm just blind).  It actually makes sense to be missing from the standpoint that it really doesn't have anything to do with the Forms namespace.  Is it somewhere else in the Framework and I just don't know about it?

For the time being, however, that one-liner is all it takes!

Tuesday, February 15, 2005 8:04:00 PM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [7]  |  Trackback