In my opinion reading the environment variable is the wrong way as there's no guarantee that the variable will be set.
The "proper" way is to call the
GetSystemMetrics API with the
SM_REMOTESESSION parameter.
Zitat:
This system metric is used in a Terminal Services environment. If the calling process is associated with a Terminal Services client session, the return value is nonzero. If the calling process is associated with the Terminal Services console session, the return value is 0.
Windows Server 2003 and Windows XP: The console session is not necessarily the physical console. For more information, see WTSGetActiveConsoleSessionId.