1QLibrary(3qt) QLibrary(3qt)
2
3
4
6 QLibrary - Wrapper for handling shared libraries
7
9 All the functions in this class are reentrant when Qt is built with
10 thread support.</p>
11
12 #include <qlibrary.h>
13
14 Public Members
15 QLibrary ( const QString & filename )
16 virtual ~QLibrary ()
17 void * resolve ( const char * symb )
18 bool load ()
19 virtual bool unload ()
20 bool isLoaded () const
21 bool autoUnload () const
22 void setAutoUnload ( bool enabled )
23 QString library () const
24
25 Static Public Members
26 void * resolve ( const QString & filename, const char * symb )
27
29 The QLibrary class provides a wrapper for handling shared libraries.
30
31 An instance of a QLibrary object can handle a single shared library and
32 provide access to the functionality in the library in a platform
33 independent way. If the library is a component server, QLibrary
34 provides access to the exported component and can directly query this
35 component for interfaces.
36
37 QLibrary ensures that the shared library is loaded and stays in memory
38 whilst it is in use. QLibrary can also unload the library on
39 destruction and release unused resources.
40
41 A typical use of QLibrary is to resolve an exported symbol in a shared
42 object, and to call the function that this symbol represents. This is
43 called "explicit linking" in contrast to" implicit linking", which is
44 done by the link step in the build process when linking an executable
45 against a library.
46
47 The following code snippet loads a library, resolves the symbol"
48 mysymbol", and calls the function if everything succeeded. If something
49 went wrong, e.g. the library file does not exist or the symbol is not
50 defined, the function pointer will be 0 and won't be called. When the
51 QLibrary object is destroyed the library will be unloaded, making all
52 references to memory allocated in the library invalid.
53
54 typedef void (*MyPrototype)();
55 MyPrototype myFunction;
56 QLibrary myLib( "mylib" );
57 myFunction = (MyPrototype) myLib.resolve( "mysymbol" );
58 if ( myFunction ) {
59 myFunction();
60 }
61
62 See also Plugins.
63
66 Creates a QLibrary object for the shared library filename. The library
67 will be unloaded in the destructor.
68
69 Note that filename does not need to include the (platform specific)
70 file extension, so calling
71
72 QLibrary lib( "mylib" );
73 is equivalent to calling
74
75 QLibrary lib( "mylib.dll" );
76 on Windows, and
77
78 QLibrary lib( "libmylib.so" );
79 on Unix. Specifying the extension is not recommended, since doing so
80 introduces a platform dependency.
81
82 If filename does not include a path, the library loader will look for
83 the file in the platform specific search paths.
84
85 See also load(), unload(), and setAutoUnload().
86
88 Deletes the QLibrary object.
89
90 The library will be unloaded if autoUnload() is TRUE (the default),
91 otherwise it stays in memory until the application exits.
92
93 See also unload() and setAutoUnload().
94
96 Returns TRUE if the library will be automatically unloaded when this
97 wrapper object is destructed; otherwise returns FALSE. The default is
98 TRUE.
99
100 See also setAutoUnload().
101
103 Returns TRUE if the library is loaded; otherwise returns FALSE.
104
105 See also unload().
106
108 Returns the filename of the shared library this QLibrary object
109 handles, including the platform specific file extension.
110
111 For example:
112
113 QLibrary lib( "mylib" );
114 QString str = lib.library();
115 will set str to "mylib.dll" on Windows, and "libmylib.so" on Linux.
116
118 Loads the library. Since resolve() always calls this function before
119 resolving any symbols it is not necessary to call it explicitly. In
120 some situations you might want the library loaded in advance, in which
121 case you would use this function.
122
123 On Darwin and Mac OS X this function uses code from dlcompat, part of
124 the OpenDarwin project.
125
126 Copyright (c) 2002 Jorge Acereda and Peter O'Gorman
127
128 Permission is hereby granted, free of charge, to any person obtaining a
129 copy of this software and associated documentation files (the"
130 Software"), to deal in the Software without restriction, including
131 without limitation the rights to use, copy, modify, merge, publish,
132 distribute, sublicense, and/or sell copies of the Software, and to
133 permit persons to whom the Software is furnished to do so, subject to
134 the following conditions:
135
136 The above copyright notice and this permission notice shall be included
137 in all copies or substantial portions of the Software.
138
139 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
140 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
141 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
142 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
143 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
144 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
145 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
146
148 Returns the address of the exported symbol symb. The library is loaded
149 if necessary. The function returns 0 if the symbol could not be
150 resolved or the library could not be loaded.
151
152 typedef int (*avgProc)( int, int );
153 avgProc avg = (avgProc) library->resolve( "avg" );
154 if ( avg )
155 return avg( 5, 8 );
156 else
157 return -1;
158
159 The symbol must be exported as a C-function from the library. This
160 requires the extern "C" notation if the library is compiled with a C++
161 compiler. On Windows you also have to explicitly export the function
162 from the DLL using the __declspec(dllexport) compiler directive.
163
164 extern "C" MY_EXPORT_MACRO int avg(int a, int b)
165 {
166 return (a + b) / 2;
167 }
168
169 with MY_EXPORT defined as
170
171 #ifdef Q_WS_WIN
172 # define MY_EXPORT __declspec(dllexport)
173 #else
174 # define MY_EXPORT
175 #endif
176
177 On Darwin and Mac OS X this function uses code from dlcompat, part of
178 the OpenDarwin project.
179
180 Copyright (c) 2002 Jorge Acereda and Peter O'Gorman
181
182 Permission is hereby granted, free of charge, to any person obtaining a
183 copy of this software and associated documentation files (the"
184 Software"), to deal in the Software without restriction, including
185 without limitation the rights to use, copy, modify, merge, publish,
186 distribute, sublicense, and/or sell copies of the Software, and to
187 permit persons to whom the Software is furnished to do so, subject to
188 the following conditions:
189
190 The above copyright notice and this permission notice shall be included
191 in all copies or substantial portions of the Software.
192
193 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
194 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
195 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
196 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
197 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
198 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
199 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
200
202 [static]
203 This is an overloaded member function, provided for convenience. It
204 behaves essentially like the above function.
205
206 Loads the library filename and returns the address of the exported
207 symbol symb. Note that like the constructor, filename does not need to
208 include the (platform specific) file extension. The library remains
209 loaded until the process exits.
210
211 The function returns 0 if the symbol could not be resolved or the
212 library could not be loaded.
213
214 This function is useful only if you want to resolve a single symbol,
215 e.g. a function pointer from a specific library once:
216
217 typedef void (*FunctionType)();
218 static FunctionType *ptrFunction = 0;
219 static bool triedResolve = FALSE;
220 if ( !ptrFunction && !triedResolve )
221 ptrFunction = QLibrary::resolve( "mylib", "mysymb" );
222 if ( ptrFunction )
223 ptrFunction();
224 else
225 ...
226
227 If you want to resolve multiple symbols, use a QLibrary object and call
228 the non-static version of resolve().
229
230 See also
231
233 If enabled is TRUE (the default), the wrapper object is set to
234 automatically unload the library upon destruction. If enabled is FALSE,
235 the wrapper object is not unloaded unless you explicitly call unload().
236
237 See also autoUnload().
238
240 Unloads the library and returns TRUE if the library could be unloaded;
241 otherwise returns FALSE.
242
243 This function is called by the destructor if autoUnload() is enabled.
244
245 See also resolve().
246
247
249 http://doc.trolltech.com/qlibrary.html
250 http://www.trolltech.com/faq/tech.html
251
253 Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the
254 license file included in the distribution for a complete license
255 statement.
256
258 Generated automatically from the source code.
259
261 If you find a bug in Qt, please report it as described in
262 http://doc.trolltech.com/bughowto.html. Good bug reports help us to
263 help you. Thank you.
264
265 The definitive Qt documentation is provided in HTML format; it is
266 located at $QTDIR/doc/html and can be read using Qt Assistant or with a
267 web browser. This man page is provided as a convenience for those users
268 who prefer man pages, although this format is not officially supported
269 by Trolltech.
270
271 If you find errors in this manual page, please report them to qt-
272 bugs@trolltech.com. Please include the name of the manual page
273 (qlibrary.3qt) and the Qt version (3.3.8).
274
275
276
277Trolltech AS 2 February 2007 QLibrary(3qt)