/************************************************************************** ** ** This file is part of Qt Creator ** ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies). ** ** Contact: Nokia Corporation (qt-info@nokia.com) ** ** No Commercial Usage ** ** This file contains pre-release code and may not be distributed. ** You may use this file in accordance with the terms and conditions ** contained in the Technology Preview License Agreement accompanying ** this package. ** ** GNU Lesser General Public License Usage ** ** Alternatively, 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. ** ** If you have questions regarding the use of this file, please contact ** Nokia at qt-info@nokia.com. ** **************************************************************************/ #include "winutils.h" #include #include #include #include #include #include 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(""); } 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(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; UINT len = 0; if (!(*verQueryValueW)(data, TEXT("\\"), &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, QString *errorMessage) { typedef DWORD (APIENTRY *GetShortPathNameProtoType)(LPCTSTR, LPTSTR, DWORD); if (name.isEmpty()) return name; const char *kernel32DLLC = "kernel32.dll"; QLibrary kernel32Lib(kernel32DLLC, 0); if (!kernel32Lib.isLoaded() && !kernel32Lib.load()) { *errorMessage = msgCannotLoad(kernel32DLLC, kernel32Lib.errorString()); return QString(); } // MinGW requires old-style casts GetShortPathNameProtoType getShortPathNameW = (GetShortPathNameProtoType)(kernel32Lib.resolve("GetShortPathNameW")); if (!getShortPathNameW) { *errorMessage = msgCannotResolve(kernel32DLLC); return QString(); } // Determine length, then convert. const LPCTSTR nameC = reinterpret_cast(name.utf16()); // MinGW const DWORD length = (*getShortPathNameW)(nameC, NULL, 0); if (length == 0) return name; TCHAR *buffer = new TCHAR[length]; (*getShortPathNameW)(nameC, buffer, length); const QString rc = QString::fromUtf16(reinterpret_cast(buffer), length); delete [] buffer; return rc; } unsigned long winQPidToPid(const Q_PID qpid) { const PROCESS_INFORMATION *processInfo = reinterpret_cast(qpid); return processInfo->dwProcessId; } } // namespace Utils