[ZZ] C + + Implements System services pause, stop, start

Source: Internet
Author: User

/*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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.