在2000/XP下如何用代码实现关闭计算机?(50分)

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

wlandll

Unregistered / Unconfirmed
GUEST, unregistred user!
在2000/XP下如何用代码实现关闭计算机?<br>在2000和XP下涉及到一个权限问题,一般需要执行如下程序段,可是我编译通不过,请高手指点<br>&nbsp;LookupPrivilegeValue(nil,'SE_SHUTDOWN_NAME',tkp.Privileges[0].LUID);<br>&nbsp;tkp.PrivilegeCount:=1;<br>&nbsp;tkp.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;<br>&nbsp;AdjustTokenPrivileges(handle,FALSE,tok,0,tok,0);<br><br>&nbsp;ExitWindowsEx(1,0); &nbsp;
 
windows nt/2000/xp 中如何执行ExitwindowsEx?<br><br>iVersionInfo: TOSVersionInfo;<br>&nbsp;iToken &nbsp; &nbsp;: THandle;<br>&nbsp;iPriveleg : TTokenPrivileges;<br>&nbsp;iaresult &nbsp;: DWORD;<br><br>begin<br>&nbsp;if Poweroff.Enabled = TRUE then flag := EWX_POWEROFF<br>&nbsp;else flag := EWX_REBOOT;<br><br>&nbsp;FillChar (iPriveleg, SizeOf (iPriveleg), #0);<br>&nbsp;iVersionInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);<br>&nbsp;GetVersionEx(iVersionInfo);<br>&nbsp;if iVersionInfo.dwPlatformId &lt;&gt; VER_PLATFORM_WIN32_NT then<br>&nbsp; &nbsp;ExitWindowsEx(flag + EWX_FORCE, 0)<br>&nbsp;else<br>&nbsp; &nbsp;if OpenProcessToken (GetCurrentProcess,<br>&nbsp; &nbsp; &nbsp; &nbsp;TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, iToken) then<br>&nbsp; &nbsp; &nbsp;if LookupPrivilegeValue (NIL,'SeShutdownPrivilege',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;iPriveleg.Privileges[0].Luid) then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;iPriveleg.PrivilegeCount := 1;<br>&nbsp; &nbsp; &nbsp; &nbsp;iPriveleg.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;<br>&nbsp; &nbsp; &nbsp; &nbsp;if AdjustTokenPrivileges (iToken,False,iPriveleg,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Sizeof(iPriveleg),iPriveleg,iaresult) then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ExitWindowsEx(flag + EWX_FORCE, 0);<br>&nbsp; &nbsp; &nbsp;end;<br>end;<br>
 
//取得权限<br>procedure TFLogOut.AdjustToken;<br>var<br>&nbsp; hdlProcessHandle : Cardinal;<br>&nbsp; hdlTokenHandle &nbsp; : Cardinal;<br>&nbsp; tmpLuid &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Int64;<br>&nbsp; tkp &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: TOKEN_PRIVILEGES;<br>&nbsp; tkpNewButIgnored : TOKEN_PRIVILEGES;<br>&nbsp; lBufferNeeded &nbsp; &nbsp;: Cardinal;<br>&nbsp; Privilege &nbsp; &nbsp; &nbsp; &nbsp;: array[0..0] of _LUID_AND_ATTRIBUTES;<br>begin<br>&nbsp; hdlProcessHandle := GetCurrentProcess;<br>&nbsp; OpenProcessToken(hdlProcessHandle,<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;hdlTokenHandle);<br>&nbsp; // Get the LUID for shutdown privilege.<br>&nbsp; LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);<br>&nbsp; Privilege[0].Luid := tmpLuid;<br>&nbsp; Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;<br>&nbsp; tkp.PrivilegeCount := 1; &nbsp; // One privilege to set<br>&nbsp; tkp.Privileges[0] := Privilege[0];<br>&nbsp; // Enable the shutdown privilege in the access token of this process.<br>&nbsp; AdjustTokenPrivileges(hdlTokenHandle,False,tkp,Sizeof(tkpNewButIgnored),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tkpNewButIgnored,lBufferNeeded);<br>end;<br>//关机<br>procedure TFLogOut.bbtOKClick(Sender: TObject);<br>begin<br>&nbsp; AdjustToken;<br>&nbsp; &nbsp;ExitWindowsEx(EWX_POWEROFF,0);<br>end;
 
以上两位老兄写的都没错,可是现在的机箱都是ATX的,执行程序后,最后会显示 现在可以安全关闭计算机 ,并不能真正断电。
 
下面这段是我写的,在xp下可以完全关闭电源,2000下却不行<br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>var &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;htmp,hToken:Thandle; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp;tkp:TOKEN_PRIVILEGES; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp;tkpOld:TOKEN_PRIVILEGES; &nbsp;<br><br>begin<br>while timetostr(time)=s do<br>OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES &nbsp;Or &nbsp;TOKEN_QUERY,hToken);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;LookupPrivilegeValue(nil, &nbsp;'SeShutdownPrivilege',tkp.Privileges[0].Luid);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tkp.PrivilegeCount:=1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tkp.Privileges[0].Attributes:= &nbsp;SE_PRIVILEGE_ENABLED; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AdjustTokenPrivileges(hToken, &nbsp;false, &nbsp;tkp, &nbsp;sizeof(tkpOld), &nbsp;tkpOld, &nbsp;htmp); &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ExitwindowsEx(EWX_SHUTDOWN,0); &nbsp;<br><br><br>end;<br><br>
 
procedure AdjustToken; //获得系统NT,win2000 的关机权限<br>var<br>&nbsp; hdlProcessHandle : Cardinal;<br>&nbsp; hdlTokenHandle &nbsp; : Cardinal;<br>&nbsp; tmpLuid &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Int64;<br>&nbsp; tkp &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: TOKEN_PRIVILEGES;<br>&nbsp; tkpNewButIgnored : TOKEN_PRIVILEGES;<br>&nbsp; lBufferNeeded &nbsp; &nbsp;: Cardinal;<br>&nbsp; Privilege &nbsp; &nbsp; &nbsp; &nbsp;: array[0..0] of _LUID_AND_ATTRIBUTES;<br>begin<br>&nbsp; hdlProcessHandle := GetCurrentProcess;<br>&nbsp; OpenProcessToken(hdlProcessHandle,(TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY),hdlTokenHandle);<br>&nbsp; LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);<br>&nbsp; Privilege[0].Luid := tmpLuid;<br>&nbsp; Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;<br>&nbsp; tkp.PrivilegeCount := 1;<br>&nbsp; tkp.Privileges[0] := Privilege[0];<br>&nbsp; AdjustTokenPrivileges(hdlTokenHandle, False, tkp, Sizeof(tkpNewButIgnored),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tkpNewButIgnored, lBufferNeeded);<br>end;<br><br>关电源:<br>AdjustToken;<br>ExitWindowsEx(EWX_POWEROFF, 0);<br>试验通过!
 
后退
顶部