Mirror

How to detect if you can run NT services (Views: 19)


Problem/Question/Abstract:

How can I detect if I can install and run a service on the current operating system? I was just going to detect the operating system, but I figured it might be better to directly detect the service control - but not sure how to do that.

Answer:

After installing a service (using the -install command line parameter) you can use the following code to start the service:

function StartService(Name: string): boolean;
var
  Scm: SC_HANDLE;
  Service: SC_HANDLE;
  p: pChar;
begin
  Result := false;
  Scm := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  if Scm <> 0 then
  begin
    Service := OpenService(Scm, pChar(Name), SC_MANAGER_ALL_ACCESS);
    if Service <> 0 then
    begin
      p := nil;
      if WinSvc.StartService(Service, 0, p) then
        Result := True;
      CloseServiceHandle(Service);
    end;
    CloseServiceHandle(Scm);
  end;
end;

<< Back to main page