如何关机(100分)

  • 主题发起人 it_boy69
  • 开始时间
I

it_boy69

Unregistered / Unconfirmed
GUEST, unregistred user!
请问如何实现用DELPHI程序关机 ?
 
http://www.tommstudio.com/newclub30/<br>这里有一些这样的文章.<br>
 
ExitWindows<br>ExitWindowsEx
 
procedure TShutDownComputer.ShutDownComputer;<br>const<br>&nbsp; SE_SHUTDOWN_NAME = 'SeShutdownPrivilege'; <br>var<br>&nbsp; hToken : THandle;<br>&nbsp; tkp : TTokenPrivileges;<br>&nbsp; tkpo : TTokenPrivileges;<br>&nbsp; zero : DWORD;<br>begin<br>&nbsp; zero:=0;<br>&nbsp; if not OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)<br>&nbsp; then begin<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br><br>&nbsp; if not OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)<br>&nbsp; then begin<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br><br>&nbsp; if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[ 0 ].Luid )<br>&nbsp; then begin<br>&nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; end;<br><br>&nbsp; tkp.PrivilegeCount := 1;<br>&nbsp; tkp.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;<br>&nbsp; AdjustTokenPrivileges( hToken, False, tkp, SizeOf( TTokenPrivileges ), tkpo, zero );<br><br>&nbsp; if Boolean( GetLastError() )<br>&nbsp; then begin<br>&nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; end<br>&nbsp; else ExitWindowsEx( EWX_FORCE or EWX_SHUTDOWN or EWX_POWEROFF, 0);<br>end;<br>end.
 
procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; &nbsp; &nbsp; &nbsp; VerInfo:TOSVersionInfo;<br>&nbsp; &nbsp; &nbsp; &nbsp; hToken:THANDLE;<br>&nbsp; &nbsp; &nbsp; &nbsp; tkp:TOKEN_PRIVILEGES;<br>&nbsp; &nbsp; &nbsp; &nbsp; Nothing:Cardinal;<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; VerInfo.dwOSVersionInfoSize:=SizeOf(VerInfo);<br>&nbsp; &nbsp; &nbsp; &nbsp; GetVersionEx(VerInfo);<br>&nbsp; &nbsp; &nbsp; &nbsp; if VerInfo.dwPlatformId=VER_PLATFORM_WIN32_NT then<br>&nbsp; &nbsp; &nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,hToken);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LookupPrivilegeValue(nil,'SeShutdownPrivilege',tkp.Privileges[0].Luid);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tkp.PrivilegeCount:= 1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tkp.Privileges[0].Attributes:= SE_PRIVILEGE_ENABLED;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AdjustTokenPrivileges(hToken, FALSE, tkp, 0,nil, Nothing);<br>&nbsp; &nbsp; &nbsp; &nbsp; End;<br>&nbsp; &nbsp; &nbsp; &nbsp; ExitWindowsEx(EWX_POWEROFF,0);//关闭计算机并断开电源.<br>end;<br>当然,你还可以使用ExitWindowsEx做点别的:<br>EWX_FORCE: &nbsp;关闭所有程序并以其他用户身份登录<br>EWX_LOGOFF: 重新启动计算机并切换到MS-DOS方式<br>EWX_REBOOT: 重新启动计算机<br>EWX_SHUTDOWN: &nbsp; 关闭计算机<br>
 
顶部