source: trunk/src/corelib/plugin/qsystemlibrary.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 5.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
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
52 The purpose of this class is to load only libraries that are located in
53 well-known and trusted locations on the filesystem. It does not suffer from
54 the security problem that QLibrary has, therefore it will never search in
55 the current directory.
56
57 The search order is the same as the order in DLL Safe search mode Windows,
58 except that we don't search:
59 * The current directory
60 * The 16-bit system directory. (normally \c{c:\windows\system})
61 * The Windows directory. (normally \c{c:\windows})
62
63 This means that the effective search order is:
64 1. Application path.
65 2. System libraries path.
66 3. Trying all paths inside the PATH environment variable.
67
68 Note, when onlySystemDirectory is true it will skip 1) and 3).
69
70 DLL Safe search mode is documented in the "Dynamic-Link Library Search
71 Order" document on MSDN.
72
73 Since library loading code is sometimes shared between Windows and WinCE,
74 this class can also be used on WinCE. However, its implementation just
75 calls the LoadLibrary() function. This is ok since it is documented as not
76 loading from the current directory on WinCE. This behaviour is documented
77 in the documentation for LoadLibrary for Windows CE at MSDN.
78 (http://msdn.microsoft.com/en-us/library/ms886736.aspx)
79*/
80
81QT_BEGIN_NAMESPACE
82
83#if defined(Q_OS_WINCE)
84HINSTANCE QSystemLibrary::load(const wchar_t *libraryName, bool onlySystemDirectory /* = true */)
85{
86 return ::LoadLibrary(libraryName);
87}
88#else
89
90#if !defined(QT_BOOTSTRAPPED)
91extern QString qAppFileName();
92#endif
93
94static QString qSystemDirectory()
95{
96 QVarLengthArray<wchar_t, MAX_PATH> fullPath;
97
98 UINT retLen = ::GetSystemDirectory(fullPath.data(), MAX_PATH);
99 if (retLen > MAX_PATH) {
100 fullPath.resize(retLen);
101 retLen = ::GetSystemDirectory(fullPath.data(), retLen);
102 }
103 // in some rare cases retLen might be 0
104 return QString::fromWCharArray(fullPath.constData(), int(retLen));
105}
106
107HINSTANCE QSystemLibrary::load(const wchar_t *libraryName, bool onlySystemDirectory /* = true */)
108{
109 QStringList searchOrder;
110
111#if !defined(QT_BOOTSTRAPPED)
112 if (!onlySystemDirectory)
113 searchOrder << QFileInfo(qAppFileName()).path();
114#endif
115 searchOrder << qSystemDirectory();
116
117 if (!onlySystemDirectory) {
118 const QString PATH(QLatin1String(qgetenv("PATH").constData()));
119 searchOrder << PATH.split(QLatin1Char(';'), QString::SkipEmptyParts);
120 }
121 QString fileName = QString::fromWCharArray(libraryName);
122 fileName.append(QLatin1String(".dll"));
123
124 // Start looking in the order specified
125 for (int i = 0; i < searchOrder.count(); ++i) {
126 QString fullPathAttempt = searchOrder.at(i);
127 if (!fullPathAttempt.endsWith(QLatin1Char('\\'))) {
128 fullPathAttempt.append(QLatin1Char('\\'));
129 }
130 fullPathAttempt.append(fileName);
131 HINSTANCE inst = ::LoadLibrary((const wchar_t *)fullPathAttempt.utf16());
132 if (inst != 0)
133 return inst;
134 }
135 return 0;
136
137}
138
139#endif //Q_OS_WINCE
140
141QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.