C++ 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/252297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:03:48  来源:igfitidea点击:

Why is RegOpenKeyEx() returning error code 2 on Vista 64bit?

c++winapiwindows-vistaregistrywow64

提问by Tim Cooper

I was making the following call:

我打了以下电话:

result = RegOpenKeyEx(key, s, 0, KEY_READ, &key);

(C++, Visual Studio 5, Vista 64bit).

(C++、Visual Studio 5、Vista 64 位)。

It is failing with error code 2 ("File not found") even though "regedit" shows that the key exists. This code has always worked on 32bit XP. Why is it "file not found" when it clearly is there?

即使“ regedit”显示密钥存在,它也会失败并显示错误代码 2(“找不到文件”)。此代码始终适用于 32 位 XP。为什么当它明确存在时“找不到文件”?

回答by Tim Cooper

I discovered that I could solve my problem using the flag: KEY_WOW64_64KEY, as in:

我发现我可以使用 flag: 解决我的问题 KEY_WOW64_64KEY,如下所示:

result = RegOpenKeyEx(key, s, 0, KEY_READ|KEY_WOW64_64KEY, &key);

For a full explanation: 32-bit and 64-bit Application Data in the Registry

完整说明:注册表中的 32 位和 64 位应用程序数据

回答by Frode Lillerud

On a Windows 64-bit system the Registry is actually divided into two parts. One section is used by 64-bit processes, and one part by 32-bit processes.

在 Windows 64 位系统上,注册表实际上分为两部分。一部分由 64 位进程使用,另一部分由 32 位进程使用。

For example, if a 32-bit application programatically writes to what it believes is HKLM\SOFTWARE\Company\Application, it's actually redirected by the WoW64-layer to HKLM\SOFTWARE\Wow6432Node\Company\Application.

例如,如果一个 32 位应用程序以编程方式写入它认为是 HKLM\SOFTWARE\Company\Application,它实际上被 WoW64 层重定向到 HKLM\SOFTWARE\Wow6432Node\Company\Application。

So when you run your 32-bit application and call RegOpenKeyEx it's actually working against the Wow6432Node\ folder, and not the regular \SOFTWARE node.

因此,当您运行 32 位应用程序并调用 RegOpenKeyEx 时,它实际上针对的是 Wow6432Node\ 文件夹,而不是常规的 \SOFTWARE 节点。

回答by GMG

You have to compile with "Use Multi-Byte Character Set" or cast string in code to (LPWSTR)

您必须使用“使用多字节字符集”进行编译或将代码中的字符串转换为 (LPWSTR)

回答by Alex

I had a similar problem. I was using:

我有一个类似的问题。我正在使用:

dwResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                                   (LPWSTR)"SOFTWARE\0test",
                                   0,
                                   WRITE_DAC ,
                                   &hKey);

That didn't work. I tried it like this and it worked:

那没有用。我像这样尝试过,它奏效了:

dwResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                                   _T("SOFTWARE\0test"),
                                   0,
                                   WRITE_DAC ,
                                   &hKey);

回答by yue

yes,win7 64B,add further flag KEY_WOW64_64KEY ,it will work. if not work, refer to http://msdn.microsoft.com/en-us/library/ms724897(v=VS.85).aspx

是的,win7 64B,添加进一步的标志 KEY_WOW64_64KEY ,它会工作。如果不起作用,请参阅http://msdn.microsoft.com/en-us/library/ms724897(v=VS.85).aspx