c++dword

How to read QWORD (64-bit) from the registry in C++?


How do I read a REG_QWORD from the registry? Most specifically the HardwareInformation.qwMemorySize . I found that with it divided by 1024 then once again divided by 1024 you can get the Video memory in megabytes. How can I read the QWORD first? I can only find how to read DWORDs.


Solution

  • You read a QWORD the exact same way you read a DWORD, using RegQueryValueEx(), just with a 64-bit integer variable instead of a 32-bit integer variable, eg:

    HKEY hKey;
    if (RegOpenKeyEx(..., KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) {
        QWORD value = 0; // or UINT64, ULONGLONG, ULONG64, ULARGE_INTEGER, etc...
        DWORD dwType, dwSize = sizeof(value);
    
        if (RegQueryValueEx(hKey, _T("HardwareInformation.qwMemorySize"), NULL, &dwType, reinterpret_cast<LPBYTE>(&value), &dwSize) == ERROR_SUCCESS) {
            if (dwType == REG_QWORD || dwType == REG_BINARY) {
                // use value as needed...
            }
        }
    
        RegCloseKey(hKey);
    }
    

    Or, using RegGetValue() instead:

    QWORD value = 0; // see above...
    DWORD dwSize = sizeof(value);
    
    if (RegGetValue(hkey, NULL, _T("HardwareInformation.qwMemorySize"), RRF_RT_QWORD, NULL, &value, &dwSize) == ERROR_SUCCESS) {
        // use value as needed...
    }