StartService

  BOOL StartService(hService, dwNumServiceArgs, lpServiceArgs)    
  SC_HANDLE hService;    
  DWORD dwNumServiceArgs;    
  LPTSTR *lpServiceArgs;    

The StartService function begins the execution of a service.

Parameters

hService

Handle obtained from a previous CreateService or OpenService call.

dwNumServiceArgs

Number of arguments in the lpServiceArgs string. This value is passed on to the service being started.

lpServiceArgs

An array of argument strings passed on to a service for its use of receiving startup parameters.

Return Value

Returns TRUE if the service has started to run and has responded with its first service status information. If the return value is FALSE, 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 SERVICE_START access.
  ERROR_INVALID_HANDLE The specified handle is invalid.
  ERROR_PATH_NOT_FOUND The service binary file could not be found.
  ERROR_SERVICE_ALREADY_RUNNING An instance of the service is already running.
  ERROR_SERVICE_REQUEST_TIMEOUT The service did not respond to the start request in a timely fashion.
  ERROR_SERVICE_NO_THREAD A thread could not be created for the Win32 service.
  ERROR_SERVICE_DATABASE_LOCKED The database is locked.
  ERROR_SERVICE_DISABLED The service has been disabled.
  ERROR_SERVICE_DEPENDENCY_FAIL The service depends on another service which has failed to start.
  ERROR_SERVICE_LOGON_FAILED The service could not be logged on.

Comments

The start of a Driver service is synchronous and the Service Control Manager does not return until the device driver is done initializing.

For a Win32 service, the Service Control Manager returns to the caller after it has spawned the service process (if one does not already exists because if the service shares a process with other services, the process may already be running) and has received notification from the service control dispatcher that the service thread was created successfully. It does not wait for the first response from the service because that may take a while. The Service Control Manager sets the following default values for the Win32 service before returning from the StartService call:

Current state of the service is set to SERVICE_START_PENDING

Control accepted is set to none (0)

Checkpoint value is set to 0

Wait hint time is set to 2 seconds

The application which issued the start request can periodically query the service status using QueryServiceStatus to check if the service has completely initialized.