c# - 授予非管理员用户访问权限以启动/停止服务 Windows 7

标签 c# windows windows-services

我有一个 Windows 服务和一个 Windows 应用程序。我想使用参数从我的 Windows 应用程序启动和停止此窗口服务。这是我必须启动该服务的条件

  foreach (ServiceController sc in ServiceController.GetServices())
            {
                if (sc.ServiceName == "serviceName")
                {
                    //service is found
                    using (ServiceController serviceController = new ServiceController("serviceName"))
                    {
                        string[] args = new string[1];
                        args[0] = "Myargument";
                        if (serviceController.Status == ServiceControllerStatus.Running) 
                        {

                        }
                        else { serviceController.Start(args); }

                }
            }

但它给我 无法在计算机“.”上打开服务 异常。我尝试使用 app.manifest 强制以管理员身份运行,但它仍然给出异常。我也试过这篇文章 here但异常仍然存在。有谁知道如何授予 Windows 7 中非管理员用户管理服务的访问权限?

最佳答案

您可以使用 SetServiceObjectSecuritySetNamedSecurityInfo更改服务的权限。

我有一些使用 ConvertStringSecurityDescriptorToSecurityDescriptor 的代码创建一个安全描述符以传递给 SetServiceObjectSecurity。给定一个安全描述符,您可以使用 GetSecurityDescriptorDacl 获取要传递给 SetNamedSecurityInfo 的 DACL。

我用来创建安全描述符的SDDL字符串如下:

wchar_t sddl[] = L"D:"
  L"(A;;CCLCSWRPWPDTLOCRRC;;;SY)"           // default permissions for local system
  L"(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)"   // default permissions for administrators
  L"(A;;CCLCSWLOCRRC;;;AU)"                 // default permissions for authenticated users
  L"(A;;CCLCSWRPWPDTLOCRRC;;;PU)"           // default permissions for power users
  L"(A;;RP;;;IU)"                           // added permission: start service for interactive users
  ;

当然,此代码需要以提升的权限运行。通常,您会在安装服务的同时设置权限。

关于c# - 授予非管理员用户访问权限以启动/停止服务 Windows 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21257757/

相关文章:

c# - 如何获得所有可能的 3 个字母排列?

c# - 如何创建 C# TCP 监听器,使所有客户端保持连接在一个线程中,仅在客户端写入时发送事件?

c - ReadProcessMemory 返回一个更大的缓冲区(C,windows)

带有 Procrun、StopMethod 问题的 Java Windows 服务

c# - Windows 服务中的 SQL 连接

python - 我所有的 python windows 服务都停止工作

c# - 当新版本不存在时使用 <bindingRedirect> 到新版本的程序集

c# - 如何在 MVC Controller 操作后返回 View ?

c++ - Windows 开发 : x86 to x64 transition

windows - 在 Linux/Mac/Windows 中使用 "drawer"构建应用程序