Home > Software engineering >  Registry query information gives incorrect values
Registry query information gives incorrect values

Time:09-18

With the code bellow i have been trying to query value information under a certain registry key. I'm just interested in the amount of values, value name length and value size. But when i run the code it only gives the correct value for number of values. The other values are too long and incorrect. If i lengthen the values it changes, but other than the number of values, the values never gets shorter unless i removes the variables outright.

The code used:

HKEY openRegister(HKEY rootKey,const wchar_t* subKey)
{   
    HKEY hKey;
    LONG result=RegOpenKeyEx(rootKey,subKey,0,KEY_QUERY_VALUE | KEY_WOW64_64KEY,&hKey);

    if(result!=ERROR_SUCCESS)
    {
        if(result==ERROR_FILE_NOT_FOUND) 
        {
            cout<<"Key not found"<<endl;            
    } 
        else 
        {
            cout<<"Error opening key"<<endl;            
        }
    }
    
    return hKey;
}

bool infoKey(HKEY regKey,DWORD &numValues,DWORD &maxNameLen,DWORD &maxValueSize)
{
    LONG result=RegQueryInfoKeyA
    (
        regKey,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        &numValues,
        &maxNameLen,
        &maxValueSize,
        NULL,
        NULL
    );
    if(result!=ERROR_SUCCESS)   
    {
        cout<<"Error query info"<<endl;
        return false;
    }
    else return true;
}

Calling openRegister and then infoKey functions, gives this output:

Number of values: 2
Max name length: 12
Max value size: 12

while looking in the Registry Editor this is the actual content:

(default) | REG_SZ | (value not set)
string1   | REG_SZ | Hello
string2b  | REG_SZ | Test

Why is the "number of values" only correct? Am i missing something really obvious?

Thanks in advance, Benji.

CodePudding user response:

The lpcbMaxValueLen parameter outputs the size of the longest data expressed in bytes. The longest data in your example is Hello, which as a Unicode string (the format the Registry internally stores strings in) is 6 characters, counting the null terminator, and so is 12 bytes in size (2 bytes per character). So maxValueSize being set to 12 is accurate.

The lpcbMaxValueNameLen parameter outputs the length of the longest name expressed in Unicode characters, not counting the null terminator. The longest name in your example is string2b, which has a length of 8 characters. So you would expect maxNameLen should be getting set to 8, not 12. But apparently there is an undocumented minimum size to name buffers. Why, who knows.

On a side note, you are opening the key using a Unicode function, and then querying its info using an ANSI function. You should not mix ANSI and Unicode APIs like that. You should be using RegQueryInfoKeyW() instead of RegQueryInfoKeyA(), to match your use of RegOpenKeyExW() (which RegOpenKeyEx() calls, since you are passing it a wchar_t string).

  • Related