dolmen has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I have a POE-based program that I would like to turn into a Windows service.

POE::Component::Daemon::Win32 exists, however it is based on Win32::Daemon. And Win32::Daemon seems to be a dead-end as I'm facing the following problems:

I tried to use SrvAny (Microsoft tool from the Windows 2003 Server Resource Kit) but I've not yet been able to catch the service stop event to cleanly exit, saving the service state. No SIGINT, no SIGTERM, the process is simply killed.

Do you kown how to catch this?

  • Comment on SrvAny based Win32 service: how to catch termination?

Replies are listed 'Best First'.
Re: SrvAny based Win32 service: how to catch termination?
by cdarke (Prior) on Sep 11, 2009 at 13:58 UTC
    Service "events" (Service Control Requests) are sent from the Service Control Manager to the main thread of the service (through a named pipe), where a control handler should be registered. This control handler function is executed on receipt of the Service Control Request. The application itself runs in a separate thread (known as "service main"). I honestly can't see how SrvAny could provide that functionality to a single-threaded non-service app. It probably just provides scafolding. There is no way it would use signals anyhow - they are UNIX architecture and poorly implemented on Windows (to conform to ISO C standards, no more).

    From the SrvAny documentation:

    "WARNING: When the service is stopped, it terminates the application via the WIN32 TerminateProcess() API: this is a drastic way to end an application. For example, it would not allow the application to prompt the user to save changes. Therefore, it is recommended to close the application BEFORE stopping the service."

    Which kinda confirms my guesses above.

    Update: I came across a 5.10 version of Win32::Daemon. Can't say I have tried it, but it might help. See the link and explaination here

      I'm aware of this 5.10 release (as I said in my question). But it is only a binary only release.

      I do not want to rely on a Perl module for which I do not have the source.

      And about SrvAny, you killed the little hope I had I missed something. :(