1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** 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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "registryaccess.h"
36 #include <QtGui/QApplication>
38 #include <QtCore/QDir>
39 #include <QtCore/QTextStream>
41 namespace RegistryAccess {
43 static QString winErrorMessage(unsigned long error)
45 QString rc = QString::fromLatin1("#%1: ").arg(error);
48 const int len = FormatMessage(
49 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
50 NULL, error, 0, (LPTSTR)&lpMsgBuf, 0, NULL);
52 rc = QString::fromUtf16(lpMsgBuf, len);
55 rc += QString::fromLatin1("<unknown error>");
60 QString msgFunctionFailed(const char *f, unsigned long error)
62 return QString::fromLatin1("'%1' failed: %2").arg(QLatin1String(f), winErrorMessage(error));
65 static bool registryReadBinaryKey(HKEY handle, // HKEY_LOCAL_MACHINE, etc.
66 const WCHAR *valueName,
68 QString *errorMessage)
73 // get size and retrieve
74 LONG rc = RegQueryValueEx(handle, valueName, 0, &type, 0, &size);
75 if (rc != ERROR_SUCCESS) {
76 *errorMessage = msgRegistryOperationFailed("read", valueName, msgFunctionFailed("RegQueryValueEx1", rc));
79 BYTE *dataC = new BYTE[size + 1];
80 // Will be Utf16 in case of a string
81 rc = RegQueryValueEx(handle, valueName, 0, &type, dataC, &size);
82 if (rc != ERROR_SUCCESS) {
83 *errorMessage = msgRegistryOperationFailed("read", valueName, msgFunctionFailed("RegQueryValueEx2", rc));
86 *data = QByteArray(reinterpret_cast<const char*>(dataC), size);
91 bool registryReadStringKey(HKEY handle, // HKEY_LOCAL_MACHINE, etc.
92 const WCHAR *valueName,
94 QString *errorMessage)
97 if (!registryReadBinaryKey(handle, valueName, &data, errorMessage))
101 *s = QString::fromUtf16(reinterpret_cast<const unsigned short*>(data.data()));
105 bool openRegistryKey(HKEY category, // HKEY_LOCAL_MACHINE, etc.
109 QString *errorMessage)
111 Q_UNUSED(debuggerRegistryKeyC); // avoid warning from MinGW
113 REGSAM accessRights = KEY_READ;
115 accessRights |= KEY_SET_VALUE;
116 const LONG rc = RegOpenKeyEx(category, key, 0, accessRights, keyHandle);
117 if (rc != ERROR_SUCCESS) {
118 *errorMessage = msgFunctionFailed("RegOpenKeyEx", rc);
120 *errorMessage += "You need administrator privileges to edit the registry.";
126 // Installation helpers: Format the debugger call with placeholders for PID and event
127 // '"[path]\qtcdebugger" [-wow] %ld %ld'.
129 QString debuggerCall(const QString &additionalOption)
132 QTextStream str(&rc);
133 str << '"' << QDir::toNativeSeparators(QApplication::applicationDirPath() + QLatin1Char('/')
134 + debuggerApplicationFileC + QLatin1String(".exe")) << '"';
135 if (!additionalOption.isEmpty())
136 str << ' ' << additionalOption;
141 bool isRegistered(HKEY handle, const QString &call, QString *errorMessage, QString *oldDebugger)
143 QString registeredDebugger;
144 registryReadStringKey(handle, debuggerRegistryValueNameC, ®isteredDebugger, errorMessage);
146 *oldDebugger = registeredDebugger;
147 return !registeredDebugger.compare(call, Qt::CaseInsensitive);
150 } // namespace RegistryAccess