Search code examples
windowsqtregistryqsettings

Reading registry value on windows with QSettings


I'm trying to read MachineGuid key from windows registry using QSettings.

The address to that key is

"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography".

I'm using the QSettings with QSettings::Native flag as follows.

QSettings settings("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography",QSettings::NativeFormat);

I can see all the subfolders and their keys and the value of MachineGuid from regedit.exe but the value function of QSettings does not seem to be working correctly.

the results are as follows:

settings.value("MachineGuid").toString();

returns empty QString.

settings.childGroups();

returns all the subfolders of Cryptography folder correctly.

settings.childKeys();

returns an empty QStringList

settings.allKeys();

returns all the keys inside Cryptography folder including the keys inside of all subfolders except for MachineGuid which is placed inside Cryptography.

I'm using Qt 5.7.1 built statically using Visual Studio 2015 on Windows 10.

I have tried codes that use Window.h header and I have successfully extracted the value but the problem with this approach is that I have to add lots of DLLs to my released software.


Solution

  • For accessing 64 Bit Windows OS with 32 Bit compiled code, the right format would be "Registry64Format" and if accessing 32 Bit OS from 64 compiler the the right format is "Registry32Format" So, in my case, after setting format to 64 bit, the key could be fetched.

    to read key "MachineGuid" on 64 bit OS with 32 bit Compiled code;

    QSettings settings("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography",
    QSettings::Registry64Format);
    auto key = settings.childKeys().at(0);