允许 Windows 服务与桌面交互

发布于 2024-10-03 18:23:40 字数 286 浏览 4 评论 0原文

如何以编程方式启用“允许服务与桌面交互”?

在services.msc中>行动>属性>登录>允许服务与桌面交互,我可以使我的服务与桌面进行交互。我希望我的服务能够播放声音(MP3、WAV 等)。

services.msc > 操作 > 属性 > 登录 > 允许服务与桌面交互

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

services.msc > Action > Properties > Log On > Allow service to interact with desktop

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽清梦 2024-10-10 18:23:40

我将在这里采取一些自由尝试从关键字解释你的问题。将来,请花更多时间写下您的问题,以便其他试图阅读和理解它们的人能够理解它们。

Windows 服务的属性窗口中的“登录”选项卡下有一个名为“允许服务与桌面交互”的复选框。如果您尝试检查以编程方式该框,您需要在使用 CreateService API。 (请参阅 MSDN)。

但是请注意,从 Windows Vista 开始,严格禁止服务直接与用户交互:

重要提示:服务无法直接与用户交互
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 the CreateService API. (See MSDN).

However, note that as of Windows Vista, services are strictly forbidden from interacting directly with a user:

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

深海不蓝 2024-10-10 18:23:40

由于该服务不在用户会话上下文中运行,因此您需要创建第二个应用程序来与该服务交互。

例如,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.

清风无影 2024-10-10 18:23:40

您需要添加serviceinstaller并在serviceinstaller的提交事件中写下以下代码。

using System.Management;
using System.ComponentModel;
using System.Configuration.Install;

private void serviceInstaller1_Committed(object sender, InstallEventArgs e)
{
    ConnectionOptions coOptions = new ConnectionOptions();
    coOptions.Impersonation = ImpersonationLevel.Impersonate;
    ManagementScope mgmtScope = new ManagementScope(@"root\CIMV2", coOptions);
    mgmtScope.Connect();
    ManagementObject wmiService;
    wmiService = new ManagementObject("Win32_Service.Name='" + serviceInstaller1.ServiceName + "'");
    ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
    InParam["DesktopInteract"] = true;
    ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);
}

You need to add serviceinstaller and write down below code in commited event of serviceinstaller.

using System.Management;
using System.ComponentModel;
using System.Configuration.Install;

private void serviceInstaller1_Committed(object sender, InstallEventArgs e)
{
    ConnectionOptions coOptions = new ConnectionOptions();
    coOptions.Impersonation = ImpersonationLevel.Impersonate;
    ManagementScope mgmtScope = new ManagementScope(@"root\CIMV2", coOptions);
    mgmtScope.Connect();
    ManagementObject wmiService;
    wmiService = new ManagementObject("Win32_Service.Name='" + serviceInstaller1.ServiceName + "'");
    ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
    InParam["DesktopInteract"] = true;
    ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文