Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing Windows shut down

To detect and prevent shutdown the computer I use very simple program. It has only one form and one private procedure like below:

TForm3 = class(TForm)
private
  procedure WMQueryEndSession(var Msg : TWMQueryEndSession) ;
         message WM_QueryEndSession;
end;

and the implementation

procedure TForm3.WMQueryEndSession(var Msg: TWMQueryEndSession);
begin
  Msg.Result := 0; //so I don't want to shutdown while my program is running
end;

I compiled it Delphi 5 and Delphi 2010. Both of them detect shutdown. But when I compiled in Delphi 2010; after preventing shutdown my program closes. (PC doesn't shutdown)

How do I get the same result from both of them?

like image 200
SimaWB Avatar asked Jun 21 '10 13:06

SimaWB


People also ask

How do I stop Windows 10 from randomly shutting down?

The best way to avoid this random Windows 10 shutdown is to disable Sleep Mode. Head to the Power and sleep settings. Under Sleep, click on the dropdown button for On battery power, PC goes to sleep after and change it to Never. Do the same for the When plugged in, PC goes to sleep after dropdown.

How do I stop Windows 10 from automatically shutting down every hour?

From the Start menu, open the Run dialog box or you can Press the "Window + R" key to open the RUN window. Type "shutdown -a" and click on the "OK" button. After clicking on the OK button or pressing the enter key, the auto-shutdown schedule or task will be canceled automatically.


2 Answers

EDIT: changed to intercept WM_ENDSESSION instead of WM_QUERYENDSESSION.

As you cannot directly change the behaviour of TApplication, you can install a TApplication message hook instead that neutralizes the WM_ENDSESSION message.

Installing such a hook is quite simple, you only have to add a method similar to the following to your mainform and register the hook in FormCreate.

function TForm25.HookEndSession(var Message: TMessage): Boolean;
begin
  result := false;
  if Message.Msg = WM_ENDSESSION then begin
    Message.Result := 0;
    result := true;
  end;
end;

procedure TForm25.FormCreate(Sender: TObject);
begin
  Application.HookMainWindow(HookEndSession);
end;
like image 83
Uwe Raabe Avatar answered Oct 01 '22 12:10

Uwe Raabe


I usually run "shutdown -a" command. You can do the same from your code to interrupt Windows from shutdown.

Regards

like image 25
opal Avatar answered Oct 01 '22 12:10

opal