Checho Checho - 4 months ago 30
C Question

Showing value's data with RegGetValue

I am trying to understand how to use the PVOID data type in the WinApi, so I tried to show a value called MyValue (it is a DWORD value) using RegGetValue function, but It did not work for me.

Here is my code:

int wmain()
{
LONG openKey;
HKEY hKey = HKEY_CURRENT_USER;
LPCWSTR subKey = L"WinSide\\Config";
DWORD options = 0;
REGSAM samDesired = KEY_READ | KEY_WRITE;

//Allocating memory for a HKEy value.
PHKEY pkOpenResult = (PHKEY)malloc(sizeof(HKEY));

//GetValue
LONG getValue;
LPCWSTR pValue = L"MyValue";
DWORD flags = RRF_RT_ANY;

//Allocationg memory for a DWORD value.
LPDWORD dataType = (LPDWORD)malloc(sizeof(DWORD));

WCHAR value[255];

PVOID pvData = (PVOID)value; //No idea if this is the rigth way.
DWORD size = 8192;
LPDWORD pcbData = &size;

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

if (openKey != ERROR_SUCCESS)
{
wprintf(L"The %s subkey could not be opened. Error code: %li\n", subKey, openKey);
free(pkOpenResult);
}
else
{
wprintf(L"Subkey opened!\n");
getValue = RegGetValue(*pkOpenResult, NULL, pValue, flags, dataType, pvData, pcbData);

if (getValue != ERROR_SUCCESS)
{
wprintf(L"Error getting value. Code: %li\n", getValue);
free(pkOpenResult);
}
else
{
wprintf(L"Value data: %s\n", pvData);
free(pkOpenResult);
}
}


return 0;

}


This is what the command prompt shows:


Subkey opened! Value data: ?


No idea how I should declare and use the pvData value to show the rigth content.

Can you help me?

There were another similar posts, but I did not understand their code, so decided to show my own.

Thank you very much.

Answer

you must select wprintf format (%x, %s, ..) based on dataType

Can you tell me what mistakes I have, so I can correct them? I'm just trying to learn

try for begin code like this:

int wmain()
{
    HKEY hKey = HKEY_CURRENT_USER;
    LPCWSTR subKey = L"WinSide\\Config";
    DWORD options = 0;
    REGSAM samDesired = KEY_READ;// | KEY_WRITE - need ?;

    HKEY OpenResult;

    LPCWSTR pValue = L"MyValue";
    DWORD flags = RRF_RT_ANY;

    //Allocationg memory for a DWORD value.
    DWORD dataType;

    WCHAR value[255];
    PVOID pvData = value;

    DWORD size = sizeof(value);// not 8192;

    LONG err = RegOpenKeyEx(hKey, subKey, options, samDesired, &OpenResult);

    if (err != ERROR_SUCCESS)
    {
        wprintf(L"The %s subkey could not be opened. Error code: %x\n", subKey, err);
    }
    else
    {
        wprintf(L"Subkey opened!\n");
        err = RegGetValue(OpenResult, NULL, pValue, flags, &dataType, pvData, &size);

        if (err != ERROR_SUCCESS)
        {
            wprintf(L"Error getting value. Code: %x\n", err);
        }
        else
        {
            switch (dataType)
            {
            case REG_DWORD:
                wprintf(L"Value data: %x\n", *(DWORD*)pvData);
                break;
            case REG_SZ:
                if ( !*((PWSTR)((PBYTE)pvData + size)-1) )
                {
                    wprintf(L"Value data: %s\n", (PWSTR)pvData);
                }
                else
                {
                    wprintf(L"Value data: %.*s\n", size / sizeof(WCHAR), (PWSTR)pvData);
                }
                break;
                //...
            }
        }
        RegCloseKey(OpenResult);// don't forget !
    }

    return err;   
}

size != 8192;

RegCloseKey - where ?

   //Allocationg memory for a DWORD value.
    LPDWORD dataType = (LPDWORD)malloc(sizeof(DWORD));

horror

Comments