SC_HANDLE WINAPI OpenSCManager(
_In_opt_ LPCTSTR lpMachineName,
_In_opt_ LPCTSTR lpDatabaseName,
_In_ DWORD dwDesiredAccess
);
函数作用:以一定的权限,在指定的计算机打开指定的SCM数据库
参数:
1. lpMachineName:目标计算机名,NULL表示本地计算机
2. lpDatabaseName:服务管理程序系统组件数据库,可以设为SERVICES_ACTIVE_DATABASE,如果为NULL,表示默认打开SERVICES_ACTIVE_DATABASE数据库
3. dwDesiredAccess:对SCM的权限,可以是以下这一些:
Access right | Description |
---|---|
SC_MANAGER_ALL_ACCESS (0xF003F) | Includes STANDARD_RIGHTS_REQUIRED, in addition to all access rights in this table. |
SC_MANAGER_CREATE_SERVICE (0x0002) | Required to call the CreateService function to create a service object and add it to the database. |
SC_MANAGER_CONNECT (0x0001) | Required to connect to the service control manager. |
SC_MANAGER_ENUMERATE_SERVICE (0x0004) |
Required to call the EnumServicesStatus or EnumServicesStatusEx function to list the services that are in the database. Required to call the NotifyServiceStatusChange function to receive notification when any service is created or deleted. |
SC_MANAGER_LOCK (0x0008) | Required to call the LockServiceDatabase function to acquire a lock on the database. |
SC_MANAGER_MODIFY_BOOT_CONFIG (0x0020) | Required to call the NotifyBootConfigStatus function. |
SC_MANAGER_QUERY_LOCK_STATUS (0x0010) | Required to call the QueryServiceLockStatus function to retrieve the lock status information for the database. |
或者上面权限的组合:
Access right | Description |
---|---|
GENERIC_READ |
|
GENERIC_WRITE |
|
GENERIC_EXECUTE |
|
GENERIC_ALL |
|
只有拥有Administrator权限的程序打开的SCM句柄,才可用于CreateService 和 LockServiceDatabase函数
不同用户拥有不同的权限,在获取所需要的权限前,要检查程序所拥有的权限:
Account | Access rights |
---|---|
Remote authenticated users |
|
Local authenticated users (including LocalService and NetworkService) |
|
LocalSystem |
|
Administrators |
|
返回值:
成功,返回指定SCM数据库的句柄;失败返回NULL,错误码可通过调用GetLastError获得。
Return code | Description |
---|---|
|
The requested access was denied. |
|
The specified database does not exist. |
说明:
(1)在获取SCM的相关权限前,系统会对程序进行一定的权限检查,是否符合权限要求
(2)当程序在连接其他计算机上的服务时,程序没有适当的权限,那么OpenSCManager将会调用失败,. To connect to a service remotely, call the LogonUser function with LOGON32_LOGON_NEW_CREDENTIALS and then call ImpersonateLoggedOnUser before calling OpenSCManager.为了远程链接一个服务,可以用LOGON32_LOGON_NEW_CREDENTIALS调用LogonUser函数然后再调用ImpersonateLoggedOnUser
(3)只有拥有Administrator权限的程序打开的SCM句柄,才可用于CreateService 和 LockServiceDatabase函数
(4)OpenSCManager函数返回的句柄只能由调用它的进程使用,可以调用CloseServiceHandle函数来关掉这个句柄。