请问怎样在WIN2000下获得关机权限?(100分)

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

Iveny

Unregistered / Unconfirmed
GUEST, unregistred user!
我想在WIN2000下用ExitWindowsEx(EWX_SHUTDOWN, 0)实现关机,请问怎样获得关机权限?<br>可以给出完整的代码吗?谢谢!
 
贴上几个吧。全是论坛摘下来的,没有实验。<br>1、<br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; AdjustToken;<br>&nbsp; ExitWindowsEx(EWX_SHUTDOWN, 0);<br>end;<br><br>//获取NT系统的操作权限<br>procedure 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>2、<br>function SetPrivilege (sPrivilegeName: string; bEnabled: Boolean) : Boolean;<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
 
我想问问有没有别的方法?(简单实现关机)
 
据说没戏,除非,除非,悄悄的把手移到机箱的Power上,然后……[:D]
 
只能使用yzhshi的方法
 
function ShutDownSystem: Boolean; //关闭计算机<br>var<br>&nbsp; VerInfo: TOSVersionInfo;<br>&nbsp; hToken: THANDLE;<br>&nbsp; tkp: TOKEN_PRIVILEGES;<br>&nbsp; Nothing: Cardinal;<br>begin<br>&nbsp; if IsWinNT then<br>&nbsp; begin<br>&nbsp; &nbsp; VerInfo.dwOSVersionInfoSize := SizeOf(VerInfo);<br>&nbsp; &nbsp; GetVersionEx(VerInfo);<br>&nbsp; &nbsp; if VerInfo.dwPlatformId = VER_PLATFORM_WIN32_NT then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,<br>&nbsp; &nbsp; &nbsp; &nbsp; hToken);<br>&nbsp; &nbsp; &nbsp; LookupPrivilegeValue(nil, 'SeShutdownPrivilege', tkp.Privileges[0].Luid);<br>&nbsp; &nbsp; &nbsp; tkp.PrivilegeCount := 1;<br>&nbsp; &nbsp; &nbsp; tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;<br>&nbsp; &nbsp; &nbsp; AdjustTokenPrivileges(hToken, FALSE, tkp, 0, nil, Nothing);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; ExitWindowsEx(EWX_FORCE + EWX_SHUTDOWN + EWX_POWEROFF, 0);<br>&nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; ExitWindowsEx(EWX_FORCE + EWX_SHUTDOWN + EWX_POWEROFF, 0);<br>&nbsp; Result := True;<br>end;
 
function IsWinNT: boolean; //判断系统是否是WINNT<br>var<br>&nbsp; MyVerInfo: TOSVersionInfo;<br>begin<br>&nbsp; MyVerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);<br>&nbsp; GetVersionEx(MyVerInfo);<br>&nbsp; if MyVerInfo.dwPlatformId = VER_PLATFORM_WIN32_NT then<br>&nbsp; &nbsp; Result := true<br>&nbsp; else<br>&nbsp; &nbsp; Result := false;<br>end;
 
ExitWindowsEx(EWX_LOGOFF , 0); &nbsp; &nbsp; &nbsp; //注销<br>ExitWindowsEx(EWX_SHUTDOWN, 0); &nbsp; &nbsp; &nbsp;//关机,但是,不关闭电源<br>ExitWindowsEx(EWX_REBOOT , 0); &nbsp; &nbsp; &nbsp; // 重启<br>ExitWindowsEx(EWX_FORCE , 0); &nbsp; &nbsp; &nbsp; &nbsp;//强制关机<br>ExitWindowsEx(EWX_POWEROFF, 0); &nbsp; &nbsp; &nbsp;//关机,并且关闭电源<br>ExitWindowsEx(EWX_FORCEIFHUNG , 0); &nbsp;//
 
其实不用判断是否是NT,98下调用调整权限的函数虽然没作用,但也不会出错。
 
function SetPrivilege(aPrivilegeName : string;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aEnabled : boolean ): boolean;<br>var<br>&nbsp; TPPrev,<br>&nbsp; TP &nbsp; &nbsp; &nbsp; &nbsp; : TTokenPrivileges;<br>&nbsp; Token &nbsp; &nbsp; &nbsp;: THandle;<br>&nbsp; dwRetLen &nbsp; : DWord;<br>begin<br>&nbsp; Result := False;<br>&nbsp; OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;or TOKEN_QUERY, Token );<br><br>&nbsp; TP.PrivilegeCount := 1;<br>&nbsp; if( LookupPrivilegeValue(nil, PChar( aPrivilegeName ),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TP.Privileges[ 0 ].LUID ) ) then<br>&nbsp; begin<br>&nbsp; &nbsp; if( aEnabled )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><br>&nbsp; &nbsp; dwRetLen := 0;<br>&nbsp; &nbsp; Result := AdjustTokenPrivileges(Token,False,TP,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SizeOf( TPPrev ),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TPPrev,dwRetLen );<br>&nbsp; end;<br><br>&nbsp; CloseHandle( Token );<br>end;<br><br><br>function WinExit( iFlags : integer ) : boolean;<br>// &nbsp; possible Flags:<br>// &nbsp; EWX_LOGOFF<br>// &nbsp; EWX_REBOOT<br>// &nbsp; EWX_SHUTDOWN<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; Result := False;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; SetPrivilege( 'SeShutdownPrivilege', False )<br>&nbsp; end<br>&nbsp; else<br>&nbsp; begin<br>&nbsp; &nbsp; Result := False;<br>&nbsp; end;<br>end;<br><br>绝对没有问题!
 

Similar threads

回复
0
查看
588
不得闲
回复
0
查看
795
不得闲
后退
顶部