Deep Windows shutdown message interception-changes from XP to Win7

Source: Internet
Author: User

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 PlainCopyprint?
  1. BOOL cendsessiondlg::onqueryendsession ()
  2. {
  3. if (! Cdialog::onqueryendsession ())
  4. return FALSE;
  5. //Record shutdown options and time
  6. CTime time = Ctime::getcurrenttime ();
  7. CString cstemp;
  8. Cstemp.format (TEXT (". \\%d.txt"), m_hwnd);
  9. CFile f (cstemp, Cfile::modecreate | Cfile::modewrite);
  10. M_csoutput.format (TEXT ("%d-%d-%d\twm_queryendsession\ttime:%d-%d-%d-%d-%d-%d\r\n"),
  11. M_queryblock,
  12. M_returntrue,
  13. M_endblock,
  14. Time. GetYear (),
  15. Time. GetMonth (),
  16. Time. GetDay (),
  17. Time. Gethour (),
  18. Time. Getminute (),
  19. Time. Getsecond ());
  20. F.write (M_csoutput.getbuffer (n), M_csoutput.getlength () *sizeof (TCHAR));
  21. if (M_queryblock = = TRUE)
  22. {
  23. MessageBox (TEXT ("Block Shutdown in Wm_queryendsession"));
  24. }
  25. if (m_returntrue = = TRUE)
  26. {
  27. return TRUE;
  28. }
  29. Else
  30. {
  31. return FALSE;
  32. }
  33. }
  34. void Cendsessiondlg::onendsession (BOOL bending)
  35. {
  36. //cdialog::onendsession (bending);
  37. //Record shutdown options and time
  38. CTime time = Ctime::getcurrenttime ();
  39. CString cstemp;
  40. Cstemp.format (TEXT (". \\%d.txt"), m_hwnd);
  41. CFile f (cstemp, Cfile::modecreate | Cfile::modewrite);
  42. Cstemp.format (TEXT ("%d-%d-%d\twm_endsession\t\ttime:%d-%d-%d-%d-%d-%d\r\n"),
  43. M_queryblock,
  44. M_returntrue,
  45. M_endblock,
  46. Time. GetYear (),
  47. Time. GetMonth (),
  48. Time. GetDay (),
  49. Time. Gethour (),
  50. Time. Getminute (),
  51. Time. Getsecond ());
  52. M_csoutput + = cstemp;
  53. F.write (M_csoutput.getbuffer (n), M_csoutput.getlength () *sizeof (TCHAR));
  54. if (M_endblock = = TRUE)
  55. {
  56. Cstemp.format (TEXT ("block Shutdown in wm_endsession---bending=%d"), bending);
  57. MessageBox (cstemp);
  58. }
  59. }

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 PlainCopy print?
  1. 1-1-1 wm_queryendsession time:2014-1-11-11-53-4
  2. <00001> 00060260 S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
  3. 1-1-0 wm_queryendsession time:2014-1-11-11-53-5
  4. <00001> 000102b0 S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
  5. 0-1-1 wm_queryendsession time:2014-1-11-11-53-6
  6. 0-1-1 wm_endsession time:2014-1-11-11-53-6
  7. <00001> 000402c4 S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
  8. <00002> 000402c4 R. Wm_queryendsession fshutdownisok:true
  9. <00003> 000402c4 S. Wm_endsession fendsession:true
  10. 0-1-0 wm_queryendsession time:2014-1-11-11-53-7
  11. 0-1-0 wm_endsession time:2014-1-11-11-53-7
  12. <00001> 00050150 S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
  13. <00002> 00050150 R. Wm_queryendsession fshutdownisok:true
  14. <00003> 00050150 S. Wm_endsession fendsession:true
  15. <00004> 00050150 R. Wm_endsession
  16. 1-0-1 wm_queryendsession time:2014-1-11-11-53-7
  17. <00001> 00030168 S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
  18. 1-0-0 wm_queryendsession time:2014-1-11-11-53-8
  19. <00001> 00030151 S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
  20. 0-0-1 wm_queryendsession time:2014-1-11-11-53-9
  21. 0-0-1 wm_endsession time:2014-1-11-11-53-9
  22. <00001> 00030160 S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
  23. <00002> 00030160 R. Wm_queryendsession Fshutdownisok:false
  24. <00003> 00030160 S. Wm_endsession Fendsession:false
  25. <00004> 00030160 R. Wm_endsession//Note Click OK to return
  26. 0-0-0
  27. <00001> 000800BC S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
  28. <00002> 000800BC R. Wm_queryendsession Fshutdownisok:false
  29. <00003> 000800BC S. Wm_endsession Fendsession:false
  30. <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?
  1. 0-0-0 wm_queryendsession time:2014-1-11-11-0-55
  2. 0-0-0 wm_endsession time:2014-1-11-11-0-55
  3. <00001> 00020DBA S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
  4. <00002> 00020DBA R. Wm_queryendsession Fshutdownisok:false
  5. <00003> 00020DBA S. Wm_endsession fendsession:true
  6. <00004> 00020DBA R. Wm_endsession
  7. 0-0-1 wm_queryendsession time:2014-1-11-11-0-55
  8. 0-0-1 wm_endsession time:2014-1-11-11-0-55
  9. <00001> 00020b2c S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
  10. <00002> 00020b2c R. Wm_queryendsession Fshutdownisok:false
  11. <00003> 00020b2c S. Wm_endsession fendsession:true
  12. 1-0-0 wm_queryendsession time:2014-1-11-11-1-0
  13. <00001> 00050CFA S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
  14. 1-0-1 wm_queryendsession time:2014-1-11-11-1-5
  15. <00001> 00020BCA S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
  16. 0-1-0 wm_queryendsession time:2014-1-11-11-1-10
  17. 0-1-0 wm_endsession time:2014-1-11-11-1-10
  18. <00001> 00020be2 S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
  19. <00002> 00020be2 R. Wm_queryendsession fshutdownisok:true
  20. <00003> 00020be2 S. Wm_endsession fendsession:true
  21. <00004> 00020be2 R. Wm_endsession
  22. 0-1-1 wm_queryendsession time:2014-1-11-11-1-10
  23. 0-1-1 wm_endsession time:2014-1-11-11-1-10
  24. <00001> 00020c4c S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
  25. <00002> 00020c4c R. Wm_queryendsession fshutdownisok:true
  26. <00003> 00020c4c S. Wm_endsession fendsession:true
  27. 1-1-0 wm_queryendsession time:2014-1-11-11-1-15
  28. <00001> 00040c5a S. Wm_queryendsession nsource:0 (Logoff or Shutdown from Windows Security Dialog)
  29. 1-1-1 wm_queryendsession time:2014-1-11-11-1-20
  30. <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

Deep Windows shutdown message interception-changes from XP to Win7

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.