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:41

I think as long as you have only one user logged in, it will automatically display on that user's desktop.

Anyway, be very careful when having a service start an exe.

If the write access to the folder with the exe is not restricted, any user can replace that exe with any other program, which will then be run with sytem rights. Take for example cmd.exe (available on all windows sytems). The next time the service tries to start your exe, you get a command shell with system rights...

查看更多
几人难应
3楼-- · 2019-01-01 07:45

The short answer is "You don't", as opening a GUI program running under another user context is a security vulnerability commonly known as a Shatter Attack.

Take a look at this MSDN article: Interactive Services. It gives some options for a service to interact with a user.

In short you have these options:

  • Display a dialog box in the user's session using the WTSSendMessage function.

  • Create a separate hidden GUI application and use the CreateProcessAsUser function to run the application within the context of the interactive user. Design the GUI application to communicate with the service through some method of interprocess communication (IPC), for example, named pipes. The service communicates with the GUI application to tell it when to display the GUI. The application communicates the results of the user interaction back to the service so that the service can take the appropriate action. Note that IPC can expose your service interfaces over the network unless you use an appropriate access control list (ACL).

    If this service runs on a multiuser system, add the application to the following key so that it is run in each session: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run. If the application uses named pipes for IPC, the server can distinguish between multiple user processes by giving each pipe a unique name based on the session ID.

查看更多
栀子花@的思念
4楼-- · 2019-01-01 07:45

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.

This is taken from : http://msdn.microsoft.com/en-us/library/ms683502(VS.85).aspx

查看更多
登录 后发表回答