⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 控制windows的服务启动和停止.txt

📁 大量Delphi开发资料
💻 TXT
字号:
本人测试通过
启动和停止服务    
    
uses WinSvc,SvcMgr ;

//Here's example I made. Hope it helps. "ServerServiceName" is the Name of

//service to control.

procedure StartServiceExecute(Sender: TObject);

Var

SCH: SC_HANDLE;

SvcSCH: SC_HANDLE;

arg: PChar;

begin

SCH:= OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);

If SCH=0 then

Begin

MessageDlg('Error: OpenSCManager', mtError, [mbOK], 0);

Exit;

End;

Screen.Cursor:= crHourGlass;

try

SvcSCH:= OpenService(SCH, PChar(ServerServiceName), SERVICE_ALL_ACCESS);

If SvcSCH=0 then

Begin

MessageDlg('Error: OpenService', mtError, [mbOK], 0);

Exit;

End;

try

arg:= nil;

If Not StartService(SvcSCH, 0, arg) then

Begin

Case GetLastError of

ERROR_ACCESS_DENIED :MessageDlg('The specified handle

was not opened with SERVICE_START access.', mtError, [mbOK], 0);

ERROR_INVALID_HANDLE :MessageDlg('The specified handle

is invalid.', mtError, [mbOK], 0);

ERROR_PATH_NOT_FOUND :MessageDlg('The service binary

file could not be found.', mtError, [mbOK], 0);

ERROR_SERVICE_ALREADY_RUNNING :MessageDlg('An instance of the

service is already running.', mtError, [mbOK], 0);

ERROR_SERVICE_DATABASE_LOCKED :MessageDlg('The database is

locked.', mtError, [mbOK], 0);

ERROR_SERVICE_DEPENDENCY_DELETED :MessageDlg('The service depends

on a service that does not exist or has been marked for deletion.', mtError,

[mbOK], 0);

ERROR_SERVICE_DEPENDENCY_FAIL :MessageDlg('The service depends

on another service that has failed to start.', mtError, [mbOK], 0);

ERROR_SERVICE_DISABLED :MessageDlg('The service has been

disabled.', mtError, [mbOK], 0);

ERROR_SERVICE_LOGON_FAILED :MessageDlg('The service could

not be logged on. This error occurs if the service was started from an

account that does not have the "Log on as a service" right.', mtError,

[mbOK], 0);

ERROR_SERVICE_MARKED_FOR_DELETE :MessageDlg('The service has been

marked for deletion.', mtError, [mbOK], 0);

ERROR_SERVICE_NO_THREAD :MessageDlg('A thread could not

be created for the service.', mtError, [mbOK], 0);

ERROR_SERVICE_REQUEST_TIMEOUT :MessageDlg('The process for the

service was started, but it did not call StartServiceCtrlDispatcher, or the

thread that called StartServiceCtrlDispatcher may be blocked in a control

handler function.', mtError, [mbOK], 0);

Else MessageDlg('Error:

StartService', mtError, [mbOK], 0);

End;{CASE}

End;

CheckServiceState;

finally

CloseServiceHandle(SvcSCH);

end;

finally

Screen.Cursor:= crDefault;

CloseServiceHandle(SCH);

end;

end;

procedure StopServiceExecute(Sender: TObject);

Var

SCH: SC_HANDLE;

SvcSCH: SC_HANDLE;

ss: TServiceStatus;

begin

SCH:= OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);

If SCH=0 then

Begin

MessageDlg('Error: OpenSCManager', mtError, [mbOK], 0);

Exit;

End;

Screen.Cursor:= crHourGlass;

try

SvcSCH:= OpenService(SCH, PChar(ServerServiceName), SERVICE_ALL_ACCESS);

If SvcSCH=0 then

Begin

MessageDlg('Error: OpenService', mtError, [mbOK], 0);

Exit;

End;

try

If Not ControlService(SvcSCH, SERVICE_CONTROL_STOP, ss) then

Begin

Case GetLastError of

ERROR_ACCESS_DENIED :MessageDlg('The specified

handle was not opened with the necessary access.', mtError, [mbOK], 0);

ERROR_DEPENDENT_SERVICES_RUNNING :MessageDlg('The service cannot

be stopped because other running services are dependent on it.', mtError,

[mbOK], 0);

ERROR_INVALID_HANDLE :MessageDlg('The specified

handle was not obtained using CreateService or OpenService, or the handle is

no longer valid.', mtError, [mbOK], 0);

ERROR_INVALID_PARAMETER :MessageDlg('The requested

control code is undefined.', mtError, [mbOK], 0);

ERROR_INVALID_SERVICE_CONTROL :MessageDlg('The requested

control code is not valid, or it is unacceptable to the service.', mtError,

[mbOK], 0);

ERROR_SERVICE_CANNOT_ACCEPT_CTRL :MessageDlg('The requested

control code cannot be sent to the service because the state of the service

is SERVICE_STOPPED, SERVICE_START_PENDING, or SERVICE_STOP_PENDING.',

mtError, [mbOK], 0);

ERROR_SERVICE_NOT_ACTIVE :MessageDlg('The service has not

been started.', mtError, [mbOK], 0);

ERROR_SERVICE_REQUEST_TIMEOUT :MessageDlg('The process for the

service was started, but it did not call StartServiceCtrlDispatcher, or the

thread that called StartServiceCtrlDispatcher may be blocked in a control

handler function.', mtError, [mbOK], 0);

ERROR_SHUTDOWN_IN_PROGRESS :MessageDlg('The system is

shutting down.', mtError, [mbOK], 0);

Else MessageDlg('Error:

ControlService', mtError, [mbOK], 0);

End;

End;

CheckServiceState;

finally

CloseServiceHandle(SvcSCH);

end;

finally

Screen.Cursor:= crDefault;

CloseServiceHandle(SCH);

end;

end;

如果出现编译错误,请删除CheckServiceState则一行代码,这个对你没有什么用!可以删除的。
 
   
  ******************************************************************************
unit untService;

interface

uses
  Windows, Forms, SysUtils, Classes, Shellapi, Registry, strUtils, WinSvc;

// 啟動關閉服務,bStart為True時,啟動,否則關閉
function _ControlService(ServiceName: string; bStart: boolean): boolean;
// 檢測服務是否存在
function ServiceExists(ServiceName: string): boolean;

implementation

function _ControlService(ServiceName: string; bStart: boolean): boolean;
var
  schSCManager, schService: SC_HANDLE;
  Argv: PChar;
  ssStatus: TServiceStatus;
begin
  Result := True;

  schSCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  if schSCManager <= 0 then
  begin
    Result := False;
    Exit;
  end;

  schService := OpenService(schSCManager, PChar(ServiceName),
    SERVICE_ALL_ACCESS);
  if schService <= 0 then
  begin
    Result := False;
    Exit;
  end;

  if bStart then
  begin
    QueryServiceStatus(schService, ssStatus);
    if ssStatus.dwCurrentState = SERVICE_RUNNING then
    begin
      Result := True;
      Exit;
    end;

    if StartService(schService, 0, Argv) then
    begin
      Sleep(1000);
      while (QueryServiceStatus(schService, ssStatus)) do
      begin
        Application.ProcessMessages;

        if ssStatus.dwCurrentState = SERVICE_START_PENDING then
          Sleep(1000)
        else
          break;
      end;

      if ssStatus.dwCurrentState <> SERVICE_RUNNING then
        Result := False;
    end
    else
      Result := False;
  end
  else
  begin
    QueryServiceStatus(schService, ssStatus);
    if ssStatus.dwCurrentState = SERVICE_STOPPED then
    begin
      Result := True;
      Exit;
    end;

    if ControlService(schService, SERVICE_CONTROL_STOP, ssStatus) then
    begin
      Sleep(1000);

      while (QueryServiceStatus(schService, ssStatus)) do
      begin
        Application.ProcessMessages;

        if ssStatus.dwCurrentState = SERVICE_STOP_PENDING then
        begin
          Sleep(1000);
        end
        else
          break;
      end;

      if ssStatus.dwCurrentState <> SERVICE_STOPPED then
        Result := False;
    end
    else
      Result := False;
  end;

  CloseServiceHandle(schService);
  CloseServiceHandle(schSCManager);
end;

function ServiceExists(ServiceName: string): boolean;
var
  schSCManager, schService: SC_HANDLE;
begin
  Result := True;

  schSCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  if schSCManager <= 0 then
  begin
    Result := False;
    Exit;
  end;

  schService := OpenService(schSCManager, PChar(ServiceName),
    SERVICE_ALL_ACCESS);
  if schService <= 0 then
  begin
    Result := False;
    Exit;
  end;

  CloseServiceHandle(schService);
  CloseServiceHandle(schSCManager);
end;

end.

  



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -