Service programs are generally written as console applications. The entry point of a console application is the main function. The main function receives arguments from the ImagePath value from the registry key for the service.
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 The 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 as described in The ServiceMain Function.
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.
If StartServiceCtrlDispatcher succeeds, the calling thread does not return until all running services in the process have terminated. 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.
For more information, see Writing a Service Program's main Function.