简体   繁体   English

Qt获取注册表项

[英]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. 和应用程序总是向我显示“错误2” msgbox。 Why I can't open the registry key? 为什么我无法打开注册表项? I think LPCWSTR("HARDWARE\\\\DEVICEMAP\\\\SERIALCOMM") is not great idea. 我认为LPCWSTR("HARDWARE\\\\DEVICEMAP\\\\SERIALCOMM")不是一个好主意。

First of all you can use QSettings to read from and write to the registry. 首先,您可以使用QSettings来读取和写入注册表。 Secondly, the DEVICEMAP is probably read-only and protected. 其次,DEVICEMAP可能是只读的并且受保护。 To check, open the registry editor (regedit) and see if you can change values there manually. 要进行检查,请打开注册表编辑器(regedit),然后查看是否可以在此处手动更改值。

If your software is about reading and writing from serial ports, you can have a look at the QtSerialPort module. 如果您的软件是关于从串行端口读取和写入的,则可以查看QtSerialPort模块。

I would personally use QSettings for this task in a way something like this: 我个人将QSettings以这种方式使用:

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 . 您可以阅读官方文档中的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM