Solved

How to handle Windows shut down from code?

Posted on 2008-10-23
8
2,196 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
  • 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
 

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now