1 /****************************************************************************
2 **
3 ** Copyright (C) 2019 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qwinregistry_p.h"
41 
42 #include <QtCore/qvarlengtharray.h>
43 
44 #include <algorithm>
45 
46 QT_BEGIN_NAMESPACE
47 
QWinRegistryKey()48 QWinRegistryKey::QWinRegistryKey() :
49     m_key(nullptr)
50 {
51 }
52 
53 // Open a key with the specified permissions (KEY_READ/KEY_WRITE).
54 // "access" is to explicitly use the 32- or 64-bit branch.
QWinRegistryKey(HKEY parentHandle,QStringView subKey,REGSAM permissions,REGSAM access)55 QWinRegistryKey::QWinRegistryKey(HKEY parentHandle, QStringView subKey,
56                                  REGSAM permissions, REGSAM access)
57 {
58     if (RegOpenKeyEx(parentHandle, reinterpret_cast<const wchar_t *>(subKey.utf16()),
59                      0, permissions | access, &m_key) != ERROR_SUCCESS) {
60         m_key = nullptr;
61     }
62 }
63 
~QWinRegistryKey()64 QWinRegistryKey::~QWinRegistryKey()
65 {
66     close();
67 }
68 
close()69 void QWinRegistryKey::close()
70 {
71     if (isValid()) {
72         RegCloseKey(m_key);
73         m_key = nullptr;
74     }
75 }
76 
stringValue(QStringView subKey) const77 QString QWinRegistryKey::stringValue(QStringView subKey) const
78 {
79     QString result;
80     if (!isValid())
81         return result;
82     DWORD type;
83     DWORD size;
84     auto subKeyC = reinterpret_cast<const wchar_t *>(subKey.utf16());
85     if (RegQueryValueEx(m_key, subKeyC, nullptr, &type, nullptr, &size) != ERROR_SUCCESS
86         || (type != REG_SZ && type != REG_EXPAND_SZ) || size <= 2) {
87         return result;
88     }
89     // Reserve more for rare cases where trailing '\0' are missing in registry.
90     // Rely on 0-termination since strings of size 256 padded with 0 have been
91     // observed (QTBUG-84455).
92     size += 2;
93     QVarLengthArray<unsigned char> buffer(static_cast<int>(size));
94     std::fill(buffer.data(), buffer.data() + size, 0u);
95     if (RegQueryValueEx(m_key, subKeyC, nullptr, &type, buffer.data(), &size) == ERROR_SUCCESS)
96           result = QString::fromWCharArray(reinterpret_cast<const wchar_t *>(buffer.constData()));
97     return result;
98 }
99 
dwordValue(QStringView subKey) const100 QPair<DWORD, bool> QWinRegistryKey::dwordValue(QStringView subKey) const
101 {
102     if (!isValid())
103         return qMakePair(0, false);
104     DWORD type;
105     auto subKeyC = reinterpret_cast<const wchar_t *>(subKey.utf16());
106     if (RegQueryValueEx(m_key, subKeyC, nullptr, &type, nullptr, nullptr) != ERROR_SUCCESS
107         || type != REG_DWORD) {
108         return qMakePair(0, false);
109     }
110     DWORD value = 0;
111     DWORD size = sizeof(value);
112     const bool ok =
113         RegQueryValueEx(m_key, subKeyC, nullptr, nullptr,
114                         reinterpret_cast<unsigned char *>(&value), &size) == ERROR_SUCCESS;
115     return qMakePair(value, ok);
116 }
117 
118 QT_END_NAMESPACE
119