Richard Mace wroteWhat's the best way of shutting down a service that is a service, having
the same effect as the user clicking stop.
Basically, I want my service to monitor a condition, which if met, it will
stop itself.
XMLRAD stop a program registered as a service running following code,
var
SCMHandle, SvcHandle: THandle;
Status: TServiceStatus;
begin
Result := False;
SCMHandle := 0;
try
SCMHandle := OpenSCManagerA(nil, nil, SC_MANAGER_CREATE_SERVICE);
if SCMHandle = 0 then
begin
SystemLogLastError('OpenSCManager');
Exit;
end;
SvcHandle := OpenServiceA(SCMHandle, PChar('MyService'),
SERVICE_ALL_ACCESS);
if SvcHandle = 0 then
begin
SystemLogLastError('OpenService');
Exit;
end;
if not ControlService(SvcHandle, SERVICE_CONTROL_STOP, Status) then
begin
SystemLog(tlError, 'Error to ControlService STOP');
Exit;
end;
CloseServiceHandle(SvcHandle);
finally
if SCMHandle 0 then
CloseServiceHandle(SCMHandle);
end;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20160204/758a6f1c/attachment.html>