DelphiFAQ Home Search:
General :: Windows :: Programming :: Windows with Delphi :: Windows API
Windows programming with Delphi

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.

Only the 40 most recently viewed articles are shown.
You can see the full list here.

Featured Article

Enumerating the current user's privileges

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; 

Generated 4:02:29 on Feb 22, 2018