mrabat
mrabat

Reputation: 912

How to get the full windows user name

I got a local account that got a username "rabi" when I execute "whoami /all" I get some information about that user: uname mikeschleppi\rabatscher .... enumerates all groups and some priviledges. But also not all information that I want....

Some of that info I can query by using the GetUsernameEx API. Nevertheless the User Account Manager shows me a nice display name (like: John Doe) and the email address that was used.

How can I get that information (especially full name) in Delphi? GetUsernameEx is obviously not the option here...

To clarify: The account window to manage my user account actually shows my full name enter image description here

So I wondered how to get that information. I'm sure the code you guys suggested works for AD (and maybe even LDAP servers) but I'm interested in the information about the local user...

Upvotes: 1

Views: 972

Answers (1)

David Heffernan
David Heffernan

Reputation: 612964

You can do this with ADSI. Here's a simple example:

{$APPTYPE CONSOLE}

uses
  SysUtils,
  ComObj,
  ActiveX,
  ActiveDs_TLB in 'ActiveDs_TLB.pas';

function ADsGetObject(lpszPathName: PChar; const riid: TGUID; out Obj): HRESULT; stdcall; external 'activeds.dll';

function GetADsUser(const Domain, Username: string): IADsUser;
var
  Path: string;
begin
  Path := 'WinNT://' + Domain + '/' + Username;
  OleCheck(ADsGetObject(PChar(Path), IID_IADsUser, Result));
end;

procedure Main;
var
  User: IADsUser;
begin
  User := GetADsUser('yourdomain', 'yourusername');
  Writeln(User.FullName);
  Writeln(User.EmailAddress);
end;

begin
  try
    OleCheck(CoInitializeEx(nil, COINIT_APARTMENTTHREADED));
    Main;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.

I obtained the TLB unit for ADSI using Embarcadero's tlibimp tool:

C:\Desktop>tlibimp -p C:\Windows\System32\activeds.tlb
Embarcadero TLIBIMP Version 12.16581
Copyright(c) 1995-2010 Embarcadero Technologies, Inc.

Opening  C:\Windows\System32\activeds.tlb
Type library loaded ....
Created  C:\Desktop\ActiveDs_TLB.dcr
Created  C:\Desktop\ActiveDs_TLB.pas

Upvotes: 1

Related Questions