<refentry id="glib-Dynamic-Loading-of-Modules">
<refmeta>
<refentrytitle role="top_of_page" id="glib-Dynamic-Loading-of-Modules.top_of_page">Dynamic Loading of Modules</refentrytitle>
<manvolnum>3</manvolnum>
<refmiscinfo>GLIB Library</refmiscinfo>
</refmeta>

<refnamediv>
<refname>Dynamic Loading of Modules</refname>
<refpurpose>portable method for dynamically loading 'plug-ins'</refpurpose>
</refnamediv>

<refsynopsisdiv id="glib-Dynamic-Loading-of-Modules.synopsis" role="synopsis">
<title role="synopsis.title">Synopsis</title>

<synopsis>

#include &lt;gmodule.h&gt;

                    <link linkend="GModule">GModule</link>;
<link linkend="gboolean">gboolean</link>            <link linkend="g-module-supported">g_module_supported</link>                  (void);
<link linkend="gchar">gchar</link>*              <link linkend="g-module-build-path">g_module_build_path</link>                 (const <link linkend="gchar">gchar</link> *directory,
                                                         const <link linkend="gchar">gchar</link> *module_name);
<link linkend="GModule">GModule</link>*            <link linkend="g-module-open">g_module_open</link>                       (const <link linkend="gchar">gchar</link> *file_name,
                                                         <link linkend="GModuleFlags">GModuleFlags</link> flags);
enum                <link linkend="GModuleFlags">GModuleFlags</link>;
<link linkend="gboolean">gboolean</link>            <link linkend="g-module-symbol">g_module_symbol</link>                     (<link linkend="GModule">GModule</link> *module,
                                                         const <link linkend="gchar">gchar</link> *symbol_name,
                                                         <link linkend="gpointer">gpointer</link> *symbol);
const <link linkend="gchar">gchar</link>*        <link linkend="g-module-name">g_module_name</link>                       (<link linkend="GModule">GModule</link> *module);
<link linkend="void">void</link>                <link linkend="g-module-make-resident">g_module_make_resident</link>              (<link linkend="GModule">GModule</link> *module);
<link linkend="gboolean">gboolean</link>            <link linkend="g-module-close">g_module_close</link>                      (<link linkend="GModule">GModule</link> *module);
const <link linkend="gchar">gchar</link>*        <link linkend="g-module-error">g_module_error</link>                      (void);

const <link linkend="gchar">gchar</link> *       (<link linkend="GModuleCheckInit">*GModuleCheckInit</link>)                 (<link linkend="GModule">GModule</link> *module);
<link linkend="void">void</link>                (<link linkend="GModuleUnload">*GModuleUnload</link>)                    (<link linkend="GModule">GModule</link> *module);
#define             <link linkend="G-MODULE-SUFFIX--CAPS">G_MODULE_SUFFIX</link>
#define             <link linkend="G-MODULE-EXPORT--CAPS">G_MODULE_EXPORT</link>
#define             <link linkend="G-MODULE-IMPORT--CAPS">G_MODULE_IMPORT</link>
</synopsis>
</refsynopsisdiv>









