Getting the user's full name and company
   


To get the user's full name and company name as defined in the registry, use this function passing one of the TUserOrCompany type. For example, to get both, call GetUserAndCompanyName(_BOTH); Note that this info is not retrieved from the same place in the registry across all machines (thanks, Microsoft!), so I try to find it in three known places.

{$DEFINE Delphi3Below}
{$IFDEF VER130} //Delphi 5
  {$UNDEF Delphi3Below}
{$ELSE}
  {$IFDEF VER120} //Delphi 4
    {$UNDEF Delphi3Below}
  {$ENDIF}
{$ENDIF}

uses Registry;

type
  TUserOrCompany = (_USER, _COMPANY, _BOTH);

function GetUserAndCompanyName(UserOrCompany: TUserOrCompany): string
{ func to return the user's name or company name or both. }
{ User and company info is stored in different places in the registry }
{ on different computers - even though the OS is the same! These are } 
{ all I can find, it you find any others let me know. Thanks to }
{ Andrew Balahonov <[email protected]> for making me aware of this. }

var
  Reg: TRegistry;
begin
  Result := '';
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_LOCAL_MACHINE;
    
{$IFDEF Delphi3Below}
    if Reg.OpenKey('\SOFTWARE\MICROSOFT\WINDOWS NT\CURRENTVERSION', False) then
    
{$ELSE}
    if Reg.OpenKeyReadOnly('\SOFTWARE\MICROSOFT\WINDOWS NT\CURRENTVERSION') then
    
{$ENDIF}
      case UserOrCompany of
        _USER : Result := Reg.ReadString('RegisteredOwner');
        _COMPANY: Result := Reg.ReadString('RegisteredOrganization');
        _BOTH : Result := Reg.ReadString('RegisteredOwner') + #13 +
                          Reg.ReadString('RegisteredOrganization'); 
      end;

    if (Result = '') or (Result = '' + #13 + '') then
      
{ Try another key. }
      begin
        Reg.CloseKey;
        Reg.RootKey := HKEY_LOCAL_MACHINE;
        
{$IFDEF Delphi3Below}
        if Reg.OpenKey('\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION', False) then
        
{$ELSE}
        if Reg.OpenKeyReadOnly('\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION') then
        
{$ENDIF}
          case UserOrCompany of
            _USER : Result := Reg.ReadString('RegisteredOwner');
            _COMPANY: Result := Reg.ReadString('RegisteredOrganization');
            _BOTH : Result := Reg.ReadString('RegisteredOwner') + #13 +
                              Reg.ReadString('RegisteredOrganization'); 
          end
      end;

    if (Result = '') or (Result = '' + #13 + '') then
    
{ Try another key. }
      begin
        Reg.CloseKey;
        Reg.RootKey := HKEY_CURRENT_USER;
        
{$IFDEF Delphi3Below}
        if Reg.OpenKey('\SOFTWARE\MICROSOFT\MS SETUP (ACME)\USER INFO', False) then 
        
{$ELSE}
        if Reg.OpenKeyReadOnly('\SOFTWARE\MICROSOFT\MS SETUP (ACME)\USER INFO') then
        
{$ENDIF}
          case UserOrCompany of
            _USER : Result := Reg.ReadString('DefName');
            _COMPANY: Result := Reg.ReadString('DefCompany');
            _BOTH : Result := Reg.ReadString('DefName') + #13 +
                              Reg.ReadString('DefCompany'); 
          end
      end;

    if (Result = '') or (Result = '' + #13 + '') then
      
{ Could not find info. }
      if UserOrCompany = _BOTH then
        Result := 'Undefined' + #13 + 'Undefined'
      else
        Result := 'Undefined';
  finally
    Reg.CloseKey;
    Reg.Free;
  end;
end;



Blue Orb Software

[email protected]