follow my heart...
分类: C/C++
2006-09-12 13:30:13
[点评:在win95/98下面,直接用exitwindowsex就可完成。但是在2k/XP下面,你必须获得底层特权,才可以完成关机操作。这也是2K比较安全的表现。]
//在WINDOWS2K/XP下实现计算机的关机操作
//必须要有SE_SHUTDOWN_NAME,不像在WIN9X中直接用ExitWindowsEx即可
//这种方法是直接将系统做为WinNT执行
HANDLE hProcess, hToken;
TOKEN_PRIVILEGES NewState;
DWORD ProcessId, ReturnLength = 0;
LUID luidPrivilegeLUID;
ProcessId = GetCurrentProcessId();
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessId);
if(!OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES, &hToken))
//return 0;
ShowMessage("ok");
if(!LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &luidPrivilegeLUID))
//return 0;
ShowMessage("ok");
NewState.PrivilegeCount = 1;
NewState.Privileges[0].Luid = luidPrivilegeLUID;
NewState.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if(AdjustTokenPrivileges(hToken, FALSE, &NewState, NULL, NULL, NULL))
ExitWindowsEx(EWX_FORCE | EWX_POWEROFF, 0);
//return 0;
ShowMessage("ok");
//当然,在一些情况下,应该选判断操作系统是WinNT还是Win9x
long lngVersion;
lngVersion=GetVersion();
if(lngVersion & 0x80000000)
xt=0;//是Win9x
else
xt=1;//是WinNT