2009-03-03 14:01:58 +01:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2012-01-26 18:33:46 +01:00
|
|
|
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
2009-03-03 14:01:58 +01:00
|
|
|
**
|
2011-11-02 15:59:12 +01:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2009-03-03 14:01:58 +01:00
|
|
|
**
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
2009-03-03 14:01:58 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-11-02 15:59:12 +01:00
|
|
|
** Nokia at qt-info@nokia.com.
|
2009-03-03 14:01:58 +01:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "winutils.h"
|
2010-11-03 09:05:14 +01:00
|
|
|
#include "qtcassert.h"
|
2010-11-01 09:12:56 +01:00
|
|
|
|
|
|
|
|
// Enable WinAPI Windows XP and later
|
|
|
|
|
#define _WIN32_WINNT 0x0501
|
2009-03-03 14:01:58 +01:00
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
|
|
#include <QtCore/QString>
|
2009-10-09 17:33:24 +02:00
|
|
|
#include <QtCore/QVector>
|
|
|
|
|
#include <QtCore/QDebug>
|
|
|
|
|
#include <QtCore/QLibrary>
|
|
|
|
|
#include <QtCore/QTextStream>
|
2010-11-03 09:05:14 +01:00
|
|
|
#include <QtCore/QDir>
|
2009-03-03 14:01:58 +01:00
|
|
|
|
|
|
|
|
namespace Utils {
|
|
|
|
|
|
2009-05-08 12:09:21 +02:00
|
|
|
QTCREATOR_UTILS_EXPORT QString winErrorMessage(unsigned long error)
|
2009-03-03 14:01:58 +01:00
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-05 12:45:02 +01:00
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-09 17:33:24 +02:00
|
|
|
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()) {
|
2009-11-05 12:45:02 +01:00
|
|
|
*errorMessage = msgCannotLoad(versionDLLC, versionLib.errorString());
|
2009-10-09 17:33:24 +02:00
|
|
|
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) {
|
2009-11-05 12:45:02 +01:00
|
|
|
*errorMessage = msgCannotResolve(versionDLLC);
|
2009-10-09 17:33:24 +02:00
|
|
|
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;
|
2010-11-01 09:12:56 +01:00
|
|
|
const LPCWSTR backslash = TEXT("\\");
|
2009-10-09 17:33:24 +02:00
|
|
|
UINT len = 0;
|
2010-11-01 09:12:56 +01:00
|
|
|
if (!(*verQueryValueW)(data, const_cast<LPWSTR>(backslash), &versionInfo, &len)) {
|
2009-10-09 17:33:24 +02:00
|
|
|
*errorMessage = QString::fromLatin1("Unable to determine version string of %1: %2").arg(name, winErrorMessage(GetLastError()));
|
|
|
|
|
return QString();
|
2010-01-29 21:33:57 +01:00
|
|
|
}
|
2009-10-09 17:33:24 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-21 20:34:39 +01:00
|
|
|
QTCREATOR_UTILS_EXPORT QString getShortPathName(const QString &name)
|
2009-11-05 12:45:02 +01:00
|
|
|
{
|
|
|
|
|
if (name.isEmpty())
|
|
|
|
|
return name;
|
|
|
|
|
|
|
|
|
|
// Determine length, then convert.
|
|
|
|
|
const LPCTSTR nameC = reinterpret_cast<LPCTSTR>(name.utf16()); // MinGW
|
2011-03-21 20:34:39 +01:00
|
|
|
const DWORD length = GetShortPathNameW(nameC, NULL, 0);
|
2009-11-05 12:45:02 +01:00
|
|
|
if (length == 0)
|
|
|
|
|
return name;
|
2011-01-05 15:31:53 +01:00
|
|
|
QScopedArrayPointer<TCHAR> buffer(new TCHAR[length]);
|
2011-03-21 20:34:39 +01:00
|
|
|
GetShortPathNameW(nameC, buffer.data(), length);
|
2011-03-21 20:24:18 +01:00
|
|
|
const QString rc = QString::fromUtf16(reinterpret_cast<const ushort *>(buffer.data()), length - 1);
|
2009-11-05 12:45:02 +01:00
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-21 20:34:39 +01:00
|
|
|
QTCREATOR_UTILS_EXPORT QString getLongPathName(const QString &name)
|
2011-01-05 14:44:52 +01:00
|
|
|
{
|
|
|
|
|
if (name.isEmpty())
|
|
|
|
|
return name;
|
|
|
|
|
|
|
|
|
|
// Determine length, then convert.
|
|
|
|
|
const LPCTSTR nameC = reinterpret_cast<LPCTSTR>(name.utf16()); // MinGW
|
2011-03-21 20:34:39 +01:00
|
|
|
const DWORD length = GetLongPathNameW(nameC, NULL, 0);
|
2011-01-05 14:44:52 +01:00
|
|
|
if (length == 0)
|
|
|
|
|
return name;
|
2011-01-05 15:31:53 +01:00
|
|
|
QScopedArrayPointer<TCHAR> buffer(new TCHAR[length]);
|
2011-03-21 20:34:39 +01:00
|
|
|
GetLongPathNameW(nameC, buffer.data(), length);
|
2011-03-21 20:24:18 +01:00
|
|
|
const QString rc = QString::fromUtf16(reinterpret_cast<const ushort *>(buffer.data()), length - 1);
|
2011-01-05 14:44:52 +01:00
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-19 11:07:34 +02:00
|
|
|
// 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;
|
2011-04-28 11:11:34 +02:00
|
|
|
// Upper case drive letter
|
|
|
|
|
if (canonicalName.size() > 2 && canonicalName.at(1) == QLatin1Char(':'))
|
|
|
|
|
canonicalName[0] = canonicalName.at(0).toUpper();
|
2011-04-19 11:07:34 +02:00
|
|
|
return canonicalName;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-29 15:35:14 +02:00
|
|
|
QTCREATOR_UTILS_EXPORT unsigned long winQPidToPid(const Q_PID qpid)
|
2010-09-10 16:23:47 +02:00
|
|
|
{
|
|
|
|
|
const PROCESS_INFORMATION *processInfo = reinterpret_cast<const PROCESS_INFORMATION*>(qpid);
|
|
|
|
|
return processInfo->dwProcessId;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-29 15:35:14 +02:00
|
|
|
QTCREATOR_UTILS_EXPORT bool winIs64BitSystem()
|
|
|
|
|
{
|
|
|
|
|
SYSTEM_INFO systemInfo;
|
|
|
|
|
GetNativeSystemInfo(&systemInfo);
|
|
|
|
|
return systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64
|
|
|
|
|
|| systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64;
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-03 09:05:14 +01:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-03 14:01:58 +01:00
|
|
|
} // namespace Utils
|