Recent

Author Topic: [SOLVED] Reading a registry key  (Read 3110 times)

petevick

  • Sr. Member
  • ****
  • Posts: 419
[SOLVED] Reading a registry key
« on: March 08, 2024, 06:16:25 pm »
I'm trying to read the values of the registry key at....
Code: Pascal  [Select][+][-]
  1. \HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders
....so far I've managed to get as far as Installer, but I can't seem to be able to read Folders. In the following scrap of code reg.OpenKeyReadOnly() is true, but reg.KeyExists() is false.....
Code: Pascal  [Select][+][-]
  1. var
  2.     reg: TRegistry;
  3.    ....
  4.    ....
  5.    ....
  6.    reg := TRegistry.Create(KEY_READ);
  7.    reg.RootKey := HKEY_LOCAL_MACHINE;
  8.    if reg.OpenKeyReadOnly('\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer')
  9.    then begin
  10.     if reg.KeyExists('Folders') then begin
  11.      ....
  12.      ....
  13.      ....
  14.     end;
  15.      reg.CloseKey;
  16.    end;
  17.  

Using Regedit it appears that the Folders key is a list of folder paths that I'll hopefully be able to read in as a TStringArray, this will then enable me to search for the path of a particular application.

This is my first foray into working with the registry and I'm not enjoying it  :D

« Last Edit: March 09, 2024, 05:26:31 pm by petevick »
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

Bart

  • Hero Member
  • *****
  • Posts: 5510
    • Bart en Mariska's Webstek
Re: Reading a registry key
« Reply #1 on: March 08, 2024, 06:38:38 pm »
Hi,

I made a test.
In RegEdit: Computer\HKEY_CURRENT_USER\SOFTWARE\FPCTEST\Foldertest
There created Folders (type REG_MULTI_SZ)
Folders contains 3 strings.

Using this code:
Code: Pascal  [Select][+][-]
  1. var
  2.   R: TRegistry;
  3.   B: Boolean;
  4.   SL: TStringList;
  5.   i: Integer;
  6.   S: String;
  7. begin
  8.   R := TRegistry.Create(KEY_READ);
  9.   SL := TStringList.Create;
  10.   try
  11.     R.RootKey := HKEY_CURRENT_USER;
  12.     B := R.OpenKeyReadOnly('Software\FPCTEST\Foldertest');
  13.     Assert(B,'OpenKey failed.');
  14.     B := R.ValueExists('Folders');
  15.     writeln('ValueExists(''Folders'')=',B);
  16.     R.ReadStringList('Folders',SL);
  17.     writeln('Read Folders:');
  18.     writeln('SL.Count: ',SL.Count);
  19.     for i := 0 to SL.Count-1 do writeln(i:2,': ',SL[i]);
  20.   finally
  21.     SL.Free;
  22.     R.Free;
  23.   end;
  24. end.

This outputs:
Code: [Select]
C:\Users\Bart\LazarusProjecten\bugs\Console\registry>multi
ValueExists('Folders')=TRUE
Read Folders:
SL.Count: 3
 0: Folder0
 1: Folder1
 2: Folder2

Fpc 3.2.2-win32 on Win10.

Notice that a REG_MULTI_SZ entry is a Value, not a Key, hence KeyExists is the wrong call here, you must use ValueExists.

Bart
« Last Edit: March 08, 2024, 06:42:27 pm by Bart »

petevick

  • Sr. Member
  • ****
  • Posts: 419
Re: Reading a registry key
« Reply #2 on: March 08, 2024, 07:06:40 pm »
Thanks for the reply Bart.
Using your code but my reg key path....

