The WinMain routine handles both registration and installation, as well as deregistration and uninstallation. When the service is run, WinMain calls CServiceModule::Start.
CServiceModule::Start sets up an array of SERVICE_TABLE_ENTRY structures that map each service to its startup function. This array is then passed to the Win32 API function, StartServiceCtrlDispatcher. In theory, one EXE could handle multiple services and the array could have multiple SERVICE_TABLE_ENTRY structures. Currently, however, an ATL-generated service supports only one service per EXE. Therefore, the array has a single entry that contains the service name and _ServiceName as the startup function. _ServiceName is a static member function of CServiceModule that calls the non-static member function, ServiceName.
Note Failure of StartServiceCtrlDispatcher to connect to the service control manager (SCM) probably means that the program is not running as a service. In this case, the program calls CServiceModule::Run directly so that the program can run as a local server. For more information about running the program as a local server, see Debugging Tips.