Solved

Canceling windows shutdown, then shutdown on my own

Posted on 2004-04-01
5
583 Views
Last Modified: 2010-04-05
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
Comment
Question by:Esopo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
shaneholmes earned 300 total points
ID: 10736932
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
 
LVL 14

Author Comment

by:Esopo
ID: 10737693
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
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10737755
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
 
LVL 14

Author Comment

by:Esopo
ID: 10738081
Thanks Shane, this is very helpful.
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10738090
Your welcome!

Shane
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses
Course of the Month11 days, left to enroll

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question