打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
DELPHI启动或停止服务
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
schService:SC_HANDLE;
schSCManager:SC_HANDLE;
ssStatus:TServiceStatus;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  schSCManager:=OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);
  schService:=OpenService(schSCManager,'alerter',SERVICE_ALL_ACCESS);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  CloseServiceHandle(schService);
  CloseServiceHandle(schSCManager);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
Argv:PChar;
begin
  if StartService(schService,0,Argv) then
  begin
    form1.caption:='Start Service Now ';
    Sleep(1000);
    while (QueryServiceStatus(schService,ssStatus)) do
    begin
      Application.ProcessMessages;
      if ssStatus.dwCurrentState=SERVICE_START_PENDING then
      begin
        form1.caption:=form1.caption+('.');
        Sleep(1000);
      end else break;
    end;
    if ssStatus.dwCurrentState=SERVICE_RUNNING then
    form1.caption:='Service Start Ok'
    else form1.caption:='Service Start Fail';
  end
  else form1.caption:='Service Start Fail';
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if ControlService(schService,SERVICE_CONTROL_STOP,ssStatus) then
  begin
    form1.caption:='Stopping Service Now ';
    Sleep(1000);
    while (QueryServiceStatus(schService,ssStatus)) do
    begin
      Application.ProcessMessages;
      if ssStatus.dwCurrentState=SERVICE_STOP_PENDING then
      begin
        form1.caption:=form1.caption+('.');
        Sleep(1000);
      end else break;
    end;
    if ssStatus.dwCurrentState=SERVICE_STOPPED then
    form1.caption:='Service Stop Ok'
    else form1.caption:='Service Stop Fail';
  end
  else form1.caption:='Service Stop Fail';
end;
end.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Delphi 中使长循环有响应
delphi实现js效果的滚动
DELPHI获得系统当前时间日期和格式化时间
在线程中如何接收通过PostThreadMessage()发送的消息? - 程序开发常见问...
ClientDataSet的隐含功能-4
Delphi 2010 新增功能之: 软键盘、触摸键盘(TTouchKeyboard)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服