这里准备三个按钮的点击函数,可以在自己的工程里调用测试效果。
记录保存电脑全部任务。
- 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."));
- }
- }
复制代码
|