My code:
procedure EnumCommPorts(aStrings: TStrings);
var
Registry: TRegistry;
begin
Registry := TRegistry.Create;
try
Registry.RootKey := HKEY_LOCAL_MACHINE;
Registry.OpenKey('hardware\devicemap\serialcomm', False);
Registry.GetKeyNames(aStrings);
finally
Registry.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
EnumCommPorts(Memo1.Lines);
end;
The screenshot below shows that there are 2 comm ports defined:
However, when I click the button Memo1 remains empty. How do I fix this?
You have a couple of mistakes.
First of all you are attempting to open the registry with write access. You won't get that as a standard user, with UAC enabled, under HKLM.
And secondly you call GetKeyNames
by mistake. That returns the names of all sub-keys. The SERIALCOMM
node has no sub-keys. Just so that you are clear, the DEVICEMAP
node has five sub-keys, one of which is SERIALCOMM
. You need to call GetValueNames
.
This code will work:
Registry.RootKey := HKEY_LOCAL_MACHINE;
Registry.OpenKeyReadOnly('hardware\devicemap\serialcomm');
Registry.GetValueNames(aStrings);