// static member function (callback) to handle commands from the // service control manager void BaseService::Handler(DWORD dwControl) { // Get a pointer to the object BaseService* pService = m_pThis; DWORD currentState = pService->m_Status.dwCurrentState; switch (dwControl) { case SERVICE_CONTROL_STOP: pService->SetStatus(SERVICE_STOP_PENDING); pService->OnStop(); currentState = SERVICE_STOPPED; break; case SERVICE_CONTROL_PAUSE: pService->SetStatus(SERVICE_PAUSE_PENDING); pService->OnPause(); currentState = SERVICE_PAUSED; break; case SERVICE_CONTROL_CONTINUE: pService->SetStatus(SERVICE_CONTINUE_PENDING); pService->OnContinue(); currentState = SERVICE_RUNNING; break; case SERVICE_CONTROL_INTERROGATE: pService->OnInterrogate(); break; case SERVICE_CONTROL_SHUTDOWN: pService->OnShutdown(); return; default: if (dwControl >= 128 && dwControl <= 255) pService->OnUserControl(dwControl); break; } // Report current status pService->SetStatus(currentState); }
// static member function (callback) void BaseService::ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv) { // Get a pointer to the C++ object BaseService* pService = m_pThis; // Register the control request handler pService->m_hServiceStatus = RegisterServiceCtrlHandler(pService->m_szServiceName, Handler); if (pService->m_hServiceStatus == NULL) return; // Start the initialization pService->SetStatus(SERVICE_START_PENDING); if (pService->OnInitialize()) { pService->SetStatus(SERVICE_RUNNING); pService->Run(); } // Tell the service manager we are stopped pService->SetStatus(SERVICE_STOPPED); }