[Lazarus] Stopping a service from within a service

Jy V jyv110 at gmail.com
Thu Feb 4 18:39:21 CET 2016


On Thu, Feb 4, 2016 at 5:41 PM, Richard Mace <richard.mace at gmail.com> wrote:

> What'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-ide.org/pipermail/lazarus/attachments/20160204/758a6f1c/attachment-0003.html>


More information about the Lazarus mailing list