1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
<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 <gmodule.h>
<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,
"%s", g_module_error (<!-- -->));
return FALSE;
}
if (!g_module_symbol (module, "say_hello", (gpointer *)&say_hello))
{
g_set_error (error, SAY_ERROR, SAY_ERROR_OPEN,
"%s: %s", filename, g_module_error (<!-- -->));
if (!g_module_close (module))
g_warning ("%s: %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 ("%s: %s", filename, g_module_error (<!-- -->));
return FALSE;
}
/* call our function in the module */
say_hello ("Hello world!");
if (!g_module_close (module))
g_warning ("%s: %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="GModule">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="g_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> :</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="g_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> :</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> :</term>
<listitem><simpara>the name of the module.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</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="g_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> :</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> :</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> :</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="GModuleFlags">GModuleFlags</primary></indexterm><programlisting>typedef enum
{
G_MODULE_BIND_LAZY = 1 << 0,
G_MODULE_BIND_LOCAL = 1 << 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="g_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> :</term>
<listitem><simpara>a <link linkend="GModule"><type>GModule</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>symbol_name</parameter> :</term>
<listitem><simpara>the name of the symbol to find.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>symbol</parameter> :</term>
<listitem><simpara>returns the pointer to the symbol value.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</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="g_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> :</term>
<listitem><simpara>a <link linkend="GModule"><type>GModule</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</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="g_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> :</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="g_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> :</term>
<listitem><simpara>a <link linkend="GModule"><type>GModule</type></link> to close.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</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="g_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> :</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="GModuleCheckInit">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> :</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> :</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="GModuleUnload">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> :</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="G_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="G_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="G_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>
|