[b]function[/b] SetPrivilege(Privilege: PChar; EnablePrivilege: Boolean;
out PreviousState: Boolean): DWORD;
[b]var[/b]
Token: THandle;
NewState: TTokenPrivileges;
Luid: TLargeInteger;
PrevState: TTokenPrivileges;
Return: DWORD;
[b]begin[/b]
PreviousState := True;
[b]if[/b] (GetVersion() > $80000000) [b]then[/b]
[color=#000080][i]//
Win9x [/i][/color]
Result := ERROR_SUCCESS
[b]else[/b]
[b]begin[/b]
[color=#000080][i]//
WinNT [/i][/color]
[b]if[/b] [b]not[/b] OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, Token) [b]then[/b]
Result := GetLastError()
[b]else[/b]
[b]try[/b]
[b]if[/b] [b]not[/b] LookupPrivilegeValue([b]nil[/b], Privilege, Luid) [b]then[/b]
Result := GetLastError()
[b]else[/b]
[b]begin[/b]
NewState.PrivilegeCount := 1;
NewState.Privileges[0].Luid := Luid;
[b]if[/b] EnablePrivilege [b]then[/b]
NewState.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
[b]else[/b]
NewState.Privileges[0].Attributes := 0;
[b]if[/b] [b]not[/b] AdjustTokenPrivileges(Token, False, NewState,
SizeOf(TTokenPrivileges), PrevState, Return) [b]then[/b]
Result := GetLastError()
[b]else[/b]
[b]begin[/b]
Result := ERROR_SUCCESS;
PreviousState :=
(PrevState.Privileges[0].Attributes [b]and[/b] SE_PRIVILEGE_ENABLED <> 0);
[b]end[/b];
[b]end[/b];
[b]finally[/b]
CloseHandle(Token);
[b]end[/b];
[b]end[/b];
[b]end[/b];