Delphi啟動/停止Windows服務,啟動類型修改為”自動”

來源:互聯網
上載者:User
unit U_StartServices;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, WinSVC, StdCtrls;

type
  TForm1 = class(TForm)
    btn_StartServices: TButton;
    btn_StopServices: TButton;

    procedure btn_StartServicesClick(Sender: TObject);
    procedure btn_StopServicesClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
function StartServices(const SvrName: string): Boolean;
implementation

{$R *.dfm}

//開啟服務

function StartServices(const SvrName: string): Boolean;
var
  SCH, SvcSCH: SC_HANDLE;
  arg: PChar;
  dwStartType: DWORD;
begin
  Result := False;
  SCH := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  if SCH <= 0 then Exit;
  SvcSCH := OpenService(SCH, PChar(SvrName), SERVICE_ALL_ACCESS);

  if (ChangeServiceConfig(
    SvcSCH, //   handle   of   service
    SERVICE_NO_CHANGE, //SERVICE_NO_CHANGE,   //   service   type:   no   change
    SERVICE_AUTO_START, //   change   service   start   type
    SERVICE_NO_CHANGE, //   error   control:   no   change
    nil, //   binary   path:   no   change
    nil, //   load   order   group:   no   change
    nil, //   tag   ID:   no   change
    nil, //   dependencies:   no   change
    nil, //   account   name:   no   change
    nil, //   password:   no   change
    nil)) then
    showmessage('Auto Start OK')
  else
    showmessage('Auto Start Error');

    if SvcSCH <= 0 then Exit;
  try
    Result := StartService(SvcSCH, 0, arg);
    CloseServiceHandle(SvcSCH);
    CloseServiceHandle(SCH);
  except
    CloseServiceHandle(SvcSCH);
    CloseServiceHandle(SCH);
    Exit;
  end;

end;

//停止服務

function StopServices(const SvrName: string): Boolean;
var
  SCH, SvcSCH: SC_HANDLE;
  SS: TServiceStatus;
begin
  Result := False;
  SCH := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  if SCH <= 0 then Exit;
  SvcSCH := OpenService(SCH, PChar(SvrName), SERVICE_ALL_ACCESS);
  if SvcSCH <= 0 then Exit;
  try
    Result := ControlService(SvcSCH, SERVICE_CONTROL_STOP, SS);
    CloseServiceHandle(SCH);
    CloseServiceHandle(SvcSCH);
  except
    CloseServiceHandle(SCH);
    CloseServiceHandle(SvcSCH);
    Exit;
  end;
end;

procedure TForm1.btn_StartServicesClick(Sender: TObject);
begin
  if StartServices('PolicyAgent') = true then
    application.MessageBox(PChar('PolicyAgent 服務啟動成功'), PChar('服務資訊'), MB_ICONINFORMATION)
  else
    application.MessageBox(PChar('PolicyAgent 服務啟動失敗'), PChar('服務資訊'), MB_ICONERROR);
end;

procedure TForm1.btn_StopServicesClick(Sender: TObject);
begin
  if StopServices('PolicyAgent') = true then
    application.MessageBox(PChar('PolicyAgent 服務停止成功'), PChar('服務資訊'), MB_ICONINFORMATION)
  else
    application.MessageBox(PChar('PolicyAgent 服務停止成功'), PChar('服務資訊'), MB_ICONERROR);
end;

end.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.