如何编程安全关机(100分)

  • 主题发起人 主题发起人 wzpok
  • 开始时间 开始时间
W

wzpok

Unregistered / Unconfirmed
GUEST, unregistred user!
我编一个程序希望到一定时间,在没有人控制下关闭所有正在运行的程序,关机,为此我调用ExitWindowsEx API函数来实现,但有很多问题。1。如果有DOS程序在运行,系统不会关机他回出一提示框要求关掉DOS程序才能关机。2。如果有DELPHI等程序运行,也不会关机,因为他要询问你是否存盘,。后来我用POSTSSAGE向所用窗口发WM_QUIT消息也不能,系统回死机。如何才能编程实现在没有人控制下关闭所有正在运行的程序,关机。<br>
 
调 用 API函 数 ExitWindowsEx可 以 首 先 重 新 驱 动 和 关 机 。 ExitWindowsEx定 义 如 下 : <br>&nbsp; &nbsp; BOOL ExitWindowsEx( <br>&nbsp; &nbsp; &nbsp;UINT uFlags, // shutdown operation <br>&nbsp; &nbsp; &nbsp;DWORD dwReserved // reserved <br>&nbsp; &nbsp; &nbsp;); <br>&nbsp; &nbsp; 其 中 uFlags菜 单 决 定 关 机 的 方 式 , EWX_REBOOT表 示 重 新 启 动 , EWX_SHUTDOWN表 示 关 机 , EWX_POWEROFF表 示 关 闭 电 源 ( 主 板 需 为 ATX结 构 ) 。 <br>&nbsp; &nbsp; 如 果 是 在 Windows NT下 , 需 要 修 改 优 先 级 , 修 改 的 方 法 参 考 : 微 软 的 Knowledge Base的 文 章:“Q161136 HOWTO: Shutdown Windows NT, Windows 95, and 98 from VB Code”
 
对不起,没有仔细看你的问题,没注意又那么多问题要考虑。等我再试试。不好意思。
 
&nbsp;没关系,谢谢你回答
 
搞定了,是从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>
 
wzpok:<br>刚刚试过,在98下也好用。不过我做的是重启。对你来说有用的是那个AdjustToken<br>函数,是吧?
 
强行关机?
 
好象我问过类似问题,找找看。在编程手记还是啥地方?
 
searching <br>&nbsp; &nbsp; &nbsp; 真是太感谢你的回答了?能告诉我是在msdn的什么的方找到的吗?
 
盗版光盘到处可以找到的,你要是支持正版久没有办法了,很大n张光盘
 
不好意思,看错了。可是我不记得当时是搜索什么关键词搜出来的了。<br>反正相关的词语你都可以搜一下。不好意思
 
&nbsp; &nbsp; 谢谢!你很热心
 
searching<br>还是老问题。如果有DOS程序在运行,系统不会关机他回出一提示框要求关掉DOS程序才能关机。2。如果有DELPHI等程序运行且没有存盘,也不会关机,因为他要询问你是否存盘<br>你的方法也不行,怎么办?
 
后退
顶部