Qt 4.8
qlibrary_win.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
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, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qplatformdefs.h"
43 #include "qlibrary_p.h"
44 #include "qfile.h"
45 #include "qdir.h"
46 #include "qfileinfo.h"
47 #include "qdir.h"
48 #include <private/qfilesystementry_p.h>
49 
50 #if defined(QT_NO_LIBRARY) && defined(Q_OS_WIN)
51 #undef QT_NO_LIBRARY
52 #pragma message("QT_NO_LIBRARY is not supported on Windows")
53 #endif
54 
55 #include "qt_windows.h"
56 
58 
59 extern QString qt_error_string(int code);
60 
62 {
63  //avoid 'Bad Image' message box
65 
66  // We make the following attempts at locating the library:
67  //
68  // WinCE
69  // if (loadHints & QLibrary::ImprovedSearchHeuristics)
70  // if (absolute)
71  // fileName
72  // fileName + ".dll"
73  // else
74  // fileName + ".dll"
75  // fileName
76  // QFileInfo(fileName).absoluteFilePath()
77  // else
78  // QFileInfo(fileName).absoluteFilePath();
79  // fileName
80  // fileName + ".dll"
81  //
82  // Windows
83  // if (loadHints & QLibrary::ImprovedSearchHeuristics)
84  // if (absolute)
85  // fileName
86  // fileName + ".dll"
87  // else
88  // fileName + ".dll"
89  // fileName
90  // else
91  // fileName
92  // fileName + ".dll"
93  //
94  // NB If it's a plugin we do not ever try the ".dll" extension
95  QStringList attempts;
96  QFileSystemEntry fsEntry(fileName);
98  if (pluginState != IsAPlugin)
99  attempts.append(fileName + QLatin1String(".dll"));
100 
101  // If the fileName is an absolute path we try that first, otherwise we
102  // use the system-specific suffix first
103  if (fsEntry.isAbsolute()) {
104  attempts.prepend(fileName);
105  } else {
106  attempts.append(fileName);
107 #if defined(Q_OS_WINCE)
108  attempts.append(QFileInfo(fileName).absoluteFilePath());
109 #endif
110  }
111  } else {
112 #ifdef Q_OS_WINCE
113  attempts.append(QFileInfo(fileName).absoluteFilePath());
114 #else
115  attempts.append(fileName);
116 #endif
117  if (pluginState != IsAPlugin) {
118 #if defined(Q_OS_WINCE)
119  attempts.append(fileName);
120 #endif
121  attempts.append(fileName + QLatin1String(".dll"));
122  }
123  }
124 
125  Q_FOREACH (const QString &attempt, attempts) {
126  pHnd = LoadLibrary((wchar_t*)QDir::toNativeSeparators(attempt).utf16());
127 
128  // If we have a handle or the last error is something other than "unable
129  // to find the module", then bail out
130  if (pHnd || ::GetLastError() != ERROR_MOD_NOT_FOUND)
131  break;
132  }
133 
134  SetErrorMode(oldmode);
135  if (!pHnd) {
136  errorString = QLibrary::tr("Cannot load library %1: %2").arg(fileName).arg(qt_error_string());
137  } else {
138  // Query the actual name of the library that was loaded
139  errorString.clear();
140 
141  wchar_t buffer[MAX_PATH];
142  ::GetModuleFileName(pHnd, buffer, MAX_PATH);
143 
144  QString moduleFileName = QString::fromWCharArray(buffer);
145  moduleFileName.remove(0, 1 + moduleFileName.lastIndexOf(QLatin1Char('\\')));
146  const QDir dir(fsEntry.path());
147  if (dir.path() == QLatin1String("."))
148  qualifiedFileName = moduleFileName;
149  else
150  qualifiedFileName = dir.filePath(moduleFileName);
151  }
152  return (pHnd != 0);
153 }
154 
156 {
157  if (!FreeLibrary(pHnd)) {
158  errorString = QLibrary::tr("Cannot unload library %1: %2").arg(fileName).arg(qt_error_string());
159  return false;
160  }
161  errorString.clear();
162  return true;
163 }
164 
165 void* QLibraryPrivate::resolve_sys(const char* symbol)
166 {
167 #ifdef Q_OS_WINCE
168  void* address = (void*)GetProcAddress(pHnd, (const wchar_t*)QString::fromLatin1(symbol).utf16());
169 #else
170  void* address = (void*)GetProcAddress(pHnd, symbol);
171 #endif
172  if (!address) {
173  errorString = QLibrary::tr("Cannot resolve symbol \"%1\" in %2: %3").arg(
175  } else {
176  errorString.clear();
177  }
178  return address;
179 }
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
The QDir class provides access to directory structures and their contents.
Definition: qdir.h:58
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
Definition: qstring.cpp:4276
#define SEM_FAILCRITICALERRORS
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
enum QLibraryPrivate::@42 pluginState
QString fileName
Definition: qlibrary_p.h:83
void prepend(const T &t)
Inserts value at the beginning of the list.
Definition: qlist.h:541
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
void * resolve_sys(const char *)
HINSTANCE pHnd
Definition: qlibrary_p.h:81
#define SetErrorMode(a)
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
QString errorString
Definition: qlibrary_p.h:99
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
int lastIndexOf(QChar c, int from=-1, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:3000
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
QString qt_error_string(int code)
Definition: qglobal.cpp:2600
#define SEM_NOOPENFILEERRORBOX
#define Q_FOREACH(variable, container)
Same as foreach(variable, container).
Definition: qglobal.h:2435
QString & remove(int i, int len)
Removes n characters from the string, starting at the given position index, and returns a reference t...
Definition: qstring.cpp:1867
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
static QString toNativeSeparators(const QString &pathName)
Returns pathName with the &#39;/&#39; separators converted to separators that are appropriate for the underly...
Definition: qdir.cpp:812
QLibrary::LoadHints loadHints
Definition: qlibrary_p.h:100
QString qualifiedFileName
Definition: qlibrary_p.h:83
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
const ushort * utf16() const
Returns the QString as a &#39;\0\&#39;-terminated array of unsigned shorts.
Definition: qstring.cpp:5290