ExitWindowsEx这个api在NT和2000下不能用,嘿嘿,剽窃一段代码给你看看
function My_ExitWindows(M_1Reboot_2Power: integer): Boolean; //重启(M:=1).关机(M:=2)
var
hToken: THANDLE;
hProc: THANDLE;
mLUID: TLargeInteger;
mPriv, mNewPriv: TOKEN_PRIVILEGES;
mBufferLength: DWord;
begin
Result:=false;
if not (M_1Reboot_2Power in [1, 2]) then exit;
hProc := GetCurrentProcess();
OpenProcessToken(hProc, TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, hToken);
LookupPrivilegeValue('', 'SeShutdownPrivilege', mLUID);
mPriv.PrivilegeCount := 1;
mPriv.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
mPriv.Privileges[0].Luid := mLUID;
AdjustTokenPrivileges(hToken, False, mPriv, (4 + (12 * mPriv.PrivilegeCount)), mNewPriv, mBufferLength);
GetLastError;
case M_1Reboot_2Power of
1: Result := ExitWindowsEx(EWX_FORCE+EWX_REBOOT,0);
2: Result := ExitWindowsEx(EWX_FORCE+EWX_POWEROFF, 0);
end;
end;
其实简单地用ExitWindowsEx(EWX_FORCE+EWX_REBOOT,0);也可以重启,但有时候在系统还没有关闭时你的程序就已经关闭了所以必需用这个方法。
这个方法可以用于WIN9X、WINNT、WIN2000。