1、记得要加入头文件:#include
2、代码
bool MainForm::shutDownSystem()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
//获取进程标志
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return false;
//获取关机特权的LUID
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
//获取这个进程的关机特权
AdjustTokenPrivileges(hToken, false, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
if (GetLastError() != ERROR_SUCCESS) return false;
// 强制关闭计算机
//if ( !ExitWindow***(EWX_SHUTDOWN | EWX_FORCE,
// SHTDN_REASON_MAJOR_OPERATINGSYSTEM | SHTDN_REASON_MINOR_UPGRADE | SHTDN_REASON_FLAG_PLANNED))
if ( !ExitWindow***(EWX_REBOOT | EWX_FORCE, 0))
return false;
return true;
}
阅读(3679) | 评论(2) | 转发(1) |