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