c++windowregistrydword

Getting REG_DWORD from windows registry as a wstring


I am working on a test which should check the registry value. My goal is to take 3 windows registry variables. I am using the modified solution from this LINK. The issue is that when I try to get the value which is REG_DWORD it just prints empty brackets. When I try to use it on REG_SZ it works perfectly fine. For now I use this code:

wstring UpgradeAutocompleteBeCopyFilesUt::ReadRegValue(HKEY root, wstring key, wstring name)
{
    HKEY hKey;
    if (RegOpenKeyEx(root, key.c_str(), 0, KEY_READ, &hKey) != ERROR_SUCCESS)
        throw "Could not open registry key";

    DWORD type;
    DWORD cbData;
    if (RegQueryValueEx(hKey, name.c_str(), NULL, &type, NULL, &cbData) != ERROR_SUCCESS)
    {
        RegCloseKey(hKey);
        throw "Could not read registry value";
    }

    if (type != REG_SZ && type != REG_DWORD)
    {
        RegCloseKey(hKey);
        throw "Incorrect registry value type";
    }

    wstring value(cbData / sizeof(wchar_t), L'\0');
    if (RegQueryValueEx(hKey, name.c_str(), NULL, NULL, reinterpret_cast<LPBYTE>(&value[0]), &cbData) != ERROR_SUCCESS)
    {
        RegCloseKey(hKey);
        throw "Could not read registry value";
    }

    
    RegCloseKey(hKey);
    
    size_t firstNull = value.find_first_of(L'\0');
    
    if (firstNull != string::npos)
        value.resize(firstNull);

    return value;
}

and this is how I print the variables:

std::wcout << L"first: " << regfirst << std::endl;
std::wcout << L"second: " << regsecond << std::endl;
std::wcout << L"third: " << regthird << std::endl;

Third one is REG_DWORD. First two are REG_SZ.

Is there any possible way to get the wstring out of the third variable? I checked registry there should be a value of "1".


Solution

  • if (type != REG_SZ && type != REG_DWORD) ...
    

    You just have to treat REG_SZ and REG_DWORD differently.

    Also add an extra +1 for the null terminator to be safe.

    wstring ReadRegValue(HKEY root, wstring key, wstring name)
    {
        HKEY hKey;
        if (RegOpenKeyEx(root, key.c_str(), 0, KEY_READ, &hKey) != ERROR_SUCCESS)
            throw "Could not open registry key";
        DWORD type;
        DWORD cbData;
        if (RegQueryValueEx(hKey, 
                name.c_str(), NULL, &type, NULL, &cbData) != ERROR_SUCCESS)
        {
            RegCloseKey(hKey);
            throw "Could not read registry value";
        }
    
        std::wstring value;
        if (type == REG_SZ)
        {
            value.resize(1 + (cbData / sizeof(wchar_t)), L'\0');
            if (0 == RegQueryValueEx(hKey, name.c_str(), NULL, NULL,
                reinterpret_cast<BYTE*>(value.data()), &cbData))
            { //okay 
            }
        }
        else if (type == REG_DWORD)
        {
            DWORD dword;
            if (0 == RegQueryValueEx(hKey, name.c_str(), NULL, &type,
                reinterpret_cast<BYTE*>(&dword), &cbData))
                value = std::to_wstring(dword);
        }
        RegCloseKey(hKey);
        return value;
    }