Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qwinregistry.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qwinregistry_p.h"
5#include <QtCore/qvarlengtharray.h>
6#include <QtCore/qendian.h>
7#include <QtCore/qlist.h>
8
10
14
15// Open a key with the specified permissions (KEY_READ/KEY_WRITE).
16// "access" is to explicitly use the 32- or 64-bit branch.
18 REGSAM permissions, REGSAM access)
19{
20 if (RegOpenKeyExW(parentHandle, reinterpret_cast<const wchar_t *>(subKey.utf16()),
21 0, permissions | access, &m_key) != ERROR_SUCCESS) {
22 m_key = nullptr;
23 }
24}
25
30
32{
33 if (isValid()) {
34 RegCloseKey(m_key);
35 m_key = nullptr;
36 }
37}
38
40{
41 // NOTE: Empty value name is allowed in Windows registry, it means the default
42 // or unnamed value of a key, you can read/write/delete such value normally.
43
44 if (!isValid())
45 return {};
46
47 // Use nullptr when we need to access the default value.
48 const auto subKeyC = subKey.isEmpty() ? nullptr : reinterpret_cast<const wchar_t *>(subKey.utf16());
49
50 // Get the size and type of the value.
51 DWORD dataType = REG_NONE;
52 DWORD dataSize = 0;
53 LONG ret = RegQueryValueExW(m_key, subKeyC, nullptr, &dataType, nullptr, &dataSize);
54 if (ret != ERROR_SUCCESS)
55 return {};
56
57 // Workaround for rare cases where the trailing '\0' is missing.
58 if (dataType == REG_SZ || dataType == REG_EXPAND_SZ)
59 dataSize += 2;
60 else if (dataType == REG_MULTI_SZ)
61 dataSize += 4;
62
63 // Get the value.
64 QVarLengthArray<unsigned char> data(dataSize);
65 std::fill(data.data(), data.data() + dataSize, 0u);
66
67 ret = RegQueryValueExW(m_key, subKeyC, nullptr, nullptr, data.data(), &dataSize);
68 if (ret != ERROR_SUCCESS)
69 return {};
70
71 switch (dataType) {
72 case REG_SZ:
73 case REG_EXPAND_SZ: {
74 if (dataSize > 0) {
76 reinterpret_cast<const wchar_t *>(data.constData()));
77 }
78 return QString();
79 }
80
81 case REG_MULTI_SZ: {
82 if (dataSize > 0) {
83 QStringList list = {};
84 int i = 0;
85 while (true) {
87 reinterpret_cast<const wchar_t *>(data.constData()) + i);
88 i += str.length() + 1;
89 if (str.isEmpty())
90 break;
92 }
93 return list;
94 }
95 return QStringList();
96 }
97
98 case REG_NONE: // No specific type, treat as binary data.
99 case REG_BINARY: {
100 if (dataSize > 0) {
102 reinterpret_cast<const wchar_t *>(data.constData()), data.size() / 2);
103 }
104 return QString();
105 }
106
107 case REG_DWORD: // Same as REG_DWORD_LITTLE_ENDIAN
108 return qFromLittleEndian<quint32>(data.constData());
109
110 case REG_DWORD_BIG_ENDIAN:
111 return qFromBigEndian<quint32>(data.constData());
112
113 case REG_QWORD: // Same as REG_QWORD_LITTLE_ENDIAN
114 return qFromLittleEndian<quint64>(data.constData());
115
116 default:
117 break;
118 }
119
120 return {};
121}
122
124{
125 return value<QString>(subKey).value_or(QString());
126}
127
128std::pair<DWORD, bool> QWinRegistryKey::dwordValue(QStringView subKey) const
129{
130 const std::optional<DWORD> val = value<DWORD>(subKey);
131 return {val.value_or(0), val.has_value()};
132}
133
void append(parameter_type t)
Definition qlist.h:458
\inmodule QtCore
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
static QString fromWCharArray(const wchar_t *string, qsizetype size=-1)
Definition qstring.h:1309
qsizetype length() const noexcept
Returns the number of characters in this string.
Definition qstring.h:191
\inmodule QtCore
Definition qvariant.h:65
QString stringValue(QStringView subKey) const
std::pair< DWORD, bool > dwordValue(QStringView subKey) const
bool isValid() const
QVariant value(QStringView subKey) const
QString str
[2]
Combined button and popup list for selecting options.
QList< QString > QStringList
Constructs a string list that contains the given string, str.
return ret
GLenum GLsizei dataSize
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum access
GLuint GLfloat * val
QList< int > list
[14]
QObject::connect nullptr