List registry keys in delphi

I am trying to install a driver on a client machine based on which version of MySQL is installed on the server and for this I want to check the version on the server using the registry key.

However, I need to list the subkey HKEY_LOCAL_MACHINE\SOFTWARE\MySQL AB . Usually, there is only one key under this key, and it usually takes the form: MySQL Server #.# , Where # denotes a number.

But since I do not know what value they have, there is a way to get the key, and then I can get the numbers on behalf of it to determine which driver to install? I think enumerating connections is the best way to get the key, but maybe clever string formatting and looping will work too?

+6
source share
1 answer

The best solution is to list auxiliary keys. Using RegEnumKeyEx , you just do it in a simple loop until there are no more keys to enumerate.

However, listing sub keys in Delphi using TRegistry even easier:

 program _EnumSubKeys; {$APPTYPE CONSOLE} uses SysUtils, Classes, Windows, Registry; procedure EnumSubKeys(RootKey: HKEY; const Key: string); var Registry: TRegistry; SubKeyNames: TStringList; Name: string; begin Registry := TRegistry.Create; Try Registry.RootKey := RootKey; Registry.OpenKeyReadOnly(Key); SubKeyNames := TStringList.Create; Try Registry.GetKeyNames(SubKeyNames); for Name in SubKeyNames do Writeln(Name); Finally SubKeyNames.Free; End; Finally Registry.Free; End; end; begin EnumSubKeys(HKEY_LOCAL_MACHINE, 'Software\Microsoft'); Readln; end. 

One thing that you should pay attention to is a search in the 64-bit representation of the registry. If you have a 64-bit version of MySQL installed, I expect it to use a 64-bit registry representation. In a 32-bit Delphi process on a 64-bit OS, you will need to redirect the registry to a side step. Do this by passing KEY_WOW64_64KEY to the TRegistry constructor.


The alternative you offer is hard coding of all possible version string values ​​in your application. It sounds like a failure awaiting release as soon as a version is released that is not on your hard-coded list.

+11
source

Source: https://habr.com/ru/post/906869/


All Articles