S60 Open C
Introduction to Glib

Introduction to Glib

Table of Contents

An Introduction to libgmodule

 


An Introduction to 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, 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); 
  • 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 alsoLibgmodule APIs section for more details about libgmodule limitations in the Symbian Glib implementation.

Give feedback of this section


©Nokia 2007

Back to top


This material, including documentation and any related computer programs, is protected by copyright controlled by Nokia. All rights are reserved. Copying, including reproducing, storing, adapting or translating, any or all of this material requires the prior written consent of Nokia. This material also contains confidential information, which may not be disclosed to others without the prior written consent of Nokia.

Nokia is a registered trademark of Nokia Corporation. S60 and logo is a trademark of Nokia Corporation. Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. Other company and product names mentioned herein may be trademarks or tradenames of their respective owners.