Unsolved How to read registry binary data using QT?
-
@Ratzz said in How to read registry binary data using QT?:
QByteArray data = QByteArray((const char*)stringg.utf16());
This is simply wrong. If it is binary already then do
QByteArray data = reg.value("OemInfMap").toByteArray();
Do NOT convert binary data to string, then to Unicode string and then to binary again!
-
QByteArray data = reg.value("OemInfMap").toByteArray();
With this why don't I get exact binary data??
-
You ar right. The REG_BINARY type is converted to QString. But there is a bug. If the length of the binary data is odd, the last data is truncated.
in qsettings_win.cpp, line 547, Qt 5.10.0
s = QString::fromWCharArray((const wchar_t *)data.constData(), data.size() / 2); -
@Ratzz Yes, just tested - QVariant contains a string, I expected it to contain QByteArray for binary data.
-
@Devopia53 said in How to read registry binary data using QT?:
You ar right. The REG_BINARY type is converted to QString. But there is a bug. If the length of the binary data is odd, the last data is truncated.
in qsettings_win.cpp, line 547, Qt 5.10.0
s = QString::fromWCharArray((const wchar_t *)data.constData(), data.size() / 2);This in itself is not a bug. A "wide char array" is an array of bytes in which each pair represents one character. Therefore the length must be even, and the code is as correct as it can be.
If the data is arbitrary bytes, as it seems to be, it should never be treated as a wide char array/
QString
.I don't know, but does (unfixed) https://bugreports.qt.io/browse/QTBUG-98 imply this is simply still not doable in Qt5?
It could be an enhancement to QSettings to support more registry types which are correctly mapped in the QVariant. Qt should map REG_BINARY to QByteArray
We can't fix this until Qt 5, because REG_BINARY is currently read by QSettings as a utf-16 encoded QString. Changing this would break existing applications.
Naughty, naughty!
-
I also know about Wide-char. If it's a problem with Wide-char, that's right. However, it is a problem that REG_BINARY type is converted to QString without used QByteArray.
-
@Devopia53
See my extra typings in my post. -
Yes I have known the problem for a long time. So, in this case, I use Windows API to handle it directly.
-
@Devopia53
So far as I can see, your approach is indeed required at present. The OP needs to use a native Windows registry call to deal correctly with the data. Perhaps you'd be kind enough to give him a skeleton nudge in this direction for what you wrote to do so? -
Hi @Devopia53
there is even a report for this: https://bugreports.qt.io/browse/QTBUG-52960
-
@JonB said in How to read registry binary data using QT?:
Perhaps you'd be kind enough to give him a skeleton nudge in this direction for what you wrote
This should help with that.
-
memset(keyValue,0,(MAX_PATH * sizeof(wchar_t)));
long lResult1= RegQueryValueEx(hKey,KeyValue1.data(),NULL,NULL,reinterpret_cast<unsigned char*>(keyValue),&bufferSize);
if (!lResult1)
command = QString::fromStdWString(keyValue);finally i used direct win32 api and able to read the binary string. above is the working code.