Windows System Services API Reference

Explore the APIs that enable interaction with and management of Windows system services. These services form the backbone of the operating system, providing essential functionalities and allowing for advanced system control.

Service Control Manager (SCM) APIs

The Service Control Manager is responsible for starting, stopping, and managing Windows services. These APIs allow applications to query and control service status.

OpenSCManager

Opens a connection to the service control manager on the specified computer.

SC_HANDLE OpenSCManager(
  LPCTSTR lpMachineName,
  LPCTSTR lpDatabaseName,
  DWORD   dwDesiredAccess
);

Parameters:

Name Type Description
lpMachineName LPCTSTR The name of the target computer.
lpDatabaseName LPCTSTR The name of the service database to open. Usually ServicesActive.
dwDesiredAccess DWORD The access rights to the service control manager.

Return Value:

SC_HANDLE: A handle to the service control manager. Returns NULL on failure.

CloseServiceHandle

Closes a handle to a service or the service control manager.

BOOL CloseServiceHandle(
  SC_HANDLE hSCObject
);

Parameters:

Name Type Description
hSCObject SC_HANDLE A handle to a service or the service control manager.

EnumServicesStatusEx

Enumerates services or unavailable service control managers.

BOOL EnumServicesStatusEx(
  SC_HANDLE                  hSCManager,
  SC_ENUM_SERVICE_TYPE       InfoLevel,
  SC_ENUM_SERVICE_STATE      State,
  LPENUM_SERVICE_STATUS_PROCESS lpServices,
  DWORD                      cbBufSize,
  LPDWORD                    pcServicesReturned,
  LPDWORD                    pResumeHandle
);

Parameters:

Name Type Description
hSCManager SC_HANDLE A handle to the service control manager.
InfoLevel SC_ENUM_SERVICE_TYPE The type of services to enumerate.
State SC_ENUM_SERVICE_STATE The state of the services to enumerate.
lpServices LPENUM_SERVICE_STATUS_PROCESS A pointer to a buffer that receives the status information.
cbBufSize DWORD The size of the buffer pointed to by lpServices.
pcServicesReturned LPDWORD A pointer to a variable that receives the count of services returned.
pResumeHandle LPDWORD A pointer to a variable that specifies the starting point for enumeration.

Registry Service APIs

Interact with the Windows Registry to configure and manage service properties.

RegOpenKeyEx

Opens an existing registry key. An application must have the appropriate access rights to the key for the function to succeed.

LONG RegOpenKeyEx(
  HKEY   hKey,
  LPCTSTR lpSubKey,
  DWORD  ulOptions,
  REGSAM samDesired,
  PHKEY  phkResult
);

Parameters:

Name Type Description
hKey HKEY A handle to an open registry key.
lpSubKey LPCTSTR The name of the subkey to open.
ulOptions DWORD Reserved; must be zero.
samDesired REGSAM A mask that specifies the desired access rights for the key.
phkResult PHKEY A pointer to a variable that receives a handle to the opened key.

RegQueryValueEx

Retrieves the data and type for the specified registry value.

LONG RegQueryValueEx(
  HKEY   hKey,
  LPCTSTR lpValueName,
  LPDWORD lpReserved,
  LPDWORD lpType,
  LPBYTE  lpData,
  LPDWORD lpcbData
);

Parameters:

Name Type Description
hKey HKEY A handle to an open registry key.
lpValueName LPCTSTR The name of the registry value to query.
lpReserved LPDWORD This parameter is reserved and must be NULL.
lpType LPDWORD A pointer to a variable that receives the type code.
lpData LPBYTE A pointer to a buffer that receives the value's data.
lpcbData LPDWORD A pointer to a variable that specifies the size of the buffer pointed to by lpData.

Service Management Functions

APIs for creating, deleting, starting, and stopping services.

CreateService

Creates a service entry in the specified service control manager database and returns a handle to the service.

SC_HANDLE CreateService(
  SC_HANDLE hSCManager,
  LPCTSTR lpServiceName,
  LPCTSTR lpDisplayName,
  DWORD   dwDesiredAccess,
  DWORD   dwServiceType,
  DWORD   dwStartType,
  DWORD   dwErrorControl,
  LPCTSTR lpBinaryPathName,
  LPCTSTR lpLoadOrderGroup,
  LPDWORD lpdwTagId,
  LPCTSTR lpDependencies,
  LPCTSTR lpServiceStartName,
  LPCTSTR lpPassword
);

Parameters:

Name Type Description
hSCManager SC_HANDLE A handle to the service control manager database.
lpServiceName LPCTSTR The unique name of the service to be installed.
lpDisplayName LPCTSTR The display name for the service.
dwDesiredAccess DWORD Access rights for the service.
dwServiceType DWORD The type of service.
dwStartType DWORD When to start the service.
dwErrorControl DWORD The error control severity.
lpBinaryPathName LPCTSTR The fully qualified path to the service executable file.
lpLoadOrderGroup LPCTSTR Name of the load order group the service belongs to.
lpdwTagId LPDWORD Pointer to a variable that receives a unique tag value.
lpDependencies LPCTSTR A string that specifies an array of null-separated service names.
lpServiceStartName LPCTSTR The account name under which the service should run.
lpPassword LPCTSTR The password for the service account.

DeleteService

Marks a specified service for deletion from the service control manager database.

BOOL DeleteService(
  SC_HANDLE hService
);

Parameters:

Name Type Description
hService SC_HANDLE A handle to the service to be deleted.

StartService

Starts a service and any processes it depends on.

BOOL StartService(
  SC_HANDLE hService,
  DWORD     dwNumServiceArgs,
  LPCWSTR   *lpServiceArgVectors
);

Parameters:

Name Type Description
hService SC_HANDLE A handle to the service to be started.
dwNumServiceArgs DWORD The number of arguments to be passed to the service.
lpServiceArgVectors LPCWSTR * An array of pointers to null-terminated strings.