Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2016-02

Stopping a service from within a service

9 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author
to Richard Mace
Richard Mace wrote 9 lines
Hi, 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. Windows Lazarus 1.4.4

i don't know of a ""best"" way but there are several... the easiest might be to monitor a disk-based semaphore file... if it exists, the service closes as you desire...

NOTE: No off-list assistance is given without prior approval. *Please keep mailing list traffic on the list* unless private contact is specifically requested and granted.

Reply by email

Jy V
to Richard Mace: What's the best way of shutting down a service that is a service, having the same effect…
Richard Mace 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.freepascal.org/pipermail/lazarus/attachments/20160204/758a6f1c/attachment.html>

Reply by email

Jy V
to Jy V
Richard Mace wrote 8 lines
> 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.

Microsoft is using a loop waiting for the status to report as STOPPED https://msdn.microsoft.com/en-us/library/windows/desktop/ms686335%28v=vs.85%29.aspx

but since you are running the code from within your running service, it may not be considered useful to wait for yourself (as a program) to have completed stop.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20160204/ae0231cd/attachment-0001.html>

Reply by email

Richard Mace
to Unknown author: i don't know of a ""best"" way but there are several... the easiest might be to monitor a…

But what code would you out in the service to terminate itself? Effectively doing the same as calling close in a GUI app?

Richard

Show quoted text 30 lines
On 02/04/2016 11:41 AM, Richard Mace wrote: > Hi, > > 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. > > Windows Lazarus 1.4.4 > i don't know of a ""best"" way but there are several... the easiest might be to monitor a disk-based semaphore file... if it exists, the service closes as you desire... -- NOTE: No off-list assistance is given without prior approval. *Please keep mailing list traffic on the list* unless private contact is specifically requested and granted. -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20160204/712fc32a/attachment.html>

Reply by email

Richard Mace
to Jy V: Basically, I want my service to monitor a condition, which if met, it will XMLRAD stop a…
Jy V wrote 15 lines
On Thu, Feb 4, 2016 at 5:41 PM, Richard Mace 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,

?So, would I run this code by creating a TProcess from within my service, to get it to "stop" my service?

Richard?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20160204/c39b5424/attachment.html>

Reply by email

Jy V
to Richard Mace
Richard Mace wrote 6 lines
On 4 February 2016 at 17:39, Jy V wrote: XMLRAD stop a program registered as a service running following code, ?So, would I run this code by creating a TProcess from within my service, to get it to "stop" my service?

Just run the code from within your program as it is done inside any XMLRAD program.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20160204/b3288807/attachment.html>

Reply by email

Jy V
to Richard Mace: ?So, would I run this code by creating a TProcess from within my service, to get it to…
Richard Mace wrote 10 lines
On 4 February 2016 at 17:39, Jy V wrote: > On Thu, Feb 4, 2016 at 5:41 PM, Richard Mace > wrote: > XMLRAD stop a program registered as a service running following code, > ?So, would I run this code by creating a TProcess from within my service, to get it to "stop" my service?

if you have the SvcHandle available inside your program since you are the very own running service,
you can even skip OpenSCManagerA and OpenServiceA and stop the process directly
calling ControlService(SvcHandle, SERVICE_CONTROL_STOP, Status) -------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20160204/ff809fd7/attachment.html>

Reply by email

Richard Mace
to Jy V

That works great thanks :)

"Jy V" wrote 27 lines
On Thu, Feb 4, 2016 at 8:57 PM, Richard Mace wrote: > On 4 February 2016 at 17:39, Jy V wrote: > >> On Thu, Feb 4, 2016 at 5:41 PM, Richard Mace >> wrote: >> XMLRAD stop a program registered as a service running following code, >> > > ?So, would I run this code by creating a TProcess from within my service, > to get it to "stop" my service? > if you have the SvcHandle available inside your program since you are the very own running service, you can even skip OpenSCManagerA and OpenServiceA and stop the process directly calling ControlService(SvcHandle, SERVICE_CONTROL_STOP, Status) -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20160205/0fa41e61/attachment.html>

Reply by email