枚举delphi中的注册表子项 [英] Enumerate registry subkeys in delphi
问题描述
我正在尝试根据服务器上安装的MySQL版本在客户端计算机上安装驱动程序,为此,我想通过注册表项检查服务器上的版本.
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.
也就是说,我需要枚举 HKEY_LOCAL_MACHINE \ SOFTWARE \ MySQL AB
的子项.通常在此键下只有一个键,并且通常采用以下格式: MySQL Server#.#
,其中#代表数字.
That said, I need to enumerate the subkey(s) of 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.
但是因为我不知道这些值是什么,所以有没有办法获取密钥,然后我可以从名称中获取数字来确定要安装哪个驱动程序?我认为枚举子项是获取密钥的最佳方法,但是也许聪明的字符串格式设置和循环也会起作用吗?
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?
推荐答案
最好的解决方案是枚举子键.使用 RegEnumKeyEx
,您只需一个简单的循环即可完成操作,直到不再有要枚举的键为止.
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.
但是,使用 TRegistry
在Delphi中枚举子键仍然更加容易:
However, enumerating sub keys in Delphi using TRegistry
is even easier still:
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.
您需要注意的一件事是必须在注册表的64位视图中进行搜索.如果您安装了64位版本的MySQL,那么我希望它使用注册表的64位视图.在64位OS上的32位Delphi进程中,您将需要绕开注册表重定向.为此,请将 KEY_WOW64_64KEY
传递给 TRegistry
构造函数.
One thing that you should watch out for is having to search in the 64 bit view of the registry. If you have the 64 bit version of MySQL installed then I expect it to use the 64 bit view of the registry. In a 32 bit Delphi process on a 64 bit OS you will need to side step registry redirection. Do that by passing KEY_WOW64_64KEY
to the TRegistry
constructor.
您建议的替代方法是将版本字符串的所有可能值硬编码到您的应用程序中.听起来这是一个等待发布的版本失败的错误,而该版本不在您的硬编码列表中.
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.
这篇关于枚举delphi中的注册表子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!