Search code examples
c++hexdecimalregedit

How can I read REG_NONE value in C++?


I want to read REG_NONE value from regedit with C++.

Here are my codes:

#include <iostream>
#include <windows.h>
using namespace std;

//--- Değişkenler ---//
//DWORD
DWORD dw_Rn_Boyut = MAX_PATH;
DWORD dw_Rn_Deger;
DWORD dw_Rn_DegerTipi = REG_NONE;

//HKEY
HKEY hkey_Rn;

//LONG
LONG long_Rn_Sonuc;

int main()
{
    long_Rn_Sonuc = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\DownloadManager\\FoldersTree\\Compressed", 0, KEY_READ | KEY_WOW64_64KEY, &hkey_Rn);

    if(long_Rn_Sonuc == ERROR_SUCCESS)
    {
        long_Rn_Sonuc = RegQueryValueEx(hkey_Rn, "pathW", 0, &dw_Rn_DegerTipi, (LPBYTE)&dw_Rn_Deger, &dw_Rn_Boyut);

        if(long_Rn_Sonuc == ERROR_SUCCESS)
        {
            cout << dw_Rn_Deger;
        }
    }

    getchar();

    return 0;
}

My app shows 3801156 as result. This value is decimal version of that reg value. It equals to 3A 00 44

Here is the reg value which I want to read:

enter image description here

But why does not my app read other hex values?

How can I fix my problem?


Solution

  • There is no such thing as a REG_NONE value. On success, your dw_Rn_DegerTipi variable will be updated with the actual value type (in this case, REG_BINARY), and your dw_Rn_Boyut variable will be updated with the number of actual bytes read.

    Your problem is that you are using the wrong data type for your dw_Rn_Dege variable. The data in question is not a DWORD value, it is a WCHAR character string, so you need a WCHAR character buffer to receive it. You are telling RegQueryValueEx() that you allocated a buffer to hold MAX_PATH bytes, but you really didn't. So you have a buffer overflow error in your code when RegQueryValueEx() tries to write more than 4 bytes to the memory address of dw_Rn_Deger.

    Try this instead:

    #include <iostream>
    #include <windows.h>
    using namespace std;
    
    //--- Değişkenler ---//
    WCHAR sz_Rn_Deger[MAX_PATH+1] = {};
    DWORD dw_Rn_Boyut = MAX_PATH * sizeof(WCHAR);
    
    //HKEY
    HKEY hkey_Rn;
    
    //LONG
    LONG long_Rn_Sonuc;
    
    int main()
    {
        long_Rn_Sonuc = RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\DownloadManager\\FoldersTree\\Compressed", 0, KEY_QUERY_VALUE | KEY_WOW64_64KEY, &hkey_Rn);
    
        if (long_Rn_Sonuc == ERROR_SUCCESS)
        {
            long_Rn_Sonuc = RegQueryValueExW(hkey_Rn, L"pathW", NULL, NULL, reinterpret_cast<LPBYTE>(&sz_Rn_Deger), &dw_Rn_Boyut);
    
            if (long_Rn_Sonuc == ERROR_SUCCESS)
            {
                wcout << sz_Rn_Deger;
            }
    
            RegCloseKey(hkey_Rn);
        }
    
        getchar();
    
        return 0;
    }