http://stackoverflow.com/questions/267838/how-can-a-windows-service-execute-a-gui-application
Roger Lipscombe's answer, to use WTSEnumerateSessions to find the right desktop, thenCreateProcessAsUser 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...
C# Run Windows Form Application from Service (and in Vista)
Have a nice day!
27 out of 83 rated this helpful - Rate this topic
Typically, services are console applications that are designed to run unattended without a graphical user interface (GUI). However, some services may require occasional interaction with a user. This page discusses the best ways to interact with the user from a service.
Important Services cannot directly interact with a user as of Windows Vista. Therefore, the techniques mentioned in the section titled Using an Interactive Service should not be used in new code.
You can use the following techniques to interact with the user from a service on all supported versions of Windows:
The following technique is also available for Windows Server 2003 and Windows XP:
By default, services use a noninteractive window station and cannot interact with the user. However, an interactive service can display a user interface and receive user input.
Caution Services running in an elevated security context, such as the LocalSystem account, should not create a window on the interactive desktop because any other application that is running on the interactive desktop can interact with this window. This exposes the service to any application that a logged-on user executes. Also, services that are running as LocalSystem should not access the interactive desktop by calling the OpenWindowStation or GetThreadDesktopfunction.
To create an interactive service, do the following when calling the CreateService function:
To determine whether a service is running as an interactive service, call the GetProcessWindowStation function to retrieve a handle to the window station, and theGetUserObjectInformation function to test whether the window station has the WSF_VISIBLE attribute.
However, note that the following registry key contains a value, NoInteractiveServices, that controls the effect of SERVICE_INTERACTIVE_PROCESS:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Windows
The NoInteractiveServices value defaults to 1, which means that no service is allowed to run interactively, regardless of whether it hasSERVICE_INTERACTIVE_PROCESS. When NoInteractiveServices is set to a 0, services with SERVICE_INTERACTIVE_PROCESS are allowed to run interactively.
Windows 7, Windows Server 2008 R2, Windows XP, and Windows Server 2003: The NoInteractiveServices value defaults to 0, which means that services withSERVICE_INTERACTIVE_PROCESS are allowed to run interactively. When NoInteractiveServices is set to a nonzero value, no service started thereafter is allowed to run interactively, regardless of whether it has SERVICE_INTERACTIVE_PROCESS.
Important All services run in Terminal Services session 0. Therefore, if an interactive service displays a user interface, it is visible only to the user who connected to session 0. Because there is no way to guarantee that the interactive user is connected to session 0, do not configure a service to run as an interactive service under Terminal Services or on a system that supports fast user switching (fast user switching is implemented using Terminal Services).