Start and Stop services

Source: Internet
Author: User
Start and Stop services include the 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 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;
   }

  // Obtain the service status
   SERVICE_STATUS status;
   if( ::QueryServiceStatus( hSvc, &status) == FALSE)
   {
     TRACE( "Get Service state error。");
     ::CloseServiceHandle( hSvc);
     ::CloseServiceHandle( hSC);
     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( hSC);
       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( 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 the 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;
}

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.