我做了一个在服务器上运行的程序,注册到了服务里。这个服务程序可以打开一个管理窗口,现在的问题是:1、如何通过管理窗口控制服务的启动和停止。
2、能直接调用ServiceStart(Sender: TService; var Started: Boolean);吗?如果能,怎样调,参数怎样写?3、服务器上的程序能获取局域网内其他机器是主板,或磁盘,或cpu 的信息吗?

解决方案 »

  1.   

    1、控制服务可以用SCMmanager做到
    2、不明白什么意思,你说的是直接在pas文件中调用ServiceStart?那就没问题
    3、不可能
      

  2.   


    SCMmanager,有没实现的简单样例。对的,pas文件中调用ServiceStart,参数怎样写?自身退出服务,怎样退?Terminate;可以执行,但好像有点问题,有没其他方法。在服务里,不接受键盘事件,这怎样处理呀?
      

  3.   

    1、建立标准的CService程序,按照Delphi向导做即可
    2、使用SCMmanager来控制(windows)服务启动停止
    3、不知直接获取其他机器信息
    下面这个服务控制程序很好用,可以直接用unit uServiceControl;interfaceuses
      Windows, SysUtils, WinSvc, SvcMgr;type                                                                             
      TServiceControl = class(TObject)                                               
      private                                                                        
        FMachine, FService: string;                                                  
        function GetNTStatus: DWord;                                                 
        function GetStatus: TCurrentStatus;  protected                                                                      
      public                                                                         
        function Start: Boolean;                                                     
        function Stop: Boolean;                                                      
        function Pause: Boolean;                                                     
        function Continue: Boolean;    function IsInstall: Boolean;    constructor Create(Service: string; Machine: string = '');
        property NTStatus: DWord read GetNTStatus;
        property Status: TCurrentStatus read GetStatus;
        property Service: string read FService write FService;
        property Machine: string read FMachine write FMachine;
      published                                                                      
      end; { TServiceControl }
    function StatusToNT(Status: TCurrentStatus): DWord;                              
    function StatusFromNt(Status: DWord): TCurrentStatus;implementationfunction StatusToNT(Status: TCurrentStatus): DWord;                              
    const                                                                            
      NTServiceStatus: array[TCurrentStatus] of DWord = (SERVICE_STOPPED,            
        SERVICE_START_PENDING, SERVICE_STOP_PENDING, SERVICE_RUNNING,                
        SERVICE_CONTINUE_PENDING, SERVICE_PAUSE_PENDING, SERVICE_PAUSED);            
    begin                                                                            
      Result := NTServiceStatus[Status];                                             
    end;function StatusFromNt(Status: DWord): TCurrentStatus;                            
    const                                                                            
      CurrentStatus: array[1..7] of TCurrentStatus = (csStopped, csStartPending,
        csStopPending, csRunning, csContinuePending, csPausePending, csPaused);      
    begin
      Result := CurrentStatus[Status];
    end;
    { TServiceControl }
    constructor TServiceControl.Create(Service: string; Machine: string = '');       
    begin                                                                            
      FService := Service;                                                           
      FMachine := Machine;                                                           
    end;
    function TServiceControl.GetStatus: TCurrentStatus;                              
    begin                                                                            
      Result := StatusFromNT(GetNTStatus);                                           
    end;
    function TServiceControl.GetNTStatus: DWord;                                     
    var                                                                              
      SvcMgr, Svc: SC_HANDLE;                                                        
      SvcStatus: TServiceStatus;                                                     
    begin                                                                            
      Result := 0;                                                                   
      SvcMgr := OpenSCManager(PChar(FMachine), nil, SC_MANAGER_CONNECT);             
      if SvcMgr = 0 then
      begin
    //    RaiseLastWin32Error;
        Exit;
      end;
      try                                                                            
        Svc := OpenService(SvcMgr, PChar(FService), SERVICE_QUERY_STATUS);           
        if Svc = 0 then RaiseLastWin32Error;                                         
        try                                                                          
          if QueryServiceStatus(Svc, SvcStatus) then                                 
            Result := SvcStatus.dwCurrentState                                       
          else                                                                       
            RaiseLastWin32Error;                                                     
        finally                                                                      
          CloseServiceHandle(Svc);                                                   
        end;                                                                         
      finally                                                                        
        CloseServiceHandle(SvcMgr);                                                  
      end;                                                                           
    end;function TServiceControl.IsInstall: Boolean; 
    var                                                                              
      SvcMgr, Svc: SC_HANDLE;                                                        
    //  SvcStatus: TServiceStatus;                                                     
    begin
      Result := False;
      SvcMgr := OpenSCManager(PChar(FMachine), nil, SC_MANAGER_CONNECT);
      if SvcMgr = 0 then RaiseLastWin32Error;
      try
        Svc := OpenService(SvcMgr, PChar(FService), SERVICE_QUERY_STATUS);
        if Svc <> 0 then
        begin
          Result := True;
        end;                                                                         
      finally                                                                        
        CloseServiceHandle(SvcMgr);                                                  
      end;                                                                           
    end;
      

  4.   

    function TServiceControl.Start: Boolean;                                         
    var                                                                              
      psTemp: PChar;                                                                 
      CheckPoint: DWord;                                                             
      SvcMgr, Svc: SC_HANDLE;                                                        
      SvcStatus: TServiceStatus;                                                     
    begin                                                                            
      SvcMgr := OpenSCManager(PChar(FMachine), nil, SC_MANAGER_CONNECT);
      CheckPoint := 0;  if SvcMgr = 0 then RaiseLastWin32Error;                                        
      try                                                                            
        Svc := OpenService(SvcMgr, PChar(FService), SERVICE_START or SERVICE_QUERY_STATUS);                                                                      
        if Svc = 0 then RaiseLastWin32Error;                                         
        try                                                                          
          psTemp := nil;                                                             
          if StartService(Svc, 0, psTemp) then                                       
          begin                                                                      
            if not QueryServiceStatus(Svc, SvcStatus) then                           
              RaiseLastWin32Error                                                    
            else                                                                     
            with SvcStatus do                                                        
            begin                                                                    
              while (SERVICE_RUNNING <> dwCurrentState) do                           
              begin                                                                  
    //            CheckPoint := dwCheckPoint;                                          
                Sleep(dwWaitHint);
                if not QueryServiceStatus(Svc, SvcStatus) then
                  Break;
    //            if dwCheckPoint < CheckPoint then
    //              Break;
                if 5 < CheckPoint then
                  Break;
                Inc(CheckPoint);
              end;
            end;                                                                     
          end;                                                                       
        finally                                                                      
          CloseServiceHandle(Svc);                                                   
        end;                                                                         
      finally                                                                        
        CloseServiceHandle(SvcMgr);                                                  
      end;                                                                           
      Result := SERVICE_RUNNING = SvcStatus.dwCurrentState;                          
    end;function TServiceControl.Stop: Boolean;                                          
    var                                                                              
      CheckPoint: DWord;                                                             
      SvcMgr, Svc: SC_HANDLE;                                                        
      SvcStatus: TServiceStatus;                                                     
    begin                                                                            
      SvcMgr := OpenSCManager(PChar(FMachine), nil, SC_MANAGER_CONNECT);
      CheckPoint := 0;
      
      if SvcMgr = 0 then RaiseLastWin32Error;                                        
      try                                                                            
        Svc := OpenService(SvcMgr, PChar(FService), SERVICE_STOP or SERVICE_QUERY_STATUS);                                                                       
        if Svc = 0 then RaiseLastWin32Error;                                         
        try                                                                          
          if ControlService(Svc, SERVICE_CONTROL_STOP, SvcStatus) then               
          begin                                                                      
            if not QueryServiceStatus(Svc, SvcStatus) then                           
              RaiseLastWin32Error                                                    
            else                                                                     
            with SvcStatus do                                                        
            begin                                                                    
              while (SERVICE_STOPPED <> dwCurrentState) do                           
              begin                                                                  
    //            CheckPoint := dwCheckPoint;
                Sleep(dwWaitHint);
                if not QueryServiceStatus(Svc, SvcStatus) then                       
                  Break;                                                             
    //            if dwCheckPoint < CheckPoint then
    //              Break;
                if 5 < CheckPoint then
                  Break;
                Inc(CheckPoint);
              end;
            end;                                                                     
          end;                                                                       
        finally                                                                      
          CloseServiceHandle(Svc);                                                   
        end;                                                                         
      finally                                                                        
        CloseServiceHandle(SvcMgr);                                                  
      end;                                                                           
      Result := SERVICE_STOPPED = SvcStatus.dwCurrentState;                          
    end;
      

  5.   


    function TServiceControl.Continue: Boolean;                                      
    var                                                                              
      CheckPoint: DWord;                                                             
      SvcMgr, Svc: SC_HANDLE;                                                        
      SvcStatus: TServiceStatus;                                                     
    begin                                                                            
      SvcMgr := OpenSCManager(PChar(FMachine), nil, SC_MANAGER_CONNECT);  CheckPoint := 0;  if SvcMgr = 0 then RaiseLastWin32Error;                                        
      try                                                                            
        Svc := OpenService(SvcMgr, PChar(FService), SERVICE_PAUSE_CONTINUE or SERVICE_QUERY_STATUS);
        if Svc = 0 then RaiseLastWin32Error;                                         
        try                                                                          
          if ControlService(Svc, SERVICE_CONTROL_CONTINUE, SvcStatus) then           
          begin                                                                      
            if not QueryServiceStatus(Svc, SvcStatus) then                           
              RaiseLastWin32Error                                                    
            else                                                                     
            with SvcStatus do                                                        
            begin
              while (SERVICE_RUNNING <> dwCurrentState) do                           
              begin                                                                  
    //            CheckPoint := dwCheckPoint;                                          
                Sleep(dwWaitHint);
                if not QueryServiceStatus(Svc, SvcStatus) then                       
                  Break;                                                             
    //            if dwCheckPoint < CheckPoint then
    //              Break;
                if 5 < CheckPoint then
                  Break;
                Inc(CheckPoint);
              end;
            end;                                                                     
          end;                                                                       
        finally                                                                      
          CloseServiceHandle(Svc);                                                   
        end;                                                                         
      finally                                                                        
        CloseServiceHandle(SvcMgr);                                                  
      end;                                                                           
      Result := SERVICE_RUNNING = SvcStatus.dwCurrentState;                          
    end;
    function TServiceControl.Pause: Boolean;                                         
    var                                                                              
      CheckPoint: DWord;                                                             
      SvcMgr, Svc: SC_HANDLE;                                                        
      SvcStatus: TServiceStatus;                                                     
    begin                                                                            
      SvcMgr := OpenSCManager(PChar(FMachine), nil, SC_MANAGER_CONNECT);  CheckPoint := 0;  if SvcMgr = 0 then RaiseLastWin32Error;                                        
      try                                                                            
        Svc := OpenService(SvcMgr, PChar(FService), SERVICE_PAUSE_CONTINUE or SERVICE_QUERY_STATUS);                                                             
        if Svc = 0 then RaiseLastWin32Error;                                         
        try                                                                          
          if ControlService(Svc, SERVICE_CONTROL_PAUSE, SvcStatus) then              
          begin                                                                      
            if not QueryServiceStatus(Svc, SvcStatus) then                           
              RaiseLastWin32Error                                                    
            else                                                                     
            with SvcStatus do                                                        
            begin                                                                    
              while (SERVICE_PAUSED <> dwCurrentState) do                            
              begin                                                                  
    //            CheckPoint := dwCheckPoint;                                          
                Sleep(dwWaitHint);
                if not QueryServiceStatus(Svc, SvcStatus) then                       
                  Break;                                                             
    //            if dwCheckPoint < CheckPoint then
    //              Break;
                if 5 < CheckPoint then
                  Break;
                Inc(CheckPoint);                                                             
              end;
            end;                                                                     
          end;                                                                       
        finally                                                                      
          CloseServiceHandle(Svc);                                                   
        end;                                                                         
      finally                                                                        
        CloseServiceHandle(SvcMgr);                                                  
      end;                                                                           
      Result := SERVICE_PAUSED = SvcStatus.dwCurrentState;                           
    end;
                                                                              
    end.
      

  6.   

    yq3woaini,谢谢。这个样例是怎样创建出来的?能说的具体点吗?另外问一下,在服务里,不接受键盘事件,这有处理的办法吗?