Solved

How to handle Windows shut down from code?

Posted on 2008-10-23
8
2,296 Views
Last Modified: 2013-11-20
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?

0
Comment
Question by:alexatsearidge
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 19

Expert Comment

by:LordOfPorts
ID: 22791465
0
 
LVL 19

Expert Comment

by:alb66
ID: 22793957
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
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 22854226
>>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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:alexatsearidge
ID: 23531184
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.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 23532234
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 :-)
0
 
LVL 19

Expert Comment

by:alb66
ID: 23535243
>>>>>> 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...
0
 

Author Comment

by:alexatsearidge
ID: 23537657
Yes, Windows shuts down properly, its not a hard "power off" shutdown. So what can I do to handle this Windows shutdown?
0
 
LVL 19

Accepted Solution

by:
alb66 earned 500 total points
ID: 23537759
You must handle the WM_ENDSESSION and/or WM_QUERYENDSESSION message, as we said in the two first comment
BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	ON_WM_QUERYENDSESSION()
	ON_WM_ENDSESSION()
END_MESSAGE_MAP()
 
BOOL CMainFrame::OnQueryEndSession() 
{
   if ( ... )
     // allow windows shutdown
     return TRUE;
   else
     // stop windows shotdown
     return FALSE;
}
 
void CMainFrame::OnEndSession( BOOL bEnding )
{
  if ( bEnding )
     // windows is shutting down
  else
     // shut down was aborted by some applications
}

Open in new window

0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question