如何关闭 Windows 2000?(100分)

  • 主题发起人 主题发起人 hding
  • 开始时间 开始时间
H

hding

Unregistered / Unconfirmed
GUEST, unregistred user!
[?]<br>ExitWindowsEx()只对Windows 9x有效,在Windows NT4/Windows 2000上无效?<br>谁有高招赐教?<br>谢谢!<br>
 
searching (2000-1-7 19:58:19) &nbsp;<br>搞定了,是从MSDN中抄的,只不过原来的是用VB写的<br>我改成了Delphi的。我的系统是NT,满足你的要求。<br>但是不知道在98侠好不好用,你试试。<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br><br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; procedure AdjustToken;<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.AdjustToken();<br>var<br>&nbsp; hdlProcessHandle : Cardinal;<br>&nbsp; hdlTokenHandle : Cardinal;<br>&nbsp; tmpLuid : Int64;<br>&nbsp; tkpPrivilegeCount : Int64;<br>&nbsp; tkp : TOKEN_PRIVILEGES;<br>&nbsp; tkpNewButIgnored : TOKEN_PRIVILEGES;<br>&nbsp; lBufferNeeded : Cardinal;<br>&nbsp; Privilege : array[0..0] of _LUID_AND_ATTRIBUTES;<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hdlProcessHandle := GetCurrentProcess;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;OpenProcessToken(hdlProcessHandle,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hdlTokenHandle);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Get the LUID for shutdown privilege.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Privilege[0].Luid := tmpLuid;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tkp.PrivilegeCount := 1; &nbsp; // One privilege to set<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tkp.Privileges[0] := Privilege[0];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Enable the shutdown privilege in the access token of this<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// process.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AdjustTokenPrivileges(hdlTokenHandle,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;False,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tkp,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Sizeof(tkpNewButIgnored),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tkpNewButIgnored,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lBufferNeeded);<br><br>&nbsp;end;<br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; &nbsp; &nbsp;AdjustToken;<br>&nbsp; &nbsp; &nbsp;ExitWindowsEx((EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT), $FFFF);<br>end;<br><br>end.<br>&nbsp;<br>
 
转贴<br>WinExitInNT(EWX_SHUTDOWN+EWX_POWEROFF+EWX_FORCE);<br><br>function SetPrivilege(sPrivilegeName: string; bEnabled: boolean ): boolean;<br>var<br>&nbsp; TPPrev,TP : TTokenPrivileges;<br>&nbsp; Token : THandle;<br>&nbsp; dwRetLen : DWord;<br>begin<br>&nbsp; Result := False;<br><br>&nbsp; OpenProcessToken(<br>&nbsp; &nbsp; &nbsp;GetCurrentProcess, //handle to process<br>&nbsp; &nbsp; &nbsp;TOKEN_ADJUST_PRIVILEGES //Required to change the privileges specified in an access token.<br>&nbsp; &nbsp; &nbsp;or TOKEN_QUERY, //Required to query the contents of an access token.<br>&nbsp; &nbsp; &nbsp;Token);<br><br>&nbsp; TP.PrivilegeCount := 1;<br>&nbsp; //retrieves the locally unique identifier (LUID) used on a specified system to<br>&nbsp; //locally represent the specified privilege name.<br>&nbsp; if( LookupPrivilegeValue(<br>&nbsp; &nbsp; &nbsp; Nil, //attempts to find the privilege name on the local system.<br>&nbsp; &nbsp; &nbsp; PChar( sPrivilegeName ), // address of string specifying the privilege<br>&nbsp; &nbsp; &nbsp; TP.Privileges[ 0 ].LUID ) // address of locally unique identifier<br>&nbsp; &nbsp; &nbsp; )then<br>&nbsp; begin<br>&nbsp; &nbsp; if( bEnabled )then //Give this privileges<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;TP.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else begin //NOT Give this privileges<br>&nbsp; &nbsp; &nbsp; &nbsp;TP.Privileges[ 0 ].Attributes := 0;<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; dwRetLen := 0;<br>&nbsp; &nbsp; //enables or disables privileges in the specified access token.<br>&nbsp; &nbsp; Result := AdjustTokenPrivileges(<br>&nbsp; &nbsp; &nbsp; &nbsp; Token, // handle to token that contains privileges<br>&nbsp; &nbsp; &nbsp; &nbsp; False, //modifies privileges<br>&nbsp; &nbsp; &nbsp; &nbsp; TP, // pointer to new privilege information<br>&nbsp; &nbsp; &nbsp; &nbsp; SizeOf( TPPrev ), // size, in bytes, of the TPPrev buffer<br>&nbsp; &nbsp; &nbsp; &nbsp; TPPrev, // receives original state of changed privileges<br>&nbsp; &nbsp; &nbsp; &nbsp; dwRetLen // receives required size of the TPPrev buffer<br>&nbsp; &nbsp; &nbsp; &nbsp; );<br>&nbsp; end;<br>&nbsp; CloseHandle( Token );<br>end;<br><br>//iFlags:<br>//下面情况必须被指定<br>// EWX_LOGOFF<br>// EWX_REBOOT<br>// EWX_SHUTDOWN<br>// 下面可以组合使用<br>// EWX_POWEROFF<br>// EWX_FORCE : terminate processes<br>function WinExitInNT( iFlags : integer ) : boolean;<br>begin<br>&nbsp; Result := True;<br>&nbsp; if( SetPrivilege('SeShutdownPrivilege', True ) )then<br>&nbsp; begin<br>&nbsp; &nbsp; if( not ExitWindowsEx( iFlags, 0 ) )then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;Result := False;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; SetPrivilege('SeShutdownPrivilege', False )<br>&nbsp; end<br>&nbsp; else begin<br>&nbsp; &nbsp; &nbsp; &nbsp;// handle errors...<br>&nbsp; &nbsp; &nbsp; &nbsp;Result := False;<br>&nbsp; end;<br>end;
 
多人接受答案了。
 

Similar threads

回复
0
查看
995
不得闲
S
回复
0
查看
782
SUNSTONE的Delphi笔记
S
S
回复
0
查看
694
SUNSTONE的Delphi笔记
S
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
后退
顶部