Mr.C64
Mr.C64

Reputation: 42984

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.

Upvotes: 2

Views: 3835

Answers (3)

zett42
zett42

Reputation: 27766

RegQueryValueEx has a more detailed error specification:

If the lpValueName registry value does not exist, the function returns ERROR_FILE_NOT_FOUND.

So if you want to be on the safe side, use that function instead of RegGetValue.

From your example I see that you don't need any of the special features of RegGetValue and I think these features could even be emulated with a combination of RegOpenKeyEx and RegQueryValueEx.

Upvotes: 4

user7860670
user7860670

Reputation: 37600

It would be reasonable to expect any returned value but ERROR_SUCCESS to be an error code. The exact list of potentially returned error codes probably varies between Windows versions.

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

But your quote from MSDN page actually contains a link to the list of system error codes, you can also get an error description at any time by calling FormatMessageW.

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 613242

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.

Upvotes: 3

Related Questions