Author: Jonas Bilinkevicius
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:
1 2 function StartService(Name: string): boolean;
3 var4 Scm: SC_HANDLE;
5 Service: SC_HANDLE;
6 p: pChar;
7 begin8 Result := false;
9 Scm := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
10 if Scm <> 0 then11 begin12 Service := OpenService(Scm, pChar(Name), SC_MANAGER_ALL_ACCESS);
13 if Service <> 0 then14 begin15 p := nil;
16 if WinSvc.StartService(Service, 0, p) then17 Result := True;
18 CloseServiceHandle(Service);
19 end;
20 CloseServiceHandle(Scm);
21 end;
22 end;