Service Entry Point

Services are generally written as console applications. The entry point of a console application is its main function. The main function receives arguments from the ImagePath value from the registry key for the service. For more information, see the Remarks section of the CreateService function.

When the SCM starts a service program, it waits for it to call the StartServiceCtrlDispatcher function. Use the following guidelines.

  • A service of type SERVICE_WIN32_OWN_PROCESS should call StartServiceCtrlDispatcher immediately, from its main thread. You can perform any initialization after the service starts, as described in Service ServiceMain Function.
  • If the service type is SERVICE_WIN32_SHARE_PROCESS and there is common initialization for all services in the program, you can perform the initialization in the main thread before calling StartServiceCtrlDispatcher, as long as it takes less than 30 seconds. Otherwise, you must create another thread to do the common initialization, while the main thread calls StartServiceCtrlDispatcher. You should still perform any service-specific initialization after the service starts.

The StartServiceCtrlDispatcher function takes a SERVICE_TABLE_ENTRY structure for each service contained in the process. Each structure specifies the service name and the entry point for the service. For an example, see Writing a Service Program's main Function.

If StartServiceCtrlDispatcher succeeds, the calling thread does not return until all running services in the process have entered the SERVICE_STOPPED state. The SCM sends control requests to this thread through a named pipe. The thread acts as a control dispatcher, performing the following tasks:

  • Create a new thread to call the appropriate entry point when a new service is started.
  • Call the appropriate handler function to handle service control requests.

Writing a Service Program's main Function