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 **************************************************************************/
35 #include "qtcassert.h"
37 // Enable WinAPI Windows XP and later
38 #define _WIN32_WINNT 0x0501
41 #include <QtCore/QString>
42 #include <QtCore/QVector>
43 #include <QtCore/QDebug>
44 #include <QtCore/QLibrary>
45 #include <QtCore/QTextStream>
46 #include <QtCore/QDir>
50 QTCREATOR_UTILS_EXPORT QString winErrorMessage(unsigned long error)
52 QString rc = QString::fromLatin1("#%1: ").arg(error);
55 const int len = FormatMessage(
56 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
57 NULL, error, 0, (LPTSTR)&lpMsgBuf, 0, NULL);
59 rc = QString::fromUtf16(lpMsgBuf, len);
62 rc += QString::fromLatin1("<unknown error>");
68 static inline QString msgCannotLoad(const char *lib, const QString &why)
70 return QString::fromLatin1("Unable load %1: %2").arg(QLatin1String(lib), why);
73 static inline QString msgCannotResolve(const char *lib)
75 return QString::fromLatin1("Unable to resolve all required symbols in %1").arg(QLatin1String(lib));
78 QTCREATOR_UTILS_EXPORT QString winGetDLLVersion(WinDLLVersionType t,
80 QString *errorMessage)
82 // Resolve required symbols from the version.dll
83 typedef DWORD (APIENTRY *GetFileVersionInfoSizeProtoType)(LPCTSTR, LPDWORD);
84 typedef BOOL (APIENTRY *GetFileVersionInfoWProtoType)(LPCWSTR, DWORD, DWORD, LPVOID);
85 typedef BOOL (APIENTRY *VerQueryValueWProtoType)(const LPVOID, LPWSTR lpSubBlock, LPVOID, PUINT);
87 const char *versionDLLC = "version.dll";
88 QLibrary versionLib(QLatin1String(versionDLLC), 0);
89 if (!versionLib.load()) {
90 *errorMessage = msgCannotLoad(versionDLLC, versionLib.errorString());
93 // MinGW requires old-style casts
94 GetFileVersionInfoSizeProtoType getFileVersionInfoSizeW = (GetFileVersionInfoSizeProtoType)(versionLib.resolve("GetFileVersionInfoSizeW"));
95 GetFileVersionInfoWProtoType getFileVersionInfoW = (GetFileVersionInfoWProtoType)(versionLib.resolve("GetFileVersionInfoW"));
96 VerQueryValueWProtoType verQueryValueW = (VerQueryValueWProtoType)(versionLib.resolve("VerQueryValueW"));
97 if (!getFileVersionInfoSizeW || !getFileVersionInfoW || !verQueryValueW) {
98 *errorMessage = msgCannotResolve(versionDLLC);
102 // Now go ahead, read version info resource
104 const LPCTSTR fileName = reinterpret_cast<LPCTSTR>(name.utf16()); // MinGWsy
105 const DWORD infoSize = (*getFileVersionInfoSizeW)(fileName, &dummy);
107 *errorMessage = QString::fromLatin1("Unable to determine the size of the version information of %1: %2").arg(name, winErrorMessage(GetLastError()));
110 QByteArray dataV(infoSize + 1, '\0');
111 char *data = dataV.data();
112 if (!(*getFileVersionInfoW)(fileName, dummy, infoSize, data)) {
113 *errorMessage = QString::fromLatin1("Unable to determine the version information of %1: %2").arg(name, winErrorMessage(GetLastError()));
116 VS_FIXEDFILEINFO *versionInfo;
117 const LPCWSTR backslash = TEXT("\\");
119 if (!(*verQueryValueW)(data, const_cast<LPWSTR>(backslash), &versionInfo, &len)) {
120 *errorMessage = QString::fromLatin1("Unable to determine version string of %1: %2").arg(name, winErrorMessage(GetLastError()));
125 case WinDLLFileVersion:
126 QTextStream(&rc) << HIWORD(versionInfo->dwFileVersionMS) << '.' << LOWORD(versionInfo->dwFileVersionMS);
128 case WinDLLProductVersion:
129 QTextStream(&rc) << HIWORD(versionInfo->dwProductVersionMS) << '.' << LOWORD(versionInfo->dwProductVersionMS);
135 QTCREATOR_UTILS_EXPORT QString getShortPathName(const QString &name)
140 // Determine length, then convert.
141 const LPCTSTR nameC = reinterpret_cast<LPCTSTR>(name.utf16()); // MinGW
142 const DWORD length = GetShortPathNameW(nameC, NULL, 0);
145 QScopedArrayPointer<TCHAR> buffer(new TCHAR[length]);
146 GetShortPathNameW(nameC, buffer.data(), length);
147 const QString rc = QString::fromUtf16(reinterpret_cast<const ushort *>(buffer.data()), length - 1);
151 QTCREATOR_UTILS_EXPORT QString getLongPathName(const QString &name)
156 // Determine length, then convert.
157 const LPCTSTR nameC = reinterpret_cast<LPCTSTR>(name.utf16()); // MinGW
158 const DWORD length = GetLongPathNameW(nameC, NULL, 0);
161 QScopedArrayPointer<TCHAR> buffer(new TCHAR[length]);
162 GetLongPathNameW(nameC, buffer.data(), length);
163 const QString rc = QString::fromUtf16(reinterpret_cast<const ushort *>(buffer.data()), length - 1);
167 QTCREATOR_UTILS_EXPORT unsigned long winQPidToPid(const Q_PID qpid)
169 const PROCESS_INFORMATION *processInfo = reinterpret_cast<const PROCESS_INFORMATION*>(qpid);
170 return processInfo->dwProcessId;
173 QTCREATOR_UTILS_EXPORT bool winIs64BitSystem()
175 SYSTEM_INFO systemInfo;
176 GetNativeSystemInfo(&systemInfo);
177 return systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64
178 || systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64;
181 QTCREATOR_UTILS_EXPORT bool winIs64BitBinary(const QString &binaryIn)
183 QTC_ASSERT(!binaryIn.isEmpty(), return false; )
185 # ifdef __GNUC__ // MinGW lacking some definitions/winbase.h
186 # define SCS_64BIT_BINARY 6
188 bool isAmd64 = false;
189 DWORD binaryType = 0;
190 const QString binary = QDir::toNativeSeparators(binaryIn);
191 bool success = GetBinaryTypeW(reinterpret_cast<const TCHAR*>(binary.utf16()), &binaryType) != 0;
192 if (success && binaryType == SCS_64BIT_BINARY)