Solved

Canceling windows shutdown, then shutdown on my own

Posted on 2004-04-01
5
578 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…

752 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