How do I know when the computer will shut down / restart / hibernate / suspend in Delphi?

2

I have a system that works with websocket in Delphi with mORMot, when I reboot the PC or when I disconnect it it executes OnClose and OnDestroy and through that I can remove the callback of the user that was logged in, but when I send hibernate or suspend I need to do the same so the user does not record as logged in.

It would also be good to warn the user that the system will be closed before it restarts / hangs up or suspend / hibernate and if possible I identify that the computer is returning from sleep / hibernation in order to register it again.

I found this example but could not understand its operation and how it is called:

Statement:

private
  procedure Hiber(var pMsg: TWMPower); message WM_POWERBROADCAST;

Implementation:

procedure Tform.Hiber(var pMsg: TWMPower);
begin
   if (pMsg.PowerEvt = PBT_APMQUERYSUSPEND) then
   begin
     // Hibernando
   end
   else if (pMsg.PowerEvt = PBT_APMRESUMESUSPEND) then
   begin
     // Retornando
   end;
   pMsg.result := 1;
end;
    
asked by anonymous 17.08.2016 / 14:38

1 answer

1

I managed to resolve. I was declaring in another form, put it in the main form and it worked. But I had to make some changes because PBT_APMQUERYSUSPEND is no longer supported from within Windows Vista.

It looks like this:

Statement:

procedure Hiber(var pMessage: TMessage); message WM_POWERBROADCAST;

Deployment:

procedure TFrm.Hiber(var pMessage: TMessage);
begin
   if pMessage.Msg = WM_POWERBROADCAST then
   begin
      if (pMessage.WParam = PBT_APMQUERYSTANDBY) or
         (pMessage.WParam = PBT_APMSUSPEND) then
      begin
         // Hibernando
         pMessage.Result := 1;
      end
      else if (pMessage.WParam = PBT_APMRESUMECRITICAL) or
         (pMessage.WParam = PBT_APMRESUMESUSPEND) or
         (pMessage.WParam = PBT_APMRESUMESTANDBY) then
      begin
         // Voltando
      end;
   end;
end;
    
17.08.2016 / 23:04