<refsect1 id="glib-Dynamic-Loading-of-Modules.description" role="desc">
<title role="desc.title">Description</title>
<para>
These functions provide a portable way to dynamically load object files
(commonly known as 'plug-ins').
The current implementation supports all systems that provide
an implementation of <link linkend="dlopen"><function>dlopen()</function></link> (e.g. Linux/Sun), as well as HP-UX via its
<link linkend="shl-load"><function>shl_load()</function></link> mechanism, and Windows platforms via DLLs.
</para>
<para>
A program which wants to use these functions must be linked to the
libraries output by the command <command>pkg-config --libs gmodule-2.0</command>.
</para>
<para>
To use them you must first determine whether dynamic loading
is supported on the platform by calling <link linkend="g-module-supported"><function>g_module_supported()</function></link>.
If it is, you can open a module with <link linkend="g-module-open"><function>g_module_open()</function></link>,
find the module's symbols (e.g. function names) with <link linkend="g-module-symbol"><function>g_module_symbol()</function></link>,
and later close the module with <link linkend="g-module-close"><function>g_module_close()</function></link>.
<link linkend="g-module-name"><function>g_module_name()</function></link> will return the file name of a currently opened module.
</para>
<para>
If any of the above functions fail, the error status can be found with
<link linkend="g-module-error"><function>g_module_error()</function></link>.
</para>
<para>
The <link linkend="GModule"><type>GModule</type></link> implementation features reference counting for opened modules,
and supports hook functions within a module which are called when the
module is loaded and unloaded (see <link linkend="GModuleCheckInit"><type>GModuleCheckInit</type></link> and <link linkend="GModuleUnload"><type>GModuleUnload</type></link>).
</para>
<para>
If your module introduces static data to common subsystems in the running
program, e.g. through calling <literal>g_quark_from_static_string ("my-module-stuff")</literal>,
it must ensure that it is never unloaded, by calling <link linkend="g-module-make-resident"><function>g_module_make_resident()</function></link>.
</para>
<para>
<example>
<title>Calling a function defined in a <structname>GModule</structname></title>
<programlisting>
/* the function signature for 'say_hello' */
typedef void (* SayHelloFunc) (const char *message);
gboolean
just_say_hello (const char *filename, GError **error)
{
  SayHelloFunc  say_hello;
  GModule      *module;
  module = g_module_open (filename, G_MODULE_BIND_LAZY);
  if (!module)
    {
      g_set_error (error, FOO_ERROR, FOO_ERROR_BLAH,
		   "&percnt;s", g_module_error (<!-- -->));
      return FALSE;
    }
  if (!g_module_symbol (module, "say_hello", (gpointer *)&amp;say_hello))
    {
      g_set_error (error, SAY_ERROR, SAY_ERROR_OPEN,
		   "&percnt;s: &percnt;s", filename, g_module_error (<!-- -->));
      if (!g_module_close (module))
	g_warning ("&percnt;s: &percnt;s", filename, g_module_error (<!-- -->));
      return FALSE;
    }
  if (say_hello == NULL)
    {
      g_set_error (error, SAY_ERROR, SAY_ERROR_OPEN, "symbol say_hello is NULL");
      if (!g_module_close (module))
	g_warning ("&percnt;s: &percnt;s", filename, g_module_error (<!-- -->));
      return FALSE;
    }
  /* call our function in the module */
  say_hello ("Hello world!");
  if (!g_module_close (module))
    g_warning ("&percnt;s: &percnt;s", filename, g_module_error (<!-- -->));
  return TRUE;
}
</programlisting>
</example>
</para>
</refsect1>

<refsect1 id="glib-Dynamic-Loading-of-Modules.details" role="details">
<title role="details.title">Details</title>
<refsect2 id="GModule" role="struct">
<title>GModule</title>
<indexterm zone="GModule"><primary sortas="Module">GModule</primary></indexterm><programlisting>typedef struct _GModule GModule;</programlisting>
<para>
The <link linkend="GModule"><type>GModule</type></link> struct is an opaque data structure to represent a
<link linkend="glib-Dynamic-Loading-of-Modules">Dynamically-Loaded Module</link>.
It should only be accessed via the following functions.
</para></refsect2>
<refsect2 id="g-module-supported" role="function">
<title>g_module_supported ()</title>
<indexterm zone="g-module-supported"><primary sortas="module_supported">g_module_supported</primary></indexterm><programlisting><link linkend="gboolean">gboolean</link>            g_module_supported                  (void);</programlisting>
<para>
Checks if modules are supported on the current platform.
</para><variablelist role="params">
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara>%TRUE if modules are supported.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="g-module-build-path" role="function">
<title>g_module_build_path ()</title>
<indexterm zone="g-module-build-path"><primary sortas="module_build_path">g_module_build_path</primary></indexterm><programlisting><link linkend="gchar">gchar</link>*              g_module_build_path                 (const <link linkend="gchar">gchar</link> *directory,
                                                         const <link linkend="gchar">gchar</link> *module_name);</programlisting>
