Jump to content
Fórum Script Brasil
  • 0

Pegar Usuário Logado Do Windows


darth_ivan
 Share

Question

3 answers to this question

Recommended Posts

  • 0
Guest --Jonas --

A função IsAdmin faz essa verificação.

function IsAdmin: Boolean;

var
  
   hAccessToken: THandle;
   ptgGroups: PTokenGroups;
   dwInfoBufferSize: DWORD;
   psidAdministrators: PSID;
   x: Integer;
   bSuccess: BOOL;

begin

   Result := False;
   bSuccess := OpenThreadToken(GetCurrentThread, TOKEN_QUERY, True,
   hAccessToken);

   if not bSuccess then
     begin
       if GetLastError = ERROR_NO_TOKEN then
           bSuccess := OpenProcessToken(GetCurrentProcess, TOKEN_QUERY,
           hAccessToken);

     end; 

   if bSuccess then 
     begin
       GetMem(ptgGroups, 1024); 
       bSuccess := GetTokenInformation(hAccessToken, TokenGroups, 
       ptgGroups, 1024, dwInfoBufferSize);
       CloseHandle(hAccessToken);

       if bSuccess then 
          begin
             AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 2,
             SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
             0, 0, 0, 0, 0, 0, psidAdministrators);
             {$R-} 
             for x := 0 to ptgGroups.GroupCount - 1 do
                 if EqualSid(psidAdministrators, ptgGroups.Groups[x].Sid) then 
                     begin
                        Result := True; 
                        Break;
                     end;

                 {$R+}

                 FreeSid(psidAdministrators); 
          end;
          FreeMem(ptgGroups); 
    end;
end; 



procedure TForm1.Button1Click(Sender: TObject);
begin
   if isAdmin then
     ShowMessage('Logado como Administrator');
end;

end;

Edited by Micheus
Adicionado o delimitador de código [code] [/code]
Link to comment
Share on other sites

  • 0

mas se ainda precisar pegar o nome do usuário logado, segue um exemplo...

procedure TForm1.Button1Click(Sender: TObject);
var
  BufSize :cardinal;
  UserName :array[0..127] of ansichar;
begin
  BufSize := SizeOf(UserName);
  if BOOL(GetUserName(UserName, BufSize)) then
    ShowMessage(UserName);
end;

Link to comment
Share on other sites

  • 0

Foi observado por um colega que para o funcionamento da function IsAdmin é necessário declarar as constastes abaixo:

const
  SECURITY_NT_AUTHORITY: TSIDIdentifierAuthority = (Value: (0, 0, 0, 0, 0, 5));
  SECURITY_BUILTIN_DOMAIN_RID = $00000020;
  DOMAIN_ALIAS_RID_ADMINS = $00000220;

conforme ref. DelphiTricks.com

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



  • Forum Statistics

    • Total Topics
      150.2k
    • Total Posts
      647.5k
×
×
  • Create New...