c++builder控制服务

//判断msdtc服务是否启动
      if(!IsServerRunning("msdtc")){
      // Application->MessageBox("未启动","提示",MB_OK);
        //启动 msdtc 服务
        xxStartService("msdtc");
      }


/

bool __fastcall TfrmMain::xxStartService(LPCSTR ServiceName)
  {
          bool   ret;
          SC_HANDLE   hService;
          SC_HANDLE   hManager;
          SERVICE_STATUS   stus; 
  
          hManager   =   OpenSCManager(NULL,
                  0,   SC_MANAGER_ALL_ACCESS); 
          if   (hManager==NULL)   return   false; 
          hService   =   OpenService(hManager, 
                  ServiceName,   SERVICE_ALL_ACCESS);
          if   (hService   &&   QueryServiceStatus(hService,&stus)){
              //如果服务在运行
              if   (   stus.dwCurrentState     ==   SERVICE_STOPPED   ){
                          //启动服务
                ret   =   StartService(hService,   NULL,   NULL   );
              } 
          } 
          else   { 
                ret   =   false; 
          } 
          if   (hService   !=   NULL) 
                          CloseServiceHandle(hService); 
                          
          CloseServiceHandle(hManager); 
  
          return   ret; 
  }


bool __fastcall TfrmMain::xxStopService(LPCSTR ServiceName)
  { 
          bool   ret; 
          SC_HANDLE   hService; 
          SC_HANDLE   hManager; 
          SERVICE_STATUS   stus;
  
          hManager   =   OpenSCManager(NULL, 
                  0,   SC_MANAGER_ALL_ACCESS); 
          if   (hManager==NULL)   return   false;
          hService   =   OpenService(hManager, 
                  ServiceName,   SERVICE_ALL_ACCESS);
          if   (hService   &&   QueryServiceStatus(hService,&stus))   {

                //判断这个服务是否在运行
                if   (   stus.dwCurrentState     ==   SERVICE_RUNNING   ){

                                //              //服务名称 关闭服务
                      ret   =   ControlService(hService,SERVICE_CONTROL_STOP,&stus); 
                    }
              }
          else 
                  ret   =   false; 
          if   (hService   !=   NULL) 
                  CloseServiceHandle(hService); 
  
          CloseServiceHandle(hManager);
  
          return   ret;
  }

   //判断系统服务是否启动
   bool __fastcall TfrmMain::IsServerRunning(LPCSTR ServiceName)
  { 
          SC_HANDLE   hScManage   =   OpenSCManager("",NULL,SC_MANAGER_ALL_ACCESS);
         
          SERVICE_STATUS   SrvStatus; 
        
          DWORD   dwStat   =   0; 
          
          if(hScManage   >   0) 
          {
                 
                  SC_HANDLE   hSrv   =   OpenService(hScManage,ServiceName,SERVICE_QUERY_STATUS);
                  
                  if(hSrv   >   0) 
                  { 
                        
                          if(QueryServiceStatus(hSrv,   &SrvStatus))
                                  dwStat   =   SrvStatus.dwCurrentState;
                        
                          CloseServiceHandle(hSrv);
                  }
          }
          CloseServiceHandle(hScManage);
          return   dwStat   ==   SERVICE_RUNNING;
  }

你可能感兴趣的:(C++,c,C#,Access,C++Builder)