Take the 2-minute tour ×

I have a windows service that can create an executable in the users windows session, via calling the "CreateProcessAsUser" function. This works fine as long as there is a windows session already there. In the case that there isn't one already I'd like to be able to create one programmatically. Is this is possible? Can't seem to find a function to do it.

share|improve this question
add comment

3 Answers

You cannot create a new session from a service. Sessions are managed by the OS. New ones get created when users logon interactively.

share|improve this answer
You can use the WTS API, such as WTSGetActiveSessionId() and WTSEnumerateSessions(), to determine if a user session exists before calling CreateProcessAsUser(). –  Remy Lebeau Dec 21 '12 at 20:52
For the record: Windows Server 2012 supports the Remote Desktop Protocol Provider API, which you could use to create a session programatically. Also, at least in theory, you could write your own Remote Desktop client (or modify one of the open source clients) to create a new session on any supported version of Windows - provided that Remote Desktop is enabled, of course. –  Harry Johnston Dec 23 '12 at 0:06
@RemyLebeau this is what I do already, I'm trying to avoid problems where the session has been unexpectedly closed. –  Robert Dec 27 '12 at 9:45
@HarryJohnston thanks, this sounds like solutions though have to admit I was hoping for something simpler. –  Robert Dec 27 '12 at 9:47
@Robert: you and me both. If you do manage to get a working solution, and are able to share it, could you let me know? (My profile includes my email address.) –  Harry Johnston Dec 27 '12 at 11:23
add comment

This isn't quite the solution for the question I asked, but it was the solution that helped achieve what I was trying to achieve by asking this question, if you see what I mean.

Rather than have having a windows services that creates a server session you can configure windows to automatically logon at boot time. This still means someone could accenditally log off, but cures the main reason for sessions disappearing: the server being rebooted. Use the following steps to activate auto-logon:

  2. Type regedit and hit enter to open the Registry Editor
  3. Then browse to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WindowsNT\CurrentVersion\Winlogon\
  4. Set AutoAdminLogon = 1 (create it if doesn't exist its a string variable)
  5. Set DefaultUserName = your username (create it if doesn't exist its a string variable)
  6. Set DefaultPassword = your password (create it if doesn't exist its a string variable)

Instructions were taken from this post: http://channel9.msdn.com/Blogs/coolstuff/Tip-Auto-Login-Your-Windows-7-User-Account

share|improve this answer
add comment

What about the LogonUser function?


share|improve this answer
Unfortunately "session" is an overloaded term in Windows. In this context, I think the OP is talking about Remote Desktop (aka Windows Terminal Services) session rather than a logon session. Robert, could you please clarify? –  Harry Johnston Dec 23 '12 at 0:01
I'm not sure I understand the difference here, it doesn't need to be a remote desktop session per se, but it does need to have a sessionid > 0 so that it can execute programs with a gui. –  Robert Dec 27 '12 at 9:54
Remote Desktop Services is the part of Windows that allows user switching and session zero isolation, as well as actual Remote Desktop connections. You're definitely talking about Remote Desktop session IDs here, so LogonUser won't solve your problem. –  Harry Johnston Dec 27 '12 at 11:22
add comment

Your Answer


