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