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

I have a system that works with websocket in Delphi with mORMot, when I restart the PC or when I turn off it executes OnClose and OnDestroy and through this I can remove the callback of the user who was logged in, but when I command hibernate or suspend I need to do the same so that the user does not appear as logged in.

It would be nice also I warn the user that the system will be closed before he restart / shut down or suspend/hibernate and if possible I identify that the computer is coming back from sleep/hibernation to be able 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;
Author: Wendel Rodrigues, 2016-08-17

1 answers

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

Got This Way:

statement:

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

implementation:

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;
 1
Author: Wendel Rodrigues, 2016-08-17 21:04:02