Joris Groosman February 2016

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.)

Answers


David Heffernan February 2016

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


SilverWarior February 2016

Are you running your application with elevated access rights as administrator or as a normal user.

When you are using OpenKey Delphi by default tries to access that registry key using Read and Write access.

Now on Windows XP this would be just fine but on Windows Vista and newer this can be blocked by UAC especially if you are trying to read registry contents from HKEY_LOCAL_MACHINE part because this part of the registry is protected and requires elevated privileges to be modified.

So you need either to launch your application with administrative rights or change the Access property to `KEY_READ'. I would recommend using the lather if you are only enumerating the USB devices.

Post Status

Asked in February 2016
Viewed 2,039 times
Voted 5
Answered 2 times

Search




Leave an answer