Auto close my program when I turn off computer lazarus / 12-Debugger ---------------------------------------------------------------------- nocuttree · 2014-10-09 13:51 When I turned off computer, windows has showed "End now" process. I want to auto close my program when I turn off computer. Thanks all! ---------------------------------------------------------------------- minesadorada · 2014-10-09 14:08 You probably need to process the WM_QUERYENDSESSION message from Windows in your app. Maybe drop a ApplicationProperties onto the form and set the OnQueryEndSession event? http://msdn.microsoft.com/en-us/library/ms700677.aspx ---------------------------------------------------------------------- nocuttree · 2014-10-09 14:52 Quote from: minesadorada on October 09, 2014, 04:08:41 pmYou probably need to process the WM_QUERYENDSESSION message from Windows in your app. Maybe drop a ApplicationProperties onto the form and set the OnQueryEndSession event? http://msdn.microsoft.com/en-us/library/ms700677.aspx My code was contained in project1.lpr file, i don't use form so how can i do it? ---------------------------------------------------------------------- Mike.Cornflake · 2014-10-09 15:03 Quote from: minesadorada on October 09, 2014, 04:08:41 pmYou probably need to process the WM_QUERYENDSESSION message from Windows in your app. Where were you 17 years ago when I was struggling big time with this?  :-)  (And there's still an app of mine in the wild with this problem - can't shutdown a PC with it running) Quote from: nocuttree on October 09, 2014, 04:52:10 pmMy code was contained in project1.lpr file, i don't use form so how can i do it? Errr...  Well, essentially you need to intercept all windows messages going to your app, and determine if one is WM_QUERYENDSESSION.   The framework is detailed here http://wiki.lazarus.freepascal.org/Win32/64_Interface#Processing_non- user_messages_in_your_window But...  Without Forms?   Is this a console app?  Do console apps even get Windows Messages?  I thought the purpose of the console was to do without the overheads of window management.  Could just be ancient thinking - I see console app and think DOS.  So, really, I've no idea.... Also, there's a big assumption here that you're on Windows.  Again - I've absolutely no idea how to do this on other OS's... ---------------------------------------------------------------------- minesadorada · 2014-10-09 15:20 Quote from: nocuttree on October 09, 2014, 04:52:10 pmQuote from: minesadorada on October 09, 2014, 04:08:41 pmYou probably need to process the WM_QUERYENDSESSION message from Windows in your app. Maybe drop a ApplicationProperties onto the form and set the OnQueryEndSession event? http://msdn.microsoft.com/en-us/library/ms700677.aspx My code was contained in project1.lpr file, i don't use form so how can i do it? You can't add a WndProc without an window. But if you do need a window, why not create a GUI app which does the message dispatching for you? You can hide the GUI or set its size to 0,0. ---------------------------------------------------------------------- nocuttree · 2014-10-10 07:09 Quote from: minesadorada on October 09, 2014, 05:20:40 pmQuote from: nocuttree on October 09, 2014, 04:52:10 pmQuote from: minesadorada on October 09, 2014, 04:08:41 pmYou probably need to process the WM_QUERYENDSESSION message from Windows in your app. Maybe drop a ApplicationProperties onto the form and set the OnQueryEndSession event? http://msdn.microsoft.com/en-us/library/ms700677.aspx My code was contained in project1.lpr file, i don't use form so how can i do it? You can't add a WndProc without an window. But if you do need a window, why not create a GUI app which does the message dispatching for you? You can hide the GUI or set its size to 0,0. Can you give an exam application? ---------------------------------------------------------------------- User137 · 2014-10-10 08:22 Code: [Select]  Application.Initialize;   Application.CreateForm(TForm1, Form1);   Application.ShowMainForm:=false; // <- This line added   Application.Run; ---------------------------------------------------------------------- ChrisF · 2014-10-10 12:40 In a Windows console program it's also possible to use SetConsoleCtrlHandler. http://msdn.microsoft.com/en- us/library/windows/desktop/ms686016%28v=vs.85%29.aspx and http://msdn.microsoft.com/en- us/library/windows/desktop/ms685049%28v=vs.85%29.aspx See attached program (TestCtrlHandler) for a basic sample. ---------------------------------------------------------------------- nocuttree · 2014-10-10 13:24 Quote from: ChrisF on October 10, 2014, 02:40:26 pmIn a Windows console program it's also possible to use SetConsoleCtrlHandler. http://msdn.microsoft.com/en- us/library/windows/desktop/ms686016%28v=vs.85%29.aspx and http://msdn.microsoft.com/en- us/library/windows/desktop/ms685049%28v=vs.85%29.aspx See attached program (TestCtrlHandler) for a basic sample. Quote from: User137 on October 10, 2014, 10:22:33 amCode: [Select]  Application.Initialize;   Application.CreateForm(TForm1, Form1);   Application.ShowMainForm:=false; // <- This line added   Application.Run;Thanks you! I want to have a app: Code: [Select]  Application.Initialize;   Application.CreateForm(TForm1, Form1);   Application.ShowMainForm:=false; // <- This line added   Application.Run;and can auto close app when I turn off computer. Can you give me an exam app? Thanks to all! ---------------------------------------------------------------------- User137 · 2014-10-10 16:01 I'm guessing that could already fix it. TForm or TApplication may automatically intercept the required windows message and close app for you. I have a TForm based application of my own that starts and closes as systray app on its own, i didn't do anything to accomplish that. ---------------------------------------------------------------------- nocuttree · 2014-10-11 15:31 Quote from: User137 on October 10, 2014, 06:01:47 pmI'm guessing that could already fix it. TForm or TApplication may automatically intercept the required windows message and close app for you. I have a TForm based application of my own that starts and closes as systray app on its own, i didn't do anything to accomplish that. Thanks you!