-----以下代码在D5+WIN2000通过。<br><br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;<br><br>type<br> TForm1 = class(TForm)<br> procedure FormCreate(Sender: TObject);<br> private<br>function SetPrivilege (sPrivilegeName: string; bEnabled: Boolean) : Boolean;<br><br> { Private declarations }<br> public<br>function WinExit(iFlags: integer):Boolean;<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br><br>function TForm1.SetPrivilege(sPrivilegeName: string;<br> bEnabled: Boolean): Boolean;<br><br>var<br> TPPrev,<br> TP : TTokenPrivileges;<br> Token :THandle;<br> dwRetLen : DWORD;<br>begin<br> result := False;<br> OpenProcessToken (GetCurrentProcess,<br> TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,<br> Token);<br> TP.PrivilegeCount := 1;<br> if LookupPrivilegeValue (nil, PChar (sPrivilegeName), TP.Privileges[0].LUID) then<br> begin<br> if bEnabled then<br> TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED<br> else<br> TP.Privileges[0].Attributes := 0;<br> dwRetLen := 0;<br> result := AdjustTokenPrivileges (<br> Token,<br> False,<br> TP,<br> SizeOf (TPPrev),<br> TPPrev,<br> dwRetLen);<br> end;<br> CloseHandle (Token);<br>end;<br>function Tform1.WinExit (iFlags: integer) : Boolean;<br><br>begin<br> result := true;<br> if SetPrivilege ('SeShutdownPrivilege', true) then<br> begin<br> if (not ExitWindowsEx (iFlags, 0)) then<br> begin<br> // handle errors...<br> result := False<br> end;<br> SetPrivilege ('SeShutdownPrivilege', False)<br> end<br> else<br> begin<br> // handle errors...<br> result := False<br> end;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> WinExit(EWX_POWEROFF + EWX_FORCE);//在这里控制是关机、重启等。<br>end;<br><br>end.