How can a Windows service execute a GUI application?

Roger Lipscombe’s answer, to use WTSEnumerateSessions to find the right desktop, then CreateProcessAsUser to start the application on that desktop (you pass it the handle of the desktop as part of the STARTUPINFO structure) is correct.

However, I would strongly recommend against doing this. In some environments, such as Terminal Server hosts with many active users, determining which desktop is the ‘active’ one isn’t easy, and may not even be possible.

But most importantly, if an application will suddenly appear on a user’s desktop, this may very well occur at a bad time (either because the user simply isn’t expecting it, or because you’re trying to launch the app when the session isn’t quite initialized yet, in the process of shutting down, or whatever).

A more conventional approach would be to put a shortcut to a small client app for your service in the global startup group. This app will then launch along with every user session, and can be used start other apps (if so desired) without any juggling of user credentials, sessions and/or desktops.

Also, this shortcut can be moved/disabled by administrators as desired, which will make deployment of your application much easier, since it doesn’t deviate from the standards used by other Windows apps…

Leave a Comment