/* --------------- Answer to system messages */ void __stdcall CtrlHandler(DWORD CtrlCode) { DWORD StatetoSend = 0; switch(CtrlCode) { case SERVICE_CONTROL_STOP: SendStatus(SERVICE_STOP_PENDING, NO_ERROR, 0, 1, 5000); StopService(); StatetoSend = SERVICE_STOPPED; break; case SERVICE_CONTROL_PAUSE: if(ServiceCurrentStatus == 1) { SendStatus(SERVICE_PAUSE_PENDING, NO_ERROR, 0, 1, 1000); PauseService(); StatetoSend = SERVICE_PAUSED; } break; case SERVICE_CONTROL_CONTINUE: if(ServiceCurrentStatus == 3) { SendStatus(SERVICE_CONTINUE_PENDING, NO_ERROR, 0, 1, 1000); ResumeService(); StatetoSend = SERVICE_RUNNING; } break; case SERVICE_CONTROL_SHUTDOWN: return; } SendStatus(StatetoSend, NO_ERROR, 0, 0, 0); }
void CService::ServiceCtrlHandler(DWORD controlCode) { DWORD currentState = 0; switch(controlCode) { case SERVICE_CONTROL_STOP: currentState = SERVICE_STOP_PENDING; SendStatusToSCM( SERVICE_STOP_PENDING, NO_ERROR, 0, 1, 5000 ); StopService(); return; case SERVICE_CONTROL_PAUSE: if (m_bRunningService && !m_bPauseService) { SendStatusToSCM( SERVICE_PAUSE_PENDING, NO_ERROR, 0, 1, 1000 ); PauseService(); currentState = SERVICE_PAUSED; } break; case SERVICE_CONTROL_CONTINUE: if (m_bRunningService && m_bPauseService) { SendStatusToSCM( SERVICE_CONTINUE_PENDING, NO_ERROR, 0, 1, 1000 ); ResumeService(); currentState = SERVICE_RUNNING; } break; case SERVICE_CONTROL_INTERROGATE: break; case SERVICE_CONTROL_SHUTDOWN: return; default: break; } SendStatusToSCM(currentState, NO_ERROR, 0, 0, 0); }
// Dispatches events received from the service // control manager VOID ServiceCtrlHandler (DWORD controlCode) { DWORD currentState = 0; BOOL success; switch(controlCode) { // There is no START option because // ServiceMain gets called on a start // Stop the service case SERVICE_CONTROL_STOP: currentState = SERVICE_STOP_PENDING; // Tell the SCM what's happening success = SendStatusToSCM( SERVICE_STOP_PENDING, NO_ERROR, 0, 1, 5000); // Not much to do if not successful // Stop the service StopService(); return; // Pause the service case SERVICE_CONTROL_PAUSE: if (runningService && !pauseService) { // Tell the SCM what's happening success = SendStatusToSCM( SERVICE_PAUSE_PENDING, NO_ERROR, 0, 1, 1000); PauseService(); currentState = SERVICE_PAUSED; } break; // Resume from a pause case SERVICE_CONTROL_CONTINUE: if (runningService && pauseService) { // Tell the SCM what's happening success = SendStatusToSCM( SERVICE_CONTINUE_PENDING, NO_ERROR, 0, 1, 1000); ResumeService(); currentState = SERVICE_RUNNING; } break; // Update current status case SERVICE_CONTROL_INTERROGATE: // it will fall to bottom and send status break; // Do nothing in a shutdown. Could do cleanup // here but it must be very quick. case SERVICE_CONTROL_SHUTDOWN: // Do nothing on shutdown return; default: break; } SendStatusToSCM(currentState, NO_ERROR, 0, 0, 0); }