Link to home
Start Free TrialLog in
Avatar of alexatsearidge
alexatsearidgeFlag for Canada

asked on

How to handle Windows shut down from code?

My MFC application needs to perform some quick cleanup code before shutting down. (Log a couple of info to the Event log and close some files).

So I have put my shutdown code in the ExitInstance() of the main class.

The user would typically stop the application by pressing the power button on the Server. I thought this was sending a WM_QUIT message to all application, which in turn should be handled by the ExitInstance(), but ExitInstance() never gets called if the app is shutdown this way.

So how is my application suppose to handle getting killed on a Windows shutdown?

Avatar of LordOfPorts
LordOfPorts
Flag of United States of America image

If, in some case, you want to abort the shut down process, you can handle the WM_QUERYENDSESSION message.
http://msdn.microsoft.com/en-us/library/aa376890(VS.85).aspx
Avatar of DanRollins
>>The user would typically stop the application by pressing the power button on the Server.
Tell the user: "DON'T EVER DO THAT!!!!"  Tell the user to use the correct method of shut down, which involves the Start Button or Ctrl+Alt+Del
Avatar of alexatsearidge

ASKER

Sorry, but this is how we intend to use it. I want it as simple as possible, I dont even hook up a keyboard to the PC.
You will not get a clean shutdown.  
Knowing that, the best you can do is periodically save whatever information you will need in order to tidy things up when your program starts next time.  ...  About once every fourteen months ought to be often enough :-)
>>>>>> The user would typically stop the application by pressing the power button on the Server

On some machines the power button is handled by software and it properly shutdown the system. Give to your user on of these machine...
Yes, Windows shuts down properly, its not a hard "power off" shutdown. So what can I do to handle this Windows shutdown?
ASKER CERTIFIED SOLUTION
Avatar of alb66
alb66
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial