CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  Visual C++ 资源

请问各位大侠怎样停止一个服务(用函数),谢谢

楼主qqpeng76(pk)2003-08-04 19:50:20 在 VC/MFC / Visual C++ 资源 提问

请问各位大侠怎样停止一个服务(用函数),谢谢 问题点数:0、回复次数:3Top

1 楼microran2000(什么时候才能看到星星?)回复于 2003-08-04 20:06:28 得分 0

DWORD   StopServiceEntry(   SC_HANDLE   hSCM,   SC_HANDLE   hService,    
              BOOL   fStopDependencies,   DWORD   dwTimeout   )   {  
   
        SERVICE_STATUS   ss;  
        DWORD   dwStartTime   =   GetTickCount();  
   
        //   Make   sure   the   service   is   not   already   stopped  
        if   (   !QueryServiceStatus(   hService,   &ss   )   )  
              return   GetLastError();  
   
        if   (   ss.dwCurrentState   ==   SERVICE_STOPPED   )    
              return   ERROR_SUCCESS;  
   
        //   If   a   stop   is   pending,   just   wait   for   it  
        while   (   ss.dwCurrentState   ==   SERVICE_STOP_PENDING   )   {  
   
              Sleep(   ss.dwWaitHint   );  
              if   (   !QueryServiceStatus(   hService,   &ss   )   )  
                    return   GetLastError();  
   
              if   (   ss.dwCurrentState   ==   SERVICE_STOPPED   )  
                    return   ERROR_SUCCESS;  
   
              if   (   GetTickCount()   -   dwStartTime   >   dwTimeout   )  
                    return   ERROR_TIMEOUT;  
        }  
   
        //   If   the   service   is   running,   dependencies   must   be   stopped   first  
        if   (   fStopDependencies   )   {  
   
              DWORD   i;  
              DWORD   dwBytesNeeded;  
              DWORD   dwCount;  
   
              LPENUM_SERVICE_STATUS       lpDependencies   =   NULL;  
              ENUM_SERVICE_STATUS           ess;  
              SC_HANDLE                               hDepService;  
   
              //   Pass   a   zero-length   buffer   to   get   the   required   buffer   size  
              if   (   EnumDependentServices(   hService,   SERVICE_ACTIVE,    
                    lpDependencies,   0,   &dwBytesNeeded,   &dwCount   )   )   {  
   
                    //   If   the   Enum   call   succeeds,   then   there   are   no   dependent  
                    //   services   so   do   nothing  
   
              }   else   {  
                     
                    if   (   GetLastError()   !=   ERROR_MORE_DATA   )  
                          return   GetLastError();   //   Unexpected   error  
   
                    //   Allocate   a   buffer   for   the   dependencies  
                    lpDependencies   =   (LPENUM_SERVICE_STATUS)   HeapAlloc(    
                                GetProcessHeap(),   HEAP_ZERO_MEMORY,   dwBytesNeeded   );  
   
                    if   (   !lpDependencies   )  
                          return   GetLastError();  
   
                    __try   {  
   
                          //   Enumerate   the   dependencies  
                          if   (   !EnumDependentServices(   hService,   SERVICE_ACTIVE,    
                                      lpDependencies,   dwBytesNeeded,   &dwBytesNeeded,  
                                      &dwCount   )   )  
                                return   GetLastError();  
   
                          for   (   i   =   0;   i   <   dwCount;   i++   )   {  
   
                                ess   =   *(lpDependencies   +   i);  
   
                                //   Open   the   service  
                                hDepService   =   OpenService(   hSCM,   ess.lpServiceName,    
                                            SERVICE_STOP   |   SERVICE_QUERY_STATUS   );  
                                if   (   !hDepService   )  
                                      return   GetLastError();  
   
                                __try   {  
   
                                      //   Send   a   stop   code  
                                      if   (   !ControlService(   hDepService,   SERVICE_CONTROL_STOP,  
                                                  &ss   )   )  
                                            return   GetLastError();  
   
                                      //   Wait   for   the   service   to   stop  
                                      while   (   ss.dwCurrentState   !=   SERVICE_STOPPED   )   {  
   
                                            Sleep(   ss.dwWaitHint   );  
                                            if   (   !QueryServiceStatus(   hDepService,   &ss   )   )  
                                                  return   GetLastError();  
   
                                            if   (   ss.dwCurrentState   ==   SERVICE_STOPPED   )  
                                                  break;  
   
                                            if   (   GetTickCount()   -   dwStartTime   >   dwTimeout   )  
                                                  return   ERROR_TIMEOUT;  
                                      }  
   
                                }   __finally   {  
   
                                      //   Always   release   the   service   handle  
                                      CloseServiceHandle(   hDepService   );  
   
                                }  
   
                          }  
   
                    }   __finally   {  
   
                          //   Always   free   the   enumeration   buffer  
                          HeapFree(   GetProcessHeap(),   0,   lpDependencies   );  
   
                    }  
              }    
        }  
   
        //   Send   a   stop   code   to   the   main   service  
        if   (   !ControlService(   hService,   SERVICE_CONTROL_STOP,   &ss   )   )  
              return   GetLastError();  
   
        //   Wait   for   the   service   to   stop  
        while   (   ss.dwCurrentState   !=   SERVICE_STOPPED   )   {  
   
              Sleep(   ss.dwWaitHint   );  
              if   (   !QueryServiceStatus(   hService,   &ss   )   )  
                    return   GetLastError();  
   
              if   (   ss.dwCurrentState   ==   SERVICE_STOPPED   )  
                    break;  
   
              if   (   GetTickCount()   -   dwStartTime   >   dwTimeout   )  
                    return   ERROR_TIMEOUT;  
        }  
   
        //   Return   success  
        return   ERROR_SUCCESS;  
  }  
   
  void   StopService(LPCTSTR   szServiceName)  
  {  
  SC_HANDLE   hSCM;  
        SC_HANDLE   hService;  
        DWORD           dwError;  
   
        if   (   szServiceName   ==NULL   )   {  
               
        return   ;  
        }  
   
        __try   {  
   
              //   Open   the   SCM   database  
              hSCM   =   OpenSCManager(   NULL,   NULL,   SC_MANAGER_CONNECT   );  
              if   (   !hSCM   )   {  
             
                    __leave;  
              }  
   
              //   Open   the   specified   service  
              hService   =   OpenService(   hSCM,   szServiceName,   SERVICE_STOP  
                          |   SERVICE_QUERY_STATUS   |   SERVICE_ENUMERATE_DEPENDENTS   );  
              if   (   !hService   )   {  
                       
                    __leave;  
              }  
   
              //   Try   to   stop   the   service,   specifying   a   30   second   timeout  
            dwError   =   StopServiceEntry(   hSCM,   hService,   TRUE,   30000   )   ;  
               
   
        }   __finally   {  
   
              if   (   hService   )  
                    CloseServiceHandle(   hService   );  
   
              if   (   hSCM   )  
                    CloseServiceHandle(   hSCM   );  
        }  
   
  }  
  DWORD   StartService(LPCTSTR   szServiceName)  
  {  
          SERVICE_STATUS   ssStatus;    
          DWORD   dwOldCheckPoint;    
          DWORD   dwStartTickCount;  
          DWORD   dwWaitTime;  
          DWORD   dwStatus;  
  SC_HANDLE   schService;  
  SC_HANDLE   schSCManager   =   OpenSCManager(   NULL,   NULL,   SC_MANAGER_CONNECT   );  
              if   (   !schSCManager   )   {  
             
                    return   -1;  
              }  
          schService   =   OpenService(    
                  schSCManager,                     //   SCM   database    
                  szServiceName,                     //   service   name  
                  SERVICE_ALL_ACCESS);    
     
          if   (schService   ==   NULL)    
          {    
  return   -1;  
          }  
     
          if   (!StartService(  
                          schService,     //   handle   to   service    
                          0,                       //   number   of   arguments    
                          NULL)   )             //   no   arguments    
          {  
                  return   -1;    
          }  
         
     
          //   Check   the   status   until   the   service   is   no   longer   start   pending.    
     
          if   (!QueryServiceStatus(    
                          schService,       //   handle   to   service    
                          &ssStatus)   )     //   address   of   status   information   structure  
          {  
                  return   -1;  
          }  
     
          //   Save   the   tick   count   and   initial   checkpoint.  
   
          dwStartTickCount   =   GetTickCount();  
          dwOldCheckPoint   =   ssStatus.dwCheckPoint;  
   
          while   (ssStatus.dwCurrentState   ==   SERVICE_START_PENDING)    
          {    
                  //   Do   not   wait   longer   than   the   wait   hint.   A   good   interval   is    
                  //   one   tenth   the   wait   hint,   but   no   less   than   1   second   and   no    
                  //   more   than   10   seconds.    
     
                  dwWaitTime   =   ssStatus.dwWaitHint   /   10;  
   
                  if(   dwWaitTime   <   1000   )  
                          dwWaitTime   =   1000;  
                  else   if   (   dwWaitTime   >   10000   )  
                          dwWaitTime   =   10000;  
   
                  Sleep(   dwWaitTime   );  
   
                  //   Check   the   status   again.    
     
                  if   (!QueryServiceStatus(    
                                  schService,       //   handle   to   service    
                                  &ssStatus)   )     //   address   of   structure  
                          break;    
     
                  if   (   ssStatus.dwCheckPoint   >   dwOldCheckPoint   )  
                  {  
                          //   The   service   is   making   progress.  
   
                          dwStartTickCount   =   GetTickCount();  
                          dwOldCheckPoint   =   ssStatus.dwCheckPoint;  
                  }  
                  else  
                  {  
                          if(GetTickCount()-dwStartTickCount   >   ssStatus.dwWaitHint)  
                          {  
                                  //   No   progress   made   within   the   wait   hint  
                                  break;  
                          }  
                  }  
          }    
   
          if   (ssStatus.dwCurrentState   ==   SERVICE_RUNNING)    
          {  
                     
                  dwStatus   =   NO_ERROR;  
          }  
          else    
          {    
                 
                  dwStatus   =   GetLastError();  
          }    
     
          CloseServiceHandle(schService);    
          return   dwStatus;  
  }  
  Top

