Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to handle Windows shut down from code?

Posted on 2008-10-23
8
Medium Priority
?
2,365 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 

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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

688 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