Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
registry.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include <QtCore/qstringlist.h>
5#include "registry_p.h"
6
8
9using namespace Qt::Literals::StringLiterals;
10
11#ifdef Q_OS_WIN32
12/*
13 Returns the path part of a registry key.
14 e.g.
15 For a key
16 "Software\\Microsoft\\VisualStudio\\8.0\\Setup\\VC\\ProductDir"
17 it returns
18 "Software\\Microsoft\\VisualStudio\\8.0\\Setup\\VC\\"
19*/
20static QString keyPath(const QString &rKey)
21{
22 int idx = rKey.lastIndexOf(u'\\');
23 if (idx == -1)
24 return QString();
25 return rKey.left(idx + 1);
26}
27
28/*
29 Returns the name part of a registry key.
30 e.g.
31 For a key
32 "Software\\Microsoft\\VisualStudio\\8.0\\Setup\\VC\\ProductDir"
33 it returns
34 "ProductDir"
35*/
36static QString keyName(const QString &rKey)
37{
38 int idx = rKey.lastIndexOf(u'\\');
39 if (idx == -1)
40 return rKey;
41
42 QString res(rKey.mid(idx + 1));
43 if (res == "Default"_L1 || res == "."_L1)
44 res = QString();
45 return res;
46}
47#endif
48
49QString qt_readRegistryKey(HKEY parentHandle, const QString &rSubkey, unsigned long options)
50{
51 QString result;
52
53#ifdef Q_OS_WIN32
54 QString rSubkeyName = keyName(rSubkey);
55 QString rSubkeyPath = keyPath(rSubkey);
56
57 HKEY handle = nullptr;
58 LONG res = RegOpenKeyEx(parentHandle, (wchar_t*)rSubkeyPath.utf16(), 0,
59 KEY_READ | options, &handle);
60
61 if (res != ERROR_SUCCESS)
62 return QString();
63
64 // get the size and type of the value
65 DWORD dataType;
66 DWORD dataSize;
67 res = RegQueryValueEx(handle, (wchar_t*)rSubkeyName.utf16(), nullptr, &dataType, nullptr, &dataSize);
68 if (res != ERROR_SUCCESS) {
69 RegCloseKey(handle);
70 return QString();
71 }
72
73 // get the value
74 QByteArray data(dataSize, 0);
75 res = RegQueryValueEx(handle, (wchar_t*)rSubkeyName.utf16(), nullptr, nullptr,
76 reinterpret_cast<unsigned char*>(data.data()), &dataSize);
77 if (res != ERROR_SUCCESS) {
78 RegCloseKey(handle);
79 return QString();
80 }
81
82 switch (dataType) {
83 case REG_EXPAND_SZ:
84 case REG_SZ: {
85 result = QString::fromWCharArray(((const wchar_t *)data.constData()));
86 break;
87 }
88
89 case REG_MULTI_SZ: {
90 QStringList l;
91 int i = 0;
92 for (;;) {
93 QString s = QString::fromWCharArray((const wchar_t *)data.constData() + i);
94 i += s.length() + 1;
95
96 if (s.isEmpty())
97 break;
98 l.append(s);
99 }
100 result = l.join(", "_L1);
101 break;
102 }
103
104 case REG_NONE:
105 case REG_BINARY: {
106 result = QString::fromWCharArray((const wchar_t *)data.constData(), data.size() / 2);
107 break;
108 }
109
110 case REG_DWORD_BIG_ENDIAN:
111 case REG_DWORD: {
112 Q_ASSERT(data.size() == sizeof(int));
113 int i;
114 memcpy((char*)&i, data.constData(), sizeof(int));
115 result = QString::number(i);
116 break;
117 }
118
119 default:
120 qWarning("QSettings: unknown data %u type in windows registry", quint32(dataType));
121 break;
122 }
123
124 RegCloseKey(handle);
125#else
126 Q_UNUSED(parentHandle);
127 Q_UNUSED(rSubkey);
128 Q_UNUSED(options);
129#endif
130
131 return result;
132}
133
134QT_END_NAMESPACE
Combined button and popup list for selecting options.
QString qt_readRegistryKey(HKEY parentHandle, const QString &rSubkey, unsigned long options)
Definition registry.cpp:49
void * HKEY
Definition registry_p.h:23