Starting from Vista, want to prevent the system shutdown began to become troublesome, not only to intercept wm_queryendsession, the operating system only give an application two seconds to save their own things, two seconds later, no matter done no, Game over! But what if you're burning a disc? Two seconds is not enough, so Vista also provides a way to prevent the system from shutting down, which isshutdownblockreasoncreatefunction, as long as the function is called, the first argument passes the handle that creates the main window, and the second argument passes a string that explains why the shutdown was blocked and will be displayed to the user. And the MSDN Tip says that this API function can only be called by the thread that created the main window, otherwise it will returnerror_access_denied。 And when you're done, you have to callShutdownblockreasondestroyThe function enables the system shutdown to proceed, and the parameter passes only the handle of the window above.
The following code is the complete implementation, can not use VS2005 compilation, because the library does not have these two functions, I am using VS2010 compiled, project files and executables at the bottom of the article.
1#include <windows.h>2#include <tchar.h>3#include"resource.h"4 5 //Flag has blocked shutdown, default is not blocked6BOOL Blockedflag =FALSE;7 8 //These two functions are purely spoiled, in order to highlight them to take out to change the name of9 BOOL Blockshutdown (HWND hwnd)Ten { One if(Shutdownblockreasoncreate (hwnd, _t ("don't turn off the machine! Unless you click force shutdown:-("))) A { - returnTRUE; - } the returnFALSE; - } - - BOOL Unblockshutdown (HWND hwnd) + { - if(Shutdownblockreasondestroy (HWND)) + { A returnTRUE; at } - returnFALSE; - } - - int_ptr CALLBACK Maindialogproc (HWND hwnddlg, UINT umsg, WPARAM WPARAM, LPARAM LPARAM) - { in Switch(umsg) - { to CaseWm_initdialog: +Setdlgitemtext (Hwnddlg, Idc_static_status, _t ("Shutdown not blocked")); - returnTRUE; the CaseWm_close: * if(Blockedflag) $ {Panax Notoginseng if(Unblockshutdown (HWNDDLG)) - { theEndDialog (Hwnddlg,0); + returnTRUE; A } the if(Idyes = = MessageBox (Hwnddlg, _t ("unblock failed ... Do you want to continue to close the program (after closing the program does not affect the shutdown)? "), _t ("Tips"), Mb_yesno)) + { -EndDialog (Hwnddlg,0); $ returnTRUE; $ } - returnFALSE; - } theEndDialog (Hwnddlg,0); - returnTRUE;Wuyi Casewm_queryendsession: the if(Blockedflag)//don't think just call Blockshutdownreasoncreate, wm_queryendsession have to stop. - { Wu returnTRUE; - } About returnFALSE; $ CaseWm_command: - Switch(LoWord (wParam)) - { - CaseIdc_button_block: A if(Blockedflag) + { theMessageBox (Hwnddlg, _t ("The shutdown has been blocked, please click the shutdown test. "), _t ("Tips"), MB_OK); - } $ Else the { the if(Blockshutdown (HWNDDLG)) the { theSetdlgitemtext (Hwnddlg, Idc_static_status, _t ("shutdown has been blocked")); -Blockedflag =TRUE; in } the Else the { AboutMessageBox (Hwnddlg, _t ("Stop the shutdown failed ..."), _t ("Tips"), MB_OK); the } the } the returnTRUE; + CaseIdc_button_unblock: - if(!Blockedflag) the {BayiMessageBox (Hwnddlg, _t ("shutdown is not blocked, no cancellation is required. "), _t ("Tips"), MB_OK); the } the Else - { - if(Unblockshutdown (HWNDDLG)) the { theSetdlgitemtext (Hwnddlg, Idc_static_status, _t ("Shutdown not blocked")); theBlockedflag =FALSE; the } - Else the { theMessageBox (Hwnddlg, _t ("unblock failed ..."), _t ("Tips"), MB_OK); the }94 } the returnTRUE; the default: the returnFALSE;98 } About default: - returnFALSE;101 }102 returnFALSE;103 }104 intWINAPI WinMain (hinstance hinstance, hinstance hprevinstance, LPSTR lpCmdLine,intnshowcmd) the {106 DialogBox (hinstance, Makeintresource (Idd_maindialog), NULL, maindialogproc);107 return 0;108}
is to run the effect.
The above figure should be seen by many people, but usually disappears just as soon as the display, so it won't disappear until the program calls the Shutdownblockreasondestroy.
It is also important to note that if your computer installed 360 security guard ORQQ computer Butler and other optimization software, usually one-click Optimization to optimize off ... You will not see this dialog box, the program is directly shot off, because this can speed up the shutdown speed:-(
Complete C language Engineering and executable program files are in the compressed package, in order to facilitate the VS2010 of the Child Shoes test program effect, Msvcr100.dll I also put in the release directory, is http://files.cnblogs.com/pianoid /shutdownblocker.rar.