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