Recent

Author Topic: Services in Windows 10  (Read 5645 times)

jsonnabend

  • New Member
  • *
  • Posts: 15
Services in Windows 10
« on: November 26, 2015, 05:37:57 pm »
I'm trying to get a service installed and running in Windows 10.  I found this thread:

http://forum.lazarus.freepascal.org/index.php?topic=28357.0

and looked at some sample code from this thread:

http://forum.lazarus.freepascal.org/index.php?topic=20274.0

but I can't get even simple test code to run as a service.  When I try to start the service, I get a message from Windows that the service did not respond to the start request.

Does anyone have some sample code of a service that works on Windows 10?

TIA

balazsszekely

  • Guest
Re: Services in Windows 10
« Reply #1 on: November 26, 2015, 07:11:01 pm »
This should work, just add mode delphi and windows to the uses clauses:
http://stackoverflow.com/a/5562436

jsonnabend

  • New Member
  • *
  • Posts: 15
Re: Services in Windows 10
« Reply #2 on: November 27, 2015, 01:18:46 pm »
No, no luck with that code.  I substituted JWAWinSvc for WinSvc and changed one argument type error, but no joy.  The service installed but would not start.

balazsszekely

  • Guest
Re: Services in Windows 10
« Reply #3 on: November 27, 2015, 04:21:46 pm »
Quote
No, no luck with that code.  I substituted JWAWinSvc for WinSvc and changed one argument type error, but no joy.  The service installed but would not start.
Here you go(attachment).

jsonnabend

  • New Member
  • *
  • Posts: 15
Re: Services in Windows 10
« Reply #4 on: November 27, 2015, 08:46:37 pm »
Thanks for the upload.  I will take a look today.  I also found the "ClearnDirs" example code (can't believe I missed it earlier), and that simple service compiles, installs and runs, too.

jsonnabend

  • New Member
  • *
  • Posts: 15
Re: Services in Windows 10
« Reply #5 on: November 27, 2015, 11:51:32 pm »
GetMem, your code compiles, installs and starts.  I can't figure out what routines execute when the service starts and stops.

balazsszekely

  • Guest
Re: Services in Windows 10
« Reply #6 on: November 28, 2015, 06:50:34 am »
Quote
@jsonnabend
GetMem, your code compiles, installs and starts.  I can't figure out what routines execute when the service starts and stops.

Code: Pascal  [Select][+][-]
  1.   //..
  2.   if StatusHandle <> 0 then
  3.   begin
  4.     ReportSvcStatus(SERVICE_RUNNING, NO_ERROR, 0);
  5.     try
  6.       Stopped := False;
  7.       Paused  := False;
  8.  
  9.       //service successfully started, create a thread here
  10.  
  11.       MainProc;
  12.     finally
  13.       ReportSvcStatus(SERVICE_STOPPED, NO_ERROR, 0);
  14.     end;
  15.   end;
  16.   //...
  17.  

The thead execute event should look like this or something similar:
Code: Pascal  [Select][+][-]
  1. procedure TServiceThread.Execute;
  2. begin
  3.   while not Stopped do
  4.   begin
  5.     if not Paused do
  6.     begin
  7.       //your code here
  8.     end;
  9.     Sleep(10);
  10.   end;
  11. end;

Replace SERVICE_STOP_PENDING with SERVICE_STOP inside ServiceCtrlHandler so the service can stop properly. Actually you can use SERVICE_STOP_PENDING if you have a lengthy process, but don't forget to call SERVICE_STOP after.

 

TinyPortal © 2005-2018