VC---強制重啓電腦的代碼
BOOL CTestAllView::OnFshutdown()
{
// TODO: Add your control notification handler code here
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
// Get a token for this process.
// OpenProcessToken() 這個函數的作用是打開一個進程的訪問令牌
// GetCurrentProcess() 函數的作用是得到本進程的句柄
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,&hToken))
return( FALSE );
// Get the LUID for the shutdown privilege.
// LookupPrivilegeValue() 的作用是修改進程的權限
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()的作用是通知Windows NT修改本進程的權利
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);
if (GetLastError() != ERROR_SUCCESS)
return FALSE;
// Shut down the system and force all applications to close.
if (!ExitWindowsEx(EWX_POWEROFF | EWX_FORCE, 0)) //參數在這裏設置。 強行退出WINDOWS。
return FALSE;
return TRUE;
}
{
// TODO: Add your control notification handler code here
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
// Get a token for this process.
// OpenProcessToken() 這個函數的作用是打開一個進程的訪問令牌
// GetCurrentProcess() 函數的作用是得到本進程的句柄
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,&hToken))
return( FALSE );
// Get the LUID for the shutdown privilege.
// LookupPrivilegeValue() 的作用是修改進程的權限
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()的作用是通知Windows NT修改本進程的權利
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);
if (GetLastError() != ERROR_SUCCESS)
return FALSE;
// Shut down the system and force all applications to close.
if (!ExitWindowsEx(EWX_POWEROFF | EWX_FORCE, 0)) //參數在這裏設置。 強行退出WINDOWS。
return FALSE;
return TRUE;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.