在应用程序中如何增加‘计算机重启’的功能啊? ( 积分: 30 )

  • 主题发起人 主题发起人 shanliqun
  • 开始时间 开始时间
S

shanliqun

Unregistered / Unconfirmed
GUEST, unregistred user!
我现在的系统是win2000的,在我的应用程序中增加了‘关闭计算机’和‘重启计算机’的功能,但在编译环境下这两个功能是可用的,但当只运行exe时,那两个功能就不起作用了。这个关机和重启一定要和编译环境绑定吗?是不是要把系统的什么.DLL拷贝到我应用程序所在的文件夹啊? <br>我程序中的相关代码如下:<br>procedure Tfrm_posmain.ShutDown;<br> &nbsp;procedure AdjustToken(); //获取关机控制权<br> &nbsp;var<br> &nbsp; &nbsp;hdlProcessHandle, hdlTokenHandle, lBufferNeeded: Cardinal;<br> &nbsp; &nbsp;tmpLuid: Int64;<br> &nbsp;//tkpPrivilegeCount: Int64;<br> &nbsp; &nbsp;tkp, tkpNewButIgnored: TOKEN_PRIVILEGES;<br> &nbsp; &nbsp;Privilege: array[0..0] of _LUID_AND_ATTRIBUTES;<br> &nbsp;begin<br> &nbsp; &nbsp;hdlProcessHandle := GetCurrentProcess;<br> &nbsp; &nbsp;OpenProcessToken(hdlProcessHandle,<br> &nbsp; &nbsp; &nbsp;(TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY),<br> &nbsp; &nbsp; &nbsp; hdlTokenHandle);<br> &nbsp;// Get the LUID for shutdown privilege.<br> &nbsp; &nbsp;LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);<br> &nbsp; &nbsp;Privilege[0].Luid := tmpLuid;<br> &nbsp; &nbsp;Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;<br> &nbsp; &nbsp;tkp.PrivilegeCount := 1; // One privilege to set<br> &nbsp; &nbsp;tkp.Privileges[0] := Privilege[0];<br> &nbsp;// Enable the shutdown privilege in the access token of this process.<br> &nbsp; &nbsp;AdjustTokenPrivileges(hdlTokenHandle,<br> &nbsp; &nbsp; &nbsp;False,<br> &nbsp; &nbsp; &nbsp;tkp,<br> &nbsp; &nbsp; &nbsp;Sizeof(tkpNewButIgnored),<br> &nbsp; &nbsp; &nbsp;tkpNewButIgnored,<br> &nbsp; &nbsp; &nbsp;lBufferNeeded);<br> &nbsp;end;<br>begin<br> &nbsp;AdjustToken;<br> &nbsp;case send of<br> &nbsp; &nbsp;1: ExitWindowsEx(EWX_FORCE, 0); //在紧急情况下强制关机。 ;<br> &nbsp; &nbsp;2: ExitWindowsEx(EWX_LOGOFF, 0); //以其他用户身份登录。 ;<br> &nbsp; &nbsp;3: ExitWindowsEx(EWX_POWEROFF, 0); //关闭系统并关闭电源。<br> &nbsp; &nbsp;4: if application.MessageBox('是否重新启动', '重启计算机提示框', 1+48)=1 then &nbsp;//idok<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;showmessage('reboot');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ExitWindowsEx(EWX_REBOOT, $FFFF); //重启操作系统<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br> &nbsp; &nbsp;5: if application.MessageBox('是否关闭', '关闭计算机提示框', 1+48)=1 then &nbsp;//idok<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;showmessage('close');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ExitWindowsEx(EWX_SHUTDOWN, $FFFF); //关机;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br> &nbsp;else<br> &nbsp; &nbsp;EXIT;<br> &nbsp;end;<br>end;<br><br><br>//<br>procedure Tfrm_posmain.N87Click(Sender: TObject);<br>begin<br> &nbsp;send:=4;<br> &nbsp;ShutDown;<br>end;<br>procedure Tfrm_posmain.N88Click(Sender: TObject);<br>begin<br> &nbsp;send:=5;<br> &nbsp;ShutDown;<br>end;
 
