Q:

Windows sessions questions

I'm developing an app (AppRunner) that executes another app (APP) using the console logged in user's token (CreateProcessAsUser). AppRunner should be able to execute APP from any session/user combo, including session 0 + System or a standard user's session / username.  
Since this topic is new for me, I have a few questions about sessions for which I didn't find answers after googling:
 1. Can there be more than 1 active sessions?
 2. Is it possible to not have any active sessions?
 3. Is there a scenario where user System will fail getting the active session's user token? (WTSQueryUserToken)
 4. If I enumerate through sessions using WTSEnumerateSessions, and I failed finding an active session, is there any point using WTSGetActiveConsoleSessionId? or WTSGetActiveConsoleSessionId is just a wrapper of WTSEnumerateSessions?  
Thank you very much! 

A:

Can there be more than 1 active sessions?

There can be multiple sessions in the WTSActive state at a time, yes.  These represent sessions with logged in users, both local (on the physical machine) as well as remote.  Though, only 1 local session at a time will ever be attached to the physical keyboard/mouse/monitor (KMM) at a time.  That session is what you see onscreen, and it may or may not have a user logged in to it.  And a local user can switch between multiple local sessions, or log in/out of them, at will, thus affecting which session is attached to the KMM at any given moment.  Users that are remoted in will be logged in to their own sessions, but those sessions will never be attached to the KMM.

Is it possible to not have any active sessions?

Yes, if there are no users logged in.

Is there a scenario where user System will fail getting the active session's user token? (WTSQueryUserToken)

Yes.  If there is no user logged in to the requested session, WTSQueryUserToken() will fail and GetLastError() will return ERROR_NO_TOKEN.  Also, even if you query a remote session that has a logged in user, WTSQueryUserToken() may or may not fail depending on HOW the user logged in to the session.

If I enumerate through sessions using WTSEnumerateSessions, and I failed finding an active session, is there any point using WTSGetActiveConsoleSessionId? or WTSGetActiveConsoleSessionId is just a wrapper of WTSEnumerateSessions? 

WTSGetActiveConsoleSessionId() returns the session that is currently attached to the KMM.  That session may or may not have a user logged in, but it can still display a UI.

