1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qplatformdefs.h"
41 #include "qlibrary_p.h"
42 #include "qfile.h"
43 #include "qdir.h"
44 #include "qfileinfo.h"
45 #include <private/qfilesystementry_p.h>
46 
47 #include <qt_windows.h>
48 
49 QT_BEGIN_NAMESPACE
50 
51 extern QString qt_error_string(int code);
52 
suffixes_sys(const QString & fullVersion)53 QStringList QLibraryPrivate::suffixes_sys(const QString& fullVersion)
54 {
55     Q_UNUSED(fullVersion);
56     return QStringList(QStringLiteral(".dll"));
57 }
58 
prefixes_sys()59 QStringList QLibraryPrivate::prefixes_sys()
60 {
61     return QStringList();
62 }
63 
load_sys()64 bool QLibraryPrivate::load_sys()
65 {
66 #ifndef Q_OS_WINRT
67     //avoid 'Bad Image' message box
68     UINT oldmode = SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOOPENFILEERRORBOX);
69 #endif
70     // We make the following attempts at locating the library:
71     //
72     // Windows
73     // if (absolute)
74     //     fileName
75     //     fileName + ".dll"
76     // else
77     //     fileName + ".dll"
78     //     fileName
79     //
80     // NB If it's a plugin we do not ever try the ".dll" extension
81     QMutexLocker locker(&mutex);
82     QStringList attempts;
83 
84     if (pluginState != IsAPlugin)
85         attempts.append(fileName + QLatin1String(".dll"));
86 
87     // If the fileName is an absolute path we try that first, otherwise we
88     // use the system-specific suffix first
89     QFileSystemEntry fsEntry(fileName);
90     if (fsEntry.isAbsolute())
91         attempts.prepend(fileName);
92     else
93         attempts.append(fileName);
94 #ifdef Q_OS_WINRT
95     if (fileName.startsWith(QLatin1Char('/')))
96         attempts.prepend(QDir::rootPath() + fileName);
97 #endif
98 
99     locker.unlock();
100     Handle hnd = nullptr;
101     for (const QString &attempt : qAsConst(attempts)) {
102 #ifndef Q_OS_WINRT
103         hnd = LoadLibrary(reinterpret_cast<const wchar_t*>(QDir::toNativeSeparators(attempt).utf16()));
104 #else // Q_OS_WINRT
105         QString path = QDir::toNativeSeparators(QDir::current().relativeFilePath(attempt));
106         hnd = LoadPackagedLibrary(reinterpret_cast<LPCWSTR>(path.utf16()), 0);
107         if (hnd)
108             qualifiedFileName = attempt;
109 #endif // !Q_OS_WINRT
110 
111         // If we have a handle or the last error is something other than "unable
112         // to find the module", then bail out
113         if (hnd || ::GetLastError() != ERROR_MOD_NOT_FOUND)
114             break;
115     }
116 
117 #ifndef Q_OS_WINRT
118     SetErrorMode(oldmode);
119 #endif
120     locker.relock();
121     if (!hnd) {
122         errorString = QLibrary::tr("Cannot load library %1: %2").arg(
123                     QDir::toNativeSeparators(fileName), qt_error_string());
124     } else {
125         // Query the actual name of the library that was loaded
126         errorString.clear();
127 
128 #ifndef Q_OS_WINRT
129         wchar_t buffer[MAX_PATH];
130         ::GetModuleFileName(hnd, buffer, MAX_PATH);
131 
132         QString moduleFileName = QString::fromWCharArray(buffer);
133         moduleFileName.remove(0, 1 + moduleFileName.lastIndexOf(QLatin1Char('\\')));
134         const QDir dir(fsEntry.path());
135         if (dir.path() == QLatin1String("."))
136             qualifiedFileName = moduleFileName;
137         else
138             qualifiedFileName = dir.filePath(moduleFileName);
139 
140         if (loadHints() & QLibrary::PreventUnloadHint) {
141             // prevent the unloading of this component
142             HMODULE hmod;
143             bool ok = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_PIN |
144                                         GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
145                                         reinterpret_cast<const wchar_t *>(hnd),
146                                         &hmod);
147             Q_ASSERT(!ok || hmod == hnd);
148             Q_UNUSED(ok);
149         }
150 #endif // !Q_OS_WINRT
151     }
152     pHnd.storeRelaxed(hnd);
153     return (pHnd != nullptr);
154 }
155 
unload_sys()156 bool QLibraryPrivate::unload_sys()
157 {
158     if (!FreeLibrary(pHnd.loadAcquire())) {
159         errorString = QLibrary::tr("Cannot unload library %1: %2").arg(
160                     QDir::toNativeSeparators(fileName),  qt_error_string());
161         return false;
162     }
163     errorString.clear();
164     return true;
165 }
166 
resolve_sys(const char * symbol)167 QFunctionPointer QLibraryPrivate::resolve_sys(const char* symbol)
168 {
169     FARPROC address = GetProcAddress(pHnd.loadAcquire(), symbol);
170     return QFunctionPointer(address);
171 }
172 QT_END_NAMESPACE
173