DelphiFAQ Home Search:

Enumerating the current user's privileges

 

commentsThis 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; // (SizeOf(Pointer) = 4*200) 

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; // for
  FreeMem(PrivName); 
  FreeMem(DisplayName); 
  FreeMem(pTokenInfo); 
end; 

Comments:

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Los Angeles, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.