我一直在使用来自HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography
的密钥MachineGuid
的值来唯一标识主机,但是使用在64位计算机上运行的32位进程的值似乎丢失了。我想它是在Wow6432Node下搜索的,它确实缺少。根据this你应该能够通过添加一个标志,以获得正确的关键,但下面的代码仍然没有出现工作。我错过了什么?如何从32位进程读取64位注册表项?
const
KEY_WOW64_64KEY=$0100;
var
r:HKEY;
s:string;
i,l:integer;
begin
//use cryptography machineguid, keep a local copy of this in initialization?
l:=40;
if RegOpenKeyEx(HKEY_LOCAL_MACHINE,PChar('Software\Microsoft\Cryptography'),
0,KEY_QUERY_VALUE,r)=ERROR_SUCCESS then
begin
SetLength(s,l);
if RegQueryValue(r,'MachineGuid',PChar(s),l)=ERROR_SUCCESS then
begin
SetLength(s,l);
RegCloseKey(r);
end
else
begin
//try from-32-to-64
RegCloseKey(r);
if RegOpenKeyEx(HKEY_LOCAL_MACHINE,PChar('Software\Microsoft\Cryptography'),
0,KEY_QUERY_VALUE or KEY_WOW64_64KEY,r)=ERROR_SUCCESS then
begin
l:=40;
if RegQueryValue(r,'MachineGuid',PChar(s),l)=ERROR_SUCCESS then
SetLength(s,l)
else
l:=0;
RegCloseKey(r);
end;
end;
end;
为什么不使用TRegistry?你的逻辑也是一团糟。你想要一个函数来调用来读取值。称它两次。第一次通过0.第二次通过KEY_WOW64_64KEY。如果第一次失败,只调用第二次。这是提取方法重构。 – 2012-02-02 22:59:26
我之前使用过TRegistry,但并不知道你通过带参数的构造函数添加KEY_WOW64_64KEY。 – 2012-02-03 10:02:32
当然可以。看到我的答案。编辑:看起来你现在找到了我的答案。另请注意,您可以随时修改'Access'属性以在已创建的注册表对象中切换视图。 – 2012-02-03 10:07:41