Recent

Author Topic: Auto close my program when I turn off computer  (Read 6775 times)

nocuttree

  • New Member
  • *
  • Posts: 36
Auto close my program when I turn off computer
« on: October 09, 2014, 03:51:33 pm »
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!
Sorry my English, Windows xp, Lazarus 1.2.2!

minesadorada

  • Sr. Member
  • ****
  • Posts: 452
  • Retired
Re: Auto close my program when I turn off computer
« Reply #1 on: October 09, 2014, 04:08:41 pm »
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
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

nocuttree

  • New Member
  • *
  • Posts: 36
Re: Auto close my program when I turn off computer
« Reply #2 on: October 09, 2014, 04:52:10 pm »
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
My code was contained in project1.lpr file, i don't use form so how can i do it?
Sorry my English, Windows xp, Lazarus 1.2.2!

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Auto close my program when I turn off computer
« Reply #3 on: October 09, 2014, 05:03:02 pm »
You 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)

My 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...
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

minesadorada

  • Sr. Member
  • ****
  • Posts: 452
  • Retired
Re: Auto close my program when I turn off computer
« Reply #4 on: October 09, 2014, 05:20:40 pm »
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
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.
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

nocuttree

  • New Member
  • *
  • Posts: 36
Re: Auto close my program when I turn off computer
« Reply #5 on: October 10, 2014, 09:09:30 am »
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
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?
« Last Edit: October 10, 2014, 02:46:00 pm by nocuttree »
Sorry my English, Windows xp, Lazarus 1.2.2!

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Auto close my program when I turn off computer
« Reply #6 on: October 10, 2014, 10:22:33 am »
Code: [Select]
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.ShowMainForm:=false; // <- This line added
  Application.Run;

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: Auto close my program when I turn off computer
« Reply #7 on: October 10, 2014, 02:40:26 pm »
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

  • New Member
  • *
  • Posts: 36
Re: Auto close my program when I turn off computer
« Reply #8 on: October 10, 2014, 03:24:40 pm »
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.
Code: [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!  :D

Sorry my English, Windows xp, Lazarus 1.2.2!

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Auto close my program when I turn off computer
« Reply #9 on: October 10, 2014, 06:01:47 pm »
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

  • New Member
  • *
  • Posts: 36
Re: Auto close my program when I turn off computer
« Reply #10 on: October 11, 2014, 05:31:24 pm »
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.
Thanks you!
Sorry my English, Windows xp, Lazarus 1.2.2!

 

TinyPortal © 2005-2018