📄 glib-dynamic-loading-of-modules.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"><title>Dynamic Loading of Modules</title><meta name="generator" content="DocBook XSL Stylesheets V1.73.2"><link rel="start" href="index.html" title="GLib Reference Manual"><link rel="up" href="glib-core.html" title="GLib Core Application Support"><link rel="prev" href="glib-Asynchronous-Queues.html" title="Asynchronous Queues"><link rel="next" href="glib-Memory-Allocation.html" title="Memory Allocation"><meta name="generator" content="GTK-Doc V1.9 (XML mode)"><link rel="stylesheet" href="style.css" type="text/css"><link rel="chapter" href="glib.html" title="GLib Overview"><link rel="chapter" href="glib-fundamentals.html" title="GLib Fundamentals"><link rel="chapter" href="glib-core.html" title="GLib Core Application Support"><link rel="chapter" href="glib-utilities.html" title="GLib Utilities"><link rel="chapter" href="glib-data-types.html" title="GLib Data Types"><link rel="chapter" href="tools.html" title="GLib Tools"><link rel="index" href="ix01.html" title="Index"><link rel="index" href="ix02.html" title="Index of deprecated symbols"><link rel="index" href="ix03.html" title="Index of new symbols in 2.2"><link rel="index" href="ix04.html" title="Index of new symbols in 2.4"><link rel="index" href="ix05.html" title="Index of new symbols in 2.6"><link rel="index" href="ix06.html" title="Index of new symbols in 2.8"><link rel="index" href="ix07.html" title="Index of new symbols in 2.10"><link rel="index" href="ix08.html" title="Index of new symbols in 2.12"><link rel="index" href="ix09.html" title="Index of new symbols in 2.14"><link rel="index" href="ix10.html" title="Index of new symbols in 2.16"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle"><td><a accesskey="p" href="glib-Asynchronous-Queues.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td><td><a accesskey="u" href="glib-core.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td><td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td><th width="100%" align="center">GLib Reference Manual</th><td><a accesskey="n" href="glib-Memory-Allocation.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td></tr><tr><td colspan="5" class="shortcuts"><nobr><a href="#id2917111" class="shortcut">Top</a>  |  <a href="#id2917417" class="shortcut">Description</a></nobr></td></tr></table><div class="refentry" lang="en"><a name="glib-Dynamic-Loading-of-Modules"></a><div class="titlepage"></div><div class="refnamediv"><table width="100%"><tr><td valign="top"><h2><a name="id2917111"></a><span class="refentrytitle">Dynamic Loading of Modules</span></h2><p>Dynamic Loading of Modules — portable method for dynamically loading 'plug-ins'</p></td><td valign="top" align="right"></td></tr></table></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">#include <gmodule.h> <a class="link" href="glib-Dynamic-Loading-of-Modules.html#GModule">GModule</a>;<a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> <a class="link" href="glib-Dynamic-Loading-of-Modules.html#g-module-supported">g_module_supported</a> (void);<a class="link" href="glib-Basic-Types.html#gchar">gchar</a>* <a class="link" href="glib-Dynamic-Loading-of-Modules.html#g-module-build-path">g_module_build_path</a> (const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *directory, const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *module_name);<a class="link" href="glib-Dynamic-Loading-of-Modules.html#GModule">GModule</a>* <a class="link" href="glib-Dynamic-Loading-of-Modules.html#g-module-open">g_module_open</a> (const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *file_name, <a class="link" href="glib-Dynamic-Loading-of-Modules.html#GModuleFlags">GModuleFlags</a> flags);enum <a class="link" href="glib-Dynamic-Loading-of-Modules.html#GModuleFlags">GModuleFlags</a>;<a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> <a class="link" href="glib-Dynamic-Loading-of-Modules.html#g-module-symbol">g_module_symbol</a> (<a class="link" href="glib-Dynamic-Loading-of-Modules.html#GModule">GModule</a> *module, const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *symbol_name, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> *symbol);const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a>* <a class="link" href="glib-Dynamic-Loading-of-Modules.html#g-module-name">g_module_name</a> (<a class="link" href="glib-Dynamic-Loading-of-Modules.html#GModule">GModule</a> *module);void <a class="link" href="glib-Dynamic-Loading-of-Modules.html#g-module-make-resident">g_module_make_resident</a> (<a class="link" href="glib-Dynamic-Loading-of-Modules.html#GModule">GModule</a> *module);<a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> <a class="link" href="glib-Dynamic-Loading-of-Modules.html#g-module-close">g_module_close</a> (<a class="link" href="glib-Dynamic-Loading-of-Modules.html#GModule">GModule</a> *module);const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a>* <a class="link" href="glib-Dynamic-Loading-of-Modules.html#g-module-error">g_module_error</a> (void);const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a>* (<a class="link" href="glib-Dynamic-Loading-of-Modules.html#GModuleCheckInit">*GModuleCheckInit</a>) (<a class="link" href="glib-Dynamic-Loading-of-Modules.html#GModule">GModule</a> *module);void (<a class="link" href="glib-Dynamic-Loading-of-Modules.html#GModuleUnload">*GModuleUnload</a>) (<a class="link" href="glib-Dynamic-Loading-of-Modules.html#GModule">GModule</a> *module);#define <a class="link" href="glib-Dynamic-Loading-of-Modules.html#G-MODULE-SUFFIX:CAPS">G_MODULE_SUFFIX</a>#define <a class="link" href="glib-Dynamic-Loading-of-Modules.html#G-MODULE-EXPORT:CAPS">G_MODULE_EXPORT</a>#define <a class="link" href="glib-Dynamic-Loading-of-Modules.html#G-MODULE-IMPORT:CAPS">G_MODULE_IMPORT</a></pre></div><div class="refsect1" lang="en"><a name="id2917417"></a><h2>Description</h2><p>These functions provide a portable way to dynamically load object files(commonly known as 'plug-ins').The current implementation supports all systems that providean implementation of <code class="function">dlopen()</code> (e.g. Linux/Sun), as well as HP-UX via its<code class="function">shl_load()</code> mechanism, and Windows platforms via DLLs.</p><p>A program which wants to use these functions must be linked to thelibraries output by the command <span class="command"><strong>pkg-config --libs gmodule-2.0</strong></span>.</p><p>To use them you must first determine whether dynamic loadingis supported on the platform by calling <a class="link" href="glib-Dynamic-Loading-of-Modules.html#g-module-supported"><code class="function">g_module_supported()</code></a>.If it is, you can open a module with <a class="link" href="glib-Dynamic-Loading-of-Modules.html#g-module-open"><code class="function">g_module_open()</code></a>,find the module's symbols (e.g. function names) with <a class="link" href="glib-Dynamic-Loading-of-Modules.html#g-module-symbol"><code class="function">g_module_symbol()</code></a>,and later close the module with <a class="link" href="glib-Dynamic-Loading-of-Modules.html#g-module-close"><code class="function">g_module_close()</code></a>.<a class="link" href="glib-Dynamic-Loading-of-Modules.html#g-module-name"><code class="function">g_module_name()</code></a> will return the file name of a currently opened module.</p><p>If any of the above functions fail, the error status can be found with<a class="link" href="glib-Dynamic-Loading-of-Modules.html#g-module-error"><code class="function">g_module_error()</code></a>.</p><p>The <a class="link" href="glib-Dynamic-Loading-of-Modules.html#GModule"><span class="type">GModule</span></a> implementation features reference counting for opened modules,and supports hook functions within a module which are called when themodule is loaded and unloaded (see <a class="link" href="glib-Dynamic-Loading-of-Modules.html#GModuleCheckInit"><span class="type">GModuleCheckInit</span></a> and <a class="link" href="glib-Dynamic-Loading-of-Modules.html#GModuleUnload"><span class="type">GModuleUnload</span></a>).</p><p>If your module introduces static data to common subsystems in the runningprogram, e.g. through calling <code class="literal">g_quark_from_static_string ("my-module-stuff")</code>,it must ensure that it is never unloaded, by calling <a class="link" href="glib-Dynamic-Loading-of-Modules.html#g-module-make-resident"><code class="function">g_module_make_resident()</code></a>.</p><p></p><div class="example"><a name="id2917600"></a><p class="title"><b>Example 11. Calling a function defined in a <span class="structname">GModule</span></b></p><div class="example-contents"><pre class="programlisting">/* the function signature for 'say_hello' */typedef void (* SayHelloFunc) (const char *message);gbooleanjust_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;}</pre></div></div><p><br class="example-break"></p></div><div class="refsect1" lang="en"><a name="id2917651"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id2917660"></a><h3><a name="GModule"></a>GModule</h3><a class="indexterm" name="id2917676"></a><pre class="programlisting">typedef struct _GModule GModule;</pre><p>The <a class="link" href="glib-Dynamic-Loading-of-Modules.html#GModule"><span class="type">GModule</span></a> struct is an opaque data structure to represent a<a class="link" href="glib-Dynamic-Loading-of-Modules.html" title="Dynamic Loading of Modules">Dynamically-Loaded Module</a>.It should only be accessed via the following functions.</p></div><hr><div class="refsect2" lang="en"><a name="id2917709"></a><h3><a name="g-module-supported"></a>g_module_supported ()</h3><a class="indexterm" name="id2917722"></a><pre class="programlisting"><a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> g_module_supported (void);</pre><p>Checks if modules are supported on the current platform.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td><td><a class="link" href="glib-Standard-Macros.html#TRUE:CAPS"><code class="literal">TRUE</code></a> if modules are supported.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2917768"></a><h3><a name="g-module-build-path"></a>g_module_build_path ()</h3><a class="indexterm" name="id2917781"></a><pre class="programlisting"><a class="link" href="glib-Basic-Types.html#gchar">gchar</a>* g_module_build_path (const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *directory, const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *module_name);</pre><p>A portable way to build the filename of a module. The platform-specificprefix and suffix are added to the filename, if needed, and the result isadded to the directory, using the correct separator character.</p><p>The directory should specify the directory where the module can be found.It can be <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a> or an empty string to indicate that the module is in a standardplatform-specific directory, though this is not recommended since thewrong module may be found.</p><p>For example, calling <a class="link" href="glib-Dynamic-Loading-of-Modules.html#g-module-build-path"><code class="function">g_module_build_path()</code></a> on a Linux system with a <em class="parameter"><code>directory</code></em>of <code class="filename">/lib</code> and a <em class="parameter"><code>module_name</code></em> of "mylibrary" will return <code class="filename">/lib/libmylibrary.so</code>. On a Windows system, using <code class="filename">\Windows</code> as the directory it will return<code class="filename">\Windows\mylibrary.dll</code>.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="parameter"><code>directory</code></em> :</span></p></td><td>the directory where the module is. This can be <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a> or the emptystring to indicate that the standard platform-specific directories will be used, though that is not recommended.</td></tr><tr><td><p><span class="term"><em class="parameter"><code>module_name</code></em> :</span></p></td><td>the name of the module.</td></tr><tr><td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td><td>the complete path of the module, including the standard libraryprefix and suffix. This should be freed when no longer needed.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2917949"></a><h3><a name="g-module-open"></a>g_module_open ()</h3><a class="indexterm" name="id2917962"></a><pre class="programlisting"><a class="link" href="glib-Dynamic-Loading-of-Modules.html#GModule">GModule</a>* g_module_open (const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *file_name, <a class="link" href="glib-Dynamic-Loading-of-Modules.html#GModuleFlags">GModuleFlags</a> flags);</pre><p>Opens a module. If the module has already been opened, its referencecount is incremented. </p><p>First of all <a class="link" href="glib-Dynamic-Loading-of-Modules.html#g-module-open"><code class="function">g_module_open()</code></a> tries to open <em class="parameter"><code>file_name</code></em> as a module. Ifthat fails and <em class="parameter"><code>file_name</code></em> 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 (<a class="link" href="glib-Dynamic-Loading-of-Modules.html#G-MODULE-SUFFIX:CAPS"><span class="type">G_MODULE_SUFFIX</span></a>), this suffix will be appended and the corresponding module will be opended. If that fails and <em class="parameter"><code>file_name</code></em> doesn't have the ".la"-suffix, this suffix is appended and <a class="link" href="glib-Dynamic-Loading-of-Modules.html#g-module-open"><code class="function">g_module_open()</code></a> tries to open the corresponding module. If eventually that fails as well, <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a> is returned.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="parameter"><code>file_name</code></em> :</span></p></td><td>the name of the file containing the module, or <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a> to obtain a <a class="link" href="glib-Dynamic-Loading-of-Modules.html#GModule"><span class="type">GModule</span></a> representing the main program itself.</td></tr><tr><td><p><span class="term"><em class="parameter"><code>flags</code></em> :</span></p></td><td>the flags used for opening the module. This can be the logicalOR of any of the <a class="link" href="glib-Dynamic-Loading-of-Modules.html#GModuleFlags"><span class="type">GModuleFlags</span></a>.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -