Start, stop, pause, and continue of Windows Services

Source: Internet
Author: User

 

SC _HANDLE scm, sHandle;

SERVICE_STATUS ServiceStatus;

Scm = OpenSCManager (NULL, NULL, SC _MANAGER_ALL_ACCESS );

If (scm! = NULL)

{

// Start the service

SHandle = OpenService (scm, "GwbnService", SERVICE_START );

If (sHandle! = NULL)

{

StartService (sHandle, 0, NULL) // start Service

}

// Stop the Service

SHandle = OpenService (scm, "GwbnService", SERVICE_STOP | SERVICE_QUERY_STATUS );

If (sHandle! = NULL)

QueryServiceStatus (sHandle, & ServiceStatus );

If (ServiceStatus. dwCurrentState = SERVICE_RUNNING | ServiceStatus. dwCurrentState = SERVICE_PAUSED)

{

ControlService (sHandle, SERVICE_CONTROL_STOP, & ServiceStatus );

}

// Pause the Service

SHandle = OpenService (scm, "GwbnService", SERVICE_PAUSE_CONTINUE | SERVICE_QUERY_STATUS );

If (sHandle! = NULL)

QueryServiceStatus (sHandle, & ServiceStatus );

If (ServiceStatus. dwCurrentState = SERVICE_RUNNING)

{

ControlService (sHandle, SERVICE_CONTROL_PAUSE, & ServiceStatus );

}

// Continue Service

SHandle = OpenService (scm, "GwbnService", SERVICE_PAUSE_CONTINUE | SERVICE_QUERY_STATUS );

If (sHandle! = NULL)

QueryServiceStatus (sHandle, & ServiceStatus );

If (ServiceStatus. dwCurrentState = SERVICE_PAUSED)

{

ControlService (sHandle, SERVICE_CONTROL_CONTINUE, & ServiceStatus );

}

 

}

 

Note: The service name and display name are different. The service name is required here.

 

 

From: Awey_001 Column

Related Article

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.