<para>
A portable way to build the filename of a module. The platform-specific
prefix and suffix are added to the filename, if needed, and the result is
added to the directory, using the correct separator character.
</para>
<para>
The directory should specify the directory where the module can be found.
It can be <link linkend="NULL--CAPS"><literal>NULL</literal></link> or an empty string to indicate that the module is in a standard
platform-specific directory, though this is not recommended since the
wrong module may be found.
</para>
<para>
For example, calling <link linkend="g-module-build-path"><function>g_module_build_path()</function></link> on a Linux system with a <parameter>directory</parameter>
of <filename>/lib</filename> and a <parameter>module_name</parameter> of "mylibrary" will return
<filename>/lib/libmylibrary.so</filename>. On a Windows system, using
<filename>\Windows</filename> as the directory it will return
<filename>\Windows\mylibrary.dll</filename>.
</para><variablelist role="params">
<varlistentry><term><parameter>directory</parameter>&#160;:</term>
<listitem><simpara>the directory where the module is. This can be <link linkend="NULL--CAPS"><literal>NULL</literal></link> or the empty
string to indicate that the standard platform-specific directories will be
used, though that is not recommended.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>module_name</parameter>&#160;:</term>
<listitem><simpara>the name of the module.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara>the complete path of the module, including the standard library
prefix and suffix. This should be freed when no longer needed.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="g-module-open" role="function">
<title>g_module_open ()</title>
<indexterm zone="g-module-open"><primary sortas="module_open">g_module_open</primary></indexterm><programlisting><link linkend="GModule">GModule</link>*            g_module_open                       (const <link linkend="gchar">gchar</link> *file_name,
                                                         <link linkend="GModuleFlags">GModuleFlags</link> flags);</programlisting>
<para>
Opens a module. If the module has already been opened, its reference
count is incremented.
</para>
<para>
First of all <link linkend="g-module-open"><function>g_module_open()</function></link> tries to open <parameter>file_name</parameter> as a module. If
that fails and <parameter>file_name</parameter> has the ".la"-suffix (and is a libtool archive)
it tries to open the corresponding module. If that fails and it doesn't
have the proper module suffix for the platform (<link linkend="G-MODULE-SUFFIX--CAPS"><type>G_MODULE_SUFFIX</type></link>), this
suffix will be appended and the corresponding module will be opended. If
that fails and <parameter>file_name</parameter> doesn't have the ".la"-suffix, this suffix is
appended and <link linkend="g-module-open"><function>g_module_open()</function></link> tries to open the corresponding module. If
eventually that fails as well, <link linkend="NULL--CAPS"><literal>NULL</literal></link> is returned.
</para><variablelist role="params">
<varlistentry><term><parameter>file_name</parameter>&#160;:</term>
<listitem><simpara>the name of the file containing the module, or <link linkend="NULL--CAPS"><literal>NULL</literal></link> to obtain
  a <link linkend="GModule"><type>GModule</type></link> representing the main program itself.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>flags</parameter>&#160;:</term>
<listitem><simpara>the flags used for opening the module. This can be the logical
OR of any of the <link linkend="GModuleFlags"><type>GModuleFlags</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara>a <link linkend="GModule"><type>GModule</type></link> on success, or <link linkend="NULL--CAPS"><literal>NULL</literal></link> on failure.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="GModuleFlags" role="enum">
<title>enum GModuleFlags</title>
<indexterm zone="GModuleFlags"><primary sortas="ModuleFlags">GModuleFlags</primary></indexterm><programlisting>typedef enum
{
  G_MODULE_BIND_LAZY	= 1 &lt;&lt; 0,
  G_MODULE_BIND_LOCAL	= 1 &lt;&lt; 1,
  G_MODULE_BIND_MASK	= 0x03
} GModuleFlags;
</programlisting>
<para>
Flags passed to <link linkend="g-module-open"><function>g_module_open()</function></link>. Note that these flags are
not supported on all platforms.
</para><variablelist role="enum">
<varlistentry id="G-MODULE-BIND-LAZY--CAPS" role="constant">
<term><literal>G_MODULE_BIND_LAZY</literal></term>
<listitem><simpara>specifies that symbols are only resolved when needed.
  The default action is to bind all symbols when the module is loaded.
