我作这过这个,可在2000下关机
void CCMPCtrlDlg::ShutDownComputer()
{
HANDLE hToken; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure
if( !OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,
&hToken) )
{
// 说明运行于98模式下
if( ExitWindowsEx(EWX_SHUTDOWN,0) != 0 )
EndDialog(IDOK);
return;
}
//下面的代码是用于2000下的
//修改权限
// 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 can't be tested
//
if (GetLastError() != ERROR_SUCCESS)
{
MessageBox("Adjuste error! ");
return;
}
if( ExitWindowsEx(EWX_SHUTDOWN,0) == 0 )
return;
else
EndDialog(IDOK);
}