Recent

Author Topic: [Solved] how to detect WM_QueryEndSession and WM_EndSession  (Read 1824 times)

HobbyDev

  • New member
  • *
  • Posts: 25
Hi everybody,

I know there are tons of post regarding this topic but none of them works, unfortunatly. I checked out this post and I tried this post. The first idea I can not even compile, the second code will never be used when system shuts down nor the user logs off. Im using Lazarus 1.8.2 and the target OS is Windows 10.

What am I missing, is there a working example for Windows 10 available?

Many Thanks
HobbyDev
« Last Edit: May 09, 2018, 09:00:41 pm by HobbyDev »

Bart

  • Hero Member
  • *****
  • Posts: 3427
    • Bart en Mariska's Webstek
Re: how to detect WM_QueryEndSession and WM_EndSession
« Reply #1 on: May 07, 2018, 10:36:54 pm »
Assign event handlers to Application.OnEndSession and Application.OnQueryEndSession?

Bart

HobbyDev

  • New member
  • *
  • Posts: 25
Re: how to detect WM_QueryEndSession and WM_EndSession
« Reply #2 on: May 08, 2018, 08:40:31 pm »
Well, I am not sure if you have something different in mind. I used the Code described here . When I try this I get the compiler error
Code: [Select]
Error: Variable identifier expected at the line
Code: Pascal  [Select]
  1. Application.OnEndSession:=@OnEndSession;
and have no idea whats wrong.

GetMem

  • Hero Member
  • *****
  • Posts: 3453
Re: how to detect WM_QueryEndSession and WM_EndSession
« Reply #3 on: May 08, 2018, 09:16:44 pm »
You need to declare it like this:
Code: Pascal  [Select]
  1. type
  2.  
  3.   { TForm1 }
  4.  
  5.   TForm1 = class(TForm)
  6.     Button1: TButton;
  7.     procedure FormCreate(Sender: TObject);
  8.   private
  9.      procedure OnEndSession(Sender: TObject);
  10.      procedure OnQueryendSession(var Cancel: Boolean);
  11.   public
  12.  
  13.   end;
  14.  
  15. var
  16.   Form1: TForm1;
  17.  
  18. implementation
  19.  
  20. {$R *.lfm}
  21.  
  22. { TForm1 }
  23.  
  24. procedure TForm1.FormCreate(Sender: TObject);
  25. begin
  26.   Application.OnEndSession := @OnEndSession;
  27.   Application.OnQueryEndSession := @OnQueryendSession;
  28. end;
  29.  
  30. procedure TForm1.OnEndSession(Sender: TObject);
  31. begin
  32.   //do something
  33. end;
  34.  
  35. procedure TForm1.OnQueryendSession(var Cancel: Boolean);
  36. begin
  37.   //do something
  38. end;

In delphi mode you can remove "@" before OnEndSession/OnQueryendSession.

HobbyDev

  • New member
  • *
  • Posts: 25
Re: how to detect WM_QueryEndSession and WM_EndSession
« Reply #4 on: May 09, 2018, 09:00:04 pm »
Thank you very much - now it is working! It took me a some time to find my problem because the code looks similar to mine. The Point was I was switching from Delphi to Lazarus some time ago and this Project has the Compiler directive {$MODE Delphi}. Because of the comment
Quote
In delphi mode you can remove "@" before OnEndSession/OnQueryendSession.
I left it out and - yes it is working!

By the way, I found an other solution in between using the TApplicationProperties Control from the "Additional" - Tab. So this is working, too.

Thanks again