program myreboot; <br><br>{$APPTYPE CONSOLE} <br>uses <br> SysUtils,Windows; <br><br>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>procedure AdjustToken;//改变进程权限 <br>var <br> hdlProcessHandle : Cardinal; <br> hdlTokenHandle : Cardinal; <br> tmpLuid : Int64; <br> tkpPrivilegeCount : Int64; <br> tkp : TOKEN_PRIVILEGES; <br> tkpNewButIgnored : TOKEN_PRIVILEGES; <br> lBufferNeeded : Cardinal; <br> Privilege : array[0..0] of _LUID_AND_ATTRIBUTES; <br>begin <br> hdlProcessHandle := GetCurrentProcess; <br> OpenProcessToken(hdlProcessHandle, <br> (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), <br> hdlTokenHandle); <br><br> // Get the LUID for shutdown privilege. <br> LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid); <br> Privilege[0].Luid := tmpLuid; <br> Privilege[0].Attributes := SE_PRIVILEGE_ENABLED; <br> tkp.PrivilegeCount := 1; // One privilege to set <br> tkp.Privileges[0] := Privilege[0]; <br> // Enable the shutdown privilege in the access token of this <br> // process. <br> AdjustTokenPrivileges(hdlTokenHandle, <br> False, <br> tkp, <br> Sizeof(tkpNewButIgnored), <br> tkpNewButIgnored, <br> lBufferNeeded); <br><br>end; <br><br><br>begin <br> { TODO -oUser -cConsole Main : Insert code here } <br> AdjustToken;//改变进程权限 <br> ExitWindowsEx((EWX_REBOOT OR EWX_FORCE), $FFFF); <br>// My_ExitWindows(1); <br>end.