[SOLVED] Reading a registry key lazarus / 34-Windows ---------------------------------------------------------------------- petevick · 2024-03-08 17:16 I'm trying to read the values of the registry key at.... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i \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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var    reg: TRegistry;   ....   ....   ....   reg := TRegistry.Create(KEY_READ);   reg.RootKey := HKEY_LOCAL_MACHINE;   if reg.OpenKeyReadOnly('\SOFTWAR E\Microsoft\Windows\CurrentVersion\Installer')   then begin    if reg.KeyExists('Folders') then begin     ....     ....     ....    end;     reg.CloseKey;   end;  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 ---------------------------------------------------------------------- Bart · 2024-03-08 17:38 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  R: TRegistry;  B: Boolean;  SL: TStringList;  i: Integer;  S: String;begin  R := TRegistry.Create(KEY_READ);  SL := TStringList.Create;  try    R.RootKey := HKEY_CURRENT_USER;    B := R.OpenKeyReadOnly('Software\FPCTEST\Foldertest');    Assert(B,'OpenKey failed.');    B := R.ValueExists('Folders');    writeln('ValueExists(''Folders'')=',B);    R.ReadStringList('Folders',SL);    writeln('Read Folders:');    writeln('SL.Count: ',SL.Count);    for i := 0 to SL.Count-1 do writeln(i:2,': ',SL[i]);  finally    SL.Free;    R.Free;  end;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 ---------------------------------------------------------------------- petevick · 2024-03-08 18:06 Thanks for the reply Bart. Using your code but my reg key path.... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  R: TRegistry;  B: Boolean;  SL: TStringList;  i: Integer;  S: String;begin  R := TRegistry.Create(KEY_READ);  SL := TStringList.Create;  try    R.RootKey := HKEY_LOCAL_MACHINE;    B := R .OpenKeyReadOnly(SOFTWARE\Microsoft\Windows\CurrentVersion\Installer') ;    Assert(B,'OpenKey failed.');    B := R.ValueExists('Folders');    writeln('ValueExists(''Folders'')=',B);    R.ReadStringList('Folders',SL);    writeln('Read Folders:');    writeln('SL.Count: ',SL.Count);    for i := 0 to SL.Count-1 do writeln(i:2,': ',SL[i]);  finally    SL.Free;    R.Free;  end;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. ---------------------------------------------------------------------- ASerge · 2024-03-08 18:28 Quote from: petevick on March 08, 2024, 06:16:25 pmbut reg.KeyExists() is false..... This is strange. I have this code that outputs the entire list: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses Registry; procedure TForm1.Button1Click(Sender: TObject);var  R: TRegistry;begin  R := TRegistry.Create;  try    R.RootKey := HKEY_LOCAL_MACHINE;    if R.OpenKeyReadOnly('\SOFTWARE\Microsoft\Windo ws\CurrentVersion\Installer\Folders') then      R.GetValueNames(Memo1.Lines);  finally    R.Free;  end;end; ---------------------------------------------------------------------- Bart · 2024-03-08 18:35 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  R: TRegistry;  B: Boolean;  S: String;begin  R := TRegistry.Create(KEY_READ);  try    R.RootKey := HKEY_CURRENT_USER;  writeln(format('CurrentPath: "%s"',[String(R.CurrentPath)]));    repeat      write('Key: ');      readln(S);      if (S <> '') then    begin        B := R.KeyExists(S);        writeln('KeyExists(''',S,''')=',B);        B := R.OpenKey(S,False);      if B then          writeln(format('CurrentPath: "%s"',[String(R.CurrentPath)]))        else          writeln(format('Unable to open key: "%s"',[S]));      end;    until (S='');  finally    R.Free;  end;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 · 2024-03-08 19:09 Thanks again Bart and ASerge, I'll check out your posts tomorrow 👍 ---------------------------------------------------------------------- petevick · 2024-03-09 08:26 Quote from: ASerge on March 08, 2024, 07:28:24 pmQuote from: petevick on March 08, 2024, 06:16:25 pmbut reg.KeyExists() is false..... This is strange. I have this code that outputs the entire list: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses Registry; procedure TForm1.Button1Click(Sender: TObject);var  R: TRegistry;begin  R := TRegistry.Create;  try    R.RootKey := HKEY_LOCAL_MACHINE;    if R.OpenKeyReadOnly('\SOFTWARE\Microsoft\Windo ws\CurrentVersion\Installer\Folders') then      R.GetValueNames(Memo1.Lines);  finally    R.Free;  end;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\Microsof t\Windows\CurrentVersion\Installer\Folders  Is it possible that running Win10 in a VirtualBox has something to do with it, seems unlikely to me ---------------------------------------------------------------------- petevick · 2024-03-09 09:13 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. ---------------------------------------------------------------------- petevick · 2024-03-09 09:30 I thought it might have been a permissions issue, but the permissions for both keys appear to be the same. ---------------------------------------------------------------------- ASerge · 2024-03-09 09:32 Quote from: petevick on March 09, 2024, 10:13:43 amCould someone confirm one way or another which are working please. Both are working. Lazarus 3.2 x64, FPC 3.2.2 ---------------------------------------------------------------------- petevick · 2024-03-09 09:40 Quote from: ASerge on March 09, 2024, 10:32:18 amQuote from: petevick on March 09, 2024, 10:13:43 amCould 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  :'( ---------------------------------------------------------------------- Bart · 2024-03-09 09:41 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 · 2024-03-09 09:46 Quote from: Bart on March 09, 2024, 10:41:31 amRunning 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 ---------------------------------------------------------------------- Bart · 2024-03-09 10:04 Quote from: petevick on March 09, 2024, 10:46:17 amQuote from: Bart on March 09, 2024, 10:41:31 amRunning 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 · 2024-03-09 10:07 Quote from: Bart on March 09, 2024, 11:04:50 amQuote from: petevick on March 09, 2024, 10:46:17 amQuote from: Bart on March 09, 2024, 10:41:31 amRunning 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 ---------------------------------------------------------------------- petevick · 2024-03-09 15:06 Quote from: Bart on March 09, 2024, 10:41:31 amRunning 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 I think you were on to something Bart, on this website - https://stackoverflow.com/questions/16938305/how-to-check-if-a- registry-key-exists - it mentions.... QuoteIf you actually need to do this with HKLM (as you state in a comment), watch out for registry redirection confusion when running 32 bit process on 64 bit system.....so I believe this could well be the issue as I use a 32bit install of Laz in a 64bit Win OS, and my little project works fine in a 32 bit Win7 VM. I'll install a 64 bit Laz to confirm. ---------------------------------------------------------------------- petevick · 2024-03-09 15:26 Quote from: petevick on March 09, 2024, 04:06:52 pm....so I believe this could well be the issue as I use a 32bit install of Laz in a 64bit Win OS, and my little project works fine in a 32 bit Win7 VM. I'll install a 64 bit Laz to confirm. This was indeed the issue !! works just fine with a 64 bit Laz and OS. Thanks to all who posted, particularly Bart who gave me the clue. ---------------------------------------------------------------------- Fibonacci · 2024-03-09 15:37 If you want to access 64 bit registry from 32 bit app simply use KEY_WOW64_64KEY access. TRegistry.Create(KEY_WOW64_64KEY) There is also FS redirection which can be disabled: https://learn.microsoft.com/en-us/windows/win32/api/wow64apiset/nf- wow64apiset-wow64disablewow64fsredirection ---------------------------------------------------------------------- petevick · 2024-03-09 16:08 Quote from: Fibonacci on March 09, 2024, 04:37:43 pmIf you want to access 64 bit registry from 32 bit app simply use KEY_WOW64_64KEY access. TRegistry.Create(KEY_WOW64_64KEY) There is also FS redirection which can be disabled: https://learn.microsoft.com/en-us/windows/win32/api/wow64apiset/nf- wow64apiset-wow64disablewow64fsredirection well I'll be damned, KEY_WOW64_64KEY works in both the example keys. Thank you very much Fibonacci, that will save me a fair bit of future hassle