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

Source: Internet
Author: User
Tags sleep function

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

Today, I want to find out how to get the system wake-up messages from sleep and sleep. I wrote an MFC Dialog Box program and pasted some core code:

// Wake up message capture LRESULT CSystemResumedMessageDlg: WindowProc (UINT message, WPARAM wParam, LPARAM lParam) {// TODO: add dedicated code and/or call the base class if (message = WM_POWERBROADCAST) & (wParam = PBT_APMRESUMEAUTOMATIC) {ShowSystemResumedTips (); // action after capturing the wake-up message} return CDialog: WindowProc (message, wParam, lParam );}
Wake up message capture reference: http://bbs.csdn.net/topics/320261685

I have previously written a code to implement system shutdown, restart, sleep, and sleep:

/* MySystemShutdown function: system shutdown and restart parameters: dwShutDownFlag: EWX_SHUTDOWN shutewx_reboot restart bForce: Force Execution return value: Success returns TRUE, otherwise, 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, FAL SE, & tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0); if (GetLastError ()! = ERROR_SUCCESS) return FALSE; if (bForce) {dwShutDownFlag | = EWX_FORCE; // force terminate the process. When this flag is set, Windows does not send messages of WM_QUERYENDSESSION and WM_ENDSESSION to programs currently running in the system. This may cause the application to lose data .} If (! ExitWindowsEx (dwShutDownFlag, kernel | SHTDN_REASON_MINOR_UPGRADE | kernel) return FALSE; return TRUE;}/* MySystemSleep function: System sleep function parameter: bHibernate: TRUE sleep FALSE sleep return value: TRUE is returned. Otherwise, FALSE */BOOL MySystemSleep (BOOL bHibernate = TRUE) // {HMODULE hModule = NULL; BOOL bRet = FALSE; // boolean winapi SetSuspendState (// _ in BOOLEAN Hibernate, // _ in BOOLEAN ForceCrit Ical, // _ in BOOLEAN DisableWakeEvent //); typedef BOOL (WINAPI * PSetSuspendState) (BOOL Hibernate, BOOL ForceCritical, BOOL DisableWakeEvent ); hModule = LoadLibrary (_ T ("PowrProf. dll "); if (hModule) {PSetSuspendState pSetSuspendState = NULL; pSetSuspendState = (PSetSuspendState): GetProcAddress (hModule," SetSuspendState "); // set the backup file name function pointer if (pSetSuspendState! = NULL) {bRet = pSetSuspendState (bHibernate,);} FreeLibrary (hModule);} return bRet ;}
Source code connection: http://download.csdn.net/detail/daiafei/8673013


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.