How to get an 'Active Session ID' in a windows server OS?
I need to execute my application without user interaction at machine start up. I need this to run in all Windows XP, Windows Vista, Windows Server 2003, Windows 7 and Windows Server 2008 OSs.
For this, I followed these steps,- Get active session id using WTSGetActiveConsoleSessionId
- Obtain the process id of the winlogon process that is running within the currently active session
- Obtain a handle to the winlogon process
- Obtain a handle to the access token of the winlogon process using OpenProcessToken
- Duplicate the access token of the winlogon process DuplicateTokenEx
- Create a new process in interactive window station and in the current user's logon session using CreateProcessAsUser
- And then close all handles.
And it works for all Workstations and when the Servers have an active console id running. It's pretty obvious because in step 1 I开发者_JAVA百科 catch the Active Console Id. I found some other ways to start my applications in OSs before Windows Session 0 Isolation was introduced.
Now I need to catch whether WTSGetActiveConsoleSessionId has returned a valid id and if not create a new session and start my application in it or catch an active RDP session an run my application in it or show the logged in users screen for the user to decide (which would be ideal since my application is a screen capturing one). I'm sure their should be a way to do this, and I need your help for this.
Thanks a lot!I finally found a solution to my problem. Thought of publishing it for the benefit of all readers.
The 7 steps mentioned here are correct.
Adding to them please note that
- Console session even if not listed as active, is still there.
- When you run the executable when console is inactive, it run with the last session id it had for the console, giving you the option to login to the machine by providing credentials
- When you provide credentials it reactivate the console session giving a new session id
- Your application gets unreachable at this point since it was launched in the previous session of the inactive console.
- Detect the new active console session id by monitoring WTSGetActiveConsoleSessionId result and if it has changed then relaunch your executable in the console session.
Hope it saved your time :)
精彩评论