Sergio Calderon
Sergio Calderon

Reputation: 867

Error code 87 with RegEnumValue

I am trying to show a single value's name using RegEnumValue (I can use RegGetValue, but i'm trying to learn how to use the RegEnumValue function), but I'm getting the error code 87, "The parameter is incorrect".

Here's my code:

int wmain()
{
    //RegOpenKeyEx
    HKEY hKey = HKEY_CURRENT_USER;
    LPCWSTR subKey = L"WinSide";
    DWORD options = 0;
    REGSAM samDesired = KEY_QUERY_VALUE;
    HKEY pkOpenResult;

    //RegEnumValue
    DWORD index = 0;
    WCHAR valueName[16383];
    LPWSTR pValueName = valueName;
    DWORD size=sizeof(valueName);
    DWORD reserved = NULL;
    DWORD type;
    WCHAR data[255];
    LPWSTR pData=data;
    DWORD sizeData = sizeof(data);

    LONG openKey = RegOpenKeyEx(hKey, subKey, options, samDesired, &pkOpenResult);

    if (openKey != ERROR_SUCCESS)
    {
        wprintf(L"Error opening the key. Code: %li\n");
    }
    else
    {
        wprintf(L"Key opened!\n");

        LONG enumValue = RegEnumValue(pkOpenResult, index, pValueName, &size,
                                        &reserved, &type, pData, &sizeData);

        if (enumValue != ERROR_SUCCESS)
            wprintf(L"Error code: %li\n", enumValue);
        else
        {
            wprintf(L"Going to show the value's name here!");
        }

        RegCloseKey(pkOpenResult);
    }


    return 0;
} 

What am I doing wrong?

So sorry for any mistake.

Upvotes: 0

Views: 577

Answers (1)

David Heffernan
David Heffernan

Reputation: 613013

lpReserved must be NULL as stated in the documentation. You pass a non-null value. That is the reason for the failure. Remove the reserved variable and simply pass NULL for lpReserved.

The lpcchValueName argument specifies characters rather than bytes. You pass the number of bytes in error. That mistake won't necessarily hurt you now but there is a buffer overrun error in the making there so you do need to fix it.

Upvotes: 1

Related Questions