哪位大哥知道2000下怎么关机?!ExitWindowsEx不行呀,好像还得调用什么函数!(10分)

  • 主题发起人 主题发起人 孤岛
  • 开始时间 开始时间

孤岛

Unregistered / Unconfirmed
GUEST, unregistred user!
哪位大哥知道2000下怎么关机?!ExitWindowsEx不行呀,好像还得调用什么函数!
 
To shut down or restart the system, the calling process must use the “AdjustTokenPrivileges” <br>function to enable the SE_SHUTDOWN_NAME privilege。<br><br><br><br>__AdjustTokenPrivileges<br>The AdjustTokenPrivileges function enables or disables privileges in the specified <br>access token. Enabling or disabling privileges in an access token requires TOKEN_ADJUST_PRIVILEGES access. <br><br>BOOL AdjustTokenPrivileges(<br>&nbsp; HANDLE TokenHandle, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// handle to token<br>&nbsp; BOOL DisableAllPrivileges, &nbsp; &nbsp; &nbsp; // disabling option<br>&nbsp; PTOKEN_PRIVILEGES NewState, &nbsp; &nbsp; &nbsp;// privilege information<br>&nbsp; DWORD BufferLength, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// size of buffer<br>&nbsp; PTOKEN_PRIVILEGES PreviousState, // original state buffer<br>&nbsp; PDWORD ReturnLength &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// required buffer size<br>);<br><br>Requirements <br>&nbsp; Windows NT/2000/XP: Included in Windows NT 3.1 and later.<br>&nbsp; Header: Declared in Winbase.h; include Windows.h.<br>&nbsp; Library: Use Advapi32.lib.<br><br>以上摘自MSDN。
 
function IsWin9x: Boolean;<br>var<br>&nbsp; OsInfo: TOSVERSIONINFO;<br>begin<br>&nbsp; OsInfo.dwOSVersionInfoSize := sizeof(TOSVERSIONINFO);<br>&nbsp; GetVersionEx(OsInfo);<br>&nbsp; Result := (OsInfo.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS);<br>end;<br><br>function SetShutdownPrivilege(Enable: Boolean): Boolean;<br>var<br>&nbsp; PrevPrivileges: TTokenPrivileges;<br>&nbsp; Privileges: TTokenPrivileges;<br>&nbsp; Token: THandle;<br>&nbsp; dwRetLen: DWord;<br>begin<br>&nbsp; Result := False;<br>&nbsp; OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, Token);<br>&nbsp; Privileges.PrivilegeCount := 1;<br>&nbsp; if LookupPrivilegeValue(nil, 'SeShutdownPrivilege', Privileges.Privileges[0].LUID) then<br>&nbsp; begin<br>&nbsp; &nbsp; if Enable then<br>&nbsp; &nbsp; &nbsp; Privileges.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; Privileges.Privileges[0].Attributes := 0;<br>&nbsp; &nbsp; dwRetLen := 0;<br>&nbsp; &nbsp; Result := AdjustTokenPrivileges(Token, False, Privileges, SizeOf(PrevPrivileges), PrevPrivileges, dwRetLen);<br>&nbsp; end;<br>&nbsp; CloseHandle(Token);<br>end;<br><br>procedure Reboot;<br>begin<br>&nbsp; Application.Terminate;<br>&nbsp; if IsWin9x then<br>&nbsp; &nbsp; ExitWindowsEx(EWX_FORCE or EWX_REBOOT, 0)<br>&nbsp; else<br>&nbsp; begin<br>&nbsp; &nbsp; SetShutdownPrivilege(True);<br>&nbsp; &nbsp; ExitWindowsEx(EWX_FORCE or EWX_REBOOT, 0);<br>&nbsp; &nbsp; SetShutdownPrivilege(False);<br>&nbsp; end;<br>end;<br><br>procedure ShutDown;<br>begin<br>&nbsp; Application.Terminate;<br>&nbsp; if IsWin9x then<br>&nbsp; &nbsp; ExitWindowsEx(EWX_FORCE or EWX_SHUTDOWN, 0)<br>&nbsp; else<br>&nbsp; begin<br>&nbsp; &nbsp; SetShutdownPrivilege(True);<br>&nbsp; &nbsp; ExitWindowsEx(EWX_FORCE or EWX_SHUTDOWN, 0);<br>&nbsp; &nbsp; SetShutdownPrivilege(False);<br>&nbsp; end;<br>end;<br><br><br>-----<br>http://www.8421.org
 
再给出一段例程,VC.NET在2000、XP下实现。<br>HANDLE hToken; <br>TOKEN_PRIVILEGES tkp; <br>&nbsp;<br>// Get a token for this process. <br>&nbsp;<br>if (!OpenProcessToken(GetCurrentProcess(), <br>&nbsp; &nbsp; &nbsp; &nbsp; TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &amp;hToken)) <br>&nbsp; &nbsp; error("OpenProcessToken"); <br>&nbsp;<br>// Get the LUID for the shutdown privilege. <br>&nbsp;<br>LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, <br>&nbsp; &nbsp; &nbsp; &nbsp; &amp;tkp.Privileges[0].Luid); <br>&nbsp;<br>tkp.PrivilegeCount = 1; &nbsp;// one privilege to set &nbsp; &nbsp;<br>tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; <br>&nbsp;<br>// Get the shutdown privilege for this process. <br>&nbsp;<br>AdjustTokenPrivileges(hToken, FALSE, &amp;tkp, 0, <br>&nbsp; &nbsp; &nbsp; &nbsp; (PTOKEN_PRIVILEGES)NULL, 0); <br>&nbsp;<br>// Cannot test the return value of AdjustTokenPrivileges. <br>&nbsp;<br>if (GetLastError() != ERROR_SUCCESS) <br>&nbsp; &nbsp; error("AdjustTokenPrivileges"); <br>&nbsp;<br>// Shut down the system and force all applications to close. <br>&nbsp;<br>if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0)) <br>&nbsp; &nbsp; error("ExitWindowsEx"); <br>
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=565798
 
接受答案了.
 
后退
顶部