Link to home
Start Free TrialLog in
Avatar of quantum2
quantum2

asked on

Intercepting a WM_ShutDown

I need to know how at the system level I can intercept a WM_Shutdown (Windows Shutdown message) and absorb it so that it doesnt occur.

The application I need to create needs to be able to tie into the system message loop and intercept any shutdown message and based on a registry flag either allow the shutdown or present a dialog that informs the user of the diversion.

Thanks

Q2
Avatar of robert_marquardt
robert_marquardt

Catch the WM_QUERYENDSESSION message and give the correct answer.
The help on "WM_QUERYENDSESSION" and "message" should give enough info. The VCL source is full of message handler methods.
add a function to your Form

TForm1 = class(TForm)
private
  procedure onClose(var Msg:TMessage) message WM_Close;
  procedure onShutdown(var Msg:TMessage) message WM_Shutdown;
end;  

You can handle these messages now yourself.
Sorry wrong syntax:

Take this one:
TForm1 = class(TForm)
private
 procedure onClose(var Msg:TMessage); message WM_Close;
 procedure onShutdown(var Msg:TMessage); message WM_Shutdown;
end;  
Avatar of quantum2

ASKER

Maverick,
is this to handle the shutdown at my individual application or at the OS. Basically, if someone slects Start/Shutdown I want to be able to intercept that message before it is broadcast across all open process and divert it.

-Q2

Robert, I will look into your suggestion.
ASKER CERTIFIED SOLUTION
Avatar of maverick65
maverick65

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 good news :

You can intercept and halt shutdowns via the WM_QUERYENDSESSION message.  Look in the Windows Platform SDK for details;  basically it's a callback message.  If you don't want windows to shutdown, just return false, and windows will stop sending these messages to applications and abort the shutdown.

The bad news :

You have absolutely, 100%, for sure, no way to ensure that you're the first window that receives this message.  This means that when you get it, there's a good chance some others got it before you and already responded one way or the other.  While this won't prevent you from preventing the shutdown, other applications that got the message before yours might have already shutdown.

In other words : You can prevent the system from shutting down, but you can't prevent any application in particular from closing due to this notification.. for all you know, your application will be the only one running once this is done.
Thanks for the links and the information. I really apprecaite it.

Q2