//Restart the specified servicevoidCpssdprinterctrlplug::restartservice (Constwchar_t*nswservicename) {Sc_handle Schscmanager=NULL; Sc_handle Schservice=NULL; Do { // ----------------------------------------------------------------------------------------------------------- -------------- //Open//Get a handle to the SCM database.Schscmanager =OpenSCManager (NULL,//Local ComputerNull//servicesactive Databasesc_manager_all_access);//Full access Rights if(NULL = =Schscmanager) {Debugwrite (L"ERROR —————— OpenSCManager failed (%d) \ n", GetLastError ()); Break; } //Get A handle to the service.Schservice =OpenService (Schscmanager,//SCM DatabaseNswservicename,//Name of serviceService_change_config);//need change config access if(Schservice = =NULL) {Debugwrite (L"ERROR —————— OpenService failed (%d) \ n", GetLastError ()); Break; } // ----------------------------------------------------------------------------------------------------------- -------------- //Stop//Make sure the service was not already stopped.DWORD dwbytesneeded; service_status_process SSP; if( ! Queryservicestatusex (Schservice, Sc_status_process_info, (LPBYTE) &SSP,sizeof(service_status_process),&dwbytesneeded)) {Debugwrite (L"ERROR —————— Queryservicestatusex failed (%d) \ n", GetLastError ()); Break; } if(ssp.dwcurrentstate! = service_stopped | | Service_stop_pending = =ssp.dwcurrentstate) Break; {Debugwrite (L"ERROR —————— Service is already stopped.\n"); Break; } //Send A stop code to the service. if( ! ControlService (Schservice, Service_control_stop, (lpservice_status) &SSP)) {Debugwrite (L"ERROR —————— ControlService failed (%d) \ n", GetLastError ()); Break; } //Wait for the service to stop.DWORD Dwstarttime =GetTickCount (); DWORD dwtimeout=30000;//30-second time-out while(Ssp.dwcurrentstate! =service_stopped) {Sleep (ssp.dwwaithint); if( ! Queryservicestatusex (Schservice, Sc_status_process_info, (LPBYTE) &SSP,sizeof(service_status_process), &dwbytesneeded)) {Debugwrite (L"ERROR —————— Queryservicestatusex failed (%d) \ n", GetLastError ()); Break; } if(Ssp.dwcurrentstate = =service_stopped) Break; if(GetTickCount ()-Dwstarttime >dwtimeout) {Debugwrite (L"ERROR —————— Wait timed out\n" ); Break; } } if(Ssp.dwcurrentstate! =service_stopped) Break; // ----------------------------------------------------------------------------------------------------------- -------------- //Start//attempt to start the service. if(! StartService (Schservice,//Handle to service 0,//Number of argumentsNULL))//no arguments{debugwrite (L"ERROR —————— StartService failed (%d) \ n", GetLastError ()); Break; } //Check the status until the service is no longer start pending.service_status_process Ssstatus; if(!queryservicestatusex (Schservice,//Handle to serviceSc_status_process_info,//info Level(LPBYTE) &ssstatus,//Address of structure sizeof(service_status_process),//size of structure&dwbytesneeded))//If buffer too small{debugwrite (L"ERROR —————— Queryservicestatusex failed (%d) \ n", GetLastError ()); Break; } //Save The tick count and initial checkpoint.Dwstarttime =GetTickCount (); DWORD Dwwaittime; DWORD Dwoldcheckpoint=Ssstatus.dwcheckpoint; while(Ssstatus.dwcurrentstate = =service_start_pending) { //Do not wait longer than the wait hint. A Good interval is//one-tenth the wait hint, but no less than 1 second and no//More than ten seconds.Dwwaittime = Ssstatus.dwwaithint/Ten; if(Dwwaittime < +) Dwwaittime= +; Else if(Dwwaittime >10000) Dwwaittime=10000; Sleep (Dwwaittime); //Check the status again. if(!queryservicestatusex (Schservice,//Handle to serviceSc_status_process_info,//info Level(LPBYTE) &ssstatus,//Address of structure sizeof(service_status_process),//size of structure&dwbytesneeded))//If buffer too small{debugwrite (L"ERROR —————— Queryservicestatusex failed (%d) \ n", GetLastError ()); Break; } if(Ssstatus.dwcheckpoint >dwoldcheckpoint) { //The service is making progress.Dwstarttime =GetTickCount (); Dwoldcheckpoint=Ssstatus.dwcheckpoint; } Else { if(GetTickCount ()-Dwstarttime >ssstatus.dwwaithint) {//No progress made within the wait hint. Break; } } } //Determine whether the service is running if(Ssstatus.dwcurrentstate! =service_running) {Debugwrite (L"ERROR —————— start spooler failed (%d) \ n", GetLastError ()); //printf ("Service not started.") \ n "); //printf ("Current state:%d\n", ssstatus.dwcurrentstate); //printf ("Exit Code:%d\n", Ssstatus.dwwin32exitcode); //printf ("Check point:%d\n", ssstatus.dwcheckpoint); //printf ("Wait Hint:%d\n", ssstatus.dwwaithint); } } while(false); if(NULL! =Schservice) Closeservicehandle (Schservice); if(NULL! =Schscmanager) Closeservicehandle (Schscmanager);}
Restart a service with the specified name under Windows