daemon app in win32

Ben Hutchings ben.hutchings at roundpoint.com
Wed Mar 28 01:59:32 CEST 2001

whisper at oz.net (Dave LeBlanc) writes:

> On Mon, 26 Mar 2001 19:02:27 +0200, Michael Bauer <Mike_B at T-Online.de>
> wrote:
> >Steve Purcell wrote:
> >
> >> Michael Bauer wrote:
> >>> i am writing a daemon application that runs both under windows and unix.
> >>> Since i am not very experienced in win32 - programming, i really would
> >>> appreciate some hints how to register my app in win32 that i can do some
> >>> cleanup-functions when windows is shut down.
> >>> 
> >>> At the moment it is just a standard console application that simply gets
> >>> killed when windows shuts down. The app does not have/need a GUI, it
> >>> should just run in the background.
> >> 
> >> 
> >> If you're using NT, take a look at the win32service API. I think it
> >> provides hooks like SvcStop that Windows will call for you (if you ask it
> >> nicely).
> >
> >Hmm, seems like this is only available under NT. Is there a more general 
> >way to accomplish this task independently of the underlying Windows-version?
> >
> >But maybe i should just get a life and write different wrappers for Win95, 
> >Win98, WinME, Win200, WinNT... :-(

You should only need two.  The Apache web server code for Windows
might be instructive.

> >still hoping... Mike

> AFAIK, Every Windows app gets sent a WM message when it's about to be
> terminated.

If it has a GUI, then its top-level window will be sent WM_CLOSE.

Console applications can register to receive such notifications in the
same way they receive control-C notifications under *some* versions of

> Most apps just let this fall through to the default
> message processor (which you have to do anyway) without doiing
> anything to clean up. You can add a message handler for this such that
> the app can gracefully (make out it's will?) prepare to end it all.

The DefWindowProc (default message handler) responds to WM_CLOSE by
calling DestroyWindow() (which runs synchronously).  The message
handler for a top-level window should respond to this by calling
PostQuitMessage(), which posts a WM_QUIT message to the calling
thread's message queue.  (The message handler for a dialog should do
something else again.)  Application frameworks like MFC should do
this stuff for you.

> Just make sure that in the end, it passes the message on to the
> default message handler so that windows can do it's own cleanup of
> your app.

Actually, what's supposed to happen is that your main message loop
calls GetMessage(), which finds a WM_QUIT message and returns 0; the
loop then exits and your cleanup code runs on the way out.

Any opinions expressed are my own and not necessarily those of Roundpoint.

More information about the Python-list mailing list