1 <!-- ##### SECTION Title ##### -->
2 Dynamic Loading of Modules
4 <!-- ##### SECTION Short_Description ##### -->
5 portable method for dynamically loading 'plug-ins'
7 <!-- ##### SECTION Long_Description ##### -->
9 These functions provide a portable way to dynamically load object files
10 (commonly known as 'plug-ins').
11 The current implementation supports all systems that provide
12 an implementation of dlopen() (e.g. Linux/Sun), as well as HP-UX via its
13 shl_load() mechanism, and Windows platforms via DLLs.
17 A program which wants to use these functions must be linked to the
18 libraries output by the command <command>pkg-config --libs gmodule-2.0</command>.
22 To use them you must first determine whether dynamic loading
23 is supported on the platform by calling g_module_supported().
24 If it is, you can open a module with g_module_open(),
25 find the module's symbols (e.g. function names) with g_module_symbol(),
26 and later close the module with g_module_close().
27 g_module_name() will return the file name of a currently opened module.
30 If any of the above functions fail, the error status can be found with
34 The #GModule implementation features reference counting for opened modules,
35 and supports hook functions within a module which are called when the
36 module is loaded and unloaded (see #GModuleCheckInit and #GModuleUnload).
39 If your module introduces static data to common subsystems in the running
40 program, e.g. through calling <literal>g_quark_from_static_string ("my-module-stuff")</literal>,
41 it must ensure that it is never unloaded, by calling g_module_make_resident().
46 <title>Calling a function defined in a <structname>GModule</structname></title>
48 /* the function signature for 'say_hello' */
49 typedef void (* SayHelloFunc) (const char *message);
52 just_say_hello (const char *filename, GError **error)
54 SayHelloFunc say_hello;
57 module = g_module_open (filename, G_MODULE_BIND_LAZY);
60 g_set_error (error, FOO_ERROR, FOO_ERROR_BLAH,
61 "%s", g_module_error (<!-- -->));
65 if (!g_module_symbol (module, "say_hello", (gpointer *)&say_hello))
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 (<!-- -->));
74 if (say_hello == NULL)
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 (<!-- -->));
82 /* call our function in the module */
83 say_hello ("Hello world!");
85 if (!g_module_close (module))
86 g_warning ("%s: %s", filename, g_module_error (<!-- -->));
94 <!-- ##### SECTION See_Also ##### -->
99 <!-- ##### SECTION Stability_Level ##### -->
102 <!-- ##### STRUCT GModule ##### -->
104 The #GModule struct is an opaque data structure to represent a
105 <link linkend="glib-Dynamic-Loading-of-Modules">Dynamically-Loaded Module</link>.
106 It should only be accessed via the following functions.
110 <!-- ##### FUNCTION g_module_supported ##### -->
112 Checks if modules are supported on the current platform.
115 @Returns: %TRUE if modules are supported.
118 <!-- ##### FUNCTION g_module_build_path ##### -->
120 A portable way to build the filename of a module. The platform-specific
121 prefix and suffix are added to the filename, if needed, and the result is
122 added to the directory, using the correct separator character.
125 The directory should specify the directory where the module can be found.
126 It can be %NULL or an empty string to indicate that the module is in a standard
127 platform-specific directory, though this is not recommended since the
128 wrong module may be found.
131 For example, calling g_module_build_path() on a Linux system with a @directory
132 of <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>.
138 @directory: the directory where the module is. This can be %NULL or the empty
139 string to indicate that the standard platform-specific directories will be
140 used, 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
143 prefix and suffix. This should be freed when no longer needed.
146 <!-- ##### FUNCTION g_module_open ##### -->
148 Opens a module. If the module has already been opened, its reference
149 count is incremented.
153 First of all g_module_open() tries to open @file_name as a module. If
154 that fails and @file_name has the ".la"-suffix (and is a libtool archive)
155 it tries to open the corresponding module. If that fails and it doesn't
156 have the proper module suffix for the platform (#G_MODULE_SUFFIX), this
157 suffix will be appended and the corresponding module will be opended. If
158 that fails and @file_name doesn't have the ".la"-suffix, this suffix is
159 appended and g_module_open() tries to open the corresponding module. If
160 eventually that fails as well, %NULL is returned.
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
166 OR of any of the #GModuleFlags.
167 @Returns: a #GModule on success, or %NULL on failure.
170 <!-- ##### ENUM GModuleFlags ##### -->
172 Flags passed to g_module_open(). Note that these flags are
173 not supported on all platforms.
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.
184 <!-- ##### FUNCTION g_module_symbol ##### -->
186 Gets a symbol pointer from a module, such as one exported by #G_MODULE_EXPORT.
189 Note that a valid symbol can be %NULL.
193 @symbol_name: the name of the symbol to find.
194 @symbol: returns the pointer to the symbol value.
195 @Returns: %TRUE on success.
198 <!-- ##### FUNCTION g_module_name ##### -->
200 Gets the filename from a #GModule.
204 @Returns: the filename of the module, or "main" if the module is the main
208 <!-- ##### FUNCTION g_module_make_resident ##### -->
210 Ensures that a module will never be unloaded.
211 Any future g_module_close() calls on the module will be ignored.
214 @module: a #GModule to make permanently resident.
217 <!-- ##### FUNCTION g_module_close ##### -->
222 @module: a #GModule to close.
223 @Returns: %TRUE on success.
226 <!-- ##### FUNCTION g_module_error ##### -->
228 Gets a string describing the last module error.
231 @Returns: a string describing the last module error.
234 <!-- ##### USER_FUNCTION GModuleCheckInit ##### -->
236 Specifies the type of the module initialization function.
237 <indexterm zone="g-module-check-init"><primary>g_module_check_init</primary></indexterm>
238 If a module contains a function named g_module_check_init() it is called
239 automatically when the module is loaded. It is passed the #GModule structure
240 and should return %NULL on success or a string describing the initialization
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.
248 <!-- ##### USER_FUNCTION GModuleUnload ##### -->
250 <indexterm zone="g-module-unload"><primary>g_module_unload</primary></indexterm>
251 Specifies the type of the module function called when it is unloaded.
252 If a module contains a function named g_module_unload() it is called
253 automatically when the module is unloaded.
254 It is passed the #GModule structure.
257 @module: the #GModule about to be unloaded.
260 <!-- ##### MACRO G_MODULE_SUFFIX ##### -->
262 Expands to the proper shared library suffix for the current platform
263 without the leading dot. For the most Unices and Linux this is "so",
264 for some HP-UX versions this is "sl" and for Windows this is "dll".
269 <!-- ##### MACRO G_MODULE_EXPORT ##### -->
271 Used to declare functions exported by modules. This is a no-op on Linux and
272 Unices, but when compiling for Windows, it marks a symbol to be exported from
273 the library or executable being built.
278 <!-- ##### MACRO G_MODULE_IMPORT ##### -->
280 Used to declare functions imported from modules.