Search code examples
c++winapiregistry

What does RegGetValue return when the requested registry value is not found?


Suppose you want to develop a function that, given a valid registry key handle and a value name, returns true if the value exists under the input key, false if it doesn't, and throws a C++ exception in all other cases.

bool RegValueExists(HKEY hKey, const std::wstring& value)
{
    LRESULT retCode = ::RegGetValue(
        hKey, 
        nullptr,                   // no subkey 
        value.c_str(), 
        RRF_RT_ANY,                // any type
        nullptr, nullptr, nullptr  // not interested in these
    );

If RegGetValue succeeds, it returns 0; so, in this case I can return true to the caller.

But, from the MSDN documentation of RegGetValue, it's not clear what the API returns in case of registry value not found:

Return value

[...] If the function fails, the return value is a system error code.

In my tests, RegGetValue returns 2 (i.e. ERROR_FILE_NOT_FOUND) in case of values not found. However, I cannot find any official MSDN page documenting that. (Moreover, since when is a registry value a file??)

Among the "system error codes" there's also an ERROR_NOT_FOUND code (1168). Would it be reasonable to expect it as a return code for "registry value not found"?

I think there should be a clear explanation of at least the common error codes in MSDN.


Solution

  • There is no documentation of all the failure modes and their error codes. That's just the way of things. Certain failure modes are explicitly called out with the error code documented.

    However, I can confirm that ERROR_FILE_NOT_FOUND is the error code associated with the failure mode described in the question.