如何在win2000中控制关机(win98中的那个函数不好用.)(50分)

  • 主题发起人 主题发起人 jyh_jack
  • 开始时间 开始时间
J

jyh_jack

Unregistered / Unconfirmed
GUEST, unregistred user!
急求一个win2000中的关机函数,win98中的那个关机函数好像在win2000中无法使用.<br>请求各位前辈不吝赐教................
 
退出系统<br>&nbsp; function GetWinVersion: String;<br><br>&nbsp; var<br><br>&nbsp; VersionInfo : TOSVersionInfo;<br><br>&nbsp; OSName : String;<br><br>&nbsp; begin<br><br>&nbsp; // set the size of the record<br><br>&nbsp; VersionInfo.dwOSVersionInfoSize := SizeOf( TOSVersionInfo );<br><br><br>&nbsp; if Windows.GetVersionEx( VersionInfo ) then<br><br>&nbsp; begin<br><br>&nbsp; with VersionInfo do<br><br>&nbsp; begin<br><br>&nbsp; case dwPlatformId of<br><br>&nbsp; VER_PLATFORM_WIN32s : OSName := 'Win32s';<br><br>&nbsp; VER_PLATFORM_WIN32_WINDOWS : OSName := 'Windows 95';<br><br>&nbsp; VER_PLATFORM_WIN32_NT : OSName := 'Windows NT';<br><br>&nbsp; end; // case dwPlatformId<br><br>&nbsp; Result := OSName + ' Version ' + IntToStr( dwMajorVersion ) + '.' + IntToStr( dwMinorVersion ) +<br><br>&nbsp; #13#10' (Build ' + IntToStr( dwBuildNumber ) + ': ' + szCSDVersion + ')';<br><br>&nbsp; end; // with VersionInfo<br><br>&nbsp; end // if GetVersionEx<br><br>&nbsp; else<br><br>&nbsp; Result := '';<br><br>&nbsp; end;<br><br><br>&nbsp; procedure ShutDown;<br><br>&nbsp; const<br><br>&nbsp; SE_SHUTDOWN_NAME = 'SeShutdownPrivilege'; // Borland forgot this declaration<br><br>&nbsp; var<br><br>&nbsp; hToken : THandle;<br><br>&nbsp; tkp : TTokenPrivileges;<br><br>&nbsp; tkpo : TTokenPrivileges;<br><br>&nbsp; zero : DWORD;<br><br>&nbsp; begin<br><br>&nbsp; if Pos( 'Windows NT', GetWinVersion) = 1 then // we've got to do a whole buch of things<br><br>&nbsp; begin<br><br>&nbsp; zero := 0;<br><br>&nbsp; if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then<br><br>&nbsp; begin<br><br>&nbsp; MessageBox( 0, 'Exit Error', 'OpenProcessToken() Failed', MB_OK );<br><br>&nbsp; Exit;<br><br>&nbsp; end; // if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)<br><br>&nbsp; if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then<br><br>&nbsp; begin<br><br>&nbsp; MessageBox( 0, 'Exit Error', 'OpenProcessToken() Failed', MB_OK );<br><br>&nbsp; Exit;<br><br>&nbsp; end; // if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)<br><br><br><br>&nbsp; // SE_SHUTDOWN_NAME<br><br>&nbsp; if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[ 0 ].Luid ) then<br><br>&nbsp; begin<br><br>&nbsp; MessageBox( 0, 'Exit Error', 'LookupPrivilegeValue() Failed', MB_OK );<br><br>&nbsp; Exit;<br><br>&nbsp; end; // if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[0].Luid )<br><br>&nbsp; tkp.PrivilegeCount := 1;<br><br>&nbsp; tkp.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;<br><br><br>&nbsp; AdjustTokenPrivileges( hToken, False, tkp, SizeOf( TTokenPrivileges ), tkpo, zero );<br><br>&nbsp; if Boolean( GetLastError() ) then<br><br>&nbsp; begin<br><br>&nbsp; MessageBox( 0, 'Exit Error', 'AdjustTokenPrivileges() Failed', MB_OK );<br><br>&nbsp; Exit;<br><br>&nbsp; end // if Boolean( GetLastError() )<br><br>&nbsp; else<br><br>&nbsp; ExitWindowsEx( EWX_FORCE or EWX_SHUTDOWN, 0 );<br><br>&nbsp; end // if OSVersion = 'Windows NT'<br><br>&nbsp; else<br><br>&nbsp; begin // just shut the machine down<br><br>&nbsp; ExitWindowsEx( EWX_FORCE or EWX_SHUTDOWN, 0 );<br><br>&nbsp; end; // else<br><br>&nbsp; end;<br><br>这个程序适用于所有Windows版本 <br><br><br><br>另给出调整关机权限:<br>procedure TForm1.AdjustToken();<br>var<br>&nbsp; hdlProcessHandle : Cardinal;<br>&nbsp; hdlTokenHandle : Cardinal;<br>&nbsp; tmpLuid : Int64;<br>&nbsp; tkpPrivilegeCount : Int64;<br>&nbsp; tkp : TOKEN_PRIVILEGES;<br>&nbsp; tkpNewButIgnored : TOKEN_PRIVILEGES;<br>&nbsp; lBufferNeeded : Cardinal;<br>&nbsp; Privilege : array[0..0] of _LUID_AND_ATTRIBUTES;<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; hdlProcessHandle := GetCurrentProcess;<br>&nbsp; &nbsp; &nbsp; &nbsp; OpenProcessToken(hdlProcessHandle,<br>&nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; hdlTokenHandle);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; // Get the LUID for shutdown privilege.<br>&nbsp; &nbsp; &nbsp; &nbsp; LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);<br>&nbsp; &nbsp; &nbsp; &nbsp; Privilege[0].Luid := tmpLuid;<br>&nbsp; &nbsp; &nbsp; &nbsp; Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;<br>&nbsp; &nbsp; &nbsp; &nbsp; tkp.PrivilegeCount := 1; &nbsp;// One privilege to set<br>&nbsp; &nbsp; &nbsp; &nbsp; tkp.Privileges[0] := Privilege[0];<br>&nbsp; &nbsp; &nbsp; &nbsp; // Enable the shutdown privilege in the access token of this<br>&nbsp; &nbsp; &nbsp; &nbsp; // process.<br>&nbsp; &nbsp; &nbsp; &nbsp; AdjustTokenPrivileges(hdlTokenHandle,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; False,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tkp,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sizeof(tkpNewButIgnored),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tkpNewButIgnored,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lBufferNeeded);<br><br>end;<br>
 
