无法从注册表中读取密钥

Chr*_*oss 1 delphi registry

我听说Windows为PC创建了一个名为"MachineID"的唯一密钥.我在我的注册表中找到了两个位置.只有"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography"位置应该是正确的.我尝试通过此函数读取值:

    Function GetMaschineID:string;
var
Reg : TRegistry;

//HKEY_CURRENT_USER\Software\Microsoft\MSNMessenger    =   {dd239a44-fa0d-43ff-a51c-5561d3e39de3}
//HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography   =   a06b0ee0-b639-4f55-9972-146776bcd5e4
begin
Reg := TRegistry.Create(KEY_READ);
try
Reg.Rootkey:=HKEY_LOCAL_MACHINE; //Hauptschlüssel
//Reg.RootKey:=HKEY_CURRENT_USER;
if Reg.OpenKey('SOFTWARE\Microsoft\Cryptography\',false) then //Unterschlüssel öffnen
//if Reg.OpenKey('Software\Microsoft\MSNMessenger\',false) then //Unterschlüssel öffnen
    begin
    Result:=Reg.ReadString('MachineGuid');
    end;
finally
     Reg.Free;
end;
end;
Run Code Online (Sandbox Code Playgroud)

此版本导致空字符串; 你看到注册表的结果评论."hkey_Current_user"的第二个版本带来了预期的字符串结果.

我的代码有什么问题,或者注册表的部分内容是否受到保护?

Dav*_*nan 7

可能的解释1

因为HKLM您需要注册表重定向.您有一个32位进程,并尝试从注册表的64位视图中读取一个键.默认情况下,您的32位进程被重定向到32位视图(实现细节)Wow6432Node.

使用KEY_WOW64_64KEY访问标志从64位视图读取.详细信息如下:如何从32位进程读取64位注册表项?

可能的解释2

由于您正在请求写访问权限且标准用户没有写访问权限,因此您的OpenKey密钥调用失败.请改用.HKLMHKLMOpenKeyReadOnly

其他建议

At the very least you should have debugged this a bit more. Does the call to Reg.OpenKey succeed or fail? You should have debugged enough to know that. Perhaps you did but did not say. If Reg.OpenKey failed then explanation 2 is most likely. Even then, you may subsequently suffer from the other problem.

Note also that your function does not assign to the function result variable, or raise an error, if the call to Reg.OpenKey fails. I would expect that the compiler would have warned you about that.