dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
winutils.cpp
Go to the documentation of this file.
1 
13 /*
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  * for more details.
23  *
24  * You should have received a copy of the GNU General Public License along
25  * with this program; if not, see <http://www.gnu.org/licenses/>
26  */
27 
28 #include "winutils.h"
29 #include <windows.h>
30 
31 #include <QtCore/QString>
32 #include <QtCore/QVector>
33 #include <QtCore/QDebug>
34 #include <QtCore/QLibrary>
35 #include <QtCore/QTextStream>
36 
37 namespace Utils {
38 
39 QTCREATOR_UTILS_EXPORT QString winErrorMessage(unsigned long error)
40 {
41  QString rc = QString::fromLatin1("#%1: ").arg(error);
42  ushort *lpMsgBuf;
43 
44  const int len = FormatMessage(
45  FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
46  NULL, error, 0, (LPTSTR)&lpMsgBuf, 0, NULL);
47  if (len) {
48  rc = QString::fromUtf16(lpMsgBuf, len);
49  LocalFree(lpMsgBuf);
50  } else {
51  rc += QString::fromLatin1("<unknown error>");
52  }
53  return rc;
54 }
55 
56 QTCREATOR_UTILS_EXPORT QString winGetDLLVersion(WinDLLVersionType t,
57  const QString &name,
58  QString *errorMessage)
59 {
60  // Resolve required symbols from the version.dll
61  typedef DWORD (APIENTRY *GetFileVersionInfoSizeProtoType)(LPCTSTR, LPDWORD);
62  typedef BOOL (APIENTRY *GetFileVersionInfoWProtoType)(LPCWSTR, DWORD, DWORD, LPVOID);
63  typedef BOOL (APIENTRY *VerQueryValueWProtoType)(const LPVOID, LPWSTR lpSubBlock, LPVOID, PUINT);
64 
65  const char *versionDLLC = "version.dll";
66  QLibrary versionLib(QLatin1String(versionDLLC), 0);
67  if (!versionLib.load()) {
68  *errorMessage = QString::fromLatin1("Unable load %1: %2").arg(QLatin1String(versionDLLC), versionLib.errorString());
69  return QString();
70  }
71  // MinGW requires old-style casts
72  GetFileVersionInfoSizeProtoType getFileVersionInfoSizeW = (GetFileVersionInfoSizeProtoType)(versionLib.resolve("GetFileVersionInfoSizeW"));
73  GetFileVersionInfoWProtoType getFileVersionInfoW = (GetFileVersionInfoWProtoType)(versionLib.resolve("GetFileVersionInfoW"));
74  VerQueryValueWProtoType verQueryValueW = (VerQueryValueWProtoType)(versionLib.resolve("VerQueryValueW"));
75  if (!getFileVersionInfoSizeW || !getFileVersionInfoW || !verQueryValueW) {
76  *errorMessage = QString::fromLatin1("Unable to resolve all required symbols in %1").arg(QLatin1String(versionDLLC));
77  return QString();
78  }
79 
80  // Now go ahead, read version info resource
81  DWORD dummy = 0;
82  const LPCTSTR fileName = reinterpret_cast<LPCTSTR>(name.utf16()); // MinGWsy
83  const DWORD infoSize = (*getFileVersionInfoSizeW)(fileName, &dummy);
84  if (infoSize == 0) {
85  *errorMessage = QString::fromLatin1("Unable to determine the size of the version information of %1: %2").arg(name, winErrorMessage(GetLastError()));
86  return QString();
87  }
88  QByteArray dataV(infoSize + 1, '\0');
89  char *data = dataV.data();
90  if (!(*getFileVersionInfoW)(fileName, dummy, infoSize, data)) {
91  *errorMessage = QString::fromLatin1("Unable to determine the version information of %1: %2").arg(name, winErrorMessage(GetLastError()));
92  return QString();
93  }
94  VS_FIXEDFILEINFO *versionInfo;
95  UINT len = 0;
96  WCHAR tmp1[] = TEXT("\\");
97  LPWSTR tmp = tmp1;
98  if (!(*verQueryValueW)(data, tmp, &versionInfo, &len)) {
99  *errorMessage = QString::fromLatin1("Unable to determine version string of %1: %2").arg(name, winErrorMessage(GetLastError()));
100  return QString();
101  }
102  QString rc;
103  switch (t) {
104  case WinDLLFileVersion:
105  QTextStream(&rc) << HIWORD(versionInfo->dwFileVersionMS) << '.' << LOWORD(versionInfo->dwFileVersionMS);
106  break;
108  QTextStream(&rc) << HIWORD(versionInfo->dwProductVersionMS) << '.' << LOWORD(versionInfo->dwProductVersionMS);
109  break;
110  }
111  return rc;
112 }
113 
114 } // namespace Utils
const char t[]
Definition: coreconstants.h:40
DataFields data
QTCREATOR_UTILS_EXPORT QString winErrorMessage(unsigned long error)
Definition: winutils.cpp:39
QTCREATOR_UTILS_EXPORT QString winGetDLLVersion(WinDLLVersionType t, const QString &name, QString *errorMessage)
Definition: winutils.cpp:56
else error('Your technical computing program does not support file choosers.Please input the file name in the argument. ') end elseif nargin >0 logfile
WinDLLVersionType
Definition: winutils.h:44