I’m trying to get the session ID of the current interactive user. But it must work in a shared desktop scenario such as Citrix, when invoked from a hidden session 0.
I’ve got a couple of issues.
-
In a shared desktop scenario, there may be multiple “Active” sessions (if you run “query session” for example, you will see multiple active sessions). Therefore enumerating sessions (WTSEnumerateSessions) to find an “active” session probably wont work unless you know who the current active user for the current session is! And I’m not sure how to find this.
-
It also needs to be able to run from Session 0 (i.e. hidden). As an example, if the active user is running in session 2 but I am running my code from a hidden session 0, I need to return session 2.
I can retrieve the session ID when running from outside session 0. This is the code I’ve used:
public static uint GetCurrentSessionId()
{
uint sessionId;
uint processId = (uint)System.Diagnostics.Process.GetCurrentProcess().Id;
if (!ProcessIdToSessionId(processId, out sessionId))
{
throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
}
return sessionId;
}
But if I use this in hidden mode, System.Diagnostics.Process.GetCurrentProcess().Id gets the process ID of the current process running in session 0, so it won’t work even if I call WTSQueryUserToken afterwards.
Any pointers appreciated!