</simpara></listitem>
</varlistentry>
<varlistentry id="G-MODULE-BIND-LOCAL--CAPS" role="constant">
<term><literal>G_MODULE_BIND_LOCAL</literal></term>
<listitem><simpara>specifies that symbols in the module should
  not be added to the global name space.  The default action on most
  platforms is to place symbols in the module in the global name space,
  which may cause conflicts with existing symbols.
</simpara></listitem>
</varlistentry>
<varlistentry id="G-MODULE-BIND-MASK--CAPS" role="constant">
<term><literal>G_MODULE_BIND_MASK</literal></term>
<listitem><simpara>mask for all flags.
</simpara></listitem>
</varlistentry>
</variablelist></refsect2>
<refsect2 id="g-module-symbol" role="function">
<title>g_module_symbol ()</title>
<indexterm zone="g-module-symbol"><primary sortas="module_symbol">g_module_symbol</primary></indexterm><programlisting><link linkend="gboolean">gboolean</link>            g_module_symbol                     (<link linkend="GModule">GModule</link> *module,
                                                         const <link linkend="gchar">gchar</link> *symbol_name,
                                                         <link linkend="gpointer">gpointer</link> *symbol);</programlisting>
<para>
Gets a symbol pointer from a module, such as one exported by <link linkend="G-MODULE-EXPORT--CAPS"><type>G_MODULE_EXPORT</type></link>.
</para>
<para>
Note that a valid symbol can be <link linkend="NULL--CAPS"><literal>NULL</literal></link>.
</para><variablelist role="params">
<varlistentry><term><parameter>module</parameter>&#160;:</term>
<listitem><simpara>a <link linkend="GModule"><type>GModule</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>symbol_name</parameter>&#160;:</term>
<listitem><simpara>the name of the symbol to find.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>symbol</parameter>&#160;:</term>
<listitem><simpara>returns the pointer to the symbol value.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara>%TRUE on success.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="g-module-name" role="function">
<title>g_module_name ()</title>
<indexterm zone="g-module-name"><primary sortas="module_name">g_module_name</primary></indexterm><programlisting>const <link linkend="gchar">gchar</link>*        g_module_name                       (<link linkend="GModule">GModule</link> *module);</programlisting>
<para>
Gets the filename from a <link linkend="GModule"><type>GModule</type></link>.
</para><variablelist role="params">
<varlistentry><term><parameter>module</parameter>&#160;:</term>
<listitem><simpara>a <link linkend="GModule"><type>GModule</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara>the filename of the module, or "main" if the module is the main
program itself.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="g-module-make-resident" role="function">
<title>g_module_make_resident ()</title>
<indexterm zone="g-module-make-resident"><primary sortas="module_make_resident">g_module_make_resident</primary></indexterm><programlisting><link linkend="void">void</link>                g_module_make_resident              (<link linkend="GModule">GModule</link> *module);</programlisting>
<para>
Ensures that a module will never be unloaded.
Any future <link linkend="g-module-close"><function>g_module_close()</function></link> calls on the module will be ignored.
</para><variablelist role="params">
<varlistentry><term><parameter>module</parameter>&#160;:</term>
<listitem><simpara>a <link linkend="GModule"><type>GModule</type></link> to make permanently resident.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="g-module-close" role="function">
<title>g_module_close ()</title>
<indexterm zone="g-module-close"><primary sortas="module_close">g_module_close</primary></indexterm><programlisting><link linkend="gboolean">gboolean</link>            g_module_close                      (<link linkend="GModule">GModule</link> *module);</programlisting>
<para>
Closes a module.
</para><variablelist role="params">
<varlistentry><term><parameter>module</parameter>&#160;:</term>
<listitem><simpara>a <link linkend="GModule"><type>GModule</type></link> to close.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara>%TRUE on success.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="g-module-error" role="function">
<title>g_module_error ()</title>
<indexterm zone="g-module-error"><primary sortas="module_error">g_module_error</primary></indexterm><programlisting>const <link linkend="gchar">gchar</link>*        g_module_error                      (void);</programlisting>
<para>
Gets a string describing the last module error.
</para><variablelist role="params">
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara>a string describing the last module error.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="GModuleCheckInit" role="function">
<title>GModuleCheckInit ()</title>
<indexterm zone="GModuleCheckInit"><primary sortas="ModuleCheckInit">GModuleCheckInit</primary></indexterm><programlisting>const <link linkend="gchar">gchar</link> *       (*GModuleCheckInit)                 (<link linkend="GModule">GModule</link> *module);</programlisting>
<para>
Specifies the type of the module initialization function.
<indexterm zone="g-module-check-init"><primary>g_module_check_init</primary></indexterm>
If a module contains a function named <link linkend="g-module-check-init"><function>g_module_check_init()</function></link> it is called
automatically when the module is loaded. It is passed the <link linkend="GModule"><type>GModule</type></link> structure
and should return <link linkend="NULL--CAPS"><literal>NULL</literal></link> on success or a string describing the initialization
error.
</para><variablelist role="params">
<varlistentry><term><parameter>module</parameter>&#160;:</term>
<listitem><simpara>the <link linkend="GModule"><type>GModule</type></link> corresponding to the module which has just been loaded.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara>%NULL on success, or a string describing the initialization error.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="GModuleUnload" role="function">
<title>GModuleUnload ()</title>
<indexterm zone="GModuleUnload"><primary sortas="ModuleUnload">GModuleUnload</primary></indexterm><programlisting><link linkend="void">void</link>                (*GModuleUnload)                    (<link linkend="GModule">GModule</link> *module);</programlisting>
<para>
<indexterm zone="g-module-unload"><primary>g_module_unload</primary></indexterm>
Specifies the type of the module function called when it is unloaded.
If a module contains a function named <link linkend="g-module-unload"><function>g_module_unload()</function></link> it is called
automatically when the module is unloaded.
It is passed the <link linkend="GModule"><type>GModule</type></link> structure.
</para><variablelist role="params">
<varlistentry><term><parameter>module</parameter>&#160;:</term>
<listitem><simpara>the <link linkend="GModule"><type>GModule</type></link> about to be unloaded.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="G-MODULE-SUFFIX--CAPS" role="macro">
<title>G_MODULE_SUFFIX</title>
<indexterm zone="G-MODULE-SUFFIX--CAPS"><primary sortas="MODULE_SUFFIX">G_MODULE_SUFFIX</primary></indexterm><programlisting>#define G_MODULE_SUFFIX "so"
</programlisting>
<para>
Expands to the proper shared library suffix for the current platform
without the leading dot. For the most Unices and Linux this is "so",
for some HP-UX versions this is "sl" and for Windows this is "dll".
</para></refsect2>
<refsect2 id="G-MODULE-EXPORT--CAPS" role="macro">
<title>G_MODULE_EXPORT</title>
<indexterm zone="G-MODULE-EXPORT--CAPS"><primary sortas="MODULE_EXPORT">G_MODULE_EXPORT</primary></indexterm><programlisting>#define             G_MODULE_EXPORT</programlisting>
<para>
Used to declare functions exported by modules. This is a no-op on Linux and
Unices, but when compiling for Windows, it marks a symbol to be exported from
the library or executable being built.
</para></refsect2>
<refsect2 id="G-MODULE-IMPORT--CAPS" role="macro">
<title>G_MODULE_IMPORT</title>
<indexterm zone="G-MODULE-IMPORT--CAPS"><primary sortas="MODULE_IMPORT">G_MODULE_IMPORT</primary></indexterm><programlisting>#define	G_MODULE_IMPORT		extern
</programlisting>
<para>
Used to declare functions imported from modules.
</para></refsect2>

</refsect1>




</refentry>
