• Delphi启动/停止Windows服务,启动类型修改为"自动"


    1. unit U_StartServices;
    2. interface
    3. uses
    4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    5.   Dialogs, WinSVC, StdCtrls;
    6. type
    7.   TForm1 = class(TForm)
    8.     btn_StartServices: TButton;
    9.     btn_StopServices: TButton;
    10.     procedure btn_StartServicesClick(Sender: TObject);
    11.     procedure btn_StopServicesClick(Sender: TObject);
    12.   private
    13.     { Private declarations }
    14.   public
    15.     { Public declarations }
    16.   end;
    17. var
    18.   Form1: TForm1;
    19. function StartServices(const SvrName: string): Boolean;
    20. implementation
    21. {$R *.dfm}
    22. //开启服务
    23. function StartServices(const SvrName: string): Boolean;
    24. var
    25.   SCH, SvcSCH: SC_HANDLE;
    26.   arg: PChar;
    27.   dwStartType: DWORD;
    28. begin
    29.   Result := False;
    30.   SCH := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
    31.   if SCH <= 0 then Exit;
    32.   SvcSCH := OpenService(SCH, PChar(SvrName), SERVICE_ALL_ACCESS);
    33.   if (ChangeServiceConfig(
    34.     SvcSCH, //   handle   of   service
    35.     SERVICE_NO_CHANGE, //SERVICE_NO_CHANGE,   //   service   type:   no   change
    36.     SERVICE_AUTO_START, //   change   service   start   type
    37.     SERVICE_NO_CHANGE, //   error   control:   no   change
    38.     nil, //   binary   path:   no   change
    39.     nil, //   load   order   group:   no   change
    40.     nil, //   tag   ID:   no   change
    41.     nil, //   dependencies:   no   change
    42.     nil, //   account   name:   no   change
    43.     nil, //   password:   no   change
    44.     nil)) then
    45.     showmessage('Auto Start OK')
    46.   else
    47.     showmessage('Auto Start Error');
    48.     if SvcSCH <= 0 then Exit;
    49.   try
    50.     Result := StartService(SvcSCH, 0, arg);
    51.     CloseServiceHandle(SvcSCH);
    52.     CloseServiceHandle(SCH);
    53.   except
    54.     CloseServiceHandle(SvcSCH);
    55.     CloseServiceHandle(SCH);
    56.     Exit;
    57.   end;
    58. end;
    59. //停止服务
    60. function StopServices(const SvrName: string): Boolean;
    61. var
    62.   SCH, SvcSCH: SC_HANDLE;
    63.   SS: TServiceStatus;
    64. begin
    65.   Result := False;
    66.   SCH := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
    67.   if SCH <= 0 then Exit;
    68.   SvcSCH := OpenService(SCH, PChar(SvrName), SERVICE_ALL_ACCESS);
    69.   if SvcSCH <= 0 then Exit;
    70.   try
    71.     Result := ControlService(SvcSCH, SERVICE_CONTROL_STOP, SS);
    72.     CloseServiceHandle(SCH);
    73.     CloseServiceHandle(SvcSCH);
    74.   except
    75.     CloseServiceHandle(SCH);
    76.     CloseServiceHandle(SvcSCH);
    77.     Exit;
    78.   end;
    79. end;
    80. procedure TForm1.btn_StartServicesClick(Sender: TObject);
    81. begin
    82.   if StartServices('PolicyAgent') = true then
    83.     application.MessageBox(PChar('PolicyAgent 服务启动成功'), PChar('服务信息'), MB_ICONINFORMATION)
    84.   else
    85.     application.MessageBox(PChar('PolicyAgent 服务启动失败'), PChar('服务信息'), MB_ICONERROR);
    86. end;
    87. procedure TForm1.btn_StopServicesClick(Sender: TObject);
    88. begin
    89.   if StopServices('PolicyAgent') = true then
    90.     application.MessageBox(PChar('PolicyAgent 服务停止成功'), PChar('服务信息'), MB_ICONINFORMATION)
    91.   else
    92.     application.MessageBox(PChar('PolicyAgent 服务停止成功'), PChar('服务信息'), MB_ICONERROR);
    93. end;
    94. end.
    复制代码

    http://www.appweixin.net/thread-67-1-1.html

  • 相关阅读:
    重学微积分
    重学微积分
    重学微积分
    python打包成exe过程中遇到的问题
    日常笔记-VS
    Python项目案例开发从入门到实战-1.5Python文件的使用
    Python项目案例开发从入门到实战-1.4Python图形界面设计
    Python项目案例开发从入门到实战-1.3 Python面向对象设计
    Python项目案例开发从入门到实战-1.2 Python语法基础
    剑指offer题目汇总
  • 原文地址:https://www.cnblogs.com/findumars/p/5551157.html
Copyright © 2020-2023  润新知