StartServiceCtrlDispatcher

  BOOL StartServiceCtrlDispatcher(lpServiceStartTable)    
  LPSERVICE_TABLE_ENTRY lpServiceStartTable;    

The StartServiceCtrlDispatcher function connects the current service thread to the Service Control Manager and causes it to be the control dispatch thread for the process.

Parameters

lpServiceStartTable

A pointer to an array of SERVICE_TABLE_ENTRY structure entries, one entry for each service in the current process. The last entry in the table has NULL values to designate the end of the table.

Return Value

If successful, this routine does not return until all running services in the current process have terminated. If the return value is FALSE, an error has occurred. Use GetLastError to determine the cause of the failure.

Errors Value Meaning
  ERROR_INVALID_DATA The specified dispatch table is does not contain entries in the proper format.

Comments

This function should be called by the main service process thread as the last item of the process-wide initialization. The call does not return because the thread connects to the Service Control Manager and waits for incoming# control requests, thus becoming the control dispatcher.

When a request to start a particular service is received, the control dispatcher creates a new thread to execute the specified service main function (lpServiceProc field in the service's SERVICE_TABLE_ENTRY structure). This service main function does not return until the service terminates. When this function returns, the thread created to run the service is destroyed.

The control dispatcher also fields other control requests, i.e. SERVICE_CONTROL_PAUSE, SERVICE_CONTROL_CONTINUE, SERVICE_CONTROL_STOP, and SERVICE_CONTROL_INTERROGATE for all services in the same process. The control dispatcher invokes the targeted service's control handling function specified to it by the service itself via the RegisterServiceCtrlHandler# when it receives a control for that service.