转贴,我的程序中这是这样调用的.<br>function SetPrivilege(sPrivilegeName: string; bEnabled: boolean ): boolean;<br>var<br> TPPrev,TP : TTokenPrivileges;<br> Token : THandle;<br> dwRetLen : DWord;<br>begin<br> Result := False;<br><br> OpenProcessToken(<br> GetCurrentProcess, //handle to process<br> TOKEN_ADJUST_PRIVILEGES //Required to change the privileges specified in an access token.<br> or TOKEN_QUERY, //Required to query the contents of an access token.<br> Token);<br><br> TP.PrivilegeCount := 1;<br> //retrieves the locally unique identifier (LUID) used on a specified system to<br> //locally represent the specified privilege name.<br> if( LookupPrivilegeValue(<br> Nil, //attempts to find the privilege name on the local system.<br> PChar( sPrivilegeName ), // address of string specifying the privilege<br> TP.Privileges[ 0 ].LUID ) // address of locally unique identifier<br> )then<br> begin<br> if( bEnabled )then //Give this privileges<br> begin<br> TP.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;<br> end<br> else begin //NOT Give this privileges<br> TP.Privileges[ 0 ].Attributes := 0;<br> end;<br><br> dwRetLen := 0;<br> //enables or disables privileges in the specified access token.<br> Result := AdjustTokenPrivileges(<br> Token, // handle to token that contains privileges<br> False, //modifies privileges<br> TP, // pointer to new privilege information<br> SizeOf( TPPrev ), // size, in bytes, of the TPPrev buffer<br> TPPrev, // receives original state of changed privileges<br> dwRetLen // receives required size of the TPPrev buffer<br> );<br> end;<br> CloseHandle( Token );<br>end;<br><br>function WinExitInNT( iFlags : integer ) : boolean;<br>begin<br> Result := True;<br> if( SetPrivilege('SeShutdownPrivilege', True ) )then<br> begin<br> if( not ExitWindowsEx( iFlags, 0 ) )then<br> begin<br> Result := False;<br> end;<br> SetPrivilege('SeShutdownPrivilege', False )<br> end<br> else begin<br> // handle errors...<br> Result := False;<br> end;<br>end;<br><br>//调用.<br> WinExitInNT(EWX_SHUTDOWN+EWX_POWEROFF+EWX_FORCE);