c++qtregistryqtcoreqsettings

Qt get registry key


I tried to

QMessageBox msgBox;
HKEY regKey;
WCHAR regKeyName;
DWORD lpcValueName = MAX_PATH;
LONG err = RegOpenKeyEx(HKEY_LOCAL_MACHINE, LPCWSTR("HARDWARE\\DEVICEMAP\\SERIALCOMM"), 0, KEY_QUERY_VALUE | KEY_READ | KEY_WOW64_64KEY, &regKey);
if (err == 0)
{
    msgBox.setText("success " + QString::number(err));
} else
{
    msgBox.setText("error " + QString::number(err));
};
msgBox.exec();

and application always show me the "error 2" msgbox. Why I can't open the registry key? I think LPCWSTR("HARDWARE\\DEVICEMAP\\SERIALCOMM") is not great idea.


Solution

  • First of all you can use QSettings to read from and write to the registry. Secondly, the DEVICEMAP is probably read-only and protected. To check, open the registry editor (regedit) and see if you can change values there manually.

    If your software is about reading and writing from serial ports, you can have a look at the QtSerialPort module.