Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

QLibrary Class Reference

The QLibrary class provides a wrapper for handling shared libraries. More...

#include <qlibrary.h>

List of all members.

Public Methods

 QLibrary (const QString &filename)
 Constructor of QLibrary. More...

virtual ~QLibrary ()
 Deletes the QLibrary object. More...

void* resolve (const char *)
 Returns the address of the exported symbol symb. More...

bool load ()
 Loads the library. More...

virtual bool unload ()
 Unloads the library and returns TRUE if the library could be unloaded; otherwise returns FALSE. More...

bool isLoaded () const
 Returns TRUE if the library is loaded; otherwise returns FALSE. More...

bool autoUnload () const
 Returns TRUE if the library will be automatically unloaded when this wrapper object is destructed; otherwise returns FALSE. More...

void setAutoUnload (bool enable)
 If enabled is TRUE (the default), the wrapper object is set to automatically unload the library upon destruction. More...

QString library () const
 Returns the filename of the shared library this QLibrary object handles, including the platform specific file extension. More...

QRESULT queryInterface (const QUuid &request, QUnknownInterface **iface)
 Forwards the query to the component and returns the result. More...


Static Public Methods

void* resolve (const QString &filename, const char *)
 This is an overloaded member function, provided for convenience. More...


Detailed Description

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 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 be 0 and won't be called. When the QLibrary object is destroyed 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();
 }


Constructor & Destructor Documentation

QLibrary::QLibrary ( const QString & filename )
 

Constructor of QLibrary.

Creates a QLibrary object for the shared library filename. The library will be unloaded in the destructor.

See also:
load(), unload(), and setAutoUnload().

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" );

If filename does not include a path, the library loader will look for the file in the platform specific search paths.

QLibrary::~QLibrary ( ) [virtual]
 

Deletes the QLibrary object.

The library will be unloaded if autoUnload() is TRUE (the default), otherwise it stays in memory until the application exits.

See also:
unload() and setAutoUnload().


Member Function Documentation

bool QLibrary::autoUnload ( ) const
 

Returns TRUE if the library will be automatically unloaded when this wrapper object is destructed; otherwise returns FALSE.

The default is TRUE.

bool QLibrary::isLoaded ( ) const
 

Returns TRUE if the library is loaded; otherwise returns FALSE.

See also:
unload().

QString QLibrary::library ( ) const
 

Returns the filename of the shared library this QLibrary object handles, including the platform specific file extension.

 QLibrary lib( "mylib" );
 //set str to    "libmylib.so"
 QString str = lib.library();

bool QLibrary::load ( )
 

Loads the library.

Since resolve() always calls this function before resolving any symbols it is not necessary to call it explicitly. In some situations you might want the library loaded in advance, in which case you would use this function.

QRESULT QLibrary::queryInterface ( const QUuid & request,
QUnknownInterface ** iface )
 

Forwards the query to the component and returns the result.

request and iface are propagated to the component's queryInterface implementation.

The library gets loaded if necessary.

void * QLibrary::resolve ( const QString & filename,
const char * ) [static]
 

This is an overloaded member function, provided for convenience.

It behaves essentially like the above function. Loads the library filename and returns the address of the exported symbol symb.

The function returns 0 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( "mylib", "mysymb" );

 if ( ptrFunction )
                ptrFunction();
 else
          ...

Note:
that like the constructor, filename does not need to include the (platform specific) file extension. The library remains loaded until the process exits.

void * QLibrary::resolve ( const char * )
 

Returns the address of the exported symbol symb.

The library is loaded if necessary. The function returns 0 if the symbol could not be resolved or the library could not be loaded.

 typedef int (*avgProc)( int, int );

 avgProc avg = (avgProc) library->resolve( "avg" );
 if ( avg )
 {
           return avg( 5, 8 );
 }
 else
 {
           return -1;
        }

void QLibrary::setAutoUnload ( bool enable )
 

If enabled is TRUE (the default), the wrapper object is set to automatically unload the library upon destruction.

If enabled is FALSE, the wrapper object is not unloaded unless you explicitly call unload().

bool QLibrary::unload ( ) [virtual]
 

Unloads the library and returns TRUE if the library could be unloaded; otherwise returns FALSE.

This function is called by the destructor if autoUnload() is enabled.


The documentation for this class was generated from the following file:
Generated at Wed Mar 3 13:22:32 2004 by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001