允许 Windows 服务与桌面交互
How do I enable "Allow service to interact with desktop" programmatically?
In services.msc > Action > Properties > Log On > Allow service to interact with desktop, I can enable my service to interact with the desktop. I want my service to play sound (MP3, WAV, etc.).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将在这里采取一些自由尝试从关键字解释你的问题。将来,请花更多时间写下您的问题,以便其他试图阅读和理解它们的人能够理解它们。
Windows 服务的属性窗口中的“登录”选项卡下有一个名为“允许服务与桌面交互”的复选框。如果您尝试检查以编程方式该框,您需要在使用
CreateService
API。 (请参阅 MSDN)。但是请注意,从 Windows Vista 开始,严格禁止服务直接与用户交互:
这个“功能”已被破坏,传统观点认为您无论如何都不应该依赖它。服务并不旨在提供 UI 或允许任何类型的直接用户交互。 Microsoft 自 Windows NT 早期以来一直警告不要使用此功能,因为可能存在安全风险。 Larry Osterman 论证了为什么它总是一个坏主意。而且他不是唯一的一个 。
有一些可能 < a href="https://stackoverflow.com/questions/267838/how-can-a-windows-service-execute-a-gui-application">解决方法,但是,如果您绝对< /em> 必须具有此功能。但我强烈建议您仔细考虑其必要性,并为您的服务探索替代设计。
I'm going to take some liberties in here in trying to interpret your question from keywords. In the future, please spend more time writing your questions so that they make sense to another person who is trying to read and understand them.
There is a checkbox under the Log On tab in the properties window for a Windows service that is called "Allow service to interact with desktop." If you're trying to check that box programmatically, you need to specify the
SERVICE_INTERACTIVE_PROCESS
flag when you create your service using theCreateService
API. (See MSDN).However, note that as of Windows Vista, services are strictly forbidden from interacting directly with a user:
This "feature" is broken, and conventional wisdom dictates that you shouldn't have been relying on it anyway. Services are not meant to provide a UI or allow any type of direct user interaction. Microsoft has been cautioning that this feature be avoided since the early days of Windows NT because of the possible security risks. Larry Osterman argues why it was always a bad idea. And he is not the only one.
There are some possible workarounds, however, if you absolutely must have this functionality. But I strongly urge you to consider its necessity carefully and explore alternative designs for your service.
由于该服务不在用户会话上下文中运行,因此您需要创建第二个应用程序来与该服务交互。
例如,Microsoft SQL Server 有一个监视工具。该应用程序在用户会话中运行并连接到服务,为您提供有关服务是否正在运行的信息,并允许您停止和启动数据库服务。
由于该应用程序确实在用户会话中运行,因此您可以通过该应用程序与桌面进行交互。
Because the service does not run in the context of a user session, you create a second application to interact with the service.
For example, the Microsoft SQL server has a monitoring tool. This application runs in the user session and connects to the service providing you information on whether the service is running and allowing you to stop and start the database service.
Since that application does run in a user session, you can interact with the desktop through that application.
您需要添加serviceinstaller并在serviceinstaller的提交事件中写下以下代码。
You need to add serviceinstaller and write down below code in commited event of serviceinstaller.