C++ RegOpenKeyEx 在 HKEY_LOCAL_MACHINE 上失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/820846/
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
RegOpenKeyEx fails on HKEY_LOCAL_MACHINE
提问by Emile Vrijdags
Hi I'm trying to read a registry value that gives me the path to firefox.exe. This is stored under
嗨,我正在尝试读取一个注册表值,该值为我提供了 firefox.exe 的路径。这是存储在
HKEY_LOCAL_MACHINE\SOFTWARE\Mozilla\Mozilla Firefox 3.0.10\bin
(the version number can be found somewhere else)
(版本号可以在其他地方找到)
But I cant seem to get RegOpenKeyEx to return ERROR_SUCCESS for anything under
但我似乎无法让 RegOpenKeyEx 为下的任何内容返回 ERROR_SUCCESS
HKEY_LOCAL_MACHINE
so this test fails:
所以这个测试失败了:
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT("\SOFTWARE"),0,KEY_QUERY_VALUE,&keyHandle) == ERROR_SUCCESS)
while this test passes:
虽然此测试通过:
if(RegOpenKeyEx(HKEY_CLASSES_ROOT,TEXT("\Shell"),0,KEY_QUERY_VALUE,&keyHandle) == ERROR_SUCCESS)
回答by
The following code failed on my machine with the error code 161, which means "bad path" (look it up in winerror.h):
以下代码在我的机器上失败,错误代码为 161,这意味着“路径错误”(在 winerror.h 中查找):
long n = RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT("SOFTWARE"),
0,KEY_QUERY_VALUE, &hk );
I then changed the call to RegOpenKeyEx to use "SOFTWARE" (note no leading slashes) and it worked:
然后我更改了对 RegOpenKeyEx 的调用以使用“SOFTWARE”(注意没有前导斜杠)并且它起作用了:
#include <windows.h>
#include <iostream>
using namespace std;
int main() {
HKEY hk;
// Notice that it's SOFTWARE instead of \SOFTWARE:
long n = RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT("SOFTWARE"),
0,KEY_QUERY_VALUE, &hk );
if ( n == ERROR_SUCCESS ) {
cout << "OK" << endl;
}
else {
cout << "Failed with value " << n << endl;
}
}