VC ++ starts and stops services

Source: Internet
Author: User
Include header file: # include <winsvc. h>
The following uses Web services as an example:
 

# Include <winsvc. h>
Void cstartservicedlg: onbnclickedbutton1 ()
...{
// Open the service management object
SC _handle HSCC =: openscmanager (null,
Null, generic_execute );
If (SCS = NULL)
...{
Trace ("Open scmanager error ");
Return;
}
// Open the WWW Service.
SC _handle HSVC =: openservice (SCS, "W3SVC ",
Service_start | service_query_status | service_stop );
If (HSVC = NULL)
...{
Trace ("Open WWW erron. ");
: Closeservicehandle (SCS );
Return;
}
// Obtain the service status
Service_status status;
If (: queryservicestatus (HSVC, & Status) = false)
...{
Trace ("Get service state error. ");
: Closeservicehandle (HSVC );
: Closeservicehandle (SCS );
Return;
}
// If it is in the stopped status, start the service; otherwise, stop the service.
If (status. dwcurrentstate = service_running)
...{
// Stop the service
If (: controlservice (HSVC,
Service_control_stop, & Status) = false)
...{
Trace ("Stop Service error. ");
: Closeservicehandle (HSVC );
: Closeservicehandle (SCS );
Return;
}
// Wait for the service to stop
While (: queryservicestatus (HSVC, & Status) = true)
...{
: Sleep (status. dwwaithint );
If (status. dwcurrentstate = service_stopped)
...{
Afxmessagebox ("Stop success. ");
: Closeservicehandle (HSVC );
: Closeservicehandle (SCS );
Return;
}
}
}
Else if (status. dwcurrentstate = service_stopped)
...{
// Start the service
If (: startservice (HSVC, null, null) = false)
...{
Trace ("Start service error. ");
: Closeservicehandle (HSVC );
: Closeservicehandle (SCS );
Return;
}
// Wait for the service to start
While (: queryservicestatus (HSVC, & Status) = true)
...{
: Sleep (status. dwwaithint );
If (status. dwcurrentstate = service_running)
...{
Afxmessagebox ("START success. ");
: Closeservicehandle (HSVC );
: Closeservicehandle (SCS );
Return;
}
}
}
Trace ("START error. ");
: Closeservicehandle (HSVC );
: Closeservicehandle (SCS );
Return;
}

 

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.