且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在运行时或OnAfterInstall上启动Windows服务

更新时间:2023-12-06 15:36:34

AfterInstall 事件中,有一个示例 StartService 调用:

Here's a sample StartService call, in AfterInstall event:

procedure TServiceMainController.ServiceAfterInstall(Sender: TService);
var
  Manager, Service: SC_HANDLE;
begin
  Manager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  Win32Check(Manager <> 0);
  try
    Service := OpenService(Manager, PChar(Name), SERVICE_ALL_ACCESS);
    Win32Check(Service <> 0);
    try
      Win32Check(StartService(Service, 0, PChar(nil^)));
    finally
      CloseServiceHandle(Service);
    end;
  finally
    CloseServiceHandle(Manager);
  end;
end;

但是我不确定它是否对您有用,因为通常情况下,您应该成功使用 net start 也是如此。

However I'm not sure it will work for you, as normally you should have success with net start too.