Code: Pascal  [Select][+][-]
  1. var
  2.   R: TRegistry;
  3.   B: Boolean;
  4.   SL: TStringList;
  5.   i: Integer;
  6.   S: String;
  7. begin
  8.   R := TRegistry.Create(KEY_READ);
  9.   SL := TStringList.Create;
  10.   try
  11.     R.RootKey := HKEY_LOCAL_MACHINE;
  12.     B := R.OpenKeyReadOnly(SOFTWARE\Microsoft\Windows\CurrentVersion\Installer');
  13.    Assert(B,'OpenKey failed.');
  14.    B := R.ValueExists('Folders');
  15.    writeln('ValueExists(''Folders'')=',B);
  16.    R.ReadStringList('Folders',SL);
  17.    writeln('Read Folders:');
  18.    writeln('SL.Count: ',SL.Count);
  19.    for i := 0 to SL.Count-1 do writeln(i:2,': ',SL[i]);
  20.  finally
  21.    SL.Free;
  22.    R.Free;
  23.  end;
  24. end.

....at line 12 B is true, but at line 14 B is false, it's like Folders doesn't exist, but it does as can be seen in the attachment.
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

ASerge

  • Hero Member
  • *****
  • Posts: 2375
Re: Reading a registry key
« Reply #3 on: March 08, 2024, 07:28:24 pm »
but reg.KeyExists() is false.....
This is strange. I have this code that outputs the entire list:
Code: Pascal  [Select][+][-]
  1. uses Registry;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   R: TRegistry;
  6. begin
  7.   R := TRegistry.Create;
  8.   try
  9.     R.RootKey := HKEY_LOCAL_MACHINE;
  10.     if R.OpenKeyReadOnly('\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders') then
  11.       R.GetValueNames(Memo1.Lines);
  12.   finally
  13.     R.Free;
  14.   end;
  15. end;

Bart

  • Hero Member
  • *****
  • Posts: 5510
    • Bart en Mariska's Webstek
Re: Reading a registry key
« Reply #4 on: March 08, 2024, 07:35:18 pm »
OK your setup is different from mine.
Your "Folders" actually is a key.
And it has several Values in it.

I made a similar setup:

Computer\HKEY_CURRENT_USER\SOFTWARE\FPCTEST\Foldertest2
There I have key Folders
In Folder I have 2 Values: Folder1, Folder2

Now using this code:
Code: Pascal  [Select][+][-]
  1. var
  2.   R: TRegistry;
  3.   B: Boolean;
  4.   S: String;
  5. begin
  6.   R := TRegistry.Create(KEY_READ);
  7.   try
  8.     R.RootKey := HKEY_CURRENT_USER;
  9.     writeln(format('CurrentPath: "%s"',[String(R.CurrentPath)]));
  10.     repeat
  11.       write('Key: ');
  12.       readln(S);
  13.       if (S <> '') then
  14.       begin
  15.         B := R.KeyExists(S);
  16.         writeln('KeyExists(''',S,''')=',B);
  17.         B := R.OpenKey(S,False);
  18.         if B then
  19.           writeln(format('CurrentPath: "%s"',[String(R.CurrentPath)]))
  20.         else
  21.           writeln(format('Unable to open key: "%s"',[S]));
  22.       end;
  23.     until (S='');
  24.   finally
  25.     R.Free;
  26.   end;
  27. end.

I can do this:
Code: [Select]
C:\Users\Bart\LazarusProjecten\bugs\Console\registry>cpath
CurrentPath: ""
Key: software
KeyExists('software')=TRUE
CurrentPath: "software"
Key: fpctest
KeyExists('fpctest')=TRUE
CurrentPath: "software\fpctest"
Key: foldertest2
KeyExists('foldertest2')=TRUE
CurrentPath: "software\fpctest\foldertest2"
Key: folders
KeyExists('folders')=TRUE
CurrentPath: "software\fpctest\foldertest2\folders"

I attached a screenshot of my setup.

Bart

petevick

  • Sr. Member
  • ****
  • Posts: 419
Re: Reading a registry key
« Reply #5 on: March 08, 2024, 08:09:22 pm »
Thanks again Bart and ASerge, I'll check out your posts tomorrow 👍
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

petevick

  • Sr. Member
  • ****
  • Posts: 419
Re: Reading a registry key
« Reply #6 on: March 09, 2024, 09:26:43 am »
but reg.KeyExists() is false.....
This is strange. I have this code that outputs the entire list:
Code: Pascal  [Select][+][-]
  1. uses Registry;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   R: TRegistry;
  6. begin
  7.   R := TRegistry.Create;
  8.   try
  9.     R.RootKey := HKEY_LOCAL_MACHINE;
  10.     if R.OpenKeyReadOnly('\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders') then
  11.       R.GetValueNames(Memo1.Lines);
  12.   finally
  13.     R.Free;
  14.   end;
  15. end;
Your code doesn't work on that particular key, but it works fine on \HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\AllowedDragImageExts and lists all the file extensions, so why the hell isn't it working on \HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders  >:( >:(

Is it possible that running Win10 in a VirtualBox has something to do with it, seems unlikely to me  :-\
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

petevick

  • Sr. Member
  • ****
  • Posts: 419
Re: Reading a registry key
« Reply #7 on: March 09, 2024, 10:13:43 am »
This is a very small project that has two buttons, the first 'should' list the install folder paths, which I can't get to work, the second lists file extensions, which does work for me. Could someone confirm one way or another which are working please.
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

petevick

  • Sr. Member
  • ****
  • Posts: 419
Re: Reading a registry key
« Reply #8 on: March 09, 2024, 10:30:09 am »
I thought it might have been a permissions issue, but the permissions for both keys appear to be the same.  ::) %)
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

ASerge

  • Hero Member
  • *****
  • Posts: 2375
Re: Reading a registry key
« Reply #9 on: March 09, 2024, 10:32:18 am »
Could someone confirm one way or another which are working please.
Both are working. Lazarus 3.2 x64, FPC 3.2.2

petevick

  • Sr. Member
  • ****
  • Posts: 419
Re: Reading a registry key
« Reply #10 on: March 09, 2024, 10:40:41 am »
Could someone confirm one way or another which are working please.
Both are working. Lazarus 3.2 x64, FPC 3.2.2
Thanks ASerge, it looks like I'm stuffed then  :'(
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

Bart

  • Hero Member
  • *****
  • Posts: 5510
    • Bart en Mariska's Webstek
Re: Reading a registry key
« Reply #11 on: March 09, 2024, 10:41:31 am »
Running in 32-bit this  key ('SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders') does not seem to exist, but it runs OK in 64-bit mode.

Bart

petevick

  • Sr. Member
  • ****
  • Posts: 419
Re: Reading a registry key
« Reply #12 on: March 09, 2024, 10:46:17 am »
Running in 32-bit this  key ('SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders') does not seem to exist, but it runs OK in 64-bit mode.

Bart
Thanks Bart, just one more inconsistency to deal with.
I've just tried it with an old Windows 7 VM and both work on that, so there's a bit of hope  ::)
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

Bart

  • Hero Member
  • *****
  • Posts: 5510
    • Bart en Mariska's Webstek
Re: Reading a registry key
« Reply #13 on: March 09, 2024, 11:04:50 am »
Running in 32-bit this  key ('SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders') does not seem to exist, but it runs OK in 64-bit mode.

Bart
Thanks Bart, just one more inconsistency to deal with.
Well, inconsistency added by MS, not by fpc.

Bart

petevick

  • Sr. Member
  • ****
  • Posts: 419
Re: Reading a registry key
« Reply #14 on: March 09, 2024, 11:07:08 am »
Running in 32-bit this  key ('SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders') does not seem to exist, but it runs OK in 64-bit mode.

Bart
Thanks Bart, just one more inconsistency to deal with.
Well, inconsistency added by MS, not by fpc.

Bart
yes indeed, that's what I meant  ;)
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

 

TinyPortal © 2005-2018