程序必须拥有SE_SHUTDOWN_NAME权限才能调用关闭函数 HANDLE hToken; TOKEN_PRIVILEGES tkp; // Get a token for this process. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) Error("OpenProcessToken"); // Get the LUID for the shutdown privilege. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid); tkp.PrivilegeCount = 1; // one privilege to set tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; // Get the shutdown privilege for this process. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0); // Cannot test the return value of AdjustTokenPrivileges. if (GetLastError() != ERROR_SUCCESS) error("AdjustTokenPrivileges"); // Shut down the system and force all applications to close. if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0)) error("ExitWindowsEx");
//取得当前进程的标记 if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) ::AfxMessageBox("OpenProcessToken"); // Get the LUID for the shutdown privilege. //寻找关闭系统的特权并赋值给变量TOKEN_PRIVILEGES LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid); //设置特权 tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; // Get the shutdown privilege for this process. //为当前的进程取得关闭系统的特权 AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES)NULL, 0); // Cannot test the return value of AdjustTokenPrivileges. if (GetLastError() != ERROR_SUCCESS) // ::AfxMessageBox("AdjustTokenPrivileges"); // 关闭系统 if (!ExitWindowsEx(EWX_POWEROFF|EWX_FORCE,0L)) { if(!ExitWindowsEx(EWX_REBOOT|EWX_FORCE,0L)) { if(!ExitWindowsEx(EWX_SHUTDOWN|EWX_FORCE,0L)) { if(!ExitWindows(0,0)) ::AfxMessageBox("重启动错误"); } } }
//取得当前进程的标记 if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) ::AfxMessageBox("OpenProcessToken"); // Get the LUID for the shutdown privilege. //寻找关闭系统的特权并赋值给变量TOKEN_PRIVILEGES LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid); //设置特权 tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; // Get the shutdown privilege for this process. //为当前的进程取得关闭系统的特权 AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES)NULL, 0); // Cannot test the return value of AdjustTokenPrivileges. if (GetLastError() != ERROR_SUCCESS) // ::AfxMessageBox("AdjustTokenPrivileges"); // 关闭系统 if (!ExitWindowsEx(EWX_POWEROFF|EWX_FORCE,0L)) { if(!ExitWindowsEx(EWX_REBOOT|EWX_FORCE,0L)) { if(!ExitWindowsEx(EWX_SHUTDOWN|EWX_FORCE,0L)) { if(!ExitWindows(0,0)) ::AfxMessageBox("重启动错误"); } } }
HANDLE hToken;
TOKEN_PRIVILEGES tkp; // Get a token for this process. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
Error("OpenProcessToken"); // Get the LUID for the shutdown privilege. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid); tkp.PrivilegeCount = 1; // one privilege to set tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; // Get the shutdown privilege for this process. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0); // Cannot test the return value of AdjustTokenPrivileges. if (GetLastError() != ERROR_SUCCESS) error("AdjustTokenPrivileges"); // Shut down the system and force all applications to close. if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0)) error("ExitWindowsEx");
if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
::AfxMessageBox("OpenProcessToken");
// Get the LUID for the shutdown privilege.
//寻找关闭系统的特权并赋值给变量TOKEN_PRIVILEGES
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);
//设置特权
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Get the shutdown privilege for this process.
//为当前的进程取得关闭系统的特权
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES)NULL, 0);
// Cannot test the return value of AdjustTokenPrivileges.
if (GetLastError() != ERROR_SUCCESS)
// ::AfxMessageBox("AdjustTokenPrivileges");
// 关闭系统
if (!ExitWindowsEx(EWX_POWEROFF|EWX_FORCE,0L))
{
if(!ExitWindowsEx(EWX_REBOOT|EWX_FORCE,0L))
{
if(!ExitWindowsEx(EWX_SHUTDOWN|EWX_FORCE,0L))
{
if(!ExitWindows(0,0))
::AfxMessageBox("重启动错误");
}
}
}
if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
::AfxMessageBox("OpenProcessToken");
// Get the LUID for the shutdown privilege.
//寻找关闭系统的特权并赋值给变量TOKEN_PRIVILEGES
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);
//设置特权
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Get the shutdown privilege for this process.
//为当前的进程取得关闭系统的特权
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES)NULL, 0);
// Cannot test the return value of AdjustTokenPrivileges.
if (GetLastError() != ERROR_SUCCESS)
// ::AfxMessageBox("AdjustTokenPrivileges");
// 关闭系统
if (!ExitWindowsEx(EWX_POWEROFF|EWX_FORCE,0L))
{
if(!ExitWindowsEx(EWX_REBOOT|EWX_FORCE,0L))
{
if(!ExitWindowsEx(EWX_SHUTDOWN|EWX_FORCE,0L))
{
if(!ExitWindows(0,0))
::AfxMessageBox("重启动错误");
}
}
}