HANDLE hToken; // handle to process token TOKEN_PRIVILEGES tkp; // pointer to token structure if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken)) { ExitWindowsEx(EWX_SHUTDOWN,0); } // Get the LUID for 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 shutdown privilege for this process. AdjustTokenPrivileges(hToken,FALSE,&tkp,0,(PTOKEN_PRIVILEGES)NULL,0); // // The return value of AdjustTokenPrivileges cant be tested // if(GetLastError()!=ERROR_SUCCESS) { MessageBox("Adjuste error!"); } ExitWindowsEx(EWX_SHUTDOWN,0);
/***************************************************************************/
//关机预处理,Window2000以上的操作系统通常需要这一过程
void PreProcess()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken);
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);
}
/***************************************************************************/
//关闭计算机
void CloseComputer()
{
PreProcess();
ExitWindowsEx(EWX_SHUTDOWN|EWX_FORCE,0);
}
/***************************************************************************/
TOKEN_PRIVILEGES tkp; // pointer to token structure
if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken))
{
ExitWindowsEx(EWX_SHUTDOWN,0);
}
// Get the LUID for 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 shutdown privilege for this process.
AdjustTokenPrivileges(hToken,FALSE,&tkp,0,(PTOKEN_PRIVILEGES)NULL,0);
//
// The return value of AdjustTokenPrivileges cant be tested
//
if(GetLastError()!=ERROR_SUCCESS)
{
MessageBox("Adjuste error!");
}
ExitWindowsEx(EWX_SHUTDOWN,0);