1<!-- ##### SECTION Title ##### --> 2Dynamic Loading of Modules 3 4<!-- ##### SECTION Short_Description ##### --> 5portable method for dynamically loading 'plug-ins' 6 7<!-- ##### SECTION Long_Description ##### --> 8<para> 9These functions provide a portable way to dynamically load object files 10(commonly known as 'plug-ins'). 11The current implementation supports all systems that provide 12an implementation of dlopen() (e.g. Linux/Sun), as well as HP-UX via its 13shl_load() mechanism, and Windows platforms via DLLs. 14</para> 15 16<para> 17A program which wants to use these functions must be linked to the 18libraries output by the command <command>pkg-config --libs gmodule-2.0</command>. 19</para> 20 21<para> 22To use them you must first determine whether dynamic loading 23is supported on the platform by calling g_module_supported(). 24If it is, you can open a module with g_module_open(), 25find the module's symbols (e.g. function names) with g_module_symbol(), 26and later close the module with g_module_close(). 27g_module_name() will return the file name of a currently opened module. 28</para> 29<para> 30If any of the above functions fail, the error status can be found with 31g_module_error(). 32</para> 33<para> 34The #GModule implementation features reference counting for opened modules, 35and supports hook functions within a module which are called when the 36module is loaded and unloaded (see #GModuleCheckInit and #GModuleUnload). 37</para> 38<para> 39If your module introduces static data to common subsystems in the running 40program, e.g. through calling <literal>g_quark_from_static_string ("my-module-stuff")</literal>, 41it must ensure that it is never unloaded, by calling g_module_make_resident(). 42</para> 43 44<para> 45<example> 46<title>Calling a function defined in a <structname>GModule</structname></title> 47<programlisting> 48/* the function signature for 'say_hello' */ 49typedef void (* SayHelloFunc) (const char *message); 50 51gboolean 52just_say_hello (const char *filename, GError **error) 53{ 54 SayHelloFunc say_hello; 55 GModule *module; 56 57 module = g_module_open (filename, G_MODULE_BIND_LAZY); 58 if (!module) 59 { 60 g_set_error (error, FOO_ERROR, FOO_ERROR_BLAH, 61 "%s", g_module_error (<!-- -->)); 62 return FALSE; 63 } 64 65 if (!g_module_symbol (module, "say_hello", (gpointer *)&say_hello)) 66 { 67 g_set_error (error, SAY_ERROR, SAY_ERROR_OPEN, 68 "%s: %s", filename, g_module_error (<!-- -->)); 69 if (!g_module_close (module)) 70 g_warning ("%s: %s", filename, g_module_error (<!-- -->)); 71 return FALSE; 72 } 73 74 if (say_hello == NULL) 75 { 76 g_set_error (error, SAY_ERROR, SAY_ERROR_OPEN, "symbol say_hello is NULL"); 77 if (!g_module_close (module)) 78 g_warning ("%s: %s", filename, g_module_error (<!-- -->)); 79 return FALSE; 80 } 81 82 /* call our function in the module */ 83 say_hello ("Hello world!"); 84 85 if (!g_module_close (module)) 86 g_warning ("%s: %s", filename, g_module_error (<!-- -->)); 87 88 return TRUE; 89} 90</programlisting> 91</example> 92</para> 93 94<!-- ##### SECTION See_Also ##### --> 95<para> 96 97</para> 98 99<!-- ##### SECTION Stability_Level ##### --> 100 101 102<!-- ##### STRUCT GModule ##### --> 103<para> 104The #GModule struct is an opaque data structure to represent a 105<link linkend="glib-Dynamic-Loading-of-Modules">Dynamically-Loaded Module</link>. 106It should only be accessed via the following functions. 107</para> 108 109 110<!-- ##### FUNCTION g_module_supported ##### --> 111<para> 112Checks if modules are supported on the current platform. 113</para> 114 115@Returns: %TRUE if modules are supported. 116 117 118<!-- ##### FUNCTION g_module_build_path ##### --> 119<para> 120A portable way to build the filename of a module. The platform-specific 121prefix and suffix are added to the filename, if needed, and the result is 122added to the directory, using the correct separator character. 123</para> 124<para> 125The directory should specify the directory where the module can be found. 126It can be %NULL or an empty string to indicate that the module is in a standard 127platform-specific directory, though this is not recommended since the 128wrong module may be found. 129</para> 130<para> 131For example, calling g_module_build_path() on a Linux system with a @directory 132of <filename>/lib</filename> and a @module_name of "mylibrary" will return 133<filename>/lib/libmylibrary.so</filename>. On a Windows system, using 134<filename>\Windows</filename> as the directory it will return 135<filename>\Windows\mylibrary.dll</filename>. 136</para> 137 138@directory: the directory where the module is. This can be %NULL or the empty 139string to indicate that the standard platform-specific directories will be 140used, though that is not recommended. 141@module_name: the name of the module. 142@Returns: the complete path of the module, including the standard library 143prefix and suffix. This should be freed when no longer needed. 144 145 146<!-- ##### FUNCTION g_module_open ##### --> 147<para> 148Opens a module. If the module has already been opened, its reference 149count is incremented. 150</para> 151 152<para> 153First of all g_module_open() tries to open @file_name as a module. If 154that fails and @file_name has the ".la"-suffix (and is a libtool archive) 155it tries to open the corresponding module. If that fails and it doesn't 156have the proper module suffix for the platform (#G_MODULE_SUFFIX), this 157suffix will be appended and the corresponding module will be opended. If 158that fails and @file_name doesn't have the ".la"-suffix, this suffix is 159appended and g_module_open() tries to open the corresponding module. If 160eventually that fails as well, %NULL is returned. 161</para> 162 163@file_name: the name of the file containing the module, or %NULL to obtain 164 a #GModule representing the main program itself. 165@flags: the flags used for opening the module. This can be the logical 166OR of any of the #GModuleFlags. 167@Returns: a #GModule on success, or %NULL on failure. 168 169 170<!-- ##### ENUM GModuleFlags ##### --> 171<para> 172Flags passed to g_module_open(). Note that these flags are 173not supported on all platforms. 174</para> 175 176@G_MODULE_BIND_LAZY: specifies that symbols are only resolved when needed. 177 The default action is to bind all symbols when the module is loaded. 178@G_MODULE_BIND_LOCAL: specifies that symbols in the module should 179 not be added to the global name space. The default action on most 180 platforms is to place symbols in the module in the global name space, 181 which may cause conflicts with existing symbols. 182@G_MODULE_BIND_MASK: mask for all flags. 183 184<!-- ##### FUNCTION g_module_symbol ##### --> 185<para> 186Gets a symbol pointer from a module, such as one exported by #G_MODULE_EXPORT. 187</para> 188<para> 189Note that a valid symbol can be %NULL. 190</para> 191 192@module: a #GModule. 193@symbol_name: the name of the symbol to find. 194@symbol: returns the pointer to the symbol value. 195@Returns: %TRUE on success. 196 197 198<!-- ##### FUNCTION g_module_name ##### --> 199<para> 200Gets the filename from a #GModule. 201</para> 202 203@module: a #GModule. 204@Returns: the filename of the module, or "main" if the module is the main 205program itself. 206 207 208<!-- ##### FUNCTION g_module_make_resident ##### --> 209<para> 210Ensures that a module will never be unloaded. 211Any future g_module_close() calls on the module will be ignored. 212</para> 213 214@module: a #GModule to make permanently resident. 215 216 217<!-- ##### FUNCTION g_module_close ##### --> 218<para> 219Closes a module. 220</para> 221 222@module: a #GModule to close. 223@Returns: %TRUE on success. 224 225 226<!-- ##### FUNCTION g_module_error ##### --> 227<para> 228Gets a string describing the last module error. 229</para> 230 231@Returns: a string describing the last module error. 232 233 234<!-- ##### USER_FUNCTION GModuleCheckInit ##### --> 235<para> 236Specifies the type of the module initialization function. 237<indexterm zone="g-module-check-init"><primary>g_module_check_init</primary></indexterm> 238If a module contains a function named g_module_check_init() it is called 239automatically when the module is loaded. It is passed the #GModule structure 240and should return %NULL on success or a string describing the initialization 241error. 242</para> 243 244@module: the #GModule corresponding to the module which has just been loaded. 245@Returns: %NULL on success, or a string describing the initialization error. 246 247 248<!-- ##### USER_FUNCTION GModuleUnload ##### --> 249<para> 250<indexterm zone="g-module-unload"><primary>g_module_unload</primary></indexterm> 251Specifies the type of the module function called when it is unloaded. 252If a module contains a function named g_module_unload() it is called 253automatically when the module is unloaded. 254It is passed the #GModule structure. 255</para> 256 257@module: the #GModule about to be unloaded. 258 259 260<!-- ##### MACRO G_MODULE_SUFFIX ##### --> 261<para> 262Expands to the proper shared library suffix for the current platform 263without the leading dot. For the most Unices and Linux this is "so", 264for some HP-UX versions this is "sl" and for Windows this is "dll". 265</para> 266 267 268 269<!-- ##### MACRO G_MODULE_EXPORT ##### --> 270<para> 271Used to declare functions exported by modules. This is a no-op on Linux and 272Unices, but when compiling for Windows, it marks a symbol to be exported from 273the library or executable being built. 274</para> 275 276 277 278<!-- ##### MACRO G_MODULE_IMPORT ##### --> 279<para> 280Used to declare functions imported from modules. 281</para> 282 283 284 285