function EnablePrivilege(
const Privilege:
string; fEnable: Boolean;
out PreviousState: Boolean): DWORD;
var
Token : THandle;
NewState : TTokenPrivileges;
Luid : TLargeInteger;
PrevState : TTokenPrivileges;
Return : DWORD;
begin
SetLastError(0);
// Clear last system error state
PreviousState := True;
if (GetVersion() > $80000000)
then // Win9x
Result := ERROR_SUCCESS
else // WinNT
begin
if OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, Token)
then
begin
try
if LookupPrivilegeValue(
nil, PChar(Privilege), Luid)
then
begin
NewState.PrivilegeCount := 1;
NewState.Privileges[0].Luid := Luid;
if fEnable
then
NewState.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
else
NewState.Privileges[0].Attributes := 0;
if AdjustTokenPrivileges(Token, False, NewState, SizeOf(TTokenPrivileges), PrevState, Return)
then
begin
PreviousState := (PrevState.Privileges[0].Attributes
and SE_PRIVILEGE_ENABLED <> 0);
end;
end;
finally
CloseHandle(Token);
end;
end;
Result := GetLastError;
end;
end;