Prior to writing a software for the lab's punch-in reminders, one of the important features is to remind the night before the shutdown whether the clock has been clocked. I was in wm_endsession. A modal dialog box is shown to remind you that the basic work is working correctly in XP and that most of the time in Win7 works fine, but sometimes there are no reminders. I think there is something in the middle, the Windows shutdown scheme from XP to Win7 exactly what happened, how to effectively intercept Windows shutdown messages. For this, I searched MSDN and the online forum to combine my own assessment to give a complete description and solution, if you have similar needs, you can refer to this article.
The change in Windows shutdown behavior in MSDN is described only in contrast to Vista and XP (this is the link), but the actual evaluation shows that this descriptive document is highly misleading because he is only partially correct, and not surprisingly, Microsoft's documentation is a lot more wrong, xx Microsoft, Do the interns write the documents? If you don't want to read this misleading article, just look down.
In order to reflect the actual shutdown behavior, I wrote a small interception software, some of the code is as follows
[CPP]View PlainCopy print
- BOOL cendsessiondlg::onqueryendsession ()
- {
- if (! Cdialog::onqueryendsession ())
- return FALSE;
- //Record shutdown options and time
- CTime time = Ctime::getcurrenttime ();
- CString cstemp;
- Cstemp.format (TEXT (". \\%d.txt"), m_hwnd);
- CFile f (cstemp, Cfile::modecreate | Cfile::modewrite);
- M_csoutput.format (TEXT ("%d-%d-%d\twm_queryendsession\ttime:%d-%d-%d-%d-%d-%d\r\n"),
- M_queryblock,
- M_returntrue,
- M_endblock,
- Time. GetYear (),
- Time. GetMonth (),
- Time. GetDay (),
- Time. Gethour (),
- Time. Getminute (),
- Time. Getsecond ());
- F.write (M_csoutput.getbuffer (n), M_csoutput.getlength () *sizeof (TCHAR));
- if (M_queryblock = = TRUE)
- {
- MessageBox (TEXT ("Block Shutdown in Wm_queryendsession"));
- }
- if (m_returntrue = = TRUE)
- {
- return TRUE;
- }
- Else
- {
- return FALSE;
- }
- }
- void Cendsessiondlg::onendsession (BOOL bending)
- {
- //cdialog::onendsession (bending);
- //Record shutdown options and time
- CTime time = Ctime::getcurrenttime ();
- CString cstemp;
- Cstemp.format (TEXT (". \\%d.txt"), m_hwnd);
- CFile f (cstemp, Cfile::modecreate | Cfile::modewrite);
- Cstemp.format (TEXT ("%d-%d-%d\twm_endsession\t\ttime:%d-%d-%d-%d-%d-%d\r\n"),
- M_queryblock,
- M_returntrue,
- M_endblock,
- Time. GetYear (),
- Time. GetMonth (),
- Time. GetDay (),
- Time. Gethour (),
- Time. Getminute (),
- Time. Getsecond ());
- M_csoutput + = cstemp;
- F.write (M_csoutput.getbuffer (n), M_csoutput.getlength () *sizeof (TCHAR));
- if (M_endblock = = TRUE)
- {
- Cstemp.format (TEXT ("block Shutdown in wm_endsession---bending=%d"), bending);
- MessageBox (cstemp);
- }
- }
The software interface is as follows
The basic function of the test software is to record the time of wm_queryendsession and wm_endsession messages when the winows shuts down and the shutdown option to the log, depending on the different options in the test software interface, there are different actions in these two messages. The blocking shutdown is in the MessageBox (not returned or blocked).
Use Spy + + to capture a window message and log it
For the shutdown message Test example, we refer to the previous article (this is a link), select test Block Shutdown and cancel Shutdown in Wm_queryendsession, and test block in Wm_endsession Shutdown, exhaustive combination they have eight sets of test cases
XP evaluation test For example, the following figure in 1 for block or return true first open 8 test software instances, by number to open the software according to the open order tick the appropriate option, and then open Spy + +, set the corresponding window of the message capture, note that this setting must be in the order of the Red letter, As to why it was announced immediately after the shutdown, observe that the first test case opened first closed, and then in the order in which they were opened, and then stopped at the 7th set of test cases, yes, the shutdown behavior was blocked, and no MSDN Description of the selection dialog box pops up
Merge the logs as follows
[Plain]View PlainCopyprint?
- 1-1-1 wm_queryendsession time:2014-1-11-11-53-4
- <00001> 00060260 S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
- 1-1-0 wm_queryendsession time:2014-1-11-11-53-5
- <00001> 000102b0 S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
- 0-1-1 wm_queryendsession time:2014-1-11-11-53-6
- 0-1-1 wm_endsession time:2014-1-11-11-53-6
- <00001> 000402c4 S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
- <00002> 000402c4 R. Wm_queryendsession fshutdownisok:true
- <00003> 000402c4 S. Wm_endsession fendsession:true
- 0-1-0 wm_queryendsession time:2014-1-11-11-53-7
- 0-1-0 wm_endsession time:2014-1-11-11-53-7
- <00001> 00050150 S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
- <00002> 00050150 R. Wm_queryendsession fshutdownisok:true
- <00003> 00050150 S. Wm_endsession fendsession:true
- <00004> 00050150 R. Wm_endsession
- 1-0-1 wm_queryendsession time:2014-1-11-11-53-7
- <00001> 00030168 S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
- 1-0-0 wm_queryendsession time:2014-1-11-11-53-8
- <00001> 00030151 S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
- 0-0-1 wm_queryendsession time:2014-1-11-11-53-9
- 0-0-1 wm_endsession time:2014-1-11-11-53-9
- <00001> 00030160 S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
- <00002> 00030160 R. Wm_queryendsession Fshutdownisok:false
- <00003> 00030160 S. Wm_endsession Fendsession:false
- <00004> 00030160 R. Wm_endsession//Note Click OK to return
- 0-0-0
- <00001> 000800BC S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
- <00002> 000800BC R. Wm_queryendsession Fshutdownisok:false
- <00003> 000800BC S. Wm_endsession Fendsession:false
- <00004> 000800BC R. Wm_endsession
The analysis can be concluded as follows:
1.XP turn off the time to send wm_queryendsession and wm_endsession messages to the window, before the end of a program to send a second wm_queryendsession not online some people say that the first to send to each program wm_ Queryendsession (In fact this is the way Windows 95 is described by MSDN). Usually the first open program is closed first.
2. In Wm_queryendsession, the block Shutdown1 seconds does not return, XP will force the end of it, and the next to close the program continue to send Wm_queryendsession
3. In Wm_endsession, the block Shutdown1 seconds does not return, XP will force the end of it, and the next to close the program continue to send Wm_queryendsession
4. Immediately return false in Wm_queryendsession (at least block not more than 1 seconds to return), wm_endsession the shutdown parameter is FALSE,XP immediately stop the shutdown behavior, the current return false program still received WM_ EndSession messages, XP does not continue to send wm_queryendsession down.
Win7 Assessment
Again, the test case is the same as above
First Open Spy + +, and then open 8 test software instances, according to the number of open software according to the open order tick the appropriate options, set the corresponding window of the message capture, note that this setting must be in the order of the Red letter, and why immediately after the closure machine, observed that the last Test case opened closed , and then turn off in reverse order, after a period of time after the program to a similar to the following interface (can not put is a, specifically download test software test), to the following interface basically wait 5 seconds to close a test case until the system shuts down.
Merge the logs as follows
[Plain]View PlainCopyprint?
- 0-0-0 wm_queryendsession time:2014-1-11-11-0-55
- 0-0-0 wm_endsession time:2014-1-11-11-0-55
- <00001> 00020DBA S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
- <00002> 00020DBA R. Wm_queryendsession Fshutdownisok:false
- <00003> 00020DBA S. Wm_endsession fendsession:true
- <00004> 00020DBA R. Wm_endsession
- 0-0-1 wm_queryendsession time:2014-1-11-11-0-55
- 0-0-1 wm_endsession time:2014-1-11-11-0-55
- <00001> 00020b2c S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
- <00002> 00020b2c R. Wm_queryendsession Fshutdownisok:false
- <00003> 00020b2c S. Wm_endsession fendsession:true
- 1-0-0 wm_queryendsession time:2014-1-11-11-1-0
- <00001> 00050CFA S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
- 1-0-1 wm_queryendsession time:2014-1-11-11-1-5
- <00001> 00020BCA S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
- 0-1-0 wm_queryendsession time:2014-1-11-11-1-10
- 0-1-0 wm_endsession time:2014-1-11-11-1-10
- <00001> 00020be2 S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
- <00002> 00020be2 R. Wm_queryendsession fshutdownisok:true
- <00003> 00020be2 S. Wm_endsession fendsession:true
- <00004> 00020be2 R. Wm_endsession
- 0-1-1 wm_queryendsession time:2014-1-11-11-1-10
- 0-1-1 wm_endsession time:2014-1-11-11-1-10
- <00001> 00020c4c S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
- <00002> 00020c4c R. Wm_queryendsession fshutdownisok:true
- <00003> 00020c4c S. Wm_endsession fendsession:true
- 1-1-0 wm_queryendsession time:2014-1-11-11-1-15
- <00001> 00040c5a S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
- 1-1-1 wm_queryendsession time:2014-1-11-11-1-20
- <00001> 00050c6c S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
-
The analysis can be concluded as follows:
1.win7 shutdown when the window sends wm_queryendsession and WM_ENDSESSION messages, the previous program to the end of the second to send a wm_queryendsession not online some people say first to each program sent WM_ Queryendsession (In fact this is the way Windows 95 is described by MSDN). Typically, the program is opened first and then closed.
2. In wm_queryendsession block Shutdown5 seconds does not return, Win7 will force the end of it, and to the next to close the program continue to send Wm_queryendsession
3. In wm_endsession block Shutdown5 seconds does not return, Win7 will force the end of it, and to the next to close the program continue to send Wm_queryendsession
4. Returning false in Wm_queryendsession is the same as returning ture, wm_endsession the shutdown parameters are true,windows continue to send down wm_queryendsession.
5. Once the wm_queryendsession and wm_endsession,win7 have not finished processing all the programs within 5 seconds, they will switch to the "Force shutdown or Cancel" interface.
Changes in shutdown behavior of XP to Win7
Compare the test, you can see the following changes to XP to Win7
1. In XP The program can block shutdown, but in Win7 the program cannot prevent it from shutting down. The description of this Microsoft MSDN is to follow the user's behavior as much as possible, assuming the user presses the Shimonoseki machine, then the heart is to complete the shutdown, if the program can prevent the user shutdown, then is unfriendly program, so in Win7 simply do not allow the user program to prevent the system shutdown. So the return value of Wm_queryendsession here Ture or false is meaningless in Win7, just to be compatible with previous programs, in fact, the shutdown information received in Win7 wm_endsession is True anyway.
2. What if the user mistakenly operated by the shutdown? XP on this regardless, Win7 has a buffer interface (as above), allowing users to cancel the shutdown, in short, is more and more humane, the bitter is the developer.
3. For the program, from XP to Win7, Microsoft will be able to block the time from 1 seconds to 5 seconds, which allows the program to do more finishing work, such as saving data to files and so on. But be aware that once your finishing touches are too long and the block exceeds the specified length of time without returning, the system will teminate the program. For this Microsoft MSDN give advice, if your program to save a large amount of data please set a timed save, after all, the shutdown time behavior is not dependent. You can only put a small amount of data saved work at this time to deal with. If you want to rely on this information to do data backup, then you can only say that you are naïve.
Effective interception of Windows shutdown messages basically we intercept shutdown messages with the following requirements and Solutions 1. Stop the shutdown the front has said, this in Win7 is not possible, if you have to say my program is only for XP users, then you have to have this function can also. What you need to do is return false within 1 seconds of wm_queryendsession, preferably return false immediately. If you also want to pop up a dialog box to choose whether to shut down, then the default behavior is to shut down, before Wm_queryendsession returns false before ejecting the modal MessageBox to your user for a poor 1 seconds to choose whether to shut down. If you cancel the shutdown, you can notify the user in Wm_endsession. 2. Write parameters to file Microsoft MSDN Recommendations are immediately returned in Wm_queryendsession, put all the save operations into wm_endsession processing, of course, do not forget the XP 1 seconds and Win7 5 seconds block limit. 3. Prompt the user some information if you want to be like my software function to remind the user, then the prompt message behavior in the wm_queryendsession processing, but Win7 if the program before your program blocked for more than 5 seconds, then will switch to Win7 unique shutdown interface, This time will not see the prompt message, how to do it. We said above that the XP Win7 program shutdown sequence is not the same, You can use the Setprocessshutdownparameters function to advance the shutdown order of the current program and use the Shutdownblockreasoncreate function to create a prompt message on the shutdown interface.
Blog full test code and test log download link
The latest version of the author's lab Clock Wizard executable file and source code link, which is the main way to optimize the alarm when the shutdown is to improve the program shutdown sequence and change in the WM_QUERYENDSESSION message alert
Original, reprint please indicate from http://blog.csdn.net/wenzhou1219
http://blog.csdn.net/wenzhou1219/article/details/18138885
http://download.csdn.net/detail/wenzhou1219/6837583
Deep Windows shutdown message interception-changes from XP to Win7 (in XP The program can block shutdown, but in Win7 the program cannot prevent shutdown, the block time from 1 seconds to 5 seconds) good