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
9#ifdef Q_OS_WIN32
10/*
11 Returns the path part of a registry key.
12 e.g.
13 For a key
14 "Software\\Microsoft\\VisualStudio\\8.0\\Setup\\VC\\ProductDir"
15 it returns
16 "Software\\Microsoft\\VisualStudio\\8.0\\Setup\\VC\\"
17*/
18static QString keyPath(const QString &rKey)
19{
20 int idx = rKey.lastIndexOf(QLatin1Char('\\'));
21 if (idx == -1)
22 return QString();
23 return rKey.left(idx + 1);
24}
25
26/*
27 Returns the name part of a registry key.
28 e.g.
29 For a key
30 "Software\\Microsoft\\VisualStudio\\8.0\\Setup\\VC\\ProductDir"
31 it returns
32 "ProductDir"
33*/
34static QString keyName(const QString &rKey)
35{
36 int idx = rKey.lastIndexOf(QLatin1Char('\\'));
37 if (idx == -1)
38 return rKey;
39
40 QString res(rKey.mid(idx + 1));
41 if (res == QLatin1String("Default") || res == QLatin1String("."))
42 res = QString();
43 return res;
44}
45#endif
46
47QString qt_readRegistryKey(HKEY parentHandle, const QString &rSubkey, unsigned long options)
48{
49 QString result;
50
51#ifdef Q_OS_WIN32
52 QString rSubkeyName = keyName(rSubkey);
53 QString rSubkeyPath = keyPath(rSubkey);
54
55 HKEY handle = nullptr;
56 LONG res = RegOpenKeyEx(parentHandle, (wchar_t*)rSubkeyPath.utf16(), 0,
57 KEY_READ | options, &handle);
58
59 if (res != ERROR_SUCCESS)
60 return QString();
61
62 // get the size and type of the value
63 DWORD dataType;
64 DWORD dataSize;
65 res = RegQueryValueEx(handle, (wchar_t*)rSubkeyName.utf16(), nullptr, &dataType, nullptr, &dataSize);
66 if (res != ERROR_SUCCESS) {
67 RegCloseKey(handle);
68 return QString();
69 }
70
71 // get the value
72 QByteArray data(dataSize, 0);
73 res = RegQueryValueEx(handle, (wchar_t*)rSubkeyName.utf16(), nullptr, nullptr,
74 reinterpret_cast<unsigned char*>(data.data()), &dataSize);
75 if (res != ERROR_SUCCESS) {
76 RegCloseKey(handle);
77 return QString();
78 }
79
80 switch (dataType) {
81 case REG_EXPAND_SZ:
82 case REG_SZ: {
83 result = QString::fromWCharArray(((const wchar_t *)data.constData()));
84 break;
85 }
86
87 case REG_MULTI_SZ: {
88 QStringList l;
89 int i = 0;
90 for (;;) {
91 QString s = QString::fromWCharArray((const wchar_t *)data.constData() + i);
92 i += s.length() + 1;
93
94 if (s.isEmpty())
95 break;
96 l.append(s);
97 }
98 result = l.join(QLatin1String(", "));
99 break;
100 }
101
102 case REG_NONE:
103 case REG_BINARY: {
104 result = QString::fromWCharArray((const wchar_t *)data.constData(), data.size() / 2);
105 break;
106 }
107
108 case REG_DWORD_BIG_ENDIAN:
109 case REG_DWORD: {
110 Q_ASSERT(data.size() == sizeof(int));
111 int i;
112 memcpy((char*)&i, data.constData(), sizeof(int));
113 result = QString::number(i);
114 break;
115 }
116
117 default:
118 qWarning("QSettings: unknown data %u type in windows registry", quint32(dataType));
119 break;
120 }
121
122 RegCloseKey(handle);
123#else
124 Q_UNUSED(parentHandle);
125 Q_UNUSED(rSubkey);
126 Q_UNUSED(options);
127#endif
128
129 return result;
130}
131
132QT_END_NAMESPACE
Combined button and popup list for selecting options.