Is there any way to start a GUI application from a windows service on Windows 7?

我的梦境 提交于 2019-11-27 05:19:34
David Heffernan

This change was made for a reason and not simply to annoy developers. The correct approach is to put your UI in a different program and communicate with the session through a pipe, or some other IPC mechanism. The recommendation that services do not present UI is more than 10 years old now.

You should really try to follow these rules, even though it may seem inconvenient to begin with. On the plus side you will enjoy the benefit of keeping your service logic and UI logic separate

If your services runs under the LOCALSYSTEM account then you can check "Allow service to interact with desktop", for the benefit of legacy services that would fail if they could not show UI. But it won't help you anyway because the UI will show in session 0 where it is never seen!

I recommend you take a read of the official Microsoft document describing session 0 isolation.

There is a way to do this. If you need to show a simple message box you can use the WTSSendMessage Routine. If you need a complex UI elements you can put it in a separate program and you need to use CreateProcessAsUser Routine. In this sample provided by microsoft you can see the process.

http://blogs.msdn.com/b/codefx/archive/2010/11/26/all-in-one-windows-service-code-samples.aspx

Windows 7 introduced what is called "Session 0 isolation" that in practice means that every service (except system services) run in a separate non-interactive session. For this reason you cannot directly create a GUI from within the service, except if you run in legacy mode by flagging the Interact With Destop option, which is not good if you plan to run your service for some years in the future.

As David Heffernan said, the best is to use a client-server architecture. WCF makes it easy to communicate with named pipes.

This page is a good starting point to read about Session 0 Isolation and this white paper is also very good.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!