procedure ShutDown(Send: integer);<br> procedure AdjustToken(); //获取关机控制权<br> var<br> hdlProcessHandle, hdlTokenHandle, lBufferNeeded: Cardinal;<br> tmpLuid: Int64;<br> //tkpPrivilegeCount: Int64;<br> tkp, tkpNewButIgnored: TOKEN_PRIVILEGES;<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> // 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 process.<br> AdjustTokenPrivileges(hdlTokenHandle,<br> False,<br> tkp,<br> Sizeof(tkpNewButIgnored),<br> tkpNewButIgnored,<br> lBufferNeeded);<br> end;<br>begin<br> AdjustToken;<br> case send of<br> 1: ExitWindowsEx(EWX_FORCE, 0); //在紧急情况下强制关机。 ;<br> 2: ExitWindowsEx(EWX_LOGOFF, 0); //以其他用户身份登录。 ;<br> 3: ExitWindowsEx(EWX_POWEROFF, 0); //关闭系统并关闭电源。<br> 4: ExitWindowsEx(EWX_REBOOT, 0); //重启操作系统<br> 5: ExitWindowsEx(EWX_SHUTDOWN, 0); //关机;<br> else<br> EXIT;<br> end;<br>end;