1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the qmake application of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QtCore/qstringlist.h>
30#include "registry_p.h"
31
32QT_BEGIN_NAMESPACE
33
34#ifdef Q_OS_WIN32
35/*
36 Returns the path part of a registry key.
37 e.g.
38 For a key
39 "Software\\Microsoft\\VisualStudio\\8.0\\Setup\\VC\\ProductDir"
40 it returns
41 "Software\\Microsoft\\VisualStudio\\8.0\\Setup\\VC\\"
42*/
43static QString keyPath(const QString &rKey)
44{
45 int idx = rKey.lastIndexOf(QLatin1Char('\\'));
46 if (idx == -1)
47 return QString();
48 return rKey.left(idx + 1);
49}
50
51/*
52 Returns the name part of a registry key.
53 e.g.
54 For a key
55 "Software\\Microsoft\\VisualStudio\\8.0\\Setup\\VC\\ProductDir"
56 it returns
57 "ProductDir"
58*/
59static QString keyName(const QString &rKey)
60{
61 int idx = rKey.lastIndexOf(QLatin1Char('\\'));
62 if (idx == -1)
63 return rKey;
64
65 QString res(rKey.mid(idx + 1));
66 if (res == QLatin1String("Default") || res == QLatin1String("."))
67 res = QString();
68 return res;
69}
70#endif
71
72QString qt_readRegistryKey(HKEY parentHandle, const QString &rSubkey, unsigned long options)
73{
74 QString result;
75
76#ifdef Q_OS_WIN32
77 QString rSubkeyName = keyName(rSubkey);
78 QString rSubkeyPath = keyPath(rSubkey);
79
80 HKEY handle = nullptr;
81 LONG res = RegOpenKeyEx(parentHandle, (wchar_t*)rSubkeyPath.utf16(), 0,
82 KEY_READ | options, &handle);
83
84 if (res != ERROR_SUCCESS)
85 return QString();
86
87 // get the size and type of the value
88 DWORD dataType;
89 DWORD dataSize;
90 res = RegQueryValueEx(handle, (wchar_t*)rSubkeyName.utf16(), nullptr, &dataType, nullptr, &dataSize);
91 if (res != ERROR_SUCCESS) {
92 RegCloseKey(handle);
93 return QString();
94 }
95
96 // get the value
97 QByteArray data(dataSize, 0);
98 res = RegQueryValueEx(handle, (wchar_t*)rSubkeyName.utf16(), nullptr, nullptr,
99 reinterpret_cast<unsigned char*>(data.data()), &dataSize);
100 if (res != ERROR_SUCCESS) {
101 RegCloseKey(handle);
102 return QString();
103 }
104
105 switch (dataType) {
106 case REG_EXPAND_SZ:
107 case REG_SZ: {
108 result = QString::fromWCharArray(((const wchar_t *)data.constData()));
109 break;
110 }
111
112 case REG_MULTI_SZ: {
113 QStringList l;
114 int i = 0;
115 for (;;) {
116 QString s = QString::fromWCharArray((const wchar_t *)data.constData() + i);
117 i += s.length() + 1;
118
119 if (s.isEmpty())
120 break;
121 l.append(s);
122 }
123 result = l.join(QLatin1String(", "));
124 break;
125 }
126
127 case REG_NONE:
128 case REG_BINARY: {
129 result = QString::fromWCharArray((const wchar_t *)data.constData(), data.size() / 2);
130 break;
131 }
132
133 case REG_DWORD_BIG_ENDIAN:
134 case REG_DWORD: {
135 Q_ASSERT(data.size() == sizeof(int));
136 int i;
137 memcpy((char*)&i, data.constData(), sizeof(int));
138 result = QString::number(i);
139 break;
140 }
141
142 default:
143 qWarning("QSettings: unknown data %u type in windows registry", quint32(dataType));
144 break;
145 }
146
147 RegCloseKey(handle);
148#else
149 Q_UNUSED(parentHandle);
150 Q_UNUSED(rSubkey)
151 Q_UNUSED(options);
152#endif
153
154 return result;
155}
156
157QT_END_NAMESPACE
158
159

source code of qtbase/qmake/library/registry.cpp