Link to home
Start Free TrialLog in
Avatar of SexyFast
SexyFast

asked on

Detecting Windows Shutdown

I'm writting a windows app, that has no UI, no windows, is not a service, and is not a console application.
How can I detect a windows shutdown so my application can perform a clean exit?
Example code is useful too.
Thanks in advance.
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

These are the only ways I know of.


WM_QUERYENDSESSION
 
The WM_QUERYENDSESSION message is sent when the user chooses to end  the session or when an application calls the ExitWindows function. If any application  returns zero, the session is not ended. The system stops sending  WM_QUERYENDSESSION messages as soon as one application returns zero.


and

WM_ENDSESSION
 
The WM_ENDSESSION message is sent to an application after the system  processes the results of the WM_QUERYENDSESSION message. The  WM_ENDSESSION message informs the application whether the session is  ending.
Avatar of js-profi
js-profi

WM_QUERYENDSESSION is only sent to apps which have a windows message loop. You need at least one (pseudo) window for that.
Why not make it a service? Then, SCM will call you by callback in case of a shutdown.
Avatar of SexyFast

ASKER

Any example code for having a message loop?
ASKER CERTIFIED SOLUTION
Avatar of js-profi
js-profi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
-----
the thread which runs that loop will be blocked after that
-----

ignore that. PeekMessage only peeks as long as there are messages. you need a handling after loop so that the message loop was called again until you want to stop.