贴上几个吧。全是论坛摘下来的,没有实验。<br>1、<br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> AdjustToken;<br> ExitWindowsEx(EWX_SHUTDOWN, 0);<br>end;<br><br>//获取NT系统的操作权限<br>procedure AdjustToken();<br>var<br> hdlProcessHandle : Cardinal;<br> hdlTokenHandle : Cardinal;<br> tmpLuid : Int64;<br> tkp : TOKEN_PRIVILEGES;<br> tkpNewButIgnored : TOKEN_PRIVILEGES;<br> lBufferNeeded : Cardinal;<br> Privilege : array[0..0] of _LUID_AND_ATTRIBUTES;<br>begin<br> hdlProcessHandle := GetCurrentProcess;<br> OpenProcessToken(hdlProcessHandle,<br> (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY),<br> hdlTokenHandle);<br> // Get the LUID for shutdown privilege.<br> LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);<br> Privilege[0].Luid := tmpLuid;<br> Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;<br> tkp.PrivilegeCount := 1; // One privilege to set<br> tkp.Privileges[0] := Privilege[0];<br> // Enable the shutdown privilege in the access token of this process.<br> AdjustTokenPrivileges(hdlTokenHandle,False,tkp,Sizeof(tkpNewButIgnored),<br> tkpNewButIgnored,lBufferNeeded);<br>end;<br>------------------------------------------------------------------------------<br>2、<br>function SetPrivilege (sPrivilegeName: string; bEnabled: Boolean) : Boolean;<br>var<br> TPPrev,<br> TP : TTokenPrivileges;<br> Token :THandle;<br> dwRetLen : DWORD;<br>begin<br> result := False;<br> OpenProcessToken (GetCurrentProcess,<br> TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,<br> Token); <br> TP.PrivilegeCount := 1;<br> if LookupPrivilegeValue (nil, PChar (sPrivilegeName), TP.Privileges[0].LUID) then<br> begin<br> if bEnabled then<br> TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED<br> else<br> TP.Privileges[0].Attributes := 0;<br> dwRetLen := 0;<br> result := AdjustTokenPrivileges (<br> Token,<br> False,<br> TP,<br> SizeOf (TPPrev),<br> TPPrev,<br> dwRetLen);<br> end;<br> CloseHandle (Token);<br>end