请问:用什么函数或方法可实现关机?(25分)

  • 主题发起人 主题发起人 xuekunli
  • 开始时间 开始时间
X

xuekunli

Unregistered / Unconfirmed
GUEST, unregistred user!
请问:用什么函数或方法可实现关机?
 
请搜索已答问题,有很多解决方案。<br>例如<br>http://211.101.4.25/delphibbs/dispq.asp?lid=655879<br>http://211.101.4.25/delphibbs/dispq.asp?lid=651763
 
exitwindowsex(ewx_shutdown,0);
 
来自hubdog的‘未加证实的葵花宝典’<br><br>控制WINDOWS的开关:如关闭WINDOWS,重新启动WINDOWS等, ExitWindowsEx(UINT uFlags,DWORD dwReserved);是实现这一功能的API函数<br>首先定义常数<br>const<br>EWX_FORCE=4; //关闭所有程序并以其他用户身份登录<br>EWX_LOGOFF=0; //重新启动计算机并切换到MS-DOS方式<br>EWX_REBOOT=2; //重新启动计算机<br>EWX_SHUTDOWN=1;//关闭计算机<br>运行时给How赋值,让他等于EWX_SHUTDOWN或其他,调用以下语句<br>ExitWindowsEx(How,0);<br><br>///////////////////////////////////////////<br>Function ShutDownWindows(Flags: Byte) : Boolean;<br>begin<br>&nbsp; &nbsp; &nbsp;Result := ExitWindowsEx(Flage, 0)<br>&nbsp; &nbsp; &nbsp;// 如果成功返回 True.<br>end;<br><br>Flags可用参数:<br><br>EWX_LOGOFF:<br>&nbsp; (=0)<br>&nbsp; 安全地关闭所有进程,并关闭用户登录。<br><br>EWX_SHUTDOWN:<br>&nbsp; (=1)<br>&nbsp; 关闭系统。所有缓冲区的内容都能被安全的存盘,所有进程都将被停止。<br>&nbsp; 对于Windows NT: 必须有 SE_SHUTDOWN_NAME 的安全特权方可进行此项操作。<br>&nbsp; 对于Windows 95: 不必有任何特权。<br><br>EWX_REBOOT:<br>&nbsp; (=2)<br>&nbsp; 关闭并重新启动系统。<br>&nbsp; 对于Windows NT: 必须有 SE_SHUTDOWN_NAME 的安全特权方可进行此项操作。<br>&nbsp; 对于Windows 95: 不必有任何特权。<br><br>EWX_FORCE:<br>&nbsp; (=4)<br>&nbsp; &nbsp;强制切断连接,关闭所有应用程序。当使用这个参数时,Windows 将不向正在运行的应用程序发送 WM_QUERYENDSESSION 和 WM_ENDSESSION 消息,有可能造成数据丢失。所以推荐只在紧急时使用这个参数。<br><br>EWX_POWEROFF:<br>&nbsp; (=8)<br>&nbsp; 关闭系统并切断电源,需要ATX电源支持。<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>
 
呵呵,这么完善的,好啊
 
多人接受答案了。
 
后退
顶部