Re: [Lazarus] Stopping a service from within a service
That works great thanks :) On 4 Feb 2016 22:53, "Jy V" wrote: > 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 > Lazarus@lists.lazarus.freepascal.org > http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus > > -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Stopping a service from within a service
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 Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Stopping a service from within a service
On Thu, Feb 4, 2016 at 8:57 PM, Richard Mace wrote: > 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. -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Stopping a service from within a service
On 4 February 2016 at 17:39, Jy V wrote: > > > 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 -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Stopping a service from within a service
But what code would you out in the service to terminate itself? Effectively doing the same as calling close in a GUI app? Richard On 4 Feb 2016 19:11, wrote: > 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 > Lazarus@lists.lazarus.freepascal.org > http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus > -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Stopping a service from within a service
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. > > 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. -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Stopping a service from within a service
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, 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; -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] Stopping a service from within a service
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 Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
[Lazarus] Stopping a service from within a service
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 Thanks Richard -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus