工控编程吧
标题:
上位机MFC如何注销关闭重启系统
[打印本页]
作者:
qq263946146
时间:
2019-8-12 10:33
标题:
上位机MFC如何注销关闭重启系统
这里准备三个按钮的点击函数,可以在自己的工程里调用测试效果。
记录保存电脑全部任务。
void CDemoDlg::OnTest1()
{
if (IDNO == MessageBox(_T("是否注销系统?"), _T("Demo"), MB_YESNO))
{
return;
}
//打开进程令牌
HANDLE hToken;
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
AfxMessageBox(_T("OpenProcessToken Error."));
}
//获得LUID
TOKEN_PRIVILEGES tkp;
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)
{
AfxMessageBox(_T("AdjustTokenPrivileges Error."));
}
//注销系统
if (!ExitWindowsEx(EWX_LOGOFF, 0))
{
AfxMessageBox(_T("ExitWindowsEx Error."));
}
}
void CDemoDlg::OnTest2()
{
if (IDNO == MessageBox(_T("是否关闭系统?"), _T("Demo"), MB_YESNO))
{
return;
}
//打开进程令牌
HANDLE hToken;
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
AfxMessageBox(_T("OpenProcessToken Error."));
}
//获得LUID
TOKEN_PRIVILEGES tkp;
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)
{
AfxMessageBox(_T("AdjustTokenPrivileges Error."));
}
//关闭系统
if (!ExitWindowsEx(EWX_SHUTDOWN, 0))
{
AfxMessageBox(_T("ExitWindowsEx Error."));
}
}
void CDemoDlg::OnTest3()
{
if (IDNO == MessageBox(_T("是否重启系统?"), _T("Demo"), MB_YESNO))
{
return;
}
//打开进程令牌
HANDLE hToken;
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
AfxMessageBox(_T("OpenProcessToken Error."));
}
//获得LUID
TOKEN_PRIVILEGES tkp;
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)
{
AfxMessageBox(_T("AdjustTokenPrivileges Error."));
}
//重启系统
if (!ExitWindowsEx(EWX_REBOOT, 0))
{
AfxMessageBox(_T("ExitWindowsEx Error."));
}
}
复制代码
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4