Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading Registry Keys 1

Status
Not open for further replies.

topcat01

Programmer
Jul 10, 2003
83
GB
Hi all,

I'm trying to recurse all keys in the registry starting from 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USBSTOR', I want to display only keys equal to 'FriendlyName' in a memo as a simple list.

I tried all the demos and source code I can find but without luck. Is this area of the registry protected in anyway that would prevent me from reading it correctly?

Thanks!
 
normally you should have read permission on those keys (although a few subkeys are not accessible)

show us some code, maybe we can help...

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Out of my pile o' demos I hope to get online someday...recursing anything can be a little tough to get right.

Code:
{$APPTYPE CONSOLE}
program list_registry; uses classes, registry, windows;
  { dump data from registry for specific key given}
  {  written by Glenn9999 at tek-tips.com in 2007.
    Any usage or reposting of this code must include 
    the lines in this comment}
  const
    updkey: string = 'SOFTWARE\Microsoft\Updates';

  function print_value(appcheck: TRegistry; valuename: string): string;
    var
      checktype: TRegDataType;
    begin
      checktype :=  appcheck.GetDataType(valuename);
      if checktype = rdString then
        print_value := appcheck.ReadString(valuename);
    end;

  procedure recurse_list(keyname: string);
    var
      appcheck: TRegistry;
      mainkeys: TStringList;
      mainvalues: TStringList;
      count1, count2: integer;
    begin
      appcheck := TRegistry.Create;
      appcheck.rootkey := HKEY_LOCAL_MACHINE;
      if appcheck.openkey(keyname, false) then
        begin
          writeln('Keyname: ', keyname);
          mainvalues := TStringList.Create;
          appcheck.getvaluenames(mainvalues);
          for count2 := 0 to mainvalues.count - 1 do
            begin
              write(mainvalues.strings[count2], ' = ');
              writeln(print_value(appcheck, mainvalues.strings[count2]));
            end;
          writeln;writeln;
          mainvalues.Destroy;
          mainkeys := TStringList.Create;
          appcheck.getkeynames(mainkeys);
          for count1 := 0 to mainkeys.count - 1 do
            recurse_list(keyname + '\' + mainkeys.strings[count1]);
          mainkeys.Destroy;
        end;
      appcheck.Destroy;
    end;

  begin
   { querying HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Updates
     and all relevant sub-keys }
    recurse_list(updkey);

    write('Press a key.');readln;
  end.
 
Did this help solve your problem, topcat01?

Would it be worthy to finish out this code (clean it up, complete print_value, and add a few comments), and post it as a FAQ?
 
Hi there,

Yes it was, thanks again.

I also found this code to be useful:

Code:
procedure TForm1.ShowTypedUrls(Urls: TStrings);
var
  Reg: TRegistry;
  S: TStringList;
  i: Integer;
begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_CURRENT_USER;
    if Reg.OpenKey('Software\Microsoft\Internet Explorer\TypedURLs', False) then
    begin
      S := TStringList.Create;
      try
        reg.GetValueNames(S);
        for i := 0 to S.Count - 1 do
        begin
          Urls.Add(reg.ReadString(S.Strings[i]));
        end;
      finally
        S.Free;
      end;
      Reg.CloseKey;
    end;
  finally
    Reg.Free;
  end;
end;


Cheers!
 
Once upon a time I wrote a program that would also recurse the entire registry structure on a freshly installed OS, so I could put everything to a database, then compare certain attributes on my clients machines to assist in the detection of viruses or other misc bits of info. I ran into issues where I'd end up in an infinite recursion. I could get so far down the tree, going in and out of the tree, but at certain points, it'd just get stuck in a loop. Basically, it'd keep opening the same key over and over again.

I'll have to store these examples in my Team Coherance so I don't lose them. ;)

-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=
NEVER send the boss to do a techs job
 
May I suggest you download the Delphi Fundamentals library (An essential library!). It is available on sourceforge. It is an open source library chock full of useful stuff including all the registry functions you need for reading and writing to the registry.

Even if you don't use it, you'll probably work out what you're doing wrong by examining the code.

Adam



_______________________________
There's no place like 127.0.0.1
_______________________________
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top