Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 594
  • Last Modified:

Canceling windows shutdown, then shutdown on my own

Greetings experts,

I'm interested in developing a tool that will know when a shutdown was called in Windows, cancelling it, display my own shutdows form (saying something like 'shutting down, a moment please...'), perform a task and then shutdown again.

So I basically want a way to recognize the shutting down and stop it, then a way to shutdown on my own, within my delphi application.

Thanks,

Esopo.
0
Esopo
Asked:
Esopo
  • 3
  • 2
1 Solution
 
shaneholmesCommented:
You need to trap for the  WM_QueryEndSession message....

See example below

Shane

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
    procedure WMQueryEndSession(var Msg : TWMQueryEndSession); message WM_QueryEndSession;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
procedure TForm1.WMQueryEndSession(var Msg : TWMQueryEndSession);
begin
 Msg.Result:= 0; // do not close;  
 //Show your form here, when ready to actually close, then Msg.Result:= 1;
end;


0
 
EsopoAuthor Commented:
Shane,

I tried your code and had a little problem. I inserted a 10 sec wait before Msg.Result:=1; and it worked fine except the Win2000 (my os) dialog for closing hung applications appeared and started counting down, until my app finally waited the 10 secs and allowed shutting down. This is my code:


procedure TForm1.WMQueryEndSession(var Msg : TWMQueryEndSession);
var
J:integer;

begin
 Msg.Result:= 0; // do not close;

showmessage('Wait 10 secs');
For J:=1 to 100 do
begin
 sleep(100);
end;
Msg.Result:=1;

end;


I realized if I leave it at Msg.Result:= 0; and end the procedure then Windows will cancel the shutdown, is this right?

So, if this is true then I could cancel the shutdown and then call a timer that will do whatever I want and then shut Windows on my own with some other code snippet.

So the idea is to cancel the shutdown alltogether and then close windows with another procedure, any ideas?

Also, how can I tell how is the user trying to terminate the session, maybe the user wants to shutdown or restart or just close the session, is there a way to know?

Thanks,

Esopo
0
 
shaneholmesCommented:
Hi Esopo, yes, that is correct,

Msg.Result:= 0 tells it to shut down.

Msg.Result:= 1 tells it NOT to shut down.

also, you dont need a loop

showmessage('Wait 10 secs');
sleep(10000); //ten seconds (1000 = 1 sec)



I'm not sure if you can distinguish between a shutdown and a restart. You certainly can distinguish between a logout/exitwindows(), and shutdown/restart .

you will need to test the Msg.LParam EWX_LOGOFF
           
or
 EWX_SHUTDOWN (EW_RestartWindows or EW_RebootSystem)


to reboot

exitwindowsex(EWX_REBOOT,0);

to shutdown

exitwindowsex(EWX_SHUTDOWN,0); (or with ewx_poweroff)

to logOff

exitWindowsEx(EWX_LOGOFF + EWX_FORCE, 0);
0
 
EsopoAuthor Commented:
Thanks Shane, this is very helpful.
0
 
shaneholmesCommented:
Your welcome!

Shane
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now