libgmodule
libgmodule
has APIs that provide a portable method
for dynamically loading 'plug-ins' or, in other words, DLLs. Any program that
wants to dynamically load modules must be linked to libgmodule
.
In order to use the libgmodule
APIs, the user must check
whether dynamic loading of DLLs is supported by the platform by using the g_module_supported()
API
which returns TRUE if the dynamic loading of modules is supported. P.I.P.S.
provides the implementation of libdl
, where dynamic loading
of modules is supported, but to write code which runs across platforms the
user must do a check before using the libgmodule
APIs.
If the dynamic loading of DLLs is supported, use g_module_open()
to
open the module. It takes the name of the module as the first argument and
the flags as the second argument.
Note:
G_MODULE_BIND_LOCAL
is the only flag currently supported.
The other flags are not supported. If the user specifies any other flag, the
module is loaded using the flag G_MODULE_BIND_LOCAL
and not
by the user-specified flag.GModule * g_module_open(const gchar *file_name, GModuleFlags
flags);
file_name
: The name of the file containing
the module.
flags
: The flags used for opening the module.
Once the module is opened, the user can find the module symbols (for example,. function names) using the function
g_module_symbol()
.
gboolean g_module_symbol (GModule *module, const gchar *symbol_name,
gpointer *symbol);
*module
: This is a pointer returned when a
module is opened using g_module_open()
.
symbol_name
: This is the ordinal number of
the symbol that one wants to open. There is a difference between Symbian GLib
and OSS GLib; as in Linux the symbol_name
is the name of
the symbol and not a number. The ordinal number must be passed as a string
and not a number.
symbol
: This is the pointer to the symbol value
The GModule
can be closed by using the g_module_close()
API.
This API returns TRUE if it successfully closed the module; else it returns
FALSE.
gboolean g_module_close(GModule *module);
*module
The GModule
to close.
The following example code explains the usage of the libgmodule
APIs.
It opens a module libmoduletestplugin_a.dll
and uses
its gplugin_a_func1()
API which is ordinal number 1. Finally,
the module is closed.
#include <gmodule.h> #include <glib.h> typedef int (*SimpleFunc) (void); int main() { GModule *module = NULL; gpointer func; SimpleFunc f_a; int retVal; if (!g_module_supported()) { g_print ("Dynamic Opening of modules is not supported"); return 1; } /* G_MODULE_BIND_LAZY is overridden and the module is opened with * flag G_MODULE_BIND_LOCAL */ module = g_module_open("libmoduletestplugin_a.dll",G_MODULE_BIND_LAZY); // 1 is the ordinal number for gplugin_a_func1 if(module && g_module_symbol(module, "1" ,&func)) { f_a = (SimpleFunc)func; retVal = f_a(); g_print("Function at ordinal number 1 of module libgmodule_a returns %d",retVal); } else { g_print("Error quering symbol at ordinal number 1"); return 1; } return 0; }
See the Libgmodule APIs section in Differences
between OSS and Symbian GLib for more details about libgmodule
limitations
in the Symbian GLib implementation.