CreateService

  SC_HANDLE CreateService(hSCManager, lpServiceName, dwDesiredAccess, dwServiceType, dwStartType, dwErrorControl, lpBinaryPathName, lpLoadOrderGroup, lpDependencies, lpServiceStartName, lpPassword)    
  SC_HANDLE hSCManager;    
  LPTSTR lpServiceName;    
  DWORD dwDesiredAccess;    
  DWORD dwServiceType;    
  DWORD dwStartType;    
  DWORD dwErrorControl;    
  LPTSTR lpBinaryPathName;    
  LPTSTR lpLoadOrderGroup;    
  LPTSTR lpDependencies;    
  LPTSTR lpServiceStartName;    
  LPTSTR lpPassword ;    

The CreateService function creates a service object and add it to the Service Control Manager database.

Parameters

hSCManager

Handle obtained from a previous OpenSCManager call.

lpServiceName

Name of the service to install. NULL-terminated string that has a maximum length of 256 characters. The name is case preserved by the Service Control Manager. Service name comparisons are always case insensitive. Forward- and back-slash are invalid service name characters.

dwDesiredAccess

Access types desired to access the service. These access types will be checked against the Discretionary Access Control list of the service object to determine whether the accesses are granted or denied.

dwServiceType

Value to indicate the type of service this is.

Value Meaning

SERVICE_WIN32_OWN_PROCESS a service which runs in its own Win32 process.
SERVICE_WIN32_SHARE_PROCESS a service which shares a Win32 process with other services.
SERVICE_DRIVER an NT device driver.

dwStartType

Value to specify when to start the service.

Value Meaning

SERVICE_BOOT_START Only valid if service type is SERVICE_DRIVER. This device driver is to be started by the OS loader.
SERVICE_SYSTEM_START Only valid if service type is SERVICE_DRIVER. This device driver is to be started by IoInitSystem.
SERVICE_AUTO_START Valid for both SERVICE_DRIVER and SERVICE_WIN32 service types. This service is started by the Service Control Manager automatically during boot.
SERVICE_DEMAND_START Valid for both SERVICE_DRIVER and SERVICE_WIN32 service types. This service is started by the Service Control Manager when a start request is issued via the StartService API.
SERVICE_DISABLED This service can no longer be started.

dwErrorControl

Value to specify the severity of the error if this service fails to start during boot so that the appropriate action can be taken.

Value Meaning

SERVICE_ERROR_NORMAL Log error but system continues to boot.
SERVICE_ERROR_SEVERE Log error, and the system is rebooted with the last-known-good configuration. If the current configuration is last-known-good, press on with boot.
SERVICE_ERROR_CRITICAL Log error if possible, and system is rebooted with last-known-good configuration. If the current configuration is last-known-good, boot fails.

lpBinaryPathName

Fully-qualified path name to the service binary file.

lpLoadOrderGroup

Name of the load ordering group which this service is a member of. Groups of services are started based on the group order list specified in the registry at HKEY_LOCAL_MACHINE\System\ CurrentControlSet\Control\Service_Group_Order The group order takes precedence over the service start dependencies specified in the lpDependencies parameter.

lpDependencies

Null-separated names of services which must be running before this service can run. An empty string means that this service has no dependencies.

lpServiceStartName

If service type is SERVICE_WIN32, this name is the account name in the form of “DomainName\Username” which the service process will be logged on as when it runs. If the account belongs to the built-in domain, “.\Username” can be specified. If service type is SERVICE_DRIVER, this name must be the NT driver object name (e.g. \FileSystem\LanManRedirector or\Driver\Xns) which the I/O system uses to load the device driver.

lpPassword

Password to the account name specified by lpServiceStartName if service type is SERVICE_WIN32. This password will be changed periodically by the Service Control Manager so that it will not expire. If service type is SERVICE_DRIVER, this parameter is ignored.

Return Value

Returns a handle to the service. If return value is NULL, an error has occurred. Use GetLastError to determine the cause of the failure.

Errors Value Meaning
  ERROR_ACCESS_DENIED  
    The specified handle was not opened with SC_MANAGER_CREATE_SERVICE access.
  ERROR_INVALID_HANDLE  
    The specified handle is invalid.
  ERROR_INVALID_NAME  
    The specified service name is invalid.
  ERROR_SERVICE_EXISTS  
    The specified service is already created.
  ERROR_INVALID_SERVICE_ACCOUNT  
    The user account name does not exist, or the service is specified to share the same binary file as an already installed service but has an account name that is not the same as the installed service.
  ERROR_INVALID_PARAMETER  
    A parameter specified is invalid.

Comments

This function creates a service object and installs the service in the Service Control Manager database by creating a service name key in the registry at HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\ <lpServiceName>, where lpServiceName is the service name specified to this function. Information specified to this API is saved as values under this key. Setup programs and the service itself can create any subkey under this service name key for any service specific information.

This call returns a handle to the newly created service that should be used on future operations on the service. This handle can be closed by calling CloseServiceHandle.