Windows 2000下如何在自己的程序中实现关机!
if(m_nReboot==0)
{
if(ExitWindowsEx(EWX_REBOOT, 0)) goto Exit;
if(ExitWindowsEx(EWX_REBOOT | EWX_FORCE, 0)) goto Exit;
//因为上面的重起不成功,所以需要调整本进程的特权
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken)) goto Exit;
//获取重起的特权SE_SHUTDOWN_NAME
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 (!ExitWindowsEx(EWX_REBOOT, 0))
ExitWindowsEx(EWX_REBOOT | EWX_FORCE, 0);
}
Exit:
CDialog::OnClose();
{
if(ExitWindowsEx(EWX_REBOOT, 0)) goto Exit;
if(ExitWindowsEx(EWX_REBOOT | EWX_FORCE, 0)) goto Exit;
//因为上面的重起不成功,所以需要调整本进程的特权
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken)) goto Exit;
//获取重起的特权SE_SHUTDOWN_NAME
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 (!ExitWindowsEx(EWX_REBOOT, 0))
ExitWindowsEx(EWX_REBOOT | EWX_FORCE, 0);
}
Exit:
CDialog::OnClose();
我最擅长从零开始创造世界,所以从来不怕失败,它最多也就让我一无所有。