那个函数可以实现在windows 2000下关机?(50分)

  • 主题发起人 主题发起人 陈晨
  • 开始时间 开始时间

陈晨

Unregistered / Unconfirmed
GUEST, unregistred user!
那个函数可以用在2000下关机?
 
procedure TForm1.FormClose(Sender:TObject;var Action:TCloseAction);<br>begin<br>&nbsp; ExitWindowsEx(EWX_SHUTDOWN,0);<br>end;<br><br>另外:EWX_FORMCE:强制终止没有响应的进程<br>&nbsp; &nbsp; &nbsp; EWX_LOGOFF:终止进程,然后注销<br>&nbsp; &nbsp; &nbsp; EWX_EWX_REBOOT:重新引导系统<br>
 
ExitWindowsEx(EWX_SHUTDOWN,0);在2000下可用吗?<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;
 
楼上说得没错,ExitWindowsEx(EWX_SHUTDOWN,0);是关机函数,但在NT(包括2000)下<br>要取得关机的权限才行。<br>
 
在 2000 下用,需要多一个步骤,先:AdjustTokenPrivileges
 
多人接受答案了。
 
后退
顶部