同意。问题的关键是取得权限。
 
俺也来一段 &nbsp;:P<br>procedure ReBootWinNT;<br>var<br>&nbsp; &nbsp;currToken:THandle;<br>&nbsp; &nbsp;prevState,newState:TTokenPrivileges;<br>&nbsp; &nbsp;prevStateLen:DWORD;<br>&nbsp; &nbsp;uid:TLargeInteger;<br>begin<br>&nbsp; &nbsp; // Set the privledges so we CAN reboot...<br>&nbsp; &nbsp;OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, currToken);<br>&nbsp; &nbsp;LookupPrivilegeValue(nil, 'SeShutdownPrivilege',uid);<br>&nbsp; &nbsp;newState.PrivilegeCount:=1;<br>&nbsp; &nbsp;newState.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;<br>&nbsp; &nbsp;newState.Privileges[0].Luid := uid;<br>&nbsp; &nbsp;AdjustTokenPrivileges(currToken, False, newState, sizeof(TTokenPrivileges),prevState, prevStateLen);<br>&nbsp; // Go ahead and reboot...<br>&nbsp; &nbsp;ExitWindowsEX(EWX_REBOOT, 0);<br>end;
 
万能关机:<br>function GetWinVersion: String;<br>&nbsp; var<br>&nbsp; &nbsp; &nbsp;VersionInfo : TOSVersionInfo;<br>&nbsp; &nbsp; &nbsp;OSName &nbsp; &nbsp; &nbsp;: String;<br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp;// set the size of the record<br>&nbsp; &nbsp; &nbsp;VersionInfo.dwOSVersionInfoSize := SizeOf( TOSVersionInfo );<br>&nbsp; <br>&nbsp; &nbsp; &nbsp;if Windows.GetVersionEx( VersionInfo ) then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;with VersionInfo do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case dwPlatformId of<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VER_PLATFORM_WIN32s &nbsp; : OSName := 'Win32s';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VER_PLATFORM_WIN32_WINDOWS : OSName := 'Windows 95';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VER_PLATFORM_WIN32_NT &nbsp; &nbsp; &nbsp;: OSName := 'Windows NT';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; // case dwPlatformId<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result := OSName + ' Version ' + IntToStr( dwMajorVersion ) + '.' + IntToStr( dwMinorVersion ) +<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #13#10' (Build ' + IntToStr( dwBuildNumber ) + ': ' + szCSDVersion + ')';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end; // with VersionInfo<br>&nbsp; &nbsp; &nbsp; &nbsp; end // if GetVersionEx<br>&nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; Result := '';<br>&nbsp; end;<br>&nbsp; <br>&nbsp; procedure ShutDown;<br>&nbsp; const<br>&nbsp; &nbsp; SE_SHUTDOWN_NAME = 'SeShutdownPrivilege'; &nbsp; // Borland forgot this declaration<br>&nbsp; var<br>&nbsp; &nbsp; hToken &nbsp; &nbsp; &nbsp; : THandle;<br>&nbsp; &nbsp; tkp &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: TTokenPrivileges;<br>&nbsp; &nbsp; tkpo &nbsp; &nbsp; &nbsp; &nbsp; : TTokenPrivileges;<br>&nbsp; &nbsp; zero &nbsp; &nbsp; &nbsp; &nbsp; : DWORD;<br>&nbsp; begin<br>&nbsp; &nbsp; if Pos( 'Windows NT', GetWinVersion) = 1 &nbsp;then // we've got to do a whole buch of things<br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zero := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MessageBox( 0, 'Exit Error', 'OpenProcessToken() Failed', MB_OK );<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end; // if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MessageBox( 0, 'Exit Error', 'OpenProcessToken() Failed', MB_OK );<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end; // if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)<br>&nbsp; <br>&nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // SE_SHUTDOWN_NAME<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[ 0 ].Luid ) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageBox( 0, 'Exit Error', 'LookupPrivilegeValue() Failed', MB_OK );<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end; // if not 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; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AdjustTokenPrivileges( hToken, False, tkp, SizeOf( TTokenPrivileges ), tkpo, zero );<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Boolean( GetLastError() ) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageBox( 0, 'Exit Error', 'AdjustTokenPrivileges() Failed', MB_OK );<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end // if Boolean( GetLastError() )<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ExitWindowsEx( EWX_FORCE or EWX_SHUTDOWN, 0 );<br>&nbsp; &nbsp; &nbsp; &nbsp; end // if OSVersion = 'Windows NT'<br>&nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; begin // just shut the machine down<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ExitWindowsEx( EWX_FORCE or EWX_SHUTDOWN, 0 );<br>&nbsp; &nbsp; &nbsp; &nbsp; end; // else<br>&nbsp; end;<br><br>
 
多人接受答案了。
 
后退
顶部