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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#include "winutils.h"
#include "qtcassert.h"
// Enable WinAPI Windows XP and later
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <QtCore/QString>
#include <QtCore/QVector>
#include <QtCore/QDebug>
#include <QtCore/QLibrary>
#include <QtCore/QTextStream>
#include <QtCore/QDir>
namespace Utils {
QTCREATOR_UTILS_EXPORT QString winErrorMessage(unsigned long error)
{
QString rc = QString::fromLatin1("#%1: ").arg(error);
ushort *lpMsgBuf;
const int len = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, error, 0, (LPTSTR)&lpMsgBuf, 0, NULL);
if (len) {
rc = QString::fromUtf16(lpMsgBuf, len);
LocalFree(lpMsgBuf);
} else {
rc += QString::fromLatin1("<unknown error>");
}
return rc;
}
static inline QString msgCannotLoad(const char *lib, const QString &why)
{
return QString::fromLatin1("Unable load %1: %2").arg(QLatin1String(lib), why);
}
static inline QString msgCannotResolve(const char *lib)
{
return QString::fromLatin1("Unable to resolve all required symbols in %1").arg(QLatin1String(lib));
}
QTCREATOR_UTILS_EXPORT QString winGetDLLVersion(WinDLLVersionType t,
const QString &name,
QString *errorMessage)
{
// Resolve required symbols from the version.dll
typedef DWORD (APIENTRY *GetFileVersionInfoSizeProtoType)(LPCTSTR, LPDWORD);
typedef BOOL (APIENTRY *GetFileVersionInfoWProtoType)(LPCWSTR, DWORD, DWORD, LPVOID);
typedef BOOL (APIENTRY *VerQueryValueWProtoType)(const LPVOID, LPWSTR lpSubBlock, LPVOID, PUINT);
const char *versionDLLC = "version.dll";
QLibrary versionLib(QLatin1String(versionDLLC), 0);
if (!versionLib.load()) {
*errorMessage = msgCannotLoad(versionDLLC, versionLib.errorString());
return QString();
}
// MinGW requires old-style casts
GetFileVersionInfoSizeProtoType getFileVersionInfoSizeW = (GetFileVersionInfoSizeProtoType)(versionLib.resolve("GetFileVersionInfoSizeW"));
GetFileVersionInfoWProtoType getFileVersionInfoW = (GetFileVersionInfoWProtoType)(versionLib.resolve("GetFileVersionInfoW"));
VerQueryValueWProtoType verQueryValueW = (VerQueryValueWProtoType)(versionLib.resolve("VerQueryValueW"));
if (!getFileVersionInfoSizeW || !getFileVersionInfoW || !verQueryValueW) {
*errorMessage = msgCannotResolve(versionDLLC);
return QString();
}
// Now go ahead, read version info resource
DWORD dummy = 0;
const LPCTSTR fileName = reinterpret_cast<LPCTSTR>(name.utf16()); // MinGWsy
const DWORD infoSize = (*getFileVersionInfoSizeW)(fileName, &dummy);
if (infoSize == 0) {
*errorMessage = QString::fromLatin1("Unable to determine the size of the version information of %1: %2").arg(name, winErrorMessage(GetLastError()));
return QString();
}
QByteArray dataV(infoSize + 1, '\0');
char *data = dataV.data();
if (!(*getFileVersionInfoW)(fileName, dummy, infoSize, data)) {
*errorMessage = QString::fromLatin1("Unable to determine the version information of %1: %2").arg(name, winErrorMessage(GetLastError()));
return QString();
}
VS_FIXEDFILEINFO *versionInfo;
const LPCWSTR backslash = TEXT("\\");
UINT len = 0;
if (!(*verQueryValueW)(data, const_cast<LPWSTR>(backslash), &versionInfo, &len)) {
*errorMessage = QString::fromLatin1("Unable to determine version string of %1: %2").arg(name, winErrorMessage(GetLastError()));
return QString();
}
QString rc;
switch (t) {
case WinDLLFileVersion:
QTextStream(&rc) << HIWORD(versionInfo->dwFileVersionMS) << '.' << LOWORD(versionInfo->dwFileVersionMS);
break;
case WinDLLProductVersion:
QTextStream(&rc) << HIWORD(versionInfo->dwProductVersionMS) << '.' << LOWORD(versionInfo->dwProductVersionMS);
break;
}
return rc;
}
QTCREATOR_UTILS_EXPORT QString getShortPathName(const QString &name)
{
if (name.isEmpty())
return name;
// Determine length, then convert.
const LPCTSTR nameC = reinterpret_cast<LPCTSTR>(name.utf16()); // MinGW
const DWORD length = GetShortPathNameW(nameC, NULL, 0);
if (length == 0)
return name;
QScopedArrayPointer<TCHAR> buffer(new TCHAR[length]);
GetShortPathNameW(nameC, buffer.data(), length);
const QString rc = QString::fromUtf16(reinterpret_cast<const ushort *>(buffer.data()), length - 1);
return rc;
}
QTCREATOR_UTILS_EXPORT QString getLongPathName(const QString &name)
{
if (name.isEmpty())
return name;
// Determine length, then convert.
const LPCTSTR nameC = reinterpret_cast<LPCTSTR>(name.utf16()); // MinGW
const DWORD length = GetLongPathNameW(nameC, NULL, 0);
if (length == 0)
return name;
QScopedArrayPointer<TCHAR> buffer(new TCHAR[length]);
GetLongPathNameW(nameC, buffer.data(), length);
const QString rc = QString::fromUtf16(reinterpret_cast<const ushort *>(buffer.data()), length - 1);
return rc;
}
// makes sure that capitalization of directories is canonical.
// This mimics the logic in QDeclarative_isFileCaseCorrect
QTCREATOR_UTILS_EXPORT QString normalizePathName(const QString &name)
{
QString canonicalName = getShortPathName(name);
if (canonicalName.isEmpty())
return name;
canonicalName = getLongPathName(canonicalName);
if (canonicalName.isEmpty())
return name;
// Upper case drive letter
if (canonicalName.size() > 2 && canonicalName.at(1) == QLatin1Char(':'))
canonicalName[0] = canonicalName.at(0).toUpper();
return canonicalName;
}
QTCREATOR_UTILS_EXPORT unsigned long winQPidToPid(const Q_PID qpid)
{
const PROCESS_INFORMATION *processInfo = reinterpret_cast<const PROCESS_INFORMATION*>(qpid);
return processInfo->dwProcessId;
}
QTCREATOR_UTILS_EXPORT bool winIs64BitSystem()
{
SYSTEM_INFO systemInfo;
GetNativeSystemInfo(&systemInfo);
return systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64
|| systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64;
}
QTCREATOR_UTILS_EXPORT bool winIs64BitBinary(const QString &binaryIn)
{
QTC_ASSERT(!binaryIn.isEmpty(), return false; )
#ifdef Q_OS_WIN32
# ifdef __GNUC__ // MinGW lacking some definitions/winbase.h
# define SCS_64BIT_BINARY 6
# endif
bool isAmd64 = false;
DWORD binaryType = 0;
const QString binary = QDir::toNativeSeparators(binaryIn);
bool success = GetBinaryTypeW(reinterpret_cast<const TCHAR*>(binary.utf16()), &binaryType) != 0;
if (success && binaryType == SCS_64BIT_BINARY)
isAmd64=true;
return isAmd64;
#else
return false;
#endif
}
} // namespace Utils
|