我现在的系统是win2000的,在我的应用程序中增加了‘关闭计算机’和‘重启计算机’的功能,但在编译环境下这两个功能是可用的,但当只运行exe时,那两个功能就不起作用了。这个关机和重启一定要和编译环境绑定吗?是不是要把系统的什么.DLL拷贝到我应用程序所在的文件夹啊? <br>我程序中的相关代码如下:<br>procedure Tfrm_posmain.ShutDown;<br> &nbsp;procedure AdjustToken(); //获取关机控制权<br> &nbsp;var<br> &nbsp; &nbsp;hdlProcessHandle, hdlTokenHandle, lBufferNeeded: Cardinal;<br> &nbsp; &nbsp;tmpLuid: Int64;<br> &nbsp;//tkpPrivilegeCount: Int64;<br> &nbsp; &nbsp;tkp, tkpNewButIgnored: TOKEN_PRIVILEGES;<br> &nbsp; &nbsp;Privilege: array[0..0] of _LUID_AND_ATTRIBUTES;<br> &nbsp;begin<br> &nbsp; &nbsp;hdlProcessHandle := GetCurrentProcess;<br> &nbsp; &nbsp;OpenProcessToken(hdlProcessHandle,<br> &nbsp; &nbsp; &nbsp;(TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY),<br> &nbsp; &nbsp; &nbsp; hdlTokenHandle);<br> &nbsp;// Get the LUID for shutdown privilege.<br> &nbsp; &nbsp;LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);<br> &nbsp; &nbsp;Privilege[0].Luid := tmpLuid;<br> &nbsp; &nbsp;Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;<br> &nbsp; &nbsp;tkp.PrivilegeCount := 1; // One privilege to set<br> &nbsp; &nbsp;tkp.Privileges[0] := Privilege[0];<br> &nbsp;// Enable the shutdown privilege in the access token of this process.<br> &nbsp; &nbsp;AdjustTokenPrivileges(hdlTokenHandle,<br> &nbsp; &nbsp; &nbsp;False,<br> &nbsp; &nbsp; &nbsp;tkp,<br> &nbsp; &nbsp; &nbsp;Sizeof(tkpNewButIgnored),<br> &nbsp; &nbsp; &nbsp;tkpNewButIgnored,<br> &nbsp; &nbsp; &nbsp;lBufferNeeded);<br> &nbsp;end;<br>begin<br> &nbsp;AdjustToken;<br> &nbsp;case send of<br> &nbsp; &nbsp;1: ExitWindowsEx(EWX_FORCE, 0); //在紧急情况下强制关机。 ;<br> &nbsp; &nbsp;2: ExitWindowsEx(EWX_LOGOFF, 0); //以其他用户身份登录。 ;<br> &nbsp; &nbsp;3: ExitWindowsEx(EWX_POWEROFF, 0); //关闭系统并关闭电源。<br> &nbsp; &nbsp;4: if application.MessageBox('是否重新启动', '重启计算机提示框', 1+48)=1 then &nbsp;//idok<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;showmessage('reboot');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ExitWindowsEx(EWX_REBOOT, $FFFF); //重启操作系统<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br> &nbsp; &nbsp;5: if application.MessageBox('是否关闭', '关闭计算机提示框', 1+48)=1 then &nbsp;//idok<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;showmessage('close');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ExitWindowsEx(EWX_SHUTDOWN, $FFFF); //关机;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br> &nbsp;else<br> &nbsp; &nbsp;EXIT;<br> &nbsp;end;<br>end;<br><br><br>//<br>procedure Tfrm_posmain.N87Click(Sender: TObject);<br>begin<br> &nbsp;send:=4;<br> &nbsp;ShutDown;<br>end;<br>procedure Tfrm_posmain.N88Click(Sender: TObject);<br>begin<br> &nbsp;send:=5;<br> &nbsp;ShutDown;<br>end;
 
没这种情况啊。我的程序里面用的蛮好的。不过有一点,对于9x系统,不要调用AdjustTokenPrivileges函数,直接调用ExitWindowsEx就可以了。
 
请问你的系统是2000的吗?
 
当我把运行环境开启时,并另外打开exe程序,这时我如点击‘关机’或‘重启’功能建时,一般只有编译环境被关闭而操作系统或其它应用程序并没有关闭。
 
后退
顶部