Solved

Canceling windows shutdown, then shutdown on my own

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

735 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