Windows system shutdown, restart, sleep, hibernate, and wake-up messages

Source: Internet
Author: User
Tags sleep function

Today to find out how to get the system from sleep, hibernate wake-up message, wrote an MFC dialog box program, posted part of the core code:

Wake-up message capture Lresult Csystemresumedmessagedlg::windowproc (UINT message, WPARAM WPARAM, LPARAM LPARAM) {//TODO: Add dedicated code here and/ or call the base class if (message = = Wm_powerbroadcast) && (WParam = = pbt_apmresumeautomatic)) {showsystemresumedtips ();// Prompt operation after capturing wake-up message}return cdialog::windowproc (message, WParam, LParam);}
Wake-up message capture reference: http://bbs.csdn.net/topics/320261685

Previously wrote a system to implement shutdown, restart, sleep, hibernate implementation, together with the code:

/*mysystemshutdown function: Implement system shutdown, restart parameters: Dwshutdownflag:ewx_shutdown shutdown ewx_reboot Restart Bforce: Enforce return value: True returned, otherwise return false*/ BOOL Mysystemshutdown (DWORD dwshutdownflag = Ewx_shutdown,bool BForce = FALSE) {HANDLE htoken; Token_privileges TKP; Get a token for this process. if (! OpenProcessToken (GetCurrentProcess (), Token_adjust_privileges | Token_query, &htoken)) return (FALSE); Get the LUID for the shutdown privilege. Lookupprivilegevalue (NULL, Se_shutdown_name, &TKP. Privileges[0]. LUID); Tkp.  Privilegecount = 1; One privilege to set TKP. Privileges[0]. Attributes = se_privilege_enabled; Get The shutdown privilege for this process. AdjustTokenPrivileges (Htoken, FALSE, &TKP, 0, (ptoken_privileges) NULL, 0); if (GetLastError () = ERROR_SUCCESS) return FALSE; if (bForce) {Dwshutdownflag |= ewx_force;//forcibly terminates the process. When this flag is set, Windows does not send messages wm_queryendsession and wm_endsession messages to programs that are currently running in the system. This may cause the application to lose data. }if (! ExitWindowsEx (Dwshutdownflag, Shtdn_reason_major_operatingsystem | Shtdn_reason_minor_uPgrade | shtdn_reason_flag_planned)) return FALSE; return TRUE;} /*mysystemsleep function: To realize the system sleep, sleep function parameters: Bhibernate:true Sleep false Sleep return value: Return TRUE if successful, otherwise return False*/bool Mysystemsleep (BOOL Bhibernate = TRUE)//{hmodule hmodule = NULL; BOOL BRet = false;//boolean WINAPI setsuspendstate (//__in Boolean hibernate,//__in boolean Forcecriti cal,//__in BOOLEAN disablewakeevent//); typedef bool (WINAPI *psetsuspendstate) (BOOL hibernate,bool Forcecritica L,bool disablewakeevent) hmodule = LoadLibrary (_t ("PowrProf.dll")); if (hmodule) {Psetsuspendstate psetsuspendstate = Null;psetsuspendstate = (psetsuspendstate):: GetProcAddress (hmodule, "setsuspendstate");//Set backup file name function pointer if ( Psetsuspendstate = NULL) {BRet = Psetsuspendstate (bhibernate,0,0);} FreeLibrary (hmodule);} return bRet;}
Program Source Connection: http://download.csdn.net/detail/daiafei/8673013


Windows system shutdown, restart, sleep, hibernate, and wake-up messages

Related Article

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.