Recent

Author Topic: Stop a Windows service  (Read 1359 times)

badmintonfan

  • New Member
  • *
  • Posts: 49
Stop a Windows service
« on: November 20, 2022, 07:39:20 am »
I try to stop a windows service with the below code,but  Services.StopService('Spooler',false) always release a error message,anyone know what wrong with it

Services := TServiceManager.Create(nil);
  try
      Services.Acces := SC_MANAGER_CONNECT;
      Services.Connect;
      Services.GetServiceStatus('Spooler', ServiceStatus);
      svc_act := (ServiceStatus.dwCurrentState = SERVICE_RUNNING);
      if svc_act then
      Services.StopService('Spooler',false);
      Services.Disconnect;
   
  finally
    Services.Free;
  end;               

paweld

  • Hero Member
  • *****
  • Posts: 1596
Re: Stop a Windows service
« Reply #1 on: November 20, 2022, 08:09:30 am »
You must run application with administrative privilages
Best regards / Pozdrawiam
paweld

Thaddy

  • Hero Member
  • *****
  • Posts: 18961
  • Glad to be alive.
Re: Stop a Windows service
« Reply #2 on: November 20, 2022, 10:49:44 am »
Did you examine the example in the standard package fcl-extra?
Also, Michael van Canneyt wrote an article about how to handle this:
https://www.freepascal.org/~michael/articles/daemons/daemons.pdf that also handles Windows services.
It may indeed be that you will need elevated rights on more modern windows and especially win64. You can do that by including an appropriately written manifest, either as a resource or as a separate file. You definitely do not have to run everything as administrator. That is good for debugging but not otherwise. Simply create a proper manifest resource.
« Last Edit: November 20, 2022, 01:25:43 pm by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

badmintonfan

  • New Member
  • *
  • Posts: 49
Re: Stop a Windows service
« Reply #3 on: November 21, 2022, 05:02:58 am »
You must run application with administrative privilages
Yes,I run it with administrator,and got the same error

badmintonfan

  • New Member
  • *
  • Posts: 49
Re: Stop a Windows service
« Reply #4 on: November 21, 2022, 05:10:06 am »
Did you examine the example in the standard package fcl-extra?
Also, Michael van Canneyt wrote an article about how to handle this:
https://www.freepascal.org/~michael/articles/daemons/daemons.pdf that also handles Windows services.
It may indeed be that you will need elevated rights on more modern windows and especially win64. You can do that by including an appropriately written manifest, either as a resource or as a separate file. You definitely do not have to run everything as administrator. That is good for debugging but not otherwise. Simply create a proper manifest resource.
Thanks,I'll study the example

dbannon

  • Hero Member
  • *****
  • Posts: 3777
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Stop a Windows service
« Reply #5 on: November 21, 2022, 08:44:44 am »
Armin wrote a quite detailed tutorial on Services and Daemons, I suspect his focus was more on Windows. Might be helpful - https://wiki.freepascal.org/Daemons_and_Services

There were some examples added to Lazarus's Examples list but probably only visible if you are using main/trunk (where the Examples system has changed).

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

superc

  • Sr. Member
  • ****
  • Posts: 251
Re: Stop a Windows service
« Reply #6 on: November 21, 2022, 10:38:05 am »
Armin wrote a quite detailed tutorial on Services and Daemons, I suspect his focus was more on Windows. Might be helpful - https://wiki.freepascal.org/Daemons_and_Services

There were some examples added to Lazarus's Examples list but probably only visible if you are using main/trunk (where the Examples system has changed).

Davo

Wow! really useful and interesting, top !!!

 

TinyPortal © 2005-2018