https://msdn.microsoft.com/en-us/library/windows/desktop/aa372721(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/aa372708(v=vs.85).aspx
今天要查找如何获取系统从睡眠、休眠状态下唤醒的消息,写了个MFC对话框的程序,贴出部分核心代码:
- //唤醒消息捕获
- LRESULT CSystemResumedMessageDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
- {
- // TODO: 在此添加专用代码和/或调用基类
- if ((message == WM_POWERBROADCAST) && (wParam == PBT_APMRESUMEAUTOMATIC))
- {
- ShowSystemResumedTips();//捕获唤醒消息后的提示操作
- }
- return CDialog::WindowProc(message, wParam, lParam);
- }
唤醒消息捕获参考了:http://bbs.csdn.net/topics/320261685
之前写过一个实现系统关机、重启、睡眠、休眠的实现,一并贴出代码:
- /*
- MySystemShutdown
- 功能:实现系统关机、重启
- 参数:
- dwShutDownFlag:EWX_SHUTDOWN 关机 EWX_REBOOT 重启
- bForce:强制执行
- 返回值:成功返回TRUE,否则返回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;//强制终止进程。当此标志设置,Windows不会发送消息WM_QUERYENDSESSION和WM_ENDSESSION的消息给目前在系统中运行的程序。这可能会导致应用程序丢失数据。
- }
- if (!ExitWindowsEx(dwShutDownFlag,
- SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
- SHTDN_REASON_MINOR_UPGRADE |
- SHTDN_REASON_FLAG_PLANNED))
- return FALSE;
- return TRUE;
- }
- /*
- MySystemSleep
- 功能:实现系统休眠、睡眠功能
- 参数:
- bHibernate:TRUE休眠 FALSE睡眠
- 返回值:成功返回TRUE,否则返回FALSE
- */
- BOOL MySystemSleep(BOOL bHibernate = TRUE)//
- {
- HMODULE hModule = NULL;
- BOOL bRet = FALSE;
- // BOOLEAN WINAPI SetSuspendState(
- // __in BOOLEAN Hibernate,
- // __in BOOLEAN ForceCritical,
- // __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");//设置备份文件名函数指针
- if(pSetSuspendState != NULL)
- {
- bRet = pSetSuspendState(bHibernate,0,0);
- }
- FreeLibrary(hModule);
- }
- return bRet;
- }
程序源码连接:http://download.csdn.net/detail/daiafei/8673013
http://blog.csdn.net/daiafei/article/details/45562937