Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |
The QLibrary class provides a wrapper for handling shared libraries. More...
#include <qlibrary.h>
The QLibrary class provides a wrapper for handling shared libraries.
An instance of a QLibrary object can handle a single shared library and provide access to the functionality in the library in a platform independent way. If the library is a component server, QLibrary provides access to the exported component and can directly query this component for interfaces.
QLibrary ensures that the shared library is loaded and stays in memory whilst it is in use. QLibrary can also unload the library on destruction and release unused resources.
A typical use of QLibrary is to resolve an exported symbol in a shared object, and to e.g. call the function that this symbol represents. This is called "explicit linking" in contrast to "implicit linking", which is done by the link step in the build process when linking an executable against a library.
The following code snippet loads a library, resolves the symbol "mysymbol", and calls the function if everything succeeded. If something went wrong, e.g. the library file does not exist or the symbol is not defined, the function pointer will become a null pointer. Upon destruction of the QLibrary object the library will be unloaded, making all references to memory allocated in the library invalid.
typedef void (*MyPrototype)(); MyPrototype myFunction; QLibrary myLib( "mylib" ); myFunction = (MyProtoype) myLib.resolve( "mysymbol" ); if ( myFunction ) { myFunction(); }
Note that filename does not need to include the (platform specific) file extension, so calling
QLibrary lib( "mylib" );is equivalent to calling
QLibrary lib( "mylib.dll" );on Windows. Specifying the extension is not recommended, since doing so introduces a platform dependency.
If filename does not include a path, the library loader will look for the file in the platform specific search paths.
See also load(), unload() and setAutoUnload().
The library will be unloaded if autoUnload() is TRUE (the default), otherwise it stays in memory until the application is exited.
See also unload() and setAutoUnload().
See also setAutoUnload().
See also unload().
For example:
QLibrary lib( "mylib" ); QString str = lib.library();
will set str to "mylib.dll" on Windows, and "libmylib.so" on Linux.
typedef int (*avgProc)( int, int ); avgProc avg = (avgProc) library->resolve( "avg" ); if ( avg ) return avg( 5, 8 ); else return -1;
Loads the library filename and returns the address of the exported symbol symb. Note that like the constructor, filename does not need to include the (platform specific) file extension. The library remains loaded until the process exits.
The function returns a null pointer if the symbol could not be resolved or the library could not be loaded.
This function is useful only if you want to resolve a single symbol, e.g. a function pointer from a specific library once:
typedef void (*FunctionType)(); static FunctionType *ptrFunction = 0; static bool triedResolve = FALSE; if ( !ptrFunction && !triedResolve ) ptrFunction = QLibrary::resolve( "foo", "function" ); if ( ptrFunction ) ptrFunction(); else ...
If you want to resolve multiple symbols, use a QLibrary object and call the non-static version of resolve().
See also
See also autoUnload().
This function is called by the destructor if autoUnload() is enabled.
See also resolve().
This file is part of the Qt toolkit. Copyright © 1995-2002 Trolltech. All Rights Reserved.
Copyright © 2002 Trolltech | Trademarks | Qt version 3.0.5
|