void CDemoDlg::OnBnClickedButtonLogoff()
{
// TODO: 在此添加控件通知处理程序代码
if(IDNO == MessageBox(_T("肯定要注销吗?"),_T("Demo"),MB_YESNO))
{
return;
}
//打开进程令牌
HANDLE hToken;
if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken))
{
AfxMessageBox(_T("OpenProcessToken Error."));
return;
}
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."));
return;
}
//AfxMessageBox(_T("提权成功"));
if(!ExitWindowsEx(EWX_LOGOFF,0))
{
AfxMessageBox(_T("ExitWindowsEx Error."));
}
}
void CDemoDlg::OnBnClickedButtonShutdown()
{
// TODO: 在此添加控件通知处理程序代码
if(IDNO == MessageBox(_T("肯定要关机吗?"),_T("Demo"),MB_YESNO))
{
return;
}
//打开进程令牌
HANDLE hToken;
if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken))
{
AfxMessageBox(_T("OpenProcessToken Error."));
return;
}
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."));
return;
}
//AfxMessageBox(_T("提权成功"));
if(!ExitWindowsEx(EWX_SHUTDOWN,0))
{
AfxMessageBox(_T("ExitWindowsEx Error."));
}
}
void CDemoDlg::OnBnClickedButtonReboot()
{
// TODO: 在此添加控件通知处理程序代码
if(IDNO == MessageBox(_T("肯定要重启吗?"),_T("Demo"),MB_YESNO))
{
return;
}
//打开进程令牌
HANDLE hToken;
if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken))
{
AfxMessageBox(_T("OpenProcessToken Error."));
return;
}
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."));
return;
}
//AfxMessageBox(_T("提权成功"));
if(!ExitWindowsEx(EWX_REBOOT,0))
{
AfxMessageBox(_T("ExitWindowsEx Error."));
}
}