我正在使用 RegOpenKeyEx() 和 RegQueryValueEx() 尝试获取 Windows 注册表中六个键的值。我能够做到六个中的四个,但在某些其他方面失败了。
wchar_t * getRegKeyValue(HKEY rootKeyToGet, LPCWSTR subKeyToGet, LPCWSTR valueToGet)
{
HKEY resultHKey = 0;
wchar_t resultString[255] = L"";
DWORD dwType = REG_SZ;
DWORD resultSize = 255;
// See if the subkey exists. If it does, get its value.
if (RegOpenKeyEx(rootKeyToGet, subKeyToGet, NULL, KEY_ALL_ACCESS, &resultHKey) == ERROR_SUCCESS)
{
RegQueryValueEx(resultHKey, valueToGet, NULL, &dwType, (LPBYTE) &resultString, &resultSize);
}
RegCloseKey(resultHKey);
resultHKey = NULL;
RegCloseKey(rootKeyToGet);
rootKeyToGet = NULL;
return resultString;
}
以下是一些成功的调用:
swprintf(buffer, L"&ie=%s", getRegKeyValue(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Internet Explorer", L"Version"));
swprintf(buffer, L"&os=%s.", getRegKeyValue(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", L"CurrentVersion"));
wcscat(url, getRegKeyValue(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", L"CurrentBuild"));
不成功的调用示例:
wcscpy(buffer, getRegKeyValue(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", L"CSDVersion"));
我能够在不成功的调用中打开 key ,但对该值的查询返回一个空字符串。我以管理员身份运行 Visual Studio。最后一天一直在摸索我哪里出错了。
更新:返回的代码是 ERROR_FILE_NOT_FOUND。这些代码肯定存在于 regedit 中。
最佳答案
我猜你有一个 32 位进程和一个 64 位机器。发生这种情况时,注册表重定向会混淆问题。尝试读取 HKLM\Software\...
会被重定向到 HKLM\Software\Wow64Node\...
。因此,您需要使用 RegistryView
枚举打开注册表的 64 位 View 。
关于c - 无法读取 C 中的某些注册表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7612992/