自设关机(ewx_shutdown)(50分)

  • 主题发起人 主题发起人 精诚
  • 开始时间 开始时间

精诚

Unregistered / Unconfirmed
GUEST, unregistred user!
&nbsp;procedure TForm1.Button1Click(Sender: TObject);<br>const<br>&nbsp; EWX_FORCE=4;<br>&nbsp; EWX_LOGOFF=0;<br>&nbsp; EWX_REBOOT=2;<br>&nbsp; EWX_SHUTDOWN=1;<br>begin<br>&nbsp; Exitwindowsex(how,1);<br>end;<br>前面的uses是否少了什么呢?
 
若是Windows NT系列还要设置特权才能关机
 
如何在程序中关闭windows<br>bool exitwindowsex( &nbsp;unit uFlags, &nbsp;DWORD dwReserved);<br>uFlags的值:<br>EWX_FORCE=4;//以其他用户身份登陆<br>EWX_LOGOFF=0;//重启,并切换到MS-DOS方式<br>EWX_REBOOT=2;//重启<br>EWX_SHUTDOWN=1;//关闭计算机<br>dwReserved一般为0 BOOL ExitWindowsEx( &nbsp;UINT uFlags, &nbsp;DWORD dwReserved );<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 值 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 含义<br>&nbsp; &nbsp;uFlags &nbsp; &nbsp;EWX_LOGOFF &nbsp; &nbsp;当前用户退出登陆<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;EWX_POWEROFF &nbsp;关掉电源,需主板支持 &nbsp; *<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;EWX_REBOOT &nbsp; &nbsp;重启 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;EWX_SHUTDOWN &nbsp;关机 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;EWX_FORCE &nbsp; &nbsp; 中断进程<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;EWX_FORCEIFHUNG &nbsp;中断进程(只用于2000)<br>&nbsp; &nbsp;凡是后面标*者,在NT下需先调用AdjustTokenPrivileges<br>设置SE_SHUTDOWN_NAME 的特
 
在2k和nt下要需要取得关机权限。下面一个可以,我试过了!<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>end.<br>
 
&nbsp;今天刚查到,也可以为:<br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; Exitwindowsex(EWX_LOGOFF,0);//可实现关闭所有程序,其它应该也类同。 <br>end;
 
后退
顶部