Solved

How to handle Windows shut down from code?

Posted on 2008-10-23
8
2,228 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 108
deburging in oracle form 12 105
sumHeights2  challenge 7 114
I could not set window to top 4 20
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 (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 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