Solved

Canceling windows shutdown, then shutdown on my own

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now