2 楼microran2000(什么时候才能看到星星?)回复于 2003-08-04 20:07:32 得分 0

调用  
  StopService("Messenger");  
  startService("Messenger");Top

3 楼qqpeng76(pk)回复于 2003-08-04 20:23:34 得分 0

有   StopService   这个方法吗  
   
    MSDN   中查不出呀Top

相关问题

  • 请教各位large()函数怎样用?
  • 停止函数执行
  • 怎样停止第三方钩子函数的动态链接库?
  • 请教各位,怎样通过函数名得到程序包中函数的函数指针?类似DLL的GetProcAddress()
  • 各位大虾:怎样在MFC中定义全局函数和静态数组
  • 各位高手帮忙:怎样从对话框接收一个函数?
  • 谢谢各位!进一步请教,怎样设置send函数的参数啊?
  • 怎样在PHP中调用C写的函数啊,各位大大帮帮忙
  • 回调函数怎样影响类的实例?各位大虾帮帮忙吧!!
  • 各位大虾, 怎样在自定义函数中运用window变量?

关键词

  • service
  • null
  • hservice
  • dwstarttime
  • dwcurrentstate
  • dwtimeout
  • ss
  • stopped
  • hscm
  • queryservicestatus

得分解答快速导航

  • 帖主:qqpeng76

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo