不要打开服务控制管理器

时间:2015-10-23 10:58:46

标签: c# windows-services

我无法打开服务控制管理器(SCM)来创建服务,函数返回给我0:

[DllImport("advapi32.dll", EntryPoint = "OpenSCManagerW", ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]
static extern IntPtr OpenSCManager(string machineName, string databaseName, ScManagerAccess dwDesiredAccess);

...
IntPtr scm = OpenScManager(ScManagerAccess.SC_MANAGER_CREATE_SERVICE | ScManagerAccess.SC_MANAGER_CONNECT);
...

private static IntPtr OpenScManager(ScManagerAccess rights)
    {
        IntPtr scm = OpenSCManager(null, null, rights);
        if (scm == IntPtr.Zero)
            throw new ApplicationException("Could not connect to service control manager.");
        return scm;
    }

[Flags]
public enum ScManagerAccess
{
    SC_MANAGER_CONNECT = 0x0001,
    SC_MANAGER_CREATE_SERVICE = 0x0002,
    SC_MANAGER_ENUMERATE_SERVICE = 0x0004,
    SC_MANAGER_LOCK = 0x0008,
    SC_MANAGER_QUERY_LOCK_STATUS = 0x0010,
    SC_MANAGER_MODIFY_BOOT_CONFIG = 0x0020,
    SC_MANAGER_STANDARD_RIGHTS_REQUIRED = (int)StandartAccess.STANDARD_RIGHTS_REQUIRED,
    SC_MANAGER_ALL_ACCESS = (SC_MANAGER_STANDARD_RIGHTS_REQUIRED |
                             SC_MANAGER_CONNECT |
                             SC_MANAGER_CREATE_SERVICE |
                             SC_MANAGER_ENUMERATE_SERVICE |
                             SC_MANAGER_LOCK |
                             SC_MANAGER_QUERY_LOCK_STATUS |
                             SC_MANAGER_MODIFY_BOOT_CONFIG)
}

但如果我尝试仅使用access进行连接,则返回给我而不是0。

什么错了?也许如果我想使用访问创建我需要一些权限来启动应用程序?

0 个答案:

没有答案