如何安装以管理员身份运行的 Windows 服务?

发布于 2024-09-24 08:57:44 字数 772 浏览 4 评论 0原文

我编写了一个安装程序,用于安装需要启动/停止另一个服务 (B) 的 Windows 服务 (A)。但是,当 A 尝试启动/停止 B 时,我收到此异常:

System.InvalidOperationException: 无法在计算机“.”上打开 MyService 服务。 ---> System.ComponentModel.Win32Exception:访问被拒绝

安装程序将该服务安装为本地服务,并通过 UAC 弹出窗口请求管理员权限,我授予了该权限。我还在服务中添加了一个 app.manifest 文件,该文件设置为请求管理员权限:

但我仍然收到该错误。

这就是我启动服务的方式(当然,停止是相同的,除了它调用 Stop):

using (Mutex mutex = new Mutex(false, "MyServiceLock"))
{
    mutex.WaitOne();

    if (ServiceExists(serviceName) == true)
    {
        using (ServiceController serviceController = new ServiceController(serviceName, "."))
        {
            serviceController.Start(); // this line throws the exception
        }
    }

    mutex.ReleaseMutex();
}

为什么对此服务的访问可能被拒绝?

I've written an installer that installs a windows service (A) that needs to start/stop another service (B). However, when A tries to start/stop B, I get this exception:

System.InvalidOperationException: Cannot open MyService service on computer '.'. ---> System.ComponentModel.Win32Exception: Access is denied

The installer installs the service as a local service, and it requests admin rights via the UAC pop-up, which I grant. I've also added an app.manifest file to the service that is set to ask for admin rights:

Yet I'm still getting that error.

This is how I start the service (stopping is the same, except it calls Stop, of course):

using (Mutex mutex = new Mutex(false, "MyServiceLock"))
{
    mutex.WaitOne();

    if (ServiceExists(serviceName) == true)
    {
        using (ServiceController serviceController = new ServiceController(serviceName, "."))
        {
            serviceController.Start(); // this line throws the exception
        }
    }

    mutex.ReleaseMutex();
}

Why might access to this service be denied?

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

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

发布评论

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

评论(2

挽袖吟 2024-10-01 08:57:44

服务不能请求 UAC 提升。在我看来,您描述的 UAC 提示实际上是安装程序请求的,而不是服务请求的。服务通常已经使用非常特权的帐户(默认情况下为 LocalSystem)运行。请确保将服务配置为使用此类特权帐户,而不是受限用户帐户。

A service cannot ask for a UAC elevation. It sounds to me that the UAC prompt you describe is actually requested by the installer, not the service. Services normally run with a very privileged account already, LocalSystem by default. Do make sure that you configure the service to use such a privileged account, not a restricted user account.

梦里南柯 2024-10-01 08:57:44

作为一个快速测试,如果您打开 services.msc 并检查您的服务器“运行方式”并输入您的凭据,错误会消失吗? LocalService 可能无权停止其他服务。提供 UAC 提示权限可能只允许您首先安装该服务,而不是告诉它以管理员身份运行。

As a quick test, if you open up services.msc and check your server to "run as" and enter your credentials, does the error go away? It may be that the LocalService does not have access to stop other services. Providing the UAC prompt permission is likely only allowing you to install the service in the first place, not telling it to run as administrator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文