source: trunk/tools/shared/windows/registry.cpp@ 769

Last change on this file since 769 was 769, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.3 sources from branches/vendor/nokia/qt.

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