Search code examples
delphiregistry

Why doesn't my code find any registry keys?


I want to list USB VIDs from the registry, and I wrote the following Delphi code:

procedure FindUSBvids(VIDs: TStrings);
var
  Reg1: TRegistry;
begin
  Reg1 := TRegistry.Create;
  try
    Reg1.RootKey := HKEY_LOCAL_MACHINE;
    Reg1.OpenKey('System\CurrentControlSet\Enum\USB', False);
    Reg1.GetKeyNames(VIDs);
  finally
    Reg1.Free;
  end;
end;

procedure TForm1.Button6Click(Sender: TObject);
  FindUSBvids(Memo2.Lines);
end;

Unfortunately the Memo remains empty. I triple-checked the key, it exists and has a couple dozen subkeys. What am I doing wrong?

(Note: I'm not sure this is the right way to find USB VIDs, but that's not the point. It's just that I'm puzzled why the code doesn't produce any result.)


Solution

  • You are asking for write access to the registry key. Use OpenKeyReadOnly instead of OpenKey.