/*Name: System service Management language: C + + by Unixcroot (Croot) reprinted from the Internet introduction: State acquisition of Windows system Services, service paused, open, stop operation code*/voidCstartservicedlg::onbnclickedbutton1 () {//Open the Service management objectSc_handle HSC =:: OpenSCManager (null, NULL, generic_execute); if(HSC = =NULL) {TRACE ("Open Scmanager Error"); return; } //Open the WWW service. Sc_handle hsvc =:: OpenService (HSC,"w3svc", Service_start| Service_query_status |service_stop); if(Hsvc = =NULL) {TRACE ("Open www erron. "); :: Closeservicehandle (HSC); return; } //get the status of a serviceService_status STATUS; if(:: QueryServiceStatus (hsvc, &status) = =FALSE) {TRACE ("Get Service State error. "); :: Closeservicehandle (HSVC); :: Closeservicehandle (HSC); return; } //Start the service if it is in a stopped state, or stop the service. if(Status.dwcurrentstate = =service_running) { //Stop Service if(:: ControlService (Hsvc, Service_control_stop,&status) = =FALSE) {TRACE ("Stop service error. "); :: Closeservicehandle (HSVC); :: Closeservicehandle (HSC); return; } //wait for service to stop while(:: QueryServiceStatus (hsvc, &status) = =TRUE) {:: Sleep (Status.dwwaithint); if(Status.dwcurrentstate = =service_stopped) {AfxMessageBox ("stop success. "); :: Closeservicehandle (HSVC); :: Closeservicehandle (HSC); return; } } } Else if(Status.dwcurrentstate = =service_stopped) { //Start the service if(:: StartService (HSVC, NULL, null) = =FALSE) {TRACE ("Start service error. "); :: Closeservicehandle (HSVC); :: Closeservicehandle (HSC); return; } //wait for service to start while(:: QueryServiceStatus (hsvc, &status) = =TRUE) {:: Sleep (Status.dwwaithint); if(Status.dwcurrentstate = =service_running) {AfxMessageBox ("start success. "); :: Closeservicehandle (HSVC); :: Closeservicehandle (HSC); return; }}} TRACE ("start error. "); :: Closeservicehandle (HSVC); :: Closeservicehandle (HSC); return;}
[ZZ] C + + Implements System services pause, stop, start