05 Apr
Posted by: Christian Wimmer in: JEDI Windows API Headers
Unfortunately Borland didn’t implement a public register and start function into the class TServiceApplication. So we have to do it ourselves.
First we publish the protected RegisterServices function to allow (un-)install our service(s) programmatically.
{ TServiceApplicationEx }
procedure TServiceApplicationEx.RegisterServices(Install, Silent: Boolean);
begin
inherited;
end;
Of course we need a tool function that starts our service
try
hSvc := OpenService(hSCM, PChar(Service.Name), SERVICE_START);
if hSvc = 0 then
RaiseLastOSError; try
SetLength(args, ParamCount);
for i := 0 to ParamCount-1 do
begin
GetMem(args[i], Length(ParamStr(i+1))+2);
StringCchCopy(args[i], Length(ParamStr(i+1))+2, PChar(ParamStr(i+1)));
end;
try
if not StartServiceA(hSvc, Length(args), @args[0]) then
RaiseLastOSError;
finally
for i := 0 to ParamCount-1 do
begin
FreeMem(args[i]);
end;
end;
finally
CloseServiceHandle(hSvc);
end;
finally
CloseServiceHandle(hScm);
end;
end;
Finally we can adjust the main application to install, uninstall or run the service.
Leave a reply