有没有办法从 Windows 7 上的 Windows 服务启动 GUI 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5063731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:13:22  来源:igfitidea点击:

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

c#windowswindows-services

提问by Brian

I have done a lot of searching to find a way to start a GUI application from a windows service on Windows 7. Most of what I have found is that with Windows 7 services now run in a separate user session and can not display any graphical interface to the current user. I'm wondering is there is any kind of workaround or different way of accomplishing something like this? Can the service start a process in a different user session?

我进行了大量搜索以找到一种从 Windows 7 上的 Windows 服务启动 GUI 应用程序的方法。我发现的大部分内容是 Windows 7 服务现在在单独的用户会话中运行,并且无法显示任何图形界面给当前用户。我想知道是否有任何解决方法或不同的方式来完成这样的事情?该服务能否在不同的用户会话中启动进程?

回答by 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.

进行此更改是有原因的,而不仅仅是为了惹恼开发人员。正确的方法是将您的 UI 放在不同的程序中,并通过管道或其他一些 IPC 机制与会话进行通信。服务不呈现 UI 的建议已经有 10 多年的历史了。

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

您真的应该尝试遵守这些规则,即使开始时可能看起来不方便。从好的方面来说,您将享受将服务逻辑和 UI 逻辑分开的好处

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!

如果您的服务在 LOCALSYSTEM 帐户下运行,那么您可以选中“允许服务与桌面交互”,以便遗留服务在无法显示 UI 时会失败。但无论如何它都无济于事,因为 UI 将在会话 0 中显示它从未见过!

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

我建议您阅读描述会话 0 隔离的 Microsoft官方文档

回答by Eswar Yaganti

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.

有一种方法可以做到这一点。如果您需要显示一个简单的消息框,您可以使用 WTSSendMessage Routine。如果你需要一个复杂的 UI 元素,你可以把它放在一个单独的程序中,你需要使用 CreateProcessAsUser Routine。在微软提供的这个示例中,您可以看到该过程。

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

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

回答by Francesco De Vittori

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 Destopoption, which is not good if you plan to run your service for some years in the future.

Windows 7 引入了所谓的“会话 0 隔离”,这实际上意味着每个服务(系统服务除外)都在单独的非交互式会话中运行。出于这个原因,您不能直接从服务中创建 GUI,除非您通过标记Interact With Destop选项在传统模式下运行,如果您计划在未来几年运行您的服务,这并不好。

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

正如 David Heffernan 所说,最好的方法是使用客户端-服务器架构。WCF 使与命名管道通信变得容易。

This pageis a good starting point to read about Session 0 Isolation and this white paperis also very good.

这个页面是阅读 Session 0 Isolation 的一个很好的起点,这个白皮书也非常好。