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