C++学习笔记-后台服务程序开发模式(四)

类别:VC语言 点击:0 评论:0 推荐:

5.停止和删除已安装的服务程序

 

//停止和删除已安装的服务程序

void removeService()

{

     SC_HANDLE schService;

     SC_HANDLE schSCManager;

     //打开服务管理数据库

     schSCManager=OpenSCManager(

                       NULL,    //本地计算机

                       NULL,    //默认的数据库

                       SC_MANAGER_ALL_ACCESS  //要求所有的访问权

                       );

     if(schSCManager)

     {

         //获取服务程序句柄

         schService=OpenService(

              schSCManager,          //服务管理数据库句柄

              TEXT(SZSERVICENAME),   //服务名

              SERVICE_ALL_ACCESS     //响应所有的访问请求

              );

         if(schService)

         {

              //试图停止服务

              if(ControlService(

                   schService,                 //服务程序句柄

                   SERVICE_CONTROL_STOP,  //停止服务请求码

                   &ssStatus              //接收最后的服务状态信息

                   ))

              {

                   _tprintf(TEXT("Stopping %s."),TEXT(SZAPPNAME));

                   Sleep(1000);

 

                   //等待服务停止

                   //

                   while(QueryServiceStatus(schService,&ssStatus))

                   {

                       if(SERVICE_STOP_PENDING==ssStatus.dwCurrentState)

                       {

                            _tprintf(TEXT("."));

                            Sleep(1000);

                       }

                       else

                            break;

                   }

                   if(SERVICE_STOPPED==ssStatus.dwCurrentState)

                       _tprintf(TEXT("\n %s stopped. \n"),TEXT(SZAPPNAME));

                   else

                       _tprintf(TEXT("\n %s failed to stopp. \n"),TEXT(SZAPPNAME));

              }

              //删除已安装的服务程序安装

              if(DeleteService(schService))

                   _tprintf(TEXT("%s removed. \n"),TEXT(SZAPPNAME));

              else

_tprintf(TEXT("DeleteService failed - %s. \n"), GetLastError());

              CloseServiceHandle(schService);

         }

         else

              _tprintf(TEXT("OpenService failed - %s \n"),GetLastError());

         CloseServiceHandle(schSCManager);

     }

     else

         _tprintf(TEXT("OpenSCManager failed - %s \n"),GetLastError());

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

     在编译程序的时候,我们会发觉ServiceStop();ServicePause();ServiceContinue();等三个函数没有具体实现,这对于理解此文的人来说应该不难编写,在此我可以给点文档内的参考:声明     SetTheServiceStatus()函数,

//
   //  SetTheServiceStatus -   This just wraps up SetServiceStatus.
   //
   void SetTheServiceStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode,
                            DWORD dwCheckPoint,   DWORD dwWaitHint)
   {
      SERVICE_STATUS ss;  // Current status of the service.

      //
      // Disable control requests until the service is started.
      //
      if (dwCurrentState == SERVICE_START_PENDING)
         ss.dwControlsAccepted = 0;
      else
         ss.dwControlsAccepted =
                       SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN;
                       // Other flags include SERVICE_ACCEPT_PAUSE_CONTINUE
                       // and SERVICE_ACCEPT_SHUTDOWN.

      // Initialize ss structure.
      ss.dwServiceType             = SERVICE_WIN32_OWN_PROCESS;
      ss.dwServiceSpecificExitCode = 0;
      ss.dwCurrentState            = dwCurrentState;
      ss.dwWin32ExitCode           = dwWin32ExitCode;
      ss.dwCheckPoint              = dwCheckPoint;
      ss.dwWaitHint                = dwWaitHint;

      // Send status of the service to the Service Controller.
      if (!SetServiceStatus(ssh, &ss))
         ErrorStopService(TEXT("SetServiceStatus"));
   }

    然后用如下的方式来调用函数来实现源程序中缺少的功能 : 

    SetTheServiceStatus(SERVICE_STOPPED, GetLastError(), 0, 0);// Stop the service.


本文地址:http://com.8s8s.com/it/it997.htm