如何通过Delphi禁用服务?

Ede*_*tto 15 delphi service

我使用的例程可以通过Delphi启动和停止服务,但我也需要能够禁用它们,是否可能?

Rob*_*edy 19

打开的服务OpenService,然后通过将禁用它Service_Disabled作为dwStartType为参数ChangeServiceConfig.指定空指针或Service_No_Change其余参数,因为您不想更改它们.


Chr*_*mer 8

您可以使用JEDI组件库(JCL)中的文件JclSvcCtrl.pas.我写了一个你可以使用的伪示例.但是,请注意我没有测试它.但以这种方式它应该工作(省略错误检查):

M := TJclSCManager.Create;
M.Refresh(true);  //Not sure if true is needed or not (refresh all services)
For i := 0 to M.ServiceCount -1 do
begin
  S := M.Services[i]; //TJclNtService
  if CompareText(S.ServiceName, 'bla') then
  begin
    S.Stop;
    S.StartType := sstDisabled;   
    S.Commit;
    break;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

  • 当然你也可以使用M.FindService.它完全一样! (2认同)

Ger*_*lí- 7

除了使用以前的方法,如果您需要更多控制,您可以使用WMI.
使用Win32_Service类可以访问机器上安装的所有服务信息,您可以访问方法:启动,停止,暂停,恢复,查询,创建,删除,更改,更改创建模式...

在这里(Web/SourceForge),您可以找到一组与WMI 一起使用的组件(GLibWMI组件库); 有一个名为CServiceInfo thah为您提供此类的所有信息和一些方法.

除了包装tere是一些演示; 一个叫做(ServiceControl)并实现所有方法.

替代文字http://img341.imageshack.us/img341/8505/imagen336.png

所有包都包含来源.查看它对您有用的代码.

问候.


Sim*_*aWB 5

ShellExecute(0, nil, 'cmd.exe', 'sc config "the service name" start=disabled', nil, SW_HIDE);
ShellExecute(0, nil, 'cmd.exe', 'sc config "the service name" start=auto', nil, SW_HIDE);
ShellExecute(0, nil, 'cmd.exe', 'sc config "the service name" start=demand', nil, SW_HIDE);
Run Code Online (Sandbox Code Playgroud)

  • 这是穷人的服务管理.sc.exe旨在由命令行或脚本使用 - 并且在旧版Windows上不可用 - 应用程序应使用SCM API.如果您真的想使用SC,则无需调用shell.直接运行sc.exe. (12认同)