W
www
Unregistered / Unconfirmed
GUEST, unregistred user!
我用下面的代码在win2000下关机,为什么最后总是出现"现在可以安全的关闭计算机了"的提示,而不是直接关闭计算机电源???<br><br>unit guan;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> procedure shutdown;<br> procedure Button1Click(Sender: TObject);<br> private<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure Tform1.ShutDown;<br>const<br> SE_SHUTDOWN_NAME = 'SeShutdownPrivilege'; // Borland forgot this declaration<br>var<br> hToken : THandle;<br> tkp : TTokenPrivileges;<br> tkpo : TTokenPrivileges;<br> zero : DWORD;<br>begin<br> zero:=0;<br> if not OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then begin<br> MessageBox( 0, 'Exit Error', 'OpenProcessToken() Failed', MB_OK );<br> Exit;<br> end; // if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)<br> if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then begin<br> MessageBox( 0, 'Exit Error', 'OpenProcessToken() Failed', MB_OK );<br> Exit;<br> end; // if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)<br><br>// SE_SHUTDOWN_NAME<br> if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[ 0 ].Luid ) then begin<br> MessageBox( 0, 'Exit Error', 'LookupPrivilegeValue() Failed', MB_OK );<br> Exit;<br> end; // if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[0].Luid )<br> tkp.PrivilegeCount := 1;<br> tkp.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;<br> AdjustTokenPrivileges( hToken, False, tkp, SizeOf( TTokenPrivileges ), tkpo, zero );<br> if Boolean( GetLastError() ) then begin<br> MessageBox( 0, 'Exit Error', 'AdjustTokenPrivileges() Failed', MB_OK );<br> Exit;<br> end // if Boolean( GetLastError() )<br> else ExitWindowsEx( EWX_FORCE or EWX_SHUTDOWN, EWX_POWEROFF );<br> <br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> shutdown;<br>end;<br><br>end.<br>