Introduction to Glib |
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, we must check whether dsynamic 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. Open C provides the implementation of libdl, where dynamic loading of modules is supported, but to write code which runs across platforms we must do a check before using the libgmodule APIs.
If the dynamic loading of DLLs is supported, we can open the module using g_module_open(). 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);
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);
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);
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 alsoLibgmodule APIs section for more details about libgmodule limitations in the Symbian Glib implementation.
©Nokia 2007 |