当前位置:首页
开发技术指南» 文章正文
    引言:
 

 

    摘要: 我在用javamail做收邮件的程序时,有时会出现这样一个异常,我不知道这是什么意思,为什么会产生。请大家帮心解释一下(java.lang.classcastexception) type exception report message internal server errordescription the server encountered an internal error (i......
    摘要: 为什么有的编译器,能编译通过,而有的要报错呢? 有没有什么讲究? 《汇编语言程序设计》中用的是哪个编译器? 有没有相关编译器说明的网站? 谢谢。 ......


各位老大,如何实现对IIS服务的控制啊

各位老大,如何实现对IIS服务的控制啊??主要包括停止服务、启动服务啊等等。因为我们的头头让我做一个远程管理IIS服务的东东,最主要是对WEB服务的操作啊,救命啊。  
  各位最好给段源代码读读,谢谢!  
  我把分追加到300分。  
  明天如果解决了此问题,下午18:00肯定结贴。如果没有顺延,多多赐教啊。

NO.1   作者: masterz

Stopping   a   Service  
   
  A   service   can   be   stopped   with   the   ControlService   function   by   sending   a   SERVICE_CONTROL_STOP   request.   If   the   SCM   receives   a   SERVICE_CONTROL_STOP   request   for   a   service,   it   instructs   the   service   to   stop   by   forwarding   the   request   to   the   services   ServiceMain   function.   However,   if   the   SCM   determines   that   other   running   services   are   dependent   on   the   specified   service,   it   will   not   forward   the   stop   request.   Instead,   it   returns   ERROR_DEPENDENT_SERVICES_RUNNING.   Therefore,   to   programmatically   stop   such   a   service,   you   must   first   enumerate   and   stop   its   dependent   services.  
   
   
  The   following   code   implements   a   StopService   function,   which   optionally   attempts   to   stop   the   specified   services   dependent   services.  
   
  #include   <windows.h>  
  #include   <tchar.h>  
  #include   <stdio.h>  
   
  //   This   function   attempts   to   stop   a   service.   It   allows   the   caller   to   //   specify   whether   dependent   services   should   also   be   stopped.   It   also    
  //   accepts   a   timeout   value,   to   prevent   a   scenario   in   which   a   service    
  //   shutdown   hangs,   then   the   application   stopping   the   service   hangs.  
  //    
  //   Parameters:        
  //       hSCM   -   Handle   to   the   service   control   manager.  
  //       hService   -   Handle   to   the   service   to   be   stopped.  
  //       fStopDependencies   -   Indicates   whether   to   stop   dependent   services.  
  //       dwTimeout   -   maximum   time   (in   milliseconds)   to   wait  
  //    
  //   If   the   operation   is   successful,   returns   ERROR_SUCCESS.   Otherwise,   //   returns   a   system   error   code.  
   
  DWORD   StopService(   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;  
  }  
   
  //   Helper   function   to   display   an   error   message    
   
  void   DisplayError(   LPTSTR   szAPI,   DWORD   dwError   )    
  {  
        LPTSTR   lpBuffer   =   NULL;  
   
        FormatMessage(   FORMAT_MESSAGE_ALLOCATE_BUFFER   |  
                    FORMAT_MESSAGE_FROM_SYSTEM,   NULL,   dwError,  
                    MAKELANGID(LANG_NEUTRAL,   SUBLANG_DEFAULT),  
                    (LPTSTR)   &lpBuffer,   0,   NULL   );  
   
        _tprintf(   TEXT("%s   failed:\n"),   szAPI   );  
        _tprintf(   TEXT("         error   code   =   %u\n"),   dwError   );  
        _tprintf(   TEXT("         message         =   %s\n"),   lpBuffer   );  
   
        LocalFree(   lpBuffer   );  
  }  
   
  //   Entry   point   for   the   program.   This   function   contains   sample   code    
  //   demonstrating   how   to   use   the   StopService   function   implemented    
  //   above.  
  //    
  //   Parameters:        
  //       argc   -   the   number   of   command-line   arguments  
  //       argv[]   -   an   array   of   command-line   arguments  
   
  void   _tmain(   int   argc,   TCHAR   *argv[]   )    
  {  
        SC_HANDLE   hSCM;  
        SC_HANDLE   hService;  
        DWORD           dwError;  
   
        if   (   argc   <   2   )    
        {  
              _tprintf(   TEXT("usage:   \"%s\"   <ServiceName>\n"),   argv[0]   );  
              return;  
        }  
   
        __try    
        {  
              //   Open   the   SCM   database  
              hSCM   =   OpenSCManager(   NULL,   NULL,   SC_MANAGER_CONNECT   );  
              if   (   !hSCM   )    
              {  
                    DisplayError(   TEXT("OpenSCManager()"),   GetLastError()   );  
                    __leave;  
              }  
   
              //   Open   the   specified   service  
              hService   =   OpenService(   hSCM,   argv[1],   SERVICE_STOP  
                          |   SERVICE_QUERY_STATUS   |   SERVICE_ENUMERATE_DEPENDENTS   );  
              if   (   !hService   )    
              {  
                    DisplayError(   TEXT("OpenService()"),   GetLastError()   );  
                    __leave;  
              }  
   
              //   Try   to   stop   the   service,   specifying   a   30   second   timeout  
              dwError   =   StopService(   hSCM,   hService,   TRUE,   30000   )   ;  
              if   (   dwError   ==   ERROR_SUCCESS   )  
                    _tprintf(   TEXT("Service   stopped.\n")   );  
              else  
                    DisplayError(   TEXT("StopService()"),   dwError   );  
   
        }    
        __finally    
        {  
              if   (   hService   )  
                    CloseServiceHandle(   hService   );  
   
              if   (   hSCM   )  
                    CloseServiceHandle(   hSCM   );  
        }  
  }  
   
 


 ·如何更新blob类型数据    »显示摘要«
    摘要: 后台数据库为sql server2000,有一个表中包含一个text字段,与pb中对应的数据类型为blob,但是在更新数据库时候,无法成功,不知何故,代码段如下: blob lb_zw string ls_id lb_zw = blob(...) ls_id = ... ...... updateblob t_kyxx set wz_zw = :lb_zw where wz_id = :l......
» 本期热门文章:

©2000-2007 All Rights Reserved. 最佳浏览:1024X768 MSIE