调用ExitWindowsEx()函数系统关机的问题,使用ExitWindowsEx(WWX_SHUTDOWN or EWX_POWEROFF,0)为何不能关闭

  • 主题发起人 主题发起人 比尔.丐痴
  • 开始时间 开始时间

比尔.丐痴

Unregistered / Unconfirmed
GUEST, unregistred user!
调用ExitWindowsEx()函数系统关机的问题,使用ExitWindowsEx(WWX_SHUTDOWN or EWX_POWEROFF,0)为何不能关闭系统?(50分)<br />在使用ExitWindowsEx的PAI来关闭计算机,ExitWindowsEx(WWX_SHUTDOWN or EWX_POWEROFF,0)为何不能关闭系统,查看MSDN说:“要关机,对于NT系统,其调用进程必须有SE_SHUTDOWS_NAME权限,对于98则没有。”那该如何获得SE_SHUTDOWS_NAME的权限呢?
 
procedure ShutDown(Send: integer);<br>&nbsp; procedure AdjustToken(); //获取关机控制权<br>&nbsp; var<br>&nbsp; &nbsp; hdlProcessHandle, hdlTokenHandle, lBufferNeeded: Cardinal;<br>&nbsp; &nbsp; tmpLuid: Int64;<br>&nbsp; //tkpPrivilegeCount: Int64;<br>&nbsp; &nbsp; tkp, tkpNewButIgnored: TOKEN_PRIVILEGES;<br>&nbsp; &nbsp; Privilege: array[0..0] of _LUID_AND_ATTRIBUTES;<br>&nbsp; begin<br>&nbsp; &nbsp; hdlProcessHandle := GetCurrentProcess;<br>&nbsp; &nbsp; OpenProcessToken(hdlProcessHandle,<br>&nbsp; &nbsp; &nbsp; (TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY),<br>&nbsp; &nbsp; &nbsp; hdlTokenHandle);<br>&nbsp; // Get the LUID for shutdown privilege.<br>&nbsp; &nbsp; LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);<br>&nbsp; &nbsp; Privilege[0].Luid := tmpLuid;<br>&nbsp; &nbsp; Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;<br>&nbsp; &nbsp; tkp.PrivilegeCount := 1; // One privilege to set<br>&nbsp; &nbsp; tkp.Privileges[0] := Privilege[0];<br>&nbsp; // Enable the shutdown privilege in the access token of this process.<br>&nbsp; &nbsp; AdjustTokenPrivileges(hdlTokenHandle,<br>&nbsp; &nbsp; &nbsp; False,<br>&nbsp; &nbsp; &nbsp; tkp,<br>&nbsp; &nbsp; &nbsp; Sizeof(tkpNewButIgnored),<br>&nbsp; &nbsp; &nbsp; tkpNewButIgnored,<br>&nbsp; &nbsp; &nbsp; lBufferNeeded);<br>&nbsp; end;<br>begin<br>&nbsp; AdjustToken;<br>&nbsp; case send of<br>&nbsp; &nbsp; 1: ExitWindowsEx(EWX_FORCE, 0); //在紧急情况下强制关机。 ;<br>&nbsp; &nbsp; 2: ExitWindowsEx(EWX_LOGOFF, 0); //以其他用户身份登录。 ;<br>&nbsp; &nbsp; 3: ExitWindowsEx(EWX_POWEROFF, 0); //关闭系统并关闭电源。<br>&nbsp; &nbsp; 4: ExitWindowsEx(EWX_REBOOT, 0); //重启操作系统<br>&nbsp; &nbsp; 5: ExitWindowsEx(EWX_SHUTDOWN, 0); //关机;<br>&nbsp; else<br>&nbsp; &nbsp; EXIT;<br>&nbsp; end;<br>end;
 
uses<br>&nbsp; ShlObj;
 
接受答案了.
 
后退
顶部