[英]Enumerate registry subkeys in delphi
I'm attempting to install a driver on a client machine based on which version of MySQL is installed on the server and to do that I'd like to check the version on the server via registry key. 我正在尝试根据服务器上安装的MySQL版本在客户端计算机上安装驱动程序,为此,我想通过注册表项检查服务器上的版本。
That said, I need to enumerate the subkey(s) of HKEY_LOCAL_MACHINE\\SOFTWARE\\MySQL AB
. 也就是说,我需要枚举HKEY_LOCAL_MACHINE\\SOFTWARE\\MySQL AB
的子项。 There is usually just one key under this one and it is generally of the form: MySQL Server #.#
, where # stands for a number. 在这一个下通常只有一个密钥,它通常是以下形式: MySQL Server #.#
,其中#代表一个数字。
But because I don't know which value those are, is there a way to get the key and then I can get the numbers from the name to determine which driver to install? 但是因为我不知道它们是哪个值,有没有办法获取密钥然后我可以从名称中获取数字以确定要安装哪个驱动程序? I'm thinking that enumerating the subkeys is the best way to get the key, but perhaps a clever string formatting and loop would work too? 我认为枚举子键是获取键的最佳方法,但也许一个聪明的字符串格式和循环也可以工作?
The best solution is to enumerate the sub keys. 最好的解决方案是枚举子键。 Using RegEnumKeyEx
you just do that in a simple loop until there are no more keys left to enumerate. 使用RegEnumKeyEx
您只需在一个简单的循环中执行此操作,直到没有其他键可以枚举。
However, enumerating sub keys in Delphi using TRegistry
is even easier still: 但是,使用TRegistry
枚举Delphi中的子键仍然更容易:
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 watch out for is having to search in the 64 bit view of the registry. 您应该注意的一件事是必须在注册表的64位视图中进行搜索。 If you have the 64 bit version of MySQL installed then I expect it to use the 64 bit view of the registry. 如果您安装了64位版本的MySQL,那么我希望它使用注册表的64位视图。 In a 32 bit Delphi process on a 64 bit OS you will need to side step registry redirection. 在64位操作系统上的32位Delphi进程中,您需要侧面注册表重定向。 Do that by passing KEY_WOW64_64KEY
to the TRegistry
constructor. 通过将KEY_WOW64_64KEY
传递给TRegistry
构造函数来做到这一点。
The alternative that you propose is to hard code all the possible values of version string into your application. 您建议的替代方法是将版本字符串的所有可能值硬编码到应用程序中。 That sounds like a failure waiting to happen as soon as a version is released which isn't in your hard coded list. 一旦发布版本不在您的硬编码列表中,这听起来就像是等待发生的失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.