Platform SDK: DLLs, Processes, and Threads |
The OpenSCManager function establishes a connection to the service control manager on the specified computer and opens the specified service control manager database.
SC_HANDLE OpenSCManager( LPCTSTR lpMachineName, // computer name LPCTSTR lpDatabaseName, // SCM database name DWORD dwDesiredAccess // access type );
Object access | Description |
---|---|
SC_MANAGER_ALL_ACCESS | Includes STANDARD_RIGHTS_REQUIRED, in addition to all of the access types listed in this table. |
SC_MANAGER_CONNECT | Enables connecting to the service control manager. |
SC_MANAGER_CREATE_SERVICE | Enables calling of the CreateService function to create a service object and add it to the database. |
SC_MANAGER_ENUMERATE_SERVICE | Enables calling of the EnumServicesStatus function to list the services that are in the database. |
SC_MANAGER_LOCK | Enables calling of the LockServiceDatabase function to acquire a lock on the database. |
SC_MANAGER_QUERY_LOCK_STATUS | Enables calling of the QueryServiceLockStatus function to retrieve the lock status information for the database. |
The dwDesiredAccess parameter can specify any or all of the following generic access types.
Generic access | Service manager access |
---|---|
GENERIC_READ | Combines the following access types: STANDARD_RIGHTS_READ, SC_MANAGER_ENUMERATE_SERVICE, and SC_MANAGER_QUERY_LOCK_STATUS. |
GENERIC_WRITE | Combines the following access types: STANDARD_RIGHTS_WRITE and SC_MANAGER_CREATE_SERVICE. |
GENERIC_EXECUTE | Combines the following access types: STANDARD_RIGHTS_EXECUTE, SC_MANAGER_CONNECT, and SC_MANAGER_LOCK. |
If the function succeeds, the return value is a handle to the specified service control manager database.
If the function fails, the return value is NULL. To get extended error information, call GetLastError.
The following error codes can be set by the SCM. Other error codes can be set by the registry functions that are called by the SCM.
Error code | Meaning |
---|---|
ERROR_ACCESS_DENIED | The requested access was denied. |
ERROR_DATABASE_DOES_NOT_EXIST | The specified database does not exist. |
ERROR_INVALID_PARAMETER | A specified parameter is invalid. |
When a process uses the OpenSCManager function to open a handle to a service control manager database, the system performs a security check before granting the requested access. All processes are permitted SC_MANAGER_CONNECT, SC_MANAGER_ENUMERATE_SERVICE, and SC_MANAGER_QUERY_LOCK_STATUS access to all service control manager databases. This enables any process to open a service control manager database handle that it can use in the OpenService, EnumServicesStatus, and QueryServiceLockStatus functions. Only processes with Administrator privileges are able to open a database handle used by the CreateService and LockServiceDatabase functions.
The returned handle is only valid for the process that called the OpenSCManager function. It can be closed by calling the CloseServiceHandle function.
Windows NT/2000: Requires Windows NT 3.1 or later.
Header: Declared in Winsvc.h; include Windows.h.
Library: Use Advapi32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000.
Services Overview, Service Functions, CloseServiceHandle, CreateService, EnumServicesStatus, LockServiceDatabase, OpenService, QueryServiceLockStatus