OSDN Git Service

2c4bb30de6626c7f7de4fdb734b87fb845b351a7
[qt-creator-jp/qt-creator-jp.git] / src / libs / utils / winutils.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "winutils.h"
35 #include "qtcassert.h"
36
37 // Enable WinAPI Windows XP and later
38 #define _WIN32_WINNT 0x0501
39 #include <windows.h>
40
41 #include <QtCore/QString>
42 #include <QtCore/QVector>
43 #include <QtCore/QDebug>
44 #include <QtCore/QLibrary>
45 #include <QtCore/QTextStream>
46 #include <QtCore/QDir>
47
48 namespace Utils {
49
50 QTCREATOR_UTILS_EXPORT QString winErrorMessage(unsigned long error)
51 {
52     QString rc = QString::fromLatin1("#%1: ").arg(error);
53     ushort *lpMsgBuf;
54
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);
58     if (len) {
59         rc = QString::fromUtf16(lpMsgBuf, len);
60         LocalFree(lpMsgBuf);
61     } else {
62         rc += QString::fromLatin1("<unknown error>");
63     }
64     return rc;
65 }
66
67
68 static inline QString msgCannotLoad(const char *lib, const QString &why)
69 {
70     return QString::fromLatin1("Unable load %1: %2").arg(QLatin1String(lib), why);
71 }
72
73 static inline QString msgCannotResolve(const char *lib)
74 {
75     return QString::fromLatin1("Unable to resolve all required symbols in  %1").arg(QLatin1String(lib));
76 }
77
78 QTCREATOR_UTILS_EXPORT QString winGetDLLVersion(WinDLLVersionType t,
79                                                 const QString &name,
80                                                 QString *errorMessage)
81 {
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);
86
87     const char *versionDLLC = "version.dll";
88     QLibrary versionLib(QLatin1String(versionDLLC), 0);
89     if (!versionLib.load()) {
90         *errorMessage = msgCannotLoad(versionDLLC, versionLib.errorString());
91         return QString();
92     }
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);
99         return QString();
100     }
101
102     // Now go ahead, read version info resource
103     DWORD dummy = 0;
104     const LPCTSTR fileName = reinterpret_cast<LPCTSTR>(name.utf16()); // MinGWsy
105     const DWORD infoSize = (*getFileVersionInfoSizeW)(fileName, &dummy);
106     if (infoSize == 0) {
107         *errorMessage = QString::fromLatin1("Unable to determine the size of the version information of %1: %2").arg(name, winErrorMessage(GetLastError()));
108         return QString();
109     }
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()));
114         return QString();
115     }
116     VS_FIXEDFILEINFO  *versionInfo;
117     const LPCWSTR backslash = TEXT("\\");
118     UINT len = 0;
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()));
121         return QString();
122     }
123     QString rc;
124     switch (t) {
125     case WinDLLFileVersion:
126         QTextStream(&rc) << HIWORD(versionInfo->dwFileVersionMS) << '.' << LOWORD(versionInfo->dwFileVersionMS);
127         break;
128     case WinDLLProductVersion:
129         QTextStream(&rc) << HIWORD(versionInfo->dwProductVersionMS) << '.' << LOWORD(versionInfo->dwProductVersionMS);
130         break;
131     }
132     return rc;
133 }
134
135 QTCREATOR_UTILS_EXPORT QString getShortPathName(const QString &name)
136 {
137     if (name.isEmpty())
138         return name;
139
140     // Determine length, then convert.
141     const LPCTSTR nameC = reinterpret_cast<LPCTSTR>(name.utf16()); // MinGW
142     const DWORD length = GetShortPathNameW(nameC, NULL, 0);
143     if (length == 0)
144         return name;
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);
148     return rc;
149 }
150
151 QTCREATOR_UTILS_EXPORT QString getLongPathName(const QString &name)
152 {
153     if (name.isEmpty())
154         return name;
155
156     // Determine length, then convert.
157     const LPCTSTR nameC = reinterpret_cast<LPCTSTR>(name.utf16()); // MinGW
158     const DWORD length = GetLongPathNameW(nameC, NULL, 0);
159     if (length == 0)
160         return name;
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);
164     return rc;
165 }
166
167 QTCREATOR_UTILS_EXPORT unsigned long winQPidToPid(const Q_PID qpid)
168 {
169     const PROCESS_INFORMATION *processInfo = reinterpret_cast<const PROCESS_INFORMATION*>(qpid);
170     return processInfo->dwProcessId;
171 }
172
173 QTCREATOR_UTILS_EXPORT bool winIs64BitSystem()
174 {
175     SYSTEM_INFO systemInfo;
176     GetNativeSystemInfo(&systemInfo);
177     return systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64
178             || systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64;
179 }
180
181 QTCREATOR_UTILS_EXPORT bool winIs64BitBinary(const QString &binaryIn)
182 {
183        QTC_ASSERT(!binaryIn.isEmpty(), return false; )
184 #ifdef Q_OS_WIN32
185 #  ifdef __GNUC__   // MinGW lacking some definitions/winbase.h
186 #    define SCS_64BIT_BINARY 6
187 #  endif
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)
193             isAmd64=true;
194         return isAmd64;
195 #else
196         return false;
197 #endif
198 }
199
200 } // namespace Utils