Windows with Delphi Windows API (94) Windows Filesystem (41) Windows Forms (69) Windows Graphics (38)
Exchange Links About this site Links to us 
New related comments Number of comments in the last 48 hoursPlay WAV files 1 new comments
|
Enumerating the current user's privileges
This article has not been rated yet. After reading, feel free to leave comments and rate it.
Question: How can I obtain the current user's privileges?
Answer: Use OpenProcessToken() to obtain an access token for the current process (it could be a different process as well). This access token contains the security information for your session. All processes run under the same logon (session) have the same access token, so it doesn't matter which process you use.
The access token identifies the user, the user's groups and privileges.
Then you need to call GetTokenInformation() to obtain the information associated with the access token.
LookupPrivilegeName() and LookupPrivilegeDisplayName() are used to obtain a human readable string representation of each privilege.
 | |  | | procedure TForm1.Button1Click(Sender: TObject);
const
TokenSize = 800;
var
hToken: THandle;
pTokenInfo: PTOKENPRIVILEGES;
ReturnLen: Cardinal;
i: Integer;
PrivName: PChar;
DisplayName: PChar;
NameSize: Cardinal;
DisplSize: Cardinal;
LangId: Cardinal;
begin
GetMem(pTokenInfo, TokenSize);
if not OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
ShowMessage('OpenProcessToken error');
if not GetTokenInformation(hToken, TokenPrivileges, pTokenInfo, TokenSize, ReturnLen) then
ShowMessage('GetTokenInformation error');
GetMem(PrivName, 255);
GetMem(DisplayName, 255);
for i := 0 to pTokenInfo.PrivilegeCount - 1 do
begin
DisplSize := 255;
NameSize := 255;
LookupPrivilegeName(nil, pTokenInfo.Privileges[i].Luid, PrivName, Namesize);
LookupPrivilegeDisplayName(nil, PrivName, DisplayName, DisplSize, LangId);
ListBox1.Items.Add(PrivName + #9 + DisplayName);
end;
FreeMem(PrivName);
FreeMem(DisplayName);
FreeMem(pTokenInfo);
end; | |  | |  |
Comments:
|