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

Asked

Viewed 364 times

2

I have a system that works with websocket in Delphi with mORMot, when I restart the PC or when I disconnect it runs Onclose and Ondestroy and through that I can remove the callback of the logged in user, but when I have hibernate or suspend I need to do the same so that the user is not listed as logged in.

It would be good also to warn the user that the system will be closed before it restarts/shutdown or suspend/hibernate and if possible I identify that the computer is coming back from suspension/hibernation to be able to register it again.

I found this example but could not understand its functioning 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;
  • Dude, I’m gonna give it a try there, but it looks like it’s messing with notifications/messages from windows itself. I usually use this type of methods in threads when I need to make visual changes. This code works for you ?

  • I don’t know what this procedure would be called. I couldn’t run it. But it would be something like this anyway. When it hibernates I remove the callback, when it comes back I log again.

  • Check this out: https://github.com/xn-nding-jua/PC_DIMMMER/blob/master/Core/PowerButton.pas

  • I’m not understanding what this procedure is calling.

  • Using the example you posted: you declare to the operating system that when there is a message of type WM_POWERBROADCAST he should call the trial Hiber of your application. This precedent should be created with the parameters required by the call. This is recorded in a system table that takes care of calling it every time such an event occurs.

  • That code I posted is not working here. I had found at this link: http://www.scriptbrasil.com.br/forum/topic/86661-hiberna%C3%A7%C3%A3o/

Show 1 more comment

1 answer

1


I managed to solve it. I was declared in another form, put it 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.

Stayed that 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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.