Examples Delphi

The handy procedure GetIdentities() retrieves all user identities on a Windows system and and returns them for display in a TComboBox.
This can be useful for email tools.

procedure GetIdentities(cbIdentities: TComboBox);
var
slIdentities: TStringList;
sUser: string;
sLastUsername: string;
i: Integer;
begin { GetIdentities }
cbIdentities.Items.Clear;
sLastUsername := '';
slIdentities := TStringList.Create;
with TRegistry.Create do
begin
RootKey := HKEY_CURRENT_USER;
if OpenKey('Identities', False) then
begin
sLastUsername := ReadString('Last Username');
GetKeyNames(slIdentities);
CloseKey;
end; { OpenKey() }
// get all the user names
for i := 0 to slIdentities.Count-1 do
begin
if OpenKey('Identities\'+slIdentities[i], False) then
begin
sUser := ReadString('Username');
cbIdentities.Items.Add(sUser+' - '+slIdentities[i]);
if sUser=sLastUsername then
cbIdentities.ItemIndex := i;
CloseKey;
end; { OpenKey() }
end; { for i }
Free;
end; { with TRegistry.Create }
slIdentities.Free;
end; { GetIdentities }