PDA

View Full Version : QSettings windows registry reading fails on certain keys



Kwakkie
14th December 2012, 13:29
Hi all

I'd like to read the value of a certain key in the registry. Code is easy enough:



QSettings settings("HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP\\SERIALCOM M", QSettings::NativeFormat);
QStringList keyList = settings.allKeys();
QString sComPort = settings.value(keyList[0], "").toString();


Note that I started with a key string, but as I saw there was something wrong I've tested it with reading the available keys and using that buffer to make sure the key is correct. The problem is that the value is always empty, even though there is a key with a value.

Going deeper into this, the key name is e.g. "/Device/Serial0". And this is where the problem starts. Digging into the QSettings code, value() will call actualKey() which calls normalizedKey(). normalizedKey() returns a string that never starts nor ends with a slash. So it is actually using "Device/Serial0" which doesn't exist!

Can we somehow ask the value without adjusting the key? It seems weird that the Qt lib changes our requested key behind our backs...

Kwakkie
17th December 2012, 09:36
Does anyone have an idea on how to solve this?

kuzulis
17th December 2012, 09:47
Reading the registry in this case - is a bad idea.
Look ready implementation, e.g. for QtSerialPort (http://qt-project.org/wiki/QtSerialPort) (class SerialPortInfo) or another libraries.

To enumerate devices need to use Windows SetupAPI.

Kwakkie
17th December 2012, 10:11
Thanks for the idea. I've tried this with SerialPortInfo::availablePorts(), but the port I need doesn't really "exist" as a serial port (it is some kind of virtual comm device). So unfortunately this won't work for me.

kuzulis
17th December 2012, 10:29
Your virtual device is present into Device Manager?

Kwakkie
17th December 2012, 11:44
No it isn't listed there.

Edit: to be fair, I'm not sure why that would be relevant. What if I wanted to read a completely different key starting with a "/"?

kuzulis
17th December 2012, 12:53
No it isn't listed there.
Then yes, you need to read the registry. Instead QSettings, try WinAPI I think it will be easier.