WTSQuerySessionInformation(WTSClientProtocolType) **ppBuffer = 2 = WTS_PROTOCOL_TYPE_RDP when the
current session is connected to via remote desktop and 0 = WTS_PROTOCOL_TYPE_CONSOLE when not.
WTSQuerySessionInformation(WTSConnectState) **ppBuffer = 0 = WTSActive when the session is active either
at the console or via remote desktop, 4 = WTSDisconnected when the session is not active.
When WTSGetActiveConsoleSessionId() == the value you get back from ProcessIdToSessionId(GetCurrentProcessId()) then the session is being used at the console (local computer) and is active.
GetSystemMetrics(SM_REMOTESESSION) == 1 indicates either the current session is connected to via a remote desktop client OR the current session is not active (a remote desktop connection may or may not be present on a FUS enabled machine). GetSystemMetrics(SM_REMOTESESSION) == 0 indicates the current session is the active console session (ie locally logged in to the machine). Bottom line, don't rely the non-zero value indicating a remote desktop connection unless you know code will only run when you are the active session (eg WM_PAINT handler, keyboard or mouse event handlers).
Note that on Vista, unlike XP, FUS (Fast User Switching) is enabled for domain joined computers and so you can have more than one user logged in at the same time just like you could in XP non-domain joined computers.
bool fActiveSession = false;
DWORD dwSessionID = -1; // 0 is 1st console session created on XP, 1 is 1st console session on Vista
LPTSTR pData = NULL;
DWORD cbReturned = 0;
ProcessIdToSessionId(GetCurrentProcessId(), &dwSessionID);
if( WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, dwSessionID, WTSConnectState, &pData, &cbReturned)
&& (cbReturned == sizeof(INT)) )
{
// if we get WTSActive we're in the active session, otherwise we assume we're not in the active session (WTSDisconnected)
fActiveSession = (*((INT *)pData) == WTSActive) ? true : false;
}
WTSFreeMemory(pData);
// fActiveSession indicates if we are the active session (true) or not (false)
// Note on a terminal services server, there can be more than one active session
// on most machines (ie desktops) there can be only one active session