请问如何通过编程关机?(100分)

  • 主题发起人 主题发起人 chen_ht
  • 开始时间 开始时间
C

chen_ht

Unregistered / Unconfirmed
GUEST, unregistred user!
在WINDOWS XP 系统下,如何通过编程关机,<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>
 
网上找来的,没试过。<br><br>function ShutMeDown:string;<br>var<br>&nbsp; &nbsp;hToken : THandle;<br>&nbsp; &nbsp;tkp,p &nbsp;: TTokenPrivileges;<br>&nbsp; &nbsp;RetLen : DWord;<br>&nbsp; &nbsp;ExReply: LongBool;<br>&nbsp; &nbsp;Reply &nbsp;: DWord;<br>begin<br>case Win32Platform of<br>&nbsp; &nbsp; &nbsp; &nbsp; VER_PLATFORM_WIN32_WINDOWS: //***Windows 9x/ME***//<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; ExReply:= ExitWindowsEx(EWX_POWEROFF or EWX_SHUTDOWN or EWX_FORCE,<br>0);<br>&nbsp; &nbsp; &nbsp; &nbsp; if ExReply then<br>&nbsp; &nbsp; &nbsp; &nbsp; Result:='Shutdown Initiated'<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; Result:='Shutdown failed with ' + IntToStr(GetLastError);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; VER_PLATFORM_WIN32_NT: &nbsp;//***Windows NT/2000/XP***//<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or<br>TOKEN_QUERY,hToken) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if<br>LookupPrivilegeValue(nil,'SeShutdownPrivilege',tkp.Privileges[0].Luid) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tkp.PrivilegeCount := 1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tkp.Privileges[0].Attributes :=<br>SE_PRIVILEGE_ENABLED;<br><br>AdjustTokenPrivileges(hToken,False,tkp,SizeOf(TTokenPrivileges),p,RetLen);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Reply := GetLastError;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Reply = ERROR_SUCCESS then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ExReply:= ExitWindowsEx(EWX_POWEROFF or EWX_FORCE, 0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ExReply then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result:='Shutdown Initiated'<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result:='Shutdown failed with ' + IntToStr(GetLastError);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>end; //end case<br>end;<br>end;<br>
 
要关闭xp 需要有关机特权,,,<br>&nbsp; &nbsp;先用openprcesstoken打开令牌,,然后才能访问,用LookupPrivilegeValue找出令牌特权<br>的luid,然后再设置特权属性,,用AdjustTokenPrivileges后,,就可以用exitwindowsex关机<br>了..至于这几个API 函数楼上的几位DX已经写的很清楚了...<br>也就是说对于这种像win2000和xp这样的有安全的系统,,首先要取得特权...
 
<br>ExitWindowsEx(关闭类型参数,系统保留参数);<br><br>例:exitwindowsex(ewx_shutdown,0); //关闭Windows<br><br>  其中,系统保留参数无特定意义,一般写0即可;关闭类型可以是以下几种:<br><br>  EWX_FORCE:强制关闭,Windows不会发送任何消息给正运行的程序,这可能导致数据丢失;<br><br>  EWX_LOGOFF:关闭所有正在运行的程序,注销当前用户并重新登录;<br><br>  EWX_POWEROFF:关闭Windows并关机,当然,系统必须支持电源管理;<br><br>  EWX_REBOOT:关闭Windows并重新启动;<br><br>  EWX_SHUTDOWN:关闭Windows,缓冲区内的数据将被写入磁盘。
 
To chenzheng770101:<br>&nbsp; &nbsp; 请问procedure TForm1.AdjustToken();是用来做什么的?
 
ExitWindowsEx(1, 0)
 
》procedure TForm1.AdjustToken();<br>在NT下取得关机的权限吧。
 
后退
顶部