Forum > Windows

[Solved] Daemon application: -r/--run parameter not working on Windows


Hi all,

TDaemon executables do, according to the documentation, support three command line parameters: -install, -uninstall, -run.

It seems that -run, which is supposed to start the damon in user context,  does nothing on Windows. The parameter is, however, very useful during development, since it lets one run the current code from the Lazarus IDE and seamlessly debug most non-security related problems. Under Linux this works great, but unfortunately my main machine is Windows. Under Windows, both -r and --run will do nothing, the executable starts and terminates immedately, without error.

Is there a piece of code around showing how one may get the same behaviour for -r/--run on Windows?

Many thanks,


On Windows it is not called a Daemon, but a Service.
You can use:
net start <your service>
net stop  <your service>
net pause  <your service>
net continue  <your service>
and some more..

to control your Daemon/Service. This is also documented. Windows needs an extra class you must implement.
MvC wrote an article that explains this: that covers everything you have to implement.
The example that comes with FPC is a good starting point. it is in ./packages/fcl-extra and there is a unix and a windows version of the daemon library and a cross-platform example.
I recommend first looking at the example and then read the article.


thanks once more for this valuable information, I'll look into the article you mentioned today or tomorrow, and if I find a solution include the infos into the wiki about Daemons.

Regarding the net commands ... that's not a good solution for the debugging scenario. For debugging, you need the PID of the daemon process, which is initialized only when the daemon is started. Once you get back to Lazarus from the console and provide the PID to attach to the process, most of the daemon intialization code has long been excuted, and it has had plenty of time to loop the daemon main exec routine.

I have found ways (and included the infos in the wiki) to make the executable wait at specific points in code until Lazarus catches up and attaches the debugger, which helps, but I found it still quite clumsy, compared with what I get under Linux.

Thnx, Armin.

Think I finally resolved the issue. It is by design, though not FCL's, but Windows.

In the Windows Variant of the -run parameter results in a call to the Windows API StartServiceCtrlDispatcherA function.

According to the Windows API docs, the call has been deliberately designed by Microsoft so it refuses to execute any code which has not been loaded by the Windows Service Control Manager. If one tries, the call fails with error 1063, which is exactly what happens, but unfortunately the TCustomDaemonApplication.SysEndRunDaemons code does not raise an execption, but more or less silently call Application.Terminate.

Now I guess it would not be too hard to wrap the daemon worker code into a thread and run it myself, but it would be a piece of ugly hack code stitched around the FCL TDaemon code for just the purpose of helping me debugging on Windows. Not  much to learn, ever reuse, or contribute to the community.

For my purposes I found a much easier way: now that I know that this is by Windows design, and I don't feel like I want to work around it, I move the main development platform to Linux [for this project]. Since the daemon is running under both OS'ses anyway, I won't care which one is the leading system for development and debugging.



[0] Message Index

Go to full version