Clyde
Clyde

Reputation: 1021

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.

Upvotes: 2

Views: 3797

Answers (2)

László Papp
László Papp

Reputation: 53165

I would personally use QSettings for this task in a way something like this:

QSettings settings("HARDWARE\\DEVICEMAP\\SERIALCOMM", QSettings::NativeFormat);
qDebug() << settings.value("Default", "0").toString();

This is probably not the proper semantics, but you get the idea. You can read more upon the details in the official documentation.

Upvotes: 3

Kurt Pattyn
Kurt Pattyn

Reputation: 2788

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.

Upvotes: 3

Related Questions