关于在程序中实现关机的问题(50分)

  • 主题发起人 主题发起人 Distachio
  • 开始时间 开始时间
D

Distachio

Unregistered / Unconfirmed
GUEST, unregistred user!
各位高手:<br>  我知道在程序中实现关机的功能是调用windows api函数exitwindowex(uflags,nil)<br>但是 该函数的第一个参数不知道怎么配置,虽然delphi中有定义,但它定义的那些参数<br>有些都没有用,或是看上是关机的参数,在实际运行时就关不掉了<br>比如其中有个参数是shutdown,但该参数只能退出windows,但不能自动关闭电源<br>我是在window200的环境下执行的<br>不知道,哪为高手碰到过这种问题,请给予帮助,谢谢!
 
http://delphibbs.com/delphibbs/dispq.asp?lid=2472876
 
ExitWindowsEx(EWX_POWEROFF, 0)
 
procedure Tmainform.reboot_computer;<br>var<br>&nbsp; &nbsp; &nbsp; &nbsp; hToken:THandle;<br>&nbsp; &nbsp; &nbsp; &nbsp; tkp : TOKEN_PRIVILEGES;<br>&nbsp; &nbsp; &nbsp; &nbsp; ReturnLength : DWord;<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if (not OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_ALL_ACCESS or &nbsp;TOKEN_QUERY, hToken))then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; application.Terminate;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; LookupPrivilegeValue(nil,'SeShutdownPrivilege',tkp.Privileges[0].Luid);<br>&nbsp; &nbsp; &nbsp; &nbsp; tkp.PrivilegeCount := 1;<br>&nbsp; &nbsp; &nbsp; &nbsp; tkp.Privileges[0].Attributes :=SE_PRIVILEGE_ENABLED;<br>&nbsp; &nbsp; &nbsp; &nbsp; ReturnLength :=0;<br>&nbsp; &nbsp; &nbsp; &nbsp; AdjustTokenPrivileges(hToken, FALSE, tkp, 0,nil,ReturnLength);<br>&nbsp; &nbsp; &nbsp; &nbsp; if (GetLastError() &lt;&gt; ERROR_SUCCESS) then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;application.Terminate;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; if (not ExitWindowsEx(EWX_POWEROFF, 0)) then &nbsp;//EWX_REBOOT<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;application.Terminate;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>end;
 
to 怡.梦<br>谢谢,你也是遇到我说过的那种关不掉电源的情况吗?
 
EWX_POWEROFF 关闭系统并关闭电源。<br>仔细看那个贴子啊。
 
(WIN2000关机)<br>function TForm1.SetPrivilege(sPrivilegeName: string;<br>&nbsp;bEnabled: Boolean): Boolean;<br><br>var<br>&nbsp;TPPrev,<br>&nbsp;TP &nbsp; &nbsp; &nbsp; : TTokenPrivileges;<br>&nbsp;Token :THandle;<br>&nbsp;dwRetLen : DWORD;<br>begin<br>&nbsp;result := False;<br>&nbsp;OpenProcessToken (GetCurrentProcess,<br>&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;Token);<br>&nbsp;TP.PrivilegeCount := 1;<br>&nbsp;if LookupPrivilegeValue (nil, PChar (sPrivilegeName), TP.Privileges[0].LUID) then<br>&nbsp;begin<br>&nbsp; &nbsp;if bEnabled then<br>&nbsp; &nbsp; &nbsp;TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED<br>&nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp;TP.Privileges[0].Attributes := 0;<br>&nbsp; &nbsp;dwRetLen := 0;<br>&nbsp; &nbsp;result := AdjustTokenPrivileges (<br>&nbsp; &nbsp;Token,<br>&nbsp; &nbsp;False,<br>&nbsp; &nbsp;TP,<br>&nbsp; &nbsp;SizeOf (TPPrev),<br>&nbsp; &nbsp;TPPrev,<br>&nbsp; &nbsp;dwRetLen);<br>&nbsp;end;<br>&nbsp;CloseHandle (Token);<br>end;<br>function Tform1.WinExit (iFlags: integer) : Boolean;<br><br>begin<br>&nbsp;result := true;<br>&nbsp;if SetPrivilege ('SeShutdownPrivilege', true) then<br>&nbsp;begin<br>&nbsp; &nbsp;if (not ExitWindowsEx (iFlags, 0)) then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;// handle errors...<br>&nbsp; &nbsp; &nbsp;result := False<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;SetPrivilege ('SeShutdownPrivilege', False)<br>&nbsp;end<br>&nbsp;else<br>&nbsp;begin<br>&nbsp; &nbsp;// handle errors...<br>&nbsp; &nbsp;result := False<br>&nbsp;end;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>WinExit(EWX_POWEROFF + EWX_FORCE);<br>end; &nbsp;
 
啊。。不活了。<br>我给你的是答案啊。。。
 
后退
顶部