无法读取C中的某些注册表项

时间:2011-09-30 15:57:21

标签: c windows registry

我正在使用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"));

我可以在不成功的调用中打开键,但是对该值的查询返回一个空字符串。我以管理员身份运行Visual Studio。在我出错的最后一天,我一直在挠头。

更新:返回的代码是ERROR_FILE_NOT_FOUND。这些代码绝对显示在regedit中。

1 个答案:

答案 0 :(得分:6)

我猜你有32位进程和64位机器。发生这种情况时,注册表重定向会让人感到困惑。尝试阅读HKLM\Software\...会被重定向到HKLM\Software\Wow64Node\...。因此,您需要使用RegistryView枚举打开注册表的64位视图。