summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qwinregistry.cpp
blob: d2373165771e389393fa72fe191ef1ee53b00415 (plain)
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
133
134
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qwinregistry_p.h"
#include <QtCore/qvarlengtharray.h>
#include <QtCore/qendian.h>
#include <QtCore/qlist.h>

QT_BEGIN_NAMESPACE

QWinRegistryKey::QWinRegistryKey()
{
}

// Open a key with the specified permissions (KEY_READ/KEY_WRITE).
// "access" is to explicitly use the 32- or 64-bit branch.
QWinRegistryKey::QWinRegistryKey(HKEY parentHandle, QStringView subKey,
                                 REGSAM permissions, REGSAM access)
{
    if (RegOpenKeyExW(parentHandle, reinterpret_cast<const wchar_t *>(subKey.utf16()),
                      0, permissions | access, &m_key) != ERROR_SUCCESS) {
        m_key = nullptr;
    }
}

QWinRegistryKey::~QWinRegistryKey()
{
    close();
}

void QWinRegistryKey::close()
{
    if (isValid()) {
        RegCloseKey(m_key);
        m_key = nullptr;
    }
}

QVariant QWinRegistryKey::value(QStringView subKey) const
{
    // NOTE: Empty value name is allowed in Windows registry, it means the default
    // or unnamed value of a key, you can read/write/delete such value normally.

    if (!isValid())
        return {};

    // Use nullptr when we need to access the default value.
    const auto subKeyC = subKey.isEmpty() ? nullptr : reinterpret_cast<const wchar_t *>(subKey.utf16());

    // Get the size and type of the value.
    DWORD dataType = REG_NONE;
    DWORD dataSize = 0;
    LONG ret = RegQueryValueExW(m_key, subKeyC, nullptr, &dataType, nullptr, &dataSize);
    if (ret != ERROR_SUCCESS)
        return {};

    // Workaround for rare cases where the trailing '\0' is missing.
    if (dataType == REG_SZ || dataType == REG_EXPAND_SZ)
        dataSize += 2;
    else if (dataType == REG_MULTI_SZ)
        dataSize += 4;

    // Get the value.
    QVarLengthArray<unsigned char> data(dataSize);
    std::fill(data.data(), data.data() + dataSize, 0u);

    ret = RegQueryValueExW(m_key, subKeyC, nullptr, nullptr, data.data(), &dataSize);
    if (ret != ERROR_SUCCESS)
        return {};

    switch (dataType) {
        case REG_SZ:
        case REG_EXPAND_SZ: {
            if (dataSize > 0) {
                return QString::fromWCharArray(
                    reinterpret_cast<const wchar_t *>(data.constData()));
            }
            return QString();
        }

        case REG_MULTI_SZ: {
            if (dataSize > 0) {
                QStringList list = {};
                int i = 0;
                while (true) {
                    const QString str = QString::fromWCharArray(
                        reinterpret_cast<const wchar_t *>(data.constData()) + i);
                    i += str.length() + 1;
                    if (str.isEmpty())
                        break;
                    list.append(str);
                }
                return list;
            }
            return QStringList();
        }

        case REG_NONE: // No specific type, treat as binary data.
        case REG_BINARY: {
            if (dataSize > 0) {
                return QString::fromWCharArray(
                    reinterpret_cast<const wchar_t *>(data.constData()), data.size() / 2);
            }
            return QString();
        }

        case REG_DWORD: // Same as REG_DWORD_LITTLE_ENDIAN
            return qFromLittleEndian<quint32>(data.constData());

        case REG_DWORD_BIG_ENDIAN:
            return qFromBigEndian<quint32>(data.constData());

        case REG_QWORD: // Same as REG_QWORD_LITTLE_ENDIAN
            return qFromLittleEndian<quint64>(data.constData());

        default:
            break;
    }

    return {};
}

QString QWinRegistryKey::stringValue(QStringView subKey) const
{
    return value<QString>(subKey).value_or(QString());
}

std::pair<DWORD, bool> QWinRegistryKey::dwordValue(QStringView subKey) const
{
    const std::optional<DWORD> val = value<DWORD>(subKey);
    return {val.value_or(0), val.has_value()};
}

QT_END_NAMESPACE