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 "qsystemlibrary_p.h"
41 #include <QtCore/qvarlengtharray.h>
42 #include <QtCore/qstringlist.h>
43 #include <QtCore/qfileinfo.h>
44
45 /*!
46
47 \internal
48 \class QSystemLibrary
49 \inmodule QtCore
50
51 The purpose of this class is to load only libraries that are located in
52 well-known and trusted locations on the filesystem. It does not suffer from
53 the security problem that QLibrary has, therefore it will never search in
54 the current directory.
55
56 The search order is the same as the order in DLL Safe search mode Windows,
57 except that we don't search:
58 * The current directory
59 * The 16-bit system directory. (normally \c{c:\windows\system})
60 * The Windows directory. (normally \c{c:\windows})
61
62 This means that the effective search order is:
63 1. Application path.
64 2. System libraries path.
65 3. Trying all paths inside the PATH environment variable.
66
67 Note, when onlySystemDirectory is true it will skip 1) and 3).
68
69 DLL Safe search mode is documented in the "Dynamic-Link Library Search
70 Order" document on MSDN.
71 */
72
73 QT_BEGIN_NAMESPACE
74
75 #if defined(Q_OS_WINRT)
load(const wchar_t * libraryName,bool onlySystemDirectory)76 HINSTANCE QSystemLibrary::load(const wchar_t *libraryName, bool onlySystemDirectory /* = true */)
77 {
78 Q_UNUSED(onlySystemDirectory);
79 return ::LoadPackagedLibrary(libraryName, 0);
80 }
81 #else
82
83 #if !defined(QT_BOOTSTRAPPED)
84 extern QString qAppFileName();
85 #endif
86
87 static QString qSystemDirectory()
88 {
89 QVarLengthArray<wchar_t, MAX_PATH> fullPath;
90
91 UINT retLen = ::GetSystemDirectory(fullPath.data(), MAX_PATH);
92 if (retLen > MAX_PATH) {
93 fullPath.resize(retLen);
94 retLen = ::GetSystemDirectory(fullPath.data(), retLen);
95 }
96 // in some rare cases retLen might be 0
97 return QString::fromWCharArray(fullPath.constData(), int(retLen));
98 }
99
100 HINSTANCE QSystemLibrary::load(const wchar_t *libraryName, bool onlySystemDirectory /* = true */)
101 {
102 QStringList searchOrder;
103
104 #if !defined(QT_BOOTSTRAPPED)
105 if (!onlySystemDirectory)
106 searchOrder << QFileInfo(qAppFileName()).path();
107 #endif
108 searchOrder << qSystemDirectory();
109
110 if (!onlySystemDirectory) {
111 const QString PATH(QLatin1String(qgetenv("PATH").constData()));
112 searchOrder << PATH.split(QLatin1Char(';'), Qt::SkipEmptyParts);
113 }
114 QString fileName = QString::fromWCharArray(libraryName);
115 fileName.append(QLatin1String(".dll"));
116
117 // Start looking in the order specified
118 for (int i = 0; i < searchOrder.count(); ++i) {
119 QString fullPathAttempt = searchOrder.at(i);
120 if (!fullPathAttempt.endsWith(QLatin1Char('\\'))) {
121 fullPathAttempt.append(QLatin1Char('\\'));
122 }
123 fullPathAttempt.append(fileName);
124 HINSTANCE inst = ::LoadLibrary(reinterpret_cast<const wchar_t *>(fullPathAttempt.utf16()));
125 if (inst != 0)
126 return inst;
127 }
128 return 0;
129
130 }
131
132 #endif // Q_OS_WINRT
133
134 QT_END_NAMESPACE
135