How can a Windows service execute a GUI applicatio

2019-01-01 06:47发布

I have written a Windows service that allows me to remotely run and stop applications. These applications are run using CreateProcess, and this works for me because most of them only perform backend processing. Recently, I need to run applications that present GUI to the current log in user. How do I code in C++ to allow my service to locate the currently active desktop and run the GUI on it?

9条回答
ら面具成の殇う
2楼-- · 2019-01-01 07:28

If you launch a GUI from your service it will show up on the currently active desktop.

But only if you adjusted the service permissions: You need to allow it to interact with the desktop.

查看更多
还给你的自由
3楼-- · 2019-01-01 07:32

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...

查看更多
呛了眼睛熬了心
4楼-- · 2019-01-01 07:37

That problems Session 0 , Interactive Services , Windows Service Allow Service To Interact With Desktop on Windows 7 or Windows Vista

You can read this article http://www.codeproject.com/KB/vista-security/SubvertingVistaUAC.aspx

I try explained here it's working on Windows 7

查看更多
心情的温度
5楼-- · 2019-01-01 07:40

WTSEnumerateSessions and CreateProcessAsUser.

查看更多
何处买醉
6楼-- · 2019-01-01 07:40

On Win2K, XP and Win2K3 the console user is logged on in Session 0, the same session the services live in. If a service is configured as interactive, it'll be able to show the UI on the user's desktop.

However, on Vista, no user can be logged on in Session 0. Showing UI from a service there is a bit trickier. You need to enumerate the active sessions using WTSEnumerateSessions API, find the console session and create the process as that user. Of course, you need also a token or user credentials to be able to do that. You can read more details about this process here.

查看更多
路过你的时光
7楼-- · 2019-01-01 07:41

Several people suggested WTSEnumerateSessions and CreateProcessAsUser. I wonder why no one suggested WTSGetActiveConsoleSessionId, since you said you only want to target one logged in user.

Several people sure are right to suggest CreateProcessAsUser though. If you call plain old CreateProcess the way you said, then the application's GUI will run with your service's privileges instead of the user's privileges.

查看更多
登录 后发表回答