如何在vc++中延迟windows 7或windows 8的关机/重启/休眠
How to delay shutdown/restart/hibernate in windows 7 or windows 8 in VC++
我使用的是基于vs2012的应用程序,使用c++开发。
如果应用程序正在运行并做一些处理,并且用户触发重启/关机或休眠,那么重启/关机应该暂停,直到应用程序处理完成。
一旦应用程序处理完成,windows应该重新启动/关闭。
如果可以提供一些参考或示例代码,我将非常感激
谢谢
我做了这样的事情
bool shutdownSystemWin(EShutdownActions action)
{
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;
// Shut down the system and force all applications to close.
UINT flags = EWX_FORCE;
switch (action)
{
case EShutdownActions::Shutdown:
flags |= EWX_SHUTDOWN;
break;
case EShutdownActions::PowerOff:
flags |= EWX_POWEROFF;
break;
case EShutdownActions::SuspendToRAM:
return SetSuspendState(FALSE, FALSE, FALSE);
case EShutdownActions::Hibernate:
return SetSuspendState(TRUE, FALSE, FALSE);
break;
case EShutdownActions::Reboot:
flags |= EWX_REBOOT;
break;
case EShutdownActions::LogOff:
flags |= EWX_LOGOFF;
break;
}
if (!ExitWindowsEx(flags,
SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
SHTDN_REASON_MINOR_UPGRADE |
SHTDN_REASON_FLAG_PLANNED))
return false;
//shutdown was successful
return true;
}
相关文章:
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 在Linux for Windows上编译C++代码时出错
- 在Windows上用C++裁剪HBITMAP
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 当我编译webrtc服务器时,Windows上只支持clang-cl
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- Windows/Cygwin - 不能使用 pybind11 - 犯错误
- Windows 10-使用gtkmm-3.0库和g++[包括再现]的分段故障
- 在Windows中以.exe的形式运行c++
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 如何跟踪Windows 7是否在C ++中以编程方式进入睡眠或休眠状态,就像在Windows XP中所做的那样
- Windows 是进入睡眠模式还是C++休眠
- 休眠失败时的Windows通知
- 如何发现休眠是否适用于Windows用户(使用c++)
- Windows在运行一个很长的c++ Visual Studio程序时处于休眠状态
- 如何在用户登录/关闭或PC进入睡眠/休眠时正确通知c++ Windows服务
- 如何在vc++中延迟windows 7或windows 8的关机/重启/休眠
- Windows线程从另一个线程休眠和唤醒