c0d3r 17 Posted April 20, 2023 (edited) HI, All I'm getting a strange issue, the following codes were trying logout users when Windows is going to sleep mode. For some reason, it works for some laptops, but it doesn't work for others: procedure WMPowerBroadCast(var Msg: TMessage); message WM_POWERBROADCAST; procedure TMainForm.WMPowerBroadCast(var Msg: TMessage); begin if (Msg.wParam = PBT_APMSUSPEND) or (Msg.wParam = PBT_APMSTANDBY) then begin try Signout; except end; Msg.Result := 1; end; end; Got the report from my client, the signout routine didn't get called when some laptops went to sleep (leave them opened and auto entered Sleep mode or Hitting Power button to force sleeping), but other laptops/PCs were working just fine. Edited April 20, 2023 by c0d3r Share this post Link to post
programmerdelphi2k 237 Posted April 20, 2023 if many works, and 1 dont, my bet is: verify the setup BIOS, verify if same Windows edition or setup, etc... Share this post Link to post
Angus Robertson 574 Posted April 20, 2023 Check the WM_POWER message as well. Angus Share this post Link to post
programmerdelphi2k 237 Posted April 20, 2023 my tests if SHUTDOWN type TForm1 = class(TForm) Memo1: TMemo; BtnDisplayTurnOFF: TButton; procedure BtnDisplayTurnOFFClick(Sender: TObject); private { Private declarations } public // WM_QUERYENDSESSION when MSWindows start the "shutdown" or "restart"... procedure MyWMPowerBroadcastMessage(var AMessage: TMessage); message WM_POWERBROADCAST; end; var Form1: TForm1; implementation {$R *.dfm} const MY_MONITOR_OFF = 2; { TForm1 } procedure TForm1.BtnDisplayTurnOFFClick(Sender: TObject); begin SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, MY_MONITOR_OFF); end; procedure TForm1.MyWMPowerBroadcastMessage(var AMessage: TMessage); begin case AMessage.WParam of PBT_APMQUERYSUSPEND: Memo1.Lines.Add('pc was PBT_APMQUERYSUSPEND at ' + DateTimeToStr(now)); PBT_APMQUERYSTANDBY: Memo1.Lines.Add('pc was PBT_APMQUERYSTANDBY at ' + DateTimeToStr(now)); PBT_APMQUERYSUSPENDFAILED: Memo1.Lines.Add('pc was PBT_APMQUERYSUSPENDFAILED at ' + DateTimeToStr(now)); PBT_APMQUERYSTANDBYFAILED: Memo1.Lines.Add('pc was PBT_APMQUERYSTANDBYFAILED at ' + DateTimeToStr(now)); PBT_APMSUSPEND: Memo1.Lines.Add('pc was PBT_APMSUSPEND at ' + DateTimeToStr(now)); PBT_APMSTANDBY: Memo1.Lines.Add('pc was PBT_APMSTANDBY at ' + DateTimeToStr(now)); PBT_APMRESUMECRITICAL: Memo1.Lines.Add('pc was PBT_APMRESUMECRITICAL at ' + DateTimeToStr(now)); PBT_APMRESUMESUSPEND: Memo1.Lines.Add('pc was PBT_APMRESUMESUSPEND at ' + DateTimeToStr(now)); PBT_APMRESUMESTANDBY: Memo1.Lines.Add('pc was PBT_APMRESUMESTANDBY at ' + DateTimeToStr(now)); // PBTF_APMRESUMEFROMFAILURE: = PBT_APMQUERYSTANDBY // Memo1.Lines.Add('pc was PBTF_APMRESUMEFROMFAILURE at ' + DateTimeToStr(now)); PBT_APMBATTERYLOW: Memo1.Lines.Add('pc was PBT_APMBATTERYLOW at ' + DateTimeToStr(now)); PBT_APMPOWERSTATUSCHANGE: Memo1.Lines.Add('pc was PBT_APMPOWERSTATUSCHANGE at ' + DateTimeToStr(now)); PBT_APMOEMEVENT: Memo1.Lines.Add('pc was PBT_APMOEMEVENT at ' + DateTimeToStr(now)); PBT_APMRESUMEAUTOMATIC: Memo1.Lines.Add('pc was PBT_APMRESUMEAUTOMATIC at ' + DateTimeToStr(now)); PBT_POWERSETTINGCHANGE: Memo1.Lines.Add('pc was PBT_POWERSETTINGCHANGE at ' + DateTimeToStr(now)); else // you're going to want to handle PBT_APMRESUMECRITICAL (XP and older systems) and PBT_APMRESUMEAUTOMATIC differently // IIRC you did not get notification of the suspend in this case Memo1.Lines.Add('pc was <<OTHER NON-REPORTED>> at ' + DateTimeToStr(now)); end; end; end. Share this post Link to post
c0d3r 17 Posted April 20, 2023 16 minutes ago, Angus Robertson said: Check the WM_POWER message as well. Angus The WM_POWER message is obsolete. It is provided only for compatibility with 16-bit Windows-based applications. Applications should use the WM_POWERBROADCAST message. Share this post Link to post
c0d3r 17 Posted April 20, 2023 22 minutes ago, programmerdelphi2k said: if many works, and 1 dont, my bet is: verify the setup BIOS, verify if same Windows edition or setup, etc... Works for some, won't work for others (not only 1), and they are on Windows 10 Share this post Link to post
c0d3r 17 Posted April 20, 2023 32 minutes ago, programmerdelphi2k said: my bet is: verify the setup BIOS What should I look into? Share this post Link to post
programmerdelphi2k 237 Posted April 20, 2023 maybe "State code" in hybernation options, compare with others... im not specialist in hardware, sorry! Share this post Link to post
Angus Robertson 574 Posted April 20, 2023 Quote Applications should use the WM_POWERBROADCAST message. The important word there is 'should', many Windows APIs are considered historic and obsolete but are widely used and supported. Angus Share this post Link to post
Remy Lebeau 1392 Posted April 21, 2023 7 hours ago, c0d3r said: I'm getting a strange issue, the following codes were trying logout users when Windows is going to sleep mode. For some reason, it works for some laptops, but it doesn't work for others On Vista+, you can use RegisterPowerSettingNotification() to register to receive WM_POWERBROADCAST notifications for certain power events. On Windows 8+, you can use PowerRegisterSuspendResumeNotification() to receive power notifications via a callback function instead of WM_POWERBROADCAST. IIRC, however, modern Windows also has a newer fast standby/hibernation mode that doesn't sent power notifications to applications, it just pauses and resumes them as if nothing had happened. I can't find the details about that, but I remember being involved in past discussions about it, I just don't remember where. Share this post Link to post
c0d3r 17 Posted April 21, 2023 (edited) 23 hours ago, Remy Lebeau said: On Vista+, you can use RegisterPowerSettingNotification() to register to receive WM_POWERBROADCAST notifications for certain power events. On Windows 8+, you can use PowerRegisterSuspendResumeNotification() to receive power notifications via a callback function instead of WM_POWERBROADCAST. IIRC, however, modern Windows also has a newer fast standby/hibernation mode that doesn't sent power notifications to applications, it just pauses and resumes them as if nothing had happened. I can't find the details about that, but I remember being involved in past discussions about it, I just don't remember where. Something must be changed, was told it was working just fine on the same computer, and then stop working. They are using Windows 10. Edited April 22, 2023 by c0d3r Share this post Link to post