The following works if you want to know about YOUR application which is running in YOUR session:
BOOL IsRemoteSession(void)
{
return GetSystemMetrics( SM_REMOTESESSION );
}
But not in general for any process ID.
If you want to know about any arbitrary process which could be running in any arbitrary session then you can use the below method.
You can first convert the process ID to a session ID by calling ProcessIdToSessionId. Once you have the session ID you can use it to call: WTSQuerySessionInformation. You can specify WTSInfoClass
as value WTSIsRemoteSession
and this will give you the information about if that application is a remote desktop connection or not.
BOOL IsRemoteSession(DWORD sessionID)
{
//In case WTSIsRemoteSession is not defined for you it is value 29
return WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, sessionID, WTSIsRemoteSession, NULL, NULL);
}
You should not use GetSystemMetrics(SM_REMOTESESSION) to determine if your application is running in a remote session in Windows 8 and later or Windows Server 2012 and later if the remote session may also be using the RemoteFX vGPU improvements to the Microsoft Remote Display Protocol (RDP)
From learn.microsoft.com/en-us/windows/win32/termserv/… . (A corner case you might have to check) – Dropforge