source: trunk/doc/html/qlibrary.html

Last change on this file was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 12.7 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/src/tools/qlibrary.cpp:68 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>QLibrary Class</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>QLibrary Class Reference</h1>
33
34<p>The QLibrary class provides a wrapper for handling shared libraries.
35<a href="#details">More...</a>
36<p>All the functions in this class are <a href="threads.html#reentrant">reentrant</a> when Qt is built with thread support.</p>
37<p><tt>#include &lt;<a href="qlibrary-h.html">qlibrary.h</a>&gt;</tt>
38<p><a href="qlibrary-members.html">List of all member functions.</a>
39<h2>Public Members</h2>
40<ul>
41<li class=fn><a href="#QLibrary"><b>QLibrary</b></a> ( const&nbsp;QString&nbsp;&amp;&nbsp;filename )</li>
42<li class=fn>virtual <a href="#~QLibrary"><b>~QLibrary</b></a> ()</li>
43<li class=fn>void * <a href="#resolve"><b>resolve</b></a> ( const&nbsp;char&nbsp;*&nbsp;symb )</li>
44<li class=fn>bool <a href="#load"><b>load</b></a> ()</li>
45<li class=fn>virtual bool <a href="#unload"><b>unload</b></a> ()</li>
46<li class=fn>bool <a href="#isLoaded"><b>isLoaded</b></a> () const</li>
47<li class=fn>bool <a href="#autoUnload"><b>autoUnload</b></a> () const</li>
48<li class=fn>void <a href="#setAutoUnload"><b>setAutoUnload</b></a> ( bool&nbsp;enabled )</li>
49<li class=fn>QString <a href="#library"><b>library</b></a> () const</li>
50</ul>
51<h2>Static Public Members</h2>
52<ul>
53<li class=fn>void * <a href="#resolve-2"><b>resolve</b></a> ( const&nbsp;QString&nbsp;&amp;&nbsp;filename, const&nbsp;char&nbsp;*&nbsp;symb )</li>
54</ul>
55<hr><a name="details"></a><h2>Detailed Description</h2>
56
57
58
59The QLibrary class provides a wrapper for handling shared libraries.
60<p>
61
62<p> An instance of a QLibrary object can handle a single shared
63library and provide access to the functionality in the library in
64a platform independent way. If the library is a component server,
65QLibrary provides access to the exported component and can
66directly query this component for interfaces.
67<p> QLibrary ensures that the shared library is loaded and stays in
68memory whilst it is in use. QLibrary can also unload the library
69on destruction and release unused resources.
70<p> A typical use of QLibrary is to resolve an exported symbol in a
71shared object, and to call the function that this symbol
72represents. This is called "explicit linking" in contrast to
73"implicit linking", which is done by the link step in the build
74process when linking an executable against a library.
75<p> The following code snippet loads a library, resolves the symbol
76"mysymbol", and calls the function if everything succeeded. If
77something went wrong, e.g. the library file does not exist or the
78symbol is not defined, the function pointer will be 0 and won't be
79called. When the QLibrary object is destroyed the library will be
80unloaded, making all references to memory allocated in the library
81invalid.
82<p> <pre>
83 typedef void (*MyPrototype)();
84 MyPrototype myFunction;
85
86 QLibrary myLib( "mylib" );
87 myFunction = (MyPrototype) myLib.<a href="#resolve">resolve</a>( "mysymbol" );
88 if ( myFunction ) {
89 myFunction();
90 }
91 </pre>
92
93<p>See also <a href="plugins.html">Plugins</a>.
94
95<hr><h2>Member Function Documentation</h2>
96<h3 class=fn><a name="QLibrary"></a>QLibrary::QLibrary ( const&nbsp;<a href="qstring.html">QString</a>&nbsp;&amp;&nbsp;filename )
97</h3>
98Creates a QLibrary object for the shared library <em>filename</em>. The
99library will be unloaded in the destructor.
100<p> Note that <em>filename</em> does not need to include the (platform specific)
101file extension, so calling
102<pre>
103 QLibrary lib( "mylib" );
104 </pre>
105
106is equivalent to calling
107<pre>
108 QLibrary lib( "mylib.dll" );
109 </pre>
110
111on Windows, and
112<pre>
113 QLibrary lib( "libmylib.so" );
114 </pre>
115
116on Unix. Specifying the extension is not recommended, since
117doing so introduces a platform dependency.
118<p> If <em>filename</em> does not include a path, the library loader will
119look for the file in the platform specific search paths.
120<p> <p>See also <a href="#load">load</a>(), <a href="#unload">unload</a>(), and <a href="#setAutoUnload">setAutoUnload</a>().
121
122<h3 class=fn><a name="~QLibrary"></a>QLibrary::~QLibrary ()<tt> [virtual]</tt>
123</h3>
124Deletes the QLibrary object.
125<p> The library will be unloaded if <a href="#autoUnload">autoUnload</a>() is TRUE (the
126default), otherwise it stays in memory until the application
127exits.
128<p> <p>See also <a href="#unload">unload</a>() and <a href="#setAutoUnload">setAutoUnload</a>().
129
130<h3 class=fn>bool <a name="autoUnload"></a>QLibrary::autoUnload () const
131</h3>
132Returns TRUE if the library will be automatically unloaded when
133this wrapper object is destructed; otherwise returns FALSE. The
134default is TRUE.
135<p> <p>See also <a href="#setAutoUnload">setAutoUnload</a>().
136
137<h3 class=fn>bool <a name="isLoaded"></a>QLibrary::isLoaded () const
138</h3>
139Returns TRUE if the library is loaded; otherwise returns FALSE.
140<p> <p>See also <a href="#unload">unload</a>().
141
142<h3 class=fn><a href="qstring.html">QString</a> <a name="library"></a>QLibrary::library () const
143</h3>
144Returns the filename of the shared library this QLibrary object
145handles, including the platform specific file extension.
146<p> For example:
147<pre>
148 QLibrary lib( "mylib" );
149 <a href="qstring.html">QString</a> str = lib.<a href="#library">library</a>();
150 </pre>
151
152will set <em>str</em> to "mylib.dll" on Windows, and "libmylib.so" on Linux.
153
154<h3 class=fn>bool <a name="load"></a>QLibrary::load ()
155</h3>
156Loads the library. Since <a href="#resolve">resolve</a>() always calls this function
157before resolving any symbols it is not necessary to call it
158explicitly. In some situations you might want the library loaded
159in advance, in which case you would use this function.
160<p> On Darwin and Mac OS X this function uses code from dlcompat, part of the
161OpenDarwin project.
162<p>
163<p> Copyright (c) 2002 Jorge Acereda and Peter O'Gorman
164<p> Permission is hereby granted, free of charge, to any person obtaining
165a copy of this software and associated documentation files (the
166"Software"), to deal in the Software without restriction, including
167without limitation the rights to use, copy, modify, merge, publish,
168distribute, sublicense, and/or sell copies of the Software, and to
169permit persons to whom the Software is furnished to do so, subject to
170the following conditions:
171<p> The above copyright notice and this permission notice shall be
172included in all copies or substantial portions of the Software.
173<p> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
174EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
175MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
176NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
177LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
178OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
179WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
180
181<h3 class=fn>void * <a name="resolve"></a>QLibrary::resolve ( const&nbsp;char&nbsp;*&nbsp;symb )
182</h3>
183Returns the address of the exported symbol <em>symb</em>. The library is
184loaded if necessary. The function returns 0 if the symbol could
185not be resolved or the library could not be loaded.
186<p> <pre>
187 typedef int (*avgProc)( int, int );
188
189 avgProc avg = (avgProc) library-&gt;resolve( "avg" );
190 if ( avg )
191 return avg( 5, 8 );
192 else
193 return -1;
194 </pre>
195
196<p> The symbol must be exported as a C-function from the library. This
197requires the <tt>extern "C"</tt> notation if the library is compiled
198with a C++ compiler. On Windows you also have to explicitly export
199the function from the DLL using the <tt>__declspec(dllexport)</tt>
200compiler directive.
201<p> <pre>
202 extern "C" MY_EXPORT_MACRO int avg(int a, int b)
203 {
204 return (a + b) / 2;
205 }
206 </pre>
207
208<p> with <tt>MY_EXPORT</tt> defined as
209<p> <pre>
210 #ifdef Q_WS_WIN
211 # define MY_EXPORT __declspec(dllexport)
212 #else
213 # define MY_EXPORT
214 #endif
215 </pre>
216
217<p> On Darwin and Mac OS X this function uses code from dlcompat, part of the
218OpenDarwin project.
219<p>
220<p> Copyright (c) 2002 Jorge Acereda and Peter O'Gorman
221<p> Permission is hereby granted, free of charge, to any person obtaining
222a copy of this software and associated documentation files (the
223"Software"), to deal in the Software without restriction, including
224without limitation the rights to use, copy, modify, merge, publish,
225distribute, sublicense, and/or sell copies of the Software, and to
226permit persons to whom the Software is furnished to do so, subject to
227the following conditions:
228<p> The above copyright notice and this permission notice shall be
229included in all copies or substantial portions of the Software.
230<p> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
231EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
232MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
233NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
234LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
235OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
236WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
237
238<h3 class=fn>void * <a name="resolve-2"></a>QLibrary::resolve ( const&nbsp;<a href="qstring.html">QString</a>&nbsp;&amp;&nbsp;filename, const&nbsp;char&nbsp;*&nbsp;symb )<tt> [static]</tt>
239</h3>
240This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
241<p> Loads the library <em>filename</em> and returns the address of the
242exported symbol <em>symb</em>. Note that like the constructor, <em>filename</em> does not need to include the (platform specific) file
243extension. The library remains loaded until the process exits.
244<p> The function returns 0 if the symbol could not be resolved or the
245library could not be loaded.
246<p> This function is useful only if you want to resolve a single
247symbol, e.g. a function pointer from a specific library once:
248<p> <pre>
249 typedef void (*FunctionType)();
250 static FunctionType *ptrFunction = 0;
251 static bool triedResolve = FALSE;
252 if ( !ptrFunction &amp;&amp; !triedResolve )
253 ptrFunction = QLibrary::<a href="#resolve">resolve</a>( "mylib", "mysymb" );
254
255 if ( ptrFunction )
256 ptrFunction();
257 else
258 ...
259 </pre>
260
261<p> If you want to resolve multiple symbols, use a QLibrary object and
262call the non-static version of <a href="#resolve">resolve</a>().
263<p> <p>See also
264<h3 class=fn>void <a name="setAutoUnload"></a>QLibrary::setAutoUnload ( bool&nbsp;enabled )
265</h3>
266If <em>enabled</em> is TRUE (the default), the wrapper object is set to
267automatically unload the library upon destruction. If <em>enabled</em>
268is FALSE, the wrapper object is not unloaded unless you explicitly
269call <a href="#unload">unload</a>().
270<p> <p>See also <a href="#autoUnload">autoUnload</a>().
271
272<h3 class=fn>bool <a name="unload"></a>QLibrary::unload ()<tt> [virtual]</tt>
273</h3>
274Unloads the library and returns TRUE if the library could be
275unloaded; otherwise returns FALSE.
276<p> This function is called by the destructor if <a href="#autoUnload">autoUnload</a>() is
277enabled.
278<p> <p>See also <a href="#resolve">resolve</a>().
279
280<!-- eof -->
281<hr><p>
282This file is part of the <a href="index.html">Qt toolkit</a>.
283Copyright &copy; 1995-2007
284<a href="http://www.trolltech.com/">Trolltech</a>. All Rights Reserved.<p><address><hr><div align=center>
285<table width=100% cellspacing=0 border=0><tr>
286<td>Copyright &copy; 2007
287<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
288<td align=right><div align=right>Qt 3.3.8</div>
289</table></div></address></body>
290</html>
Note: See TracBrowser for help on using the repository browser.