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 |
|
不同用戶擁有不同的權限,在獲取所需要的權限前,要檢查程序所擁有的權限:
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
(4)
OpenSCManager
函數返回的句柄只能由調用它的進程使用,可以調用CloseServiceHandle函數來關掉這個句柄。