Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qlibrary_win.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qplatformdefs.h"
5#include "qlibrary_p.h"
6
7#include "qdir.h"
8#include "qfile.h"
9#include "qfileinfo.h"
10#include <private/qfilesystementry_p.h>
11
12#include <qt_windows.h>
13
15
16using namespace Qt::StringLiterals;
17
18extern QString qt_error_string(int code);
19
21{
23 return QStringList(QStringLiteral(".dll"));
24}
25
27{
28 return QStringList();
29}
30
31bool QLibraryPrivate::load_sys()
32{
33 //avoid 'Bad Image' message box
34 UINT oldmode = SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOOPENFILEERRORBOX);
35 // We make the following attempts at locating the library:
36 //
37 // Windows
38 // if (absolute)
39 // fileName
40 // fileName + ".dll"
41 // else
42 // fileName + ".dll"
43 // fileName
44 //
45 // NB If it's a plugin we do not ever try the ".dll" extension
46 QMutexLocker locker(&mutex);
47 QStringList attempts;
48
49 if (pluginState != IsAPlugin)
50 attempts.append(fileName + ".dll"_L1);
51
52 // If the fileName is an absolute path we try that first, otherwise we
53 // use the system-specific suffix first
55 if (fsEntry.isAbsolute())
56 attempts.prepend(fileName);
57 else
58 attempts.append(fileName);
59
60 locker.unlock();
61 Handle hnd = nullptr;
62 for (const QString &attempt : std::as_const(attempts)) {
63 hnd = LoadLibrary(reinterpret_cast<const wchar_t*>(QDir::toNativeSeparators(attempt).utf16()));
64
65 // If we have a handle or the last error is something other than "unable
66 // to find the module", then bail out
67 if (hnd || ::GetLastError() != ERROR_MOD_NOT_FOUND)
68 break;
69 }
70
71 SetErrorMode(oldmode);
72 locker.relock();
73 if (!hnd) {
74 errorString = QLibrary::tr("Cannot load library %1: %2").arg(
76 } else {
77 // Query the actual name of the library that was loaded
79
80 wchar_t buffer[MAX_PATH];
81 ::GetModuleFileName(hnd, buffer, MAX_PATH);
82
83 QString moduleFileName = QString::fromWCharArray(buffer);
84 moduleFileName.remove(0, 1 + moduleFileName.lastIndexOf(u'\\'));
85 const QDir dir(fsEntry.path());
86 if (dir.path() == "."_L1)
87 qualifiedFileName = moduleFileName;
88 else
89 qualifiedFileName = dir.filePath(moduleFileName);
90
92 // prevent the unloading of this component
93 HMODULE hmod;
94 bool ok = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_PIN |
95 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
96 reinterpret_cast<const wchar_t *>(hnd),
97 &hmod);
98 Q_ASSERT(!ok || hmod == hnd);
99 Q_UNUSED(ok);
100 }
101 }
102 pHnd.storeRelaxed(hnd);
103 return (pHnd != nullptr);
104}
105
106bool QLibraryPrivate::unload_sys()
107{
108 if (!FreeLibrary(pHnd.loadAcquire())) {
109 errorString = QLibrary::tr("Cannot unload library %1: %2").arg(
111 return false;
112 }
114 return true;
115}
116
117QFunctionPointer QLibraryPrivate::resolve_sys(const char *symbol)
118{
119 FARPROC address = GetProcAddress(pHnd.loadAcquire(), symbol);
120 return QFunctionPointer(address);
121}
Type loadAcquire() const noexcept
void storeRelaxed(Type newValue) noexcept
\inmodule QtCore
Definition qdir.h:20
static QString toNativeSeparators(const QString &pathName)
Definition qdir.cpp:929
QString errorString
Definition qlibrary_p.h:91
QString qualifiedFileName
Definition qlibrary_p.h:92
QAtomicPointer< std::remove_pointer< Handle >::type > pHnd
Definition qlibrary_p.h:85
static QStringList suffixes_sys(const QString &fullVersion)
const QString fileName
Definition qlibrary_p.h:65
const QString fullVersion
Definition qlibrary_p.h:66
static QStringList prefixes_sys()
QLibrary::LoadHints loadHints() const
Definition qlibrary_p.h:74
@ PreventUnloadHint
Definition qlibrary.h:26
\inmodule QtCore
Definition qmutex.h:313
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
void clear()
Clears the contents of the string and makes it null.
Definition qstring.h:1252
QString arg(qlonglong a, int fieldwidth=0, int base=10, QChar fillChar=u' ') const
Definition qstring.cpp:8870
static QString fromWCharArray(const wchar_t *string, qsizetype size=-1)
Definition qstring.h:1309
Combined button and popup list for selecting options.
QList< QString > QStringList
Constructs a string list that contains the given string, str.
QString qt_error_string(int code)
Q_DECL_COLD_FUNCTION Q_CORE_EXPORT QString qt_error_string(int errorCode=-1)
GLenum GLuint buffer
GLuint GLuint64EXT address
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define MAX_PATH
#define QStringLiteral(str)
#define Q_UNUSED(x)
HINSTANCE HMODULE
QString dir
[11]