author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Wed, 21 Apr 2010 11:15:19 +0300 | |
branch | RCL_3 |
changeset 11 | 25a739ee40f4 |
parent 5 | d3bac044e0f0 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtCore module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
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 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qplatformdefs.h" |
|
43 |
#include "qlibrary.h" |
|
44 |
||
45 |
#ifndef QT_NO_LIBRARY |
|
46 |
||
47 |
#include "qlibrary_p.h" |
|
48 |
#include <qstringlist.h> |
|
49 |
#include <qfile.h> |
|
50 |
#include <qfileinfo.h> |
|
51 |
#include <qmutex.h> |
|
52 |
#include <qmap.h> |
|
53 |
#include <qsettings.h> |
|
54 |
#include <qdatetime.h> |
|
55 |
#ifdef Q_OS_MAC |
|
56 |
# include <private/qcore_mac_p.h> |
|
57 |
#endif |
|
58 |
#ifndef NO_ERRNO_H |
|
59 |
#include <errno.h> |
|
60 |
#endif // NO_ERROR_H |
|
61 |
#include <qdebug.h> |
|
62 |
#include <qvector.h> |
|
63 |
#include <qdir.h> |
|
64 |
||
65 |
QT_BEGIN_NAMESPACE |
|
66 |
||
67 |
//#define QT_DEBUG_COMPONENT |
|
68 |
||
69 |
#ifdef QT_NO_DEBUG |
|
70 |
# define QLIBRARY_AS_DEBUG false |
|
71 |
#else |
|
72 |
# define QLIBRARY_AS_DEBUG true |
|
73 |
#endif |
|
74 |
||
75 |
#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) |
|
76 |
// We don't use separate debug and release libs on UNIX, so we want |
|
77 |
// to allow loading plugins, regardless of how they were built. |
|
78 |
# define QT_NO_DEBUG_PLUGIN_CHECK |
|
79 |
#endif |
|
80 |
||
81 |
Q_GLOBAL_STATIC(QMutex, qt_library_mutex) |
|
82 |
||
83 |
/*! |
|
84 |
\class QLibrary |
|
85 |
\reentrant |
|
86 |
\brief The QLibrary class loads shared libraries at runtime. |
|
87 |
||
88 |
||
89 |
\ingroup plugins |
|
90 |
||
91 |
An instance of a QLibrary object operates on a single shared |
|
92 |
object file (which we call a "library", but is also known as a |
|
93 |
"DLL"). A QLibrary provides access to the functionality in the |
|
94 |
library in a platform independent way. You can either pass a file |
|
95 |
name in the constructor, or set it explicitly with setFileName(). |
|
96 |
When loading the library, QLibrary searches in all the |
|
97 |
system-specific library locations (e.g. \c LD_LIBRARY_PATH on |
|
98 |
Unix), unless the file name has an absolute path. If the file |
|
99 |
cannot be found, QLibrary tries the name with different |
|
100 |
platform-specific file suffixes, like ".so" on Unix, ".dylib" on |
|
101 |
the Mac, or ".dll" on Windows and Symbian. This makes it possible |
|
102 |
to specify shared libraries that are only identified by their |
|
103 |
basename (i.e. without their suffix), so the same code will work |
|
104 |
on different operating systems. |
|
105 |
||
106 |
The most important functions are load() to dynamically load the |
|
107 |
library file, isLoaded() to check whether loading was successful, |
|
108 |
and resolve() to resolve a symbol in the library. The resolve() |
|
109 |
function implicitly tries to load the library if it has not been |
|
110 |
loaded yet. Multiple instances of QLibrary can be used to access |
|
111 |
the same physical library. Once loaded, libraries remain in memory |
|
112 |
until the application terminates. You can attempt to unload a |
|
113 |
library using unload(), but if other instances of QLibrary are |
|
114 |
using the same library, the call will fail, and unloading will |
|
115 |
only happen when every instance has called unload(). |
|
116 |
||
117 |
A typical use of QLibrary is to resolve an exported symbol in a |
|
118 |
library, and to call the C function that this symbol represents. |
|
119 |
This is called "explicit linking" in contrast to "implicit |
|
120 |
linking", which is done by the link step in the build process when |
|
121 |
linking an executable against a library. |
|
122 |
||
123 |
Note: In Symbian resolving symbols using their names is supported |
|
124 |
only if the library is built as STDDLL. Otherwise ordinals must |
|
125 |
be used. Also, in Symbian the path of the library is ignored and |
|
126 |
system default library location is always used. |
|
127 |
||
128 |
The following code snippet loads a library, resolves the symbol |
|
129 |
"mysymbol", and calls the function if everything succeeded. If |
|
130 |
something goes wrong, e.g. the library file does not exist or the |
|
131 |
symbol is not defined, the function pointer will be 0 and won't be |
|
132 |
called. |
|
133 |
||
134 |
\snippet doc/src/snippets/code/src_corelib_plugin_qlibrary.cpp 0 |
|
135 |
||
136 |
The symbol must be exported as a C function from the library for |
|
137 |
resolve() to work. This means that the function must be wrapped in |
|
138 |
an \c{extern "C"} block if the library is compiled with a C++ |
|
139 |
compiler. On Windows, this also requires the use of a \c dllexport |
|
140 |
macro; see resolve() for the details of how this is done. For |
|
141 |
convenience, there is a static resolve() function which you can |
|
142 |
use if you just want to call a function in a library without |
|
143 |
explicitly loading the library first: |
|
144 |
||
145 |
\snippet doc/src/snippets/code/src_corelib_plugin_qlibrary.cpp 1 |
|
146 |
||
147 |
\sa QPluginLoader |
|
148 |
*/ |
|
149 |
||
150 |
/*! |
|
151 |
\enum QLibrary::LoadHint |
|
152 |
||
153 |
This enum describes the possible hints that can be used to change the way |
|
154 |
libraries are handled when they are loaded. These values indicate how |
|
155 |
symbols are resolved when libraries are loaded, and are specified using |
|
156 |
the setLoadHints() function. |
|
157 |
||
158 |
\value ResolveAllSymbolsHint |
|
159 |
Causes all symbols in a library to be resolved when it is loaded, not |
|
160 |
simply when resolve() is called. |
|
161 |
\value ExportExternalSymbolsHint |
|
162 |
Exports unresolved and external symbols in the library so that they can be |
|
163 |
resolved in other dynamically-loaded libraries loaded later. |
|
164 |
\value LoadArchiveMemberHint |
|
165 |
Allows the file name of the library to specify a particular object file |
|
166 |
within an archive file. |
|
167 |
If this hint is given, the filename of the library consists of |
|
168 |
a path, which is a reference to an archive file, followed by |
|
169 |
a reference to the archive member. |
|
170 |
||
171 |
\sa loadHints |
|
172 |
*/ |
|
173 |
||
174 |
||
175 |
#ifndef QT_NO_PLUGIN_CHECK |
|
176 |
struct qt_token_info |
|
177 |
{ |
|
178 |
qt_token_info(const char *f, const ulong fc) |
|
179 |
: fields(f), field_count(fc), results(fc), lengths(fc) |
|
180 |
{ |
|
181 |
results.fill(0); |
|
182 |
lengths.fill(0); |
|
183 |
} |
|
184 |
||
185 |
const char *fields; |
|
186 |
const ulong field_count; |
|
187 |
||
188 |
QVector<const char *> results; |
|
189 |
QVector<ulong> lengths; |
|
190 |
}; |
|
191 |
||
192 |
/* |
|
193 |
return values: |
|
194 |
1 parse ok |
|
195 |
0 eos |
|
196 |
-1 parse error |
|
197 |
*/ |
|
198 |
static int qt_tokenize(const char *s, ulong s_len, ulong *advance, |
|
199 |
qt_token_info &token_info) |
|
200 |
{ |
|
201 |
ulong pos = 0, field = 0, fieldlen = 0; |
|
202 |
char current; |
|
203 |
int ret = -1; |
|
204 |
*advance = 0; |
|
205 |
for (;;) { |
|
206 |
current = s[pos]; |
|
207 |
||
208 |
// next char |
|
209 |
++pos; |
|
210 |
++fieldlen; |
|
211 |
++*advance; |
|
212 |
||
213 |
if (! current || pos == s_len + 1) { |
|
214 |
// save result |
|
215 |
token_info.results[(int)field] = s; |
|
216 |
token_info.lengths[(int)field] = fieldlen - 1; |
|
217 |
||
218 |
// end of string |
|
219 |
ret = 0; |
|
220 |
break; |
|
221 |
} |
|
222 |
||
223 |
if (current == token_info.fields[field]) { |
|
224 |
// save result |
|
225 |
token_info.results[(int)field] = s; |
|
226 |
token_info.lengths[(int)field] = fieldlen - 1; |
|
227 |
||
228 |
// end of field |
|
229 |
fieldlen = 0; |
|
230 |
++field; |
|
231 |
if (field == token_info.field_count - 1) { |
|
232 |
// parse ok |
|
233 |
ret = 1; |
|
234 |
} |
|
235 |
if (field == token_info.field_count) { |
|
236 |
// done parsing |
|
237 |
break; |
|
238 |
} |
|
239 |
||
240 |
// reset string and its length |
|
241 |
s = s + pos; |
|
242 |
s_len -= pos; |
|
243 |
pos = 0; |
|
244 |
} |
|
245 |
} |
|
246 |
||
247 |
return ret; |
|
248 |
} |
|
249 |
||
250 |
/* |
|
251 |
returns true if the string s was correctly parsed, false otherwise. |
|
252 |
*/ |
|
253 |
static bool qt_parse_pattern(const char *s, uint *version, bool *debug, QByteArray *key) |
|
254 |
{ |
|
255 |
bool ret = true; |
|
256 |
||
257 |
qt_token_info pinfo("=\n", 2); |
|
258 |
int parse; |
|
259 |
ulong at = 0, advance, parselen = qstrlen(s); |
|
260 |
do { |
|
261 |
parse = qt_tokenize(s + at, parselen, &advance, pinfo); |
|
262 |
if (parse == -1) { |
|
263 |
ret = false; |
|
264 |
break; |
|
265 |
} |
|
266 |
||
267 |
at += advance; |
|
268 |
parselen -= advance; |
|
269 |
||
270 |
if (qstrncmp("version", pinfo.results[0], pinfo.lengths[0]) == 0) { |
|
271 |
// parse version string |
|
272 |
qt_token_info pinfo2("..-", 3); |
|
273 |
if (qt_tokenize(pinfo.results[1], pinfo.lengths[1], |
|
274 |
&advance, pinfo2) != -1) { |
|
275 |
QByteArray m(pinfo2.results[0], pinfo2.lengths[0]); |
|
276 |
QByteArray n(pinfo2.results[1], pinfo2.lengths[1]); |
|
277 |
QByteArray p(pinfo2.results[2], pinfo2.lengths[2]); |
|
278 |
*version = (m.toUInt() << 16) | (n.toUInt() << 8) | p.toUInt(); |
|
279 |
} else { |
|
280 |
ret = false; |
|
281 |
break; |
|
282 |
} |
|
283 |
} else if (qstrncmp("debug", pinfo.results[0], pinfo.lengths[0]) == 0) { |
|
284 |
*debug = qstrncmp("true", pinfo.results[1], pinfo.lengths[1]) == 0; |
|
285 |
} else if (qstrncmp("buildkey", pinfo.results[0], |
|
286 |
pinfo.lengths[0]) == 0){ |
|
287 |
// save buildkey |
|
288 |
*key = QByteArray(pinfo.results[1], pinfo.lengths[1]); |
|
289 |
} |
|
290 |
} while (parse == 1 && parselen > 0); |
|
291 |
||
292 |
return ret; |
|
293 |
} |
|
294 |
#endif // QT_NO_PLUGIN_CHECK |
|
295 |
||
296 |
#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) && !defined(Q_OS_SYMBIAN) && !defined(QT_NO_PLUGIN_CHECK) |
|
297 |
||
298 |
#if defined(Q_OS_FREEBSD) || defined(Q_OS_LINUX) |
|
299 |
# define USE_MMAP |
|
300 |
QT_BEGIN_INCLUDE_NAMESPACE |
|
301 |
# include <sys/types.h> |
|
302 |
# include <sys/mman.h> |
|
303 |
QT_END_INCLUDE_NAMESPACE |
|
304 |
#endif // Q_OS_FREEBSD || Q_OS_LINUX |
|
305 |
||
306 |
static long qt_find_pattern(const char *s, ulong s_len, |
|
307 |
const char *pattern, ulong p_len) |
|
308 |
{ |
|
309 |
/* |
|
310 |
we search from the end of the file because on the supported |
|
311 |
systems, the read-only data/text segments are placed at the end |
|
312 |
of the file. HOWEVER, when building with debugging enabled, all |
|
313 |
the debug symbols are placed AFTER the data/text segments. |
|
314 |
||
315 |
what does this mean? when building in release mode, the search |
|
316 |
is fast because the data we are looking for is at the end of the |
|
317 |
file... when building in debug mode, the search is slower |
|
318 |
because we have to skip over all the debugging symbols first |
|
319 |
*/ |
|
320 |
||
321 |
if (! s || ! pattern || p_len > s_len) return -1; |
|
322 |
ulong i, hs = 0, hp = 0, delta = s_len - p_len; |
|
323 |
||
324 |
for (i = 0; i < p_len; ++i) { |
|
325 |
hs += s[delta + i]; |
|
326 |
hp += pattern[i]; |
|
327 |
} |
|
328 |
i = delta; |
|
329 |
for (;;) { |
|
330 |
if (hs == hp && qstrncmp(s + i, pattern, p_len) == 0) |
|
331 |
return i; |
|
332 |
if (i == 0) |
|
333 |
break; |
|
334 |
--i; |
|
335 |
hs -= s[i + p_len]; |
|
336 |
hs += s[i]; |
|
337 |
} |
|
338 |
||
339 |
return -1; |
|
340 |
} |
|
341 |
||
342 |
/* |
|
343 |
This opens the specified library, mmaps it into memory, and searches |
|
344 |
for the QT_PLUGIN_VERIFICATION_DATA. The advantage of this approach is that |
|
345 |
we can get the verification data without have to actually load the library. |
|
346 |
This lets us detect mismatches more safely. |
|
347 |
||
348 |
Returns false if version/key information is not present, or if the |
|
349 |
information could not be read. |
|
350 |
Returns true if version/key information is present and successfully read. |
|
351 |
*/ |
|
352 |
static bool qt_unix_query(const QString &library, uint *version, bool *debug, QByteArray *key, QLibraryPrivate *lib = 0) |
|
353 |
{ |
|
354 |
QFile file(library); |
|
355 |
if (!file.open(QIODevice::ReadOnly)) { |
|
356 |
if (lib) |
|
357 |
lib->errorString = file.errorString(); |
|
358 |
if (qt_debug_component()) { |
|
359 |
qWarning("%s: %s", (const char*) QFile::encodeName(library), |
|
360 |
qPrintable(qt_error_string(errno))); |
|
361 |
} |
|
362 |
return false; |
|
363 |
} |
|
364 |
||
365 |
QByteArray data; |
|
366 |
char *filedata = 0; |
|
367 |
ulong fdlen = 0; |
|
368 |
||
369 |
# ifdef USE_MMAP |
|
370 |
char *mapaddr = 0; |
|
371 |
size_t maplen = file.size(); |
|
372 |
mapaddr = (char *) mmap(mapaddr, maplen, PROT_READ, MAP_PRIVATE, file.handle(), 0); |
|
373 |
if (mapaddr != MAP_FAILED) { |
|
374 |
// mmap succeeded |
|
375 |
filedata = mapaddr; |
|
376 |
fdlen = maplen; |
|
377 |
} else { |
|
378 |
// mmap failed |
|
379 |
if (qt_debug_component()) { |
|
380 |
qWarning("mmap: %s", qPrintable(qt_error_string(errno))); |
|
381 |
} |
|
382 |
if (lib) |
|
383 |
lib->errorString = QLibrary::tr("Could not mmap '%1': %2") |
|
384 |
.arg(library) |
|
385 |
.arg(qt_error_string()); |
|
386 |
# endif // USE_MMAP |
|
387 |
// try reading the data into memory instead |
|
388 |
data = file.readAll(); |
|
389 |
filedata = data.data(); |
|
390 |
fdlen = data.size(); |
|
391 |
# ifdef USE_MMAP |
|
392 |
} |
|
393 |
# endif // USE_MMAP |
|
394 |
||
395 |
// verify that the pattern is present in the plugin |
|
396 |
const char pattern[] = "pattern=QT_PLUGIN_VERIFICATION_DATA"; |
|
397 |
const ulong plen = qstrlen(pattern); |
|
398 |
long pos = qt_find_pattern(filedata, fdlen, pattern, plen); |
|
399 |
||
400 |
bool ret = false; |
|
401 |
if (pos >= 0) |
|
402 |
ret = qt_parse_pattern(filedata + pos, version, debug, key); |
|
403 |
||
404 |
if (!ret && lib) |
|
405 |
lib->errorString = QLibrary::tr("Plugin verification data mismatch in '%1'").arg(library); |
|
406 |
# ifdef USE_MMAP |
|
407 |
if (mapaddr != MAP_FAILED && munmap(mapaddr, maplen) != 0) { |
|
408 |
if (qt_debug_component()) |
|
409 |
qWarning("munmap: %s", qPrintable(qt_error_string(errno))); |
|
410 |
if (lib) |
|
411 |
lib->errorString = QLibrary::tr("Could not unmap '%1': %2") |
|
412 |
.arg(library) |
|
413 |
.arg( qt_error_string() ); |
|
414 |
} |
|
415 |
# endif // USE_MMAP |
|
416 |
||
417 |
file.close(); |
|
418 |
return ret; |
|
419 |
} |
|
420 |
||
421 |
#endif // Q_OS_UNIX && !Q_OS_MAC && !defined(Q_OS_SYMBIAN) && !defined(QT_NO_PLUGIN_CHECK) |
|
422 |
||
423 |
typedef QMap<QString, QLibraryPrivate*> LibraryMap; |
|
424 |
||
425 |
struct LibraryData { |
|
426 |
LibraryData() : settings(0) { } |
|
427 |
~LibraryData() { |
|
428 |
delete settings; |
|
429 |
} |
|
430 |
||
431 |
QSettings *settings; |
|
432 |
LibraryMap libraryMap; |
|
433 |
}; |
|
434 |
||
435 |
Q_GLOBAL_STATIC(LibraryData, libraryData) |
|
436 |
||
437 |
static LibraryMap *libraryMap() |
|
438 |
{ |
|
439 |
LibraryData *data = libraryData(); |
|
440 |
return data ? &data->libraryMap : 0; |
|
441 |
} |
|
442 |
||
443 |
QLibraryPrivate::QLibraryPrivate(const QString &canonicalFileName, const QString &version) |
|
444 |
:pHnd(0), fileName(canonicalFileName), fullVersion(version), instance(0), qt_version(0), |
|
445 |
libraryRefCount(1), libraryUnloadCount(0), pluginState(MightBeAPlugin) |
|
446 |
{ libraryMap()->insert(canonicalFileName, this); } |
|
447 |
||
448 |
QLibraryPrivate *QLibraryPrivate::findOrCreate(const QString &fileName, const QString &version) |
|
449 |
{ |
|
450 |
QMutexLocker locker(qt_library_mutex()); |
|
451 |
if (QLibraryPrivate *lib = libraryMap()->value(fileName)) { |
|
452 |
lib->libraryRefCount.ref(); |
|
453 |
return lib; |
|
454 |
} |
|
455 |
||
456 |
return new QLibraryPrivate(fileName, version); |
|
457 |
} |
|
458 |
||
459 |
QLibraryPrivate::~QLibraryPrivate() |
|
460 |
{ |
|
461 |
LibraryMap * const map = libraryMap(); |
|
462 |
if (map) { |
|
463 |
QLibraryPrivate *that = map->take(fileName); |
|
464 |
Q_ASSERT(this == that); |
|
465 |
Q_UNUSED(that); |
|
466 |
} |
|
467 |
} |
|
468 |
||
469 |
void *QLibraryPrivate::resolve(const char *symbol) |
|
470 |
{ |
|
471 |
if (!pHnd) |
|
472 |
return 0; |
|
473 |
return resolve_sys(symbol); |
|
474 |
} |
|
475 |
||
476 |
||
477 |
bool QLibraryPrivate::load() |
|
478 |
{ |
|
479 |
libraryUnloadCount.ref(); |
|
480 |
if (pHnd) |
|
481 |
return true; |
|
482 |
if (fileName.isEmpty()) |
|
483 |
return false; |
|
484 |
return load_sys(); |
|
485 |
} |
|
486 |
||
487 |
bool QLibraryPrivate::unload() |
|
488 |
{ |
|
489 |
if (!pHnd) |
|
490 |
return false; |
|
491 |
if (!libraryUnloadCount.deref()) { // only unload if ALL QLibrary instance wanted to |
|
492 |
if (instance) |
|
493 |
delete instance(); |
|
494 |
if (unload_sys()) { |
|
495 |
instance = 0; |
|
496 |
pHnd = 0; |
|
497 |
} |
|
498 |
} |
|
499 |
||
500 |
return (pHnd == 0); |
|
501 |
} |
|
502 |
||
503 |
void QLibraryPrivate::release() |
|
504 |
{ |
|
505 |
QMutexLocker locker(qt_library_mutex()); |
|
506 |
if (!libraryRefCount.deref()) |
|
507 |
delete this; |
|
508 |
} |
|
509 |
||
510 |
bool QLibraryPrivate::loadPlugin() |
|
511 |
{ |
|
512 |
if (instance) { |
|
513 |
libraryUnloadCount.ref(); |
|
514 |
return true; |
|
515 |
} |
|
516 |
if (load()) { |
|
517 |
instance = (QtPluginInstanceFunction)resolve("qt_plugin_instance"); |
|
518 |
#if defined(Q_OS_SYMBIAN) |
|
519 |
if (!instance) { |
|
520 |
// If resolving with function name failed (i.e. not STDDLL), |
|
521 |
// try resolving using known ordinal, which for |
|
522 |
// qt_plugin_instance function is always "2". |
|
523 |
instance = (QtPluginInstanceFunction)resolve("2"); |
|
524 |
} |
|
525 |
#endif |
|
526 |
return instance; |
|
527 |
} |
|
528 |
return false; |
|
529 |
} |
|
530 |
||
531 |
/*! |
|
532 |
Returns true if \a fileName has a valid suffix for a loadable |
|
533 |
library; otherwise returns false. |
|
534 |
||
535 |
\table |
|
536 |
\header \i Platform \i Valid suffixes |
|
537 |
\row \i Windows \i \c .dll |
|
538 |
\row \i Unix/Linux \i \c .so |
|
539 |
\row \i AIX \i \c .a |
|
540 |
\row \i HP-UX \i \c .sl, \c .so (HP-UXi) |
|
541 |
\row \i Mac OS X \i \c .dylib, \c .bundle, \c .so |
|
5
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
542 |
\row \i Symbian \i \c .dll |
0 | 543 |
\endtable |
544 |
||
545 |
Trailing versioning numbers on Unix are ignored. |
|
546 |
*/ |
|
547 |
bool QLibrary::isLibrary(const QString &fileName) |
|
548 |
{ |
|
549 |
#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE) |
|
550 |
return fileName.endsWith(QLatin1String(".dll")); |
|
551 |
#elif defined(Q_OS_SYMBIAN) |
|
552 |
// Plugin stubs are also considered libraries in Symbian. |
|
553 |
return (fileName.endsWith(QLatin1String(".dll")) || |
|
554 |
fileName.endsWith(QLatin1String(".qtplugin"))); |
|
555 |
#else |
|
556 |
QString completeSuffix = QFileInfo(fileName).completeSuffix(); |
|
557 |
if (completeSuffix.isEmpty()) |
|
558 |
return false; |
|
559 |
QStringList suffixes = completeSuffix.split(QLatin1Char('.')); |
|
560 |
# if defined(Q_OS_DARWIN) |
|
561 |
||
562 |
// On Mac, libs look like libmylib.1.0.0.dylib |
|
563 |
const QString lastSuffix = suffixes.at(suffixes.count() - 1); |
|
564 |
const QString firstSuffix = suffixes.at(0); |
|
565 |
||
566 |
bool valid = (lastSuffix == QLatin1String("dylib") |
|
567 |
|| firstSuffix == QLatin1String("so") |
|
568 |
|| firstSuffix == QLatin1String("bundle")); |
|
569 |
||
570 |
return valid; |
|
571 |
# else // Generic Unix |
|
572 |
QStringList validSuffixList; |
|
573 |
||
574 |
# if defined(Q_OS_HPUX) |
|
575 |
/* |
|
576 |
See "HP-UX Linker and Libraries User's Guide", section "Link-time Differences between PA-RISC and IPF": |
|
577 |
"In PA-RISC (PA-32 and PA-64) shared libraries are suffixed with .sl. In IPF (32-bit and 64-bit), |
|
578 |
the shared libraries are suffixed with .so. For compatibility, the IPF linker also supports the .sl suffix." |
|
579 |
*/ |
|
580 |
validSuffixList << QLatin1String("sl"); |
|
581 |
# if defined __ia64 |
|
582 |
validSuffixList << QLatin1String("so"); |
|
583 |
# endif |
|
584 |
# elif defined(Q_OS_AIX) |
|
585 |
validSuffixList << QLatin1String("a") << QLatin1String("so"); |
|
586 |
# elif defined(Q_OS_UNIX) |
|
587 |
validSuffixList << QLatin1String("so"); |
|
588 |
# endif |
|
589 |
||
590 |
// Examples of valid library names: |
|
591 |
// libfoo.so |
|
592 |
// libfoo.so.0 |
|
593 |
// libfoo.so.0.3 |
|
594 |
// libfoo-0.3.so |
|
595 |
// libfoo-0.3.so.0.3.0 |
|
596 |
||
597 |
int suffix; |
|
598 |
int suffixPos = -1; |
|
599 |
for (suffix = 0; suffix < validSuffixList.count() && suffixPos == -1; ++suffix) |
|
600 |
suffixPos = suffixes.indexOf(validSuffixList.at(suffix)); |
|
601 |
||
602 |
bool valid = suffixPos != -1; |
|
603 |
for (int i = suffixPos + 1; i < suffixes.count() && valid; ++i) |
|
604 |
if (i != suffixPos) |
|
605 |
suffixes.at(i).toInt(&valid); |
|
606 |
return valid; |
|
607 |
# endif |
|
608 |
#endif |
|
609 |
||
610 |
} |
|
611 |
||
612 |
bool QLibraryPrivate::isPlugin(QSettings *settings) |
|
613 |
{ |
|
614 |
errorString.clear(); |
|
615 |
if (pluginState != MightBeAPlugin) |
|
616 |
return pluginState == IsAPlugin; |
|
617 |
||
618 |
#ifndef QT_NO_PLUGIN_CHECK |
|
619 |
bool debug = !QLIBRARY_AS_DEBUG; |
|
620 |
QByteArray key; |
|
621 |
bool success = false; |
|
622 |
||
623 |
QFileInfo fileinfo(fileName); |
|
624 |
||
625 |
#ifndef QT_NO_DATESTRING |
|
626 |
lastModified = fileinfo.lastModified().toString(Qt::ISODate); |
|
627 |
#endif |
|
628 |
QString regkey = QString::fromLatin1("Qt Plugin Cache %1.%2.%3/%4") |
|
629 |
.arg((QT_VERSION & 0xff0000) >> 16) |
|
630 |
.arg((QT_VERSION & 0xff00) >> 8) |
|
631 |
.arg(QLIBRARY_AS_DEBUG ? QLatin1String("debug") : QLatin1String("false")) |
|
632 |
.arg(fileName); |
|
633 |
QStringList reg; |
|
634 |
#ifndef QT_NO_SETTINGS |
|
635 |
if (!settings) { |
|
636 |
settings = libraryData()->settings; |
|
637 |
if (!settings) { |
|
638 |
settings = new QSettings(QSettings::UserScope, QLatin1String("Trolltech")); |
|
639 |
libraryData()->settings = settings; |
|
640 |
} |
|
641 |
} |
|
642 |
reg = settings->value(regkey).toStringList(); |
|
643 |
#endif |
|
644 |
if (reg.count() == 4 && lastModified == reg.at(3)) { |
|
645 |
qt_version = reg.at(0).toUInt(0, 16); |
|
646 |
debug = bool(reg.at(1).toInt()); |
|
647 |
key = reg.at(2).toLatin1(); |
|
648 |
success = qt_version != 0; |
|
649 |
} else { |
|
650 |
#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) && !defined(Q_OS_SYMBIAN) |
|
651 |
if (!pHnd) { |
|
652 |
// use unix shortcut to avoid loading the library |
|
653 |
success = qt_unix_query(fileName, &qt_version, &debug, &key, this); |
|
654 |
} else |
|
655 |
#endif |
|
656 |
{ |
|
657 |
bool temporary_load = false; |
|
658 |
#ifdef Q_OS_WIN |
|
659 |
HMODULE hTempModule = 0; |
|
660 |
#endif |
|
661 |
if (!pHnd) { |
|
662 |
#ifdef Q_OS_WIN |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
663 |
//avoid 'Bad Image' message box |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
664 |
UINT oldmode = SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOOPENFILEERRORBOX); |
0 | 665 |
hTempModule = ::LoadLibraryEx((wchar_t*)QDir::toNativeSeparators(fileName).utf16(), 0, DONT_RESOLVE_DLL_REFERENCES); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
666 |
SetErrorMode(oldmode); |
0 | 667 |
#else |
668 |
# if defined(Q_OS_SYMBIAN) |
|
669 |
//Guard against accidentally trying to load non-plugin libraries by making sure the stub exists |
|
670 |
if (fileinfo.exists()) |
|
671 |
# endif |
|
672 |
temporary_load = load_sys(); |
|
673 |
#endif |
|
674 |
} |
|
675 |
# ifdef Q_CC_BOR |
|
676 |
typedef const char * __stdcall (*QtPluginQueryVerificationDataFunction)(); |
|
677 |
# else |
|
678 |
typedef const char * (*QtPluginQueryVerificationDataFunction)(); |
|
679 |
# endif |
|
680 |
#ifdef Q_OS_WIN |
|
681 |
QtPluginQueryVerificationDataFunction qtPluginQueryVerificationDataFunction = hTempModule |
|
682 |
? (QtPluginQueryVerificationDataFunction) |
|
683 |
#ifdef Q_OS_WINCE |
|
684 |
::GetProcAddress(hTempModule, L"qt_plugin_query_verification_data") |
|
685 |
#else |
|
686 |
::GetProcAddress(hTempModule, "qt_plugin_query_verification_data") |
|
687 |
#endif |
|
688 |
: (QtPluginQueryVerificationDataFunction) resolve("qt_plugin_query_verification_data"); |
|
689 |
#else |
|
690 |
QtPluginQueryVerificationDataFunction qtPluginQueryVerificationDataFunction = NULL; |
|
691 |
# if defined(Q_OS_SYMBIAN) |
|
692 |
if (temporary_load) { |
|
693 |
qtPluginQueryVerificationDataFunction = (QtPluginQueryVerificationDataFunction) resolve("qt_plugin_query_verification_data"); |
|
694 |
// If resolving with function name failed (i.e. not STDDLL), try resolving using known ordinal |
|
695 |
if (!qtPluginQueryVerificationDataFunction) |
|
696 |
qtPluginQueryVerificationDataFunction = (QtPluginQueryVerificationDataFunction) resolve("1"); |
|
697 |
} |
|
698 |
# else |
|
699 |
qtPluginQueryVerificationDataFunction = (QtPluginQueryVerificationDataFunction) resolve("qt_plugin_query_verification_data"); |
|
700 |
# endif |
|
701 |
#endif |
|
702 |
||
703 |
if (!qtPluginQueryVerificationDataFunction |
|
704 |
|| !qt_parse_pattern(qtPluginQueryVerificationDataFunction(), &qt_version, &debug, &key)) { |
|
705 |
qt_version = 0; |
|
706 |
key = "unknown"; |
|
707 |
if (temporary_load) |
|
708 |
unload_sys(); |
|
709 |
} else { |
|
710 |
success = true; |
|
711 |
} |
|
712 |
#ifdef Q_OS_WIN |
|
713 |
if (hTempModule) { |
|
714 |
BOOL ok = ::FreeLibrary(hTempModule); |
|
715 |
if (ok) { |
|
716 |
hTempModule = 0; |
|
717 |
} |
|
718 |
||
719 |
} |
|
720 |
#endif |
|
721 |
} |
|
722 |
||
723 |
// Qt 4.5 compatibility: stl doesn't affect binary compatibility |
|
724 |
key.replace(" no-stl", ""); |
|
725 |
||
726 |
#ifndef QT_NO_SETTINGS |
|
727 |
QStringList queried; |
|
728 |
queried << QString::number(qt_version,16) |
|
729 |
<< QString::number((int)debug) |
|
730 |
<< QLatin1String(key) |
|
731 |
<< lastModified; |
|
732 |
settings->setValue(regkey, queried); |
|
733 |
#endif |
|
734 |
} |
|
735 |
||
736 |
if (!success) { |
|
737 |
if (errorString.isEmpty()){ |
|
738 |
if (fileName.isEmpty()) |
|
739 |
errorString = QLibrary::tr("The shared library was not found."); |
|
740 |
else |
|
741 |
errorString = QLibrary::tr("The file '%1' is not a valid Qt plugin.").arg(fileName); |
|
742 |
} |
|
743 |
return false; |
|
744 |
} |
|
745 |
||
746 |
pluginState = IsNotAPlugin; // be pessimistic |
|
747 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
748 |
if ((qt_version & 0x00ff00) > (QT_VERSION & 0x00ff00) || (qt_version & 0xff0000) != (QT_VERSION & 0xff0000)) { |
0 | 749 |
if (qt_debug_component()) { |
750 |
qWarning("In %s:\n" |
|
751 |
" Plugin uses incompatible Qt library (%d.%d.%d) [%s]", |
|
752 |
(const char*) QFile::encodeName(fileName), |
|
753 |
(qt_version&0xff0000) >> 16, (qt_version&0xff00) >> 8, qt_version&0xff, |
|
754 |
debug ? "debug" : "release"); |
|
755 |
} |
|
756 |
errorString = QLibrary::tr("The plugin '%1' uses incompatible Qt library. (%2.%3.%4) [%5]") |
|
757 |
.arg(fileName) |
|
758 |
.arg((qt_version&0xff0000) >> 16) |
|
759 |
.arg((qt_version&0xff00) >> 8) |
|
760 |
.arg(qt_version&0xff) |
|
761 |
.arg(debug ? QLatin1String("debug") : QLatin1String("release")); |
|
762 |
} else if (key != QT_BUILD_KEY |
|
763 |
#ifdef QT_BUILD_KEY_COMPAT |
|
764 |
// be sure to load plugins using an older but compatible build key |
|
765 |
&& key != QT_BUILD_KEY_COMPAT |
|
766 |
#endif |
|
767 |
) { |
|
768 |
if (qt_debug_component()) { |
|
769 |
qWarning("In %s:\n" |
|
770 |
" Plugin uses incompatible Qt library\n" |
|
771 |
" expected build key \"%s\", got \"%s\"", |
|
772 |
(const char*) QFile::encodeName(fileName), |
|
773 |
QT_BUILD_KEY, |
|
774 |
key.isEmpty() ? "<null>" : (const char *) key); |
|
775 |
} |
|
776 |
errorString = QLibrary::tr("The plugin '%1' uses incompatible Qt library." |
|
777 |
" Expected build key \"%2\", got \"%3\"") |
|
778 |
.arg(fileName) |
|
779 |
.arg(QLatin1String(QT_BUILD_KEY)) |
|
780 |
.arg(key.isEmpty() ? QLatin1String("<null>") : QLatin1String((const char *) key)); |
|
781 |
#ifndef QT_NO_DEBUG_PLUGIN_CHECK |
|
782 |
} else if(debug != QLIBRARY_AS_DEBUG) { |
|
783 |
//don't issue a qWarning since we will hopefully find a non-debug? --Sam |
|
784 |
errorString = QLibrary::tr("The plugin '%1' uses incompatible Qt library." |
|
785 |
" (Cannot mix debug and release libraries.)").arg(fileName); |
|
786 |
#endif |
|
787 |
} else { |
|
788 |
pluginState = IsAPlugin; |
|
789 |
} |
|
790 |
||
791 |
return pluginState == IsAPlugin; |
|
792 |
#else |
|
793 |
Q_UNUSED(settings); |
|
794 |
return pluginState == MightBeAPlugin; |
|
795 |
#endif |
|
796 |
} |
|
797 |
||
798 |
/*! |
|
799 |
Loads the library and returns true if the library was loaded |
|
800 |
successfully; otherwise returns false. Since resolve() always |
|
801 |
calls this function before resolving any symbols it is not |
|
802 |
necessary to call it explicitly. In some situations you might want |
|
803 |
the library loaded in advance, in which case you would use this |
|
804 |
function. |
|
805 |
||
806 |
\sa unload() |
|
807 |
*/ |
|
808 |
bool QLibrary::load() |
|
809 |
{ |
|
810 |
if (!d) |
|
811 |
return false; |
|
812 |
if (did_load) |
|
813 |
return d->pHnd; |
|
814 |
did_load = true; |
|
815 |
return d->load(); |
|
816 |
} |
|
817 |
||
818 |
/*! |
|
819 |
Unloads the library and returns true if the library could be |
|
820 |
unloaded; otherwise returns false. |
|
821 |
||
822 |
This happens automatically on application termination, so you |
|
823 |
shouldn't normally need to call this function. |
|
824 |
||
825 |
If other instances of QLibrary are using the same library, the |
|
826 |
call will fail, and unloading will only happen when every instance |
|
827 |
has called unload(). |
|
828 |
||
829 |
Note that on Mac OS X 10.3 (Panther), dynamic libraries cannot be unloaded. |
|
830 |
||
831 |
\sa resolve(), load() |
|
832 |
*/ |
|
833 |
bool QLibrary::unload() |
|
834 |
{ |
|
835 |
if (did_load) { |
|
836 |
did_load = false; |
|
837 |
return d->unload(); |
|
838 |
} |
|
839 |
return false; |
|
840 |
} |
|
841 |
||
842 |
/*! |
|
843 |
Returns true if the library is loaded; otherwise returns false. |
|
844 |
||
845 |
\sa load() |
|
846 |
*/ |
|
847 |
bool QLibrary::isLoaded() const |
|
848 |
{ |
|
849 |
return d && d->pHnd; |
|
850 |
} |
|
851 |
||
852 |
||
853 |
/*! |
|
854 |
Constructs a library with the given \a parent. |
|
855 |
*/ |
|
856 |
QLibrary::QLibrary(QObject *parent) |
|
857 |
:QObject(parent), d(0), did_load(false) |
|
858 |
{ |
|
859 |
} |
|
860 |
||
861 |
||
862 |
/*! |
|
863 |
Constructs a library object with the given \a parent that will |
|
864 |
load the library specified by \a fileName. |
|
865 |
||
866 |
We recommend omitting the file's suffix in \a fileName, since |
|
867 |
QLibrary will automatically look for the file with the appropriate |
|
868 |
suffix in accordance with the platform, e.g. ".so" on Unix, |
|
869 |
".dylib" on Mac OS X, and ".dll" on Windows. (See \l{fileName}.) |
|
870 |
||
871 |
Note: In Symbian the path portion of the \a fileName is ignored. |
|
872 |
*/ |
|
873 |
QLibrary::QLibrary(const QString& fileName, QObject *parent) |
|
874 |
:QObject(parent), d(0), did_load(false) |
|
875 |
{ |
|
876 |
setFileName(fileName); |
|
877 |
} |
|
878 |
||
879 |
||
880 |
/*! |
|
881 |
Constructs a library object with the given \a parent that will |
|
882 |
load the library specified by \a fileName and major version number \a verNum. |
|
883 |
Currently, the version number is ignored on Windows and Symbian. |
|
884 |
||
885 |
We recommend omitting the file's suffix in \a fileName, since |
|
886 |
QLibrary will automatically look for the file with the appropriate |
|
887 |
suffix in accordance with the platform, e.g. ".so" on Unix, |
|
888 |
".dylib" on Mac OS X, and ".dll" on Windows. (See \l{fileName}.) |
|
889 |
||
890 |
Note: In Symbian the path portion of the \a fileName is ignored. |
|
891 |
*/ |
|
892 |
QLibrary::QLibrary(const QString& fileName, int verNum, QObject *parent) |
|
893 |
:QObject(parent), d(0), did_load(false) |
|
894 |
{ |
|
895 |
setFileNameAndVersion(fileName, verNum); |
|
896 |
} |
|
897 |
||
898 |
/*! |
|
899 |
Constructs a library object with the given \a parent that will |
|
900 |
load the library specified by \a fileName and full version number \a version. |
|
901 |
Currently, the version number is ignored on Windows and Symbian. |
|
902 |
||
903 |
We recommend omitting the file's suffix in \a fileName, since |
|
904 |
QLibrary will automatically look for the file with the appropriate |
|
905 |
suffix in accordance with the platform, e.g. ".so" on Unix, |
|
906 |
".dylib" on Mac OS X, and ".dll" on Windows. (See \l{fileName}.) |
|
907 |
||
908 |
Note: In Symbian the path portion of the \a fileName is ignored. |
|
909 |
*/ |
|
910 |
QLibrary::QLibrary(const QString& fileName, const QString &version, QObject *parent) |
|
911 |
:QObject(parent), d(0), did_load(false) |
|
912 |
{ |
|
913 |
setFileNameAndVersion(fileName, version); |
|
914 |
} |
|
915 |
||
916 |
/*! |
|
917 |
Destroys the QLibrary object. |
|
918 |
||
919 |
Unless unload() was called explicitly, the library stays in memory |
|
920 |
until the application terminates. |
|
921 |
||
922 |
\sa isLoaded(), unload() |
|
923 |
*/ |
|
924 |
QLibrary::~QLibrary() |
|
925 |
{ |
|
926 |
if (d) |
|
927 |
d->release(); |
|
928 |
} |
|
929 |
||
930 |
||
931 |
/*! |
|
932 |
\property QLibrary::fileName |
|
933 |
\brief the file name of the library |
|
934 |
||
935 |
We recommend omitting the file's suffix in the file name, since |
|
936 |
QLibrary will automatically look for the file with the appropriate |
|
937 |
suffix (see isLibrary()). |
|
938 |
||
939 |
When loading the library, QLibrary searches in all system-specific |
|
940 |
library locations (e.g. \c LD_LIBRARY_PATH on Unix), unless the |
|
941 |
file name has an absolute path. After loading the library |
|
942 |
successfully, fileName() returns the fully-qualified file name of |
|
943 |
the library, including the full path to the library if one was given |
|
944 |
in the constructor or passed to setFileName(). |
|
945 |
||
946 |
For example, after successfully loading the "GL" library on Unix |
|
947 |
platforms, fileName() will return "libGL.so". If the file name was |
|
948 |
originally passed as "/usr/lib/libGL", fileName() will return |
|
949 |
"/usr/lib/libGL.so". |
|
950 |
||
951 |
Note: In Symbian the path portion of the \a fileName is ignored. |
|
952 |
*/ |
|
953 |
||
954 |
void QLibrary::setFileName(const QString &fileName) |
|
955 |
{ |
|
956 |
QLibrary::LoadHints lh; |
|
957 |
if (d) { |
|
958 |
lh = d->loadHints; |
|
959 |
d->release(); |
|
960 |
d = 0; |
|
961 |
did_load = false; |
|
962 |
} |
|
963 |
d = QLibraryPrivate::findOrCreate(fileName); |
|
964 |
d->loadHints = lh; |
|
965 |
} |
|
966 |
||
967 |
QString QLibrary::fileName() const |
|
968 |
{ |
|
969 |
if (d) |
|
970 |
return d->qualifiedFileName.isEmpty() ? d->fileName : d->qualifiedFileName; |
|
971 |
return QString(); |
|
972 |
} |
|
973 |
||
974 |
/*! |
|
975 |
\fn void QLibrary::setFileNameAndVersion(const QString &fileName, int versionNumber) |
|
976 |
||
977 |
Sets the fileName property and major version number to \a fileName |
|
978 |
and \a versionNumber respectively. |
|
979 |
The \a versionNumber is ignored on Windows and Symbian. |
|
980 |
||
981 |
Note: In Symbian the path portion of the \a fileName is ignored. |
|
982 |
||
983 |
\sa setFileName() |
|
984 |
*/ |
|
985 |
void QLibrary::setFileNameAndVersion(const QString &fileName, int verNum) |
|
986 |
{ |
|
987 |
QLibrary::LoadHints lh; |
|
988 |
if (d) { |
|
989 |
lh = d->loadHints; |
|
990 |
d->release(); |
|
991 |
d = 0; |
|
992 |
did_load = false; |
|
993 |
} |
|
994 |
d = QLibraryPrivate::findOrCreate(fileName, verNum >= 0 ? QString::number(verNum) : QString()); |
|
995 |
d->loadHints = lh; |
|
996 |
} |
|
997 |
||
998 |
/*! |
|
999 |
\since 4.4 |
|
1000 |
||
1001 |
Sets the fileName property and full version number to \a fileName |
|
1002 |
and \a version respectively. |
|
1003 |
The \a version parameter is ignored on Windows and Symbian. |
|
1004 |
||
1005 |
Note: In Symbian the path portion of the \a fileName is ignored. |
|
1006 |
||
1007 |
\sa setFileName() |
|
1008 |
*/ |
|
1009 |
void QLibrary::setFileNameAndVersion(const QString &fileName, const QString &version) |
|
1010 |
{ |
|
1011 |
QLibrary::LoadHints lh; |
|
1012 |
if (d) { |
|
1013 |
lh = d->loadHints; |
|
1014 |
d->release(); |
|
1015 |
d = 0; |
|
1016 |
did_load = false; |
|
1017 |
} |
|
1018 |
d = QLibraryPrivate::findOrCreate(fileName, version); |
|
1019 |
d->loadHints = lh; |
|
1020 |
} |
|
1021 |
||
1022 |
/*! |
|
1023 |
Returns the address of the exported symbol \a symbol. The library is |
|
1024 |
loaded if necessary. The function returns 0 if the symbol could |
|
1025 |
not be resolved or if the library could not be loaded. |
|
1026 |
||
1027 |
Example: |
|
1028 |
\snippet doc/src/snippets/code/src_corelib_plugin_qlibrary.cpp 2 |
|
1029 |
||
1030 |
The symbol must be exported as a C function from the library. This |
|
1031 |
means that the function must be wrapped in an \c{extern "C"} if |
|
1032 |
the library is compiled with a C++ compiler. On Windows you must |
|
1033 |
also explicitly export the function from the DLL using the |
|
1034 |
\c{__declspec(dllexport)} compiler directive, for example: |
|
1035 |
||
1036 |
\snippet doc/src/snippets/code/src_corelib_plugin_qlibrary.cpp 3 |
|
1037 |
||
1038 |
with \c MY_EXPORT defined as |
|
1039 |
||
1040 |
\snippet doc/src/snippets/code/src_corelib_plugin_qlibrary.cpp 4 |
|
1041 |
||
1042 |
Note: In Symbian resolving with symbol names works only if the loaded |
|
1043 |
library was built as STDDLL. Otherwise, the ordinals must be used. |
|
1044 |
*/ |
|
1045 |
void *QLibrary::resolve(const char *symbol) |
|
1046 |
{ |
|
1047 |
if (!load()) |
|
1048 |
return 0; |
|
1049 |
return d->resolve(symbol); |
|
1050 |
} |
|
1051 |
||
1052 |
/*! |
|
1053 |
\overload |
|
1054 |
||
1055 |
Loads the library \a fileName and returns the address of the |
|
1056 |
exported symbol \a symbol. Note that \a fileName should not |
|
1057 |
include the platform-specific file suffix; (see \l{fileName}). The |
|
1058 |
library remains loaded until the application exits. |
|
1059 |
||
1060 |
The function returns 0 if the symbol could not be resolved or if |
|
1061 |
the library could not be loaded. |
|
1062 |
||
1063 |
Note: In Symbian resolving with symbol names works only if the loaded |
|
1064 |
library was built as STDDLL. Otherwise, the ordinals must be used. |
|
1065 |
||
1066 |
\sa resolve() |
|
1067 |
*/ |
|
1068 |
void *QLibrary::resolve(const QString &fileName, const char *symbol) |
|
1069 |
{ |
|
1070 |
QLibrary library(fileName); |
|
1071 |
return library.resolve(symbol); |
|
1072 |
} |
|
1073 |
||
1074 |
/*! |
|
1075 |
\overload |
|
1076 |
||
1077 |
Loads the library \a fileName with major version number \a verNum and |
|
1078 |
returns the address of the exported symbol \a symbol. |
|
1079 |
Note that \a fileName should not include the platform-specific file suffix; |
|
1080 |
(see \l{fileName}). The library remains loaded until the application exits. |
|
1081 |
\a verNum is ignored on Windows. |
|
1082 |
||
1083 |
The function returns 0 if the symbol could not be resolved or if |
|
1084 |
the library could not be loaded. |
|
1085 |
||
1086 |
Note: In Symbian resolving with symbol names works only if the loaded |
|
1087 |
library was built as STDDLL. Otherwise, the ordinals must be used. |
|
1088 |
||
1089 |
\sa resolve() |
|
1090 |
*/ |
|
1091 |
void *QLibrary::resolve(const QString &fileName, int verNum, const char *symbol) |
|
1092 |
{ |
|
1093 |
QLibrary library(fileName, verNum); |
|
1094 |
return library.resolve(symbol); |
|
1095 |
} |
|
1096 |
||
1097 |
/*! |
|
1098 |
\overload |
|
1099 |
\since 4.4 |
|
1100 |
||
1101 |
Loads the library \a fileName with full version number \a version and |
|
1102 |
returns the address of the exported symbol \a symbol. |
|
1103 |
Note that \a fileName should not include the platform-specific file suffix; |
|
1104 |
(see \l{fileName}). The library remains loaded until the application exits. |
|
1105 |
\a version is ignored on Windows. |
|
1106 |
||
1107 |
The function returns 0 if the symbol could not be resolved or if |
|
1108 |
the library could not be loaded. |
|
1109 |
||
1110 |
Note: In Symbian resolving with symbol names works only if the loaded |
|
1111 |
library was built as STDDLL. Otherwise, the ordinals must be used. |
|
1112 |
||
1113 |
\sa resolve() |
|
1114 |
*/ |
|
1115 |
void *QLibrary::resolve(const QString &fileName, const QString &version, const char *symbol) |
|
1116 |
{ |
|
1117 |
QLibrary library(fileName, version); |
|
1118 |
return library.resolve(symbol); |
|
1119 |
} |
|
1120 |
||
1121 |
/*! |
|
1122 |
\fn QString QLibrary::library() const |
|
1123 |
||
1124 |
Use fileName() instead. |
|
1125 |
*/ |
|
1126 |
||
1127 |
/*! |
|
1128 |
\fn void QLibrary::setAutoUnload( bool b ) |
|
1129 |
||
1130 |
Use load(), isLoaded(), and unload() as necessary instead. |
|
1131 |
*/ |
|
1132 |
||
1133 |
/*! |
|
1134 |
\since 4.2 |
|
1135 |
||
1136 |
Returns a text string with the description of the last error that occurred. |
|
1137 |
Currently, errorString will only be set if load(), unload() or resolve() for some reason fails. |
|
1138 |
*/ |
|
1139 |
QString QLibrary::errorString() const |
|
1140 |
{ |
|
1141 |
return (!d || d->errorString.isEmpty()) ? tr("Unknown error") : d->errorString; |
|
1142 |
} |
|
1143 |
||
1144 |
/*! |
|
1145 |
\property QLibrary::loadHints |
|
1146 |
\brief Give the load() function some hints on how it should behave. |
|
1147 |
||
1148 |
You can give some hints on how the symbols are resolved. Usually, |
|
1149 |
the symbols are not resolved at load time, but resolved lazily, |
|
1150 |
(that is, when resolve() is called). If you set the loadHint to |
|
1151 |
ResolveAllSymbolsHint, then all symbols will be resolved at load time |
|
1152 |
if the platform supports it. |
|
1153 |
||
1154 |
Setting ExportExternalSymbolsHint will make the external symbols in the |
|
1155 |
library available for resolution in subsequent loaded libraries. |
|
1156 |
||
1157 |
If LoadArchiveMemberHint is set, the file name |
|
1158 |
is composed of two components: A path which is a reference to an |
|
1159 |
archive file followed by the second component which is the reference to |
|
1160 |
the archive member. For instance, the fileName \c libGL.a(shr_64.o) will refer |
|
1161 |
to the library \c shr_64.o in the archive file named \c libGL.a. This |
|
1162 |
is only supported on the AIX platform. |
|
1163 |
||
1164 |
The interpretation of the load hints is platform dependent, and if |
|
1165 |
you use it you are probably making some assumptions on which platform |
|
1166 |
you are compiling for, so use them only if you understand the consequences |
|
1167 |
of them. |
|
1168 |
||
1169 |
By default, none of these flags are set, so libraries will be loaded with |
|
1170 |
lazy symbol resolution, and will not export external symbols for resolution |
|
1171 |
in other dynamically-loaded libraries. |
|
1172 |
*/ |
|
1173 |
void QLibrary::setLoadHints(LoadHints hints) |
|
1174 |
{ |
|
1175 |
if (!d) { |
|
1176 |
d = QLibraryPrivate::findOrCreate(QString()); // ugly, but we need a d-ptr |
|
1177 |
d->errorString.clear(); |
|
1178 |
} |
|
1179 |
d->loadHints = hints; |
|
1180 |
} |
|
1181 |
||
1182 |
QLibrary::LoadHints QLibrary::loadHints() const |
|
1183 |
{ |
|
1184 |
return d ? d->loadHints : (QLibrary::LoadHints)0; |
|
1185 |
} |
|
1186 |
||
1187 |
/* Internal, for debugging */ |
|
1188 |
bool qt_debug_component() |
|
1189 |
{ |
|
1190 |
#if defined(QT_DEBUG_COMPONENT) |
|
1191 |
return true; //compatibility? |
|
1192 |
#else |
|
1193 |
static int debug_env = -1; |
|
1194 |
if (debug_env == -1) |
|
1195 |
debug_env = QT_PREPEND_NAMESPACE(qgetenv)("QT_DEBUG_PLUGINS").toInt(); |
|
1196 |
||
1197 |
return debug_env != 0; |
|
1198 |
#endif |
|
1199 |
} |
|
1200 |
||
1201 |
QT_END_NAMESPACE |
|
1202 |
||
1203 |
#endif // QT_NO_LIBRARY |