?? glib-dynamic-loading-of-modules.html
字號:
<html xmlns:cf="http://docbook.sourceforge.net/xmlns/chunkfast/1.0"><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.69.0"><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.4 (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"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table class="navigation" 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></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><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 href="glib-Dynamic-Loading-of-Modules.html#GModule">GModule</a>;<a href="glib-Basic-Types.html#gboolean">gboolean</a> <a href="glib-Dynamic-Loading-of-Modules.html#g-module-supported">g_module_supported</a> (void);<a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-Dynamic-Loading-of-Modules.html#g-module-build-path">g_module_build_path</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *directory, const <a href="glib-Basic-Types.html#gchar">gchar</a> *module_name);<a href="glib-Dynamic-Loading-of-Modules.html#GModule">GModule</a>* <a href="glib-Dynamic-Loading-of-Modules.html#g-module-open">g_module_open</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *file_name, <a href="glib-Dynamic-Loading-of-Modules.html#GModuleFlags">GModuleFlags</a> flags);enum <a href="glib-Dynamic-Loading-of-Modules.html#GModuleFlags">GModuleFlags</a>;<a href="glib-Basic-Types.html#gboolean">gboolean</a> <a href="glib-Dynamic-Loading-of-Modules.html#g-module-symbol">g_module_symbol</a> (<a href="glib-Dynamic-Loading-of-Modules.html#GModule">GModule</a> *module, const <a href="glib-Basic-Types.html#gchar">gchar</a> *symbol_name, <a href="glib-Basic-Types.html#gpointer">gpointer</a> *symbol);const <a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-Dynamic-Loading-of-Modules.html#g-module-name">g_module_name</a> (<a href="glib-Dynamic-Loading-of-Modules.html#GModule">GModule</a> *module);void <a href="glib-Dynamic-Loading-of-Modules.html#g-module-make-resident">g_module_make_resident</a> (<a href="glib-Dynamic-Loading-of-Modules.html#GModule">GModule</a> *module);<a href="glib-Basic-Types.html#gboolean">gboolean</a> <a href="glib-Dynamic-Loading-of-Modules.html#g-module-close">g_module_close</a> (<a href="glib-Dynamic-Loading-of-Modules.html#GModule">GModule</a> *module);const <a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-Dynamic-Loading-of-Modules.html#g-module-error">g_module_error</a> (void);const <a href="glib-Basic-Types.html#gchar">gchar</a>* (<a href="glib-Dynamic-Loading-of-Modules.html#GModuleCheckInit">*GModuleCheckInit</a>) (<a href="glib-Dynamic-Loading-of-Modules.html#GModule">GModule</a> *module);void (<a href="glib-Dynamic-Loading-of-Modules.html#GModuleUnload">*GModuleUnload</a>) (<a href="glib-Dynamic-Loading-of-Modules.html#GModule">GModule</a> *module);#define <a href="glib-Dynamic-Loading-of-Modules.html#G-MODULE-SUFFIX:CAPS">G_MODULE_SUFFIX</a>#define <a href="glib-Dynamic-Loading-of-Modules.html#G-MODULE-EXPORT:CAPS">G_MODULE_EXPORT</a>#define <a href="glib-Dynamic-Loading-of-Modules.html#G-MODULE-IMPORT:CAPS">G_MODULE_IMPORT</a></pre></div><div class="refsect1" lang="en"><a name="id2899031"></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"><code class="function">dlopen()</code></code> (e.g. Linux/Sun), as well as HP-UX via its<code class="function"><code class="function">shl_load()</code></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><strong class="command">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 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 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 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 href="glib-Dynamic-Loading-of-Modules.html#g-module-close"><code class="function">g_module_close()</code></a>.<a 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 href="glib-Dynamic-Loading-of-Modules.html#g-module-error"><code class="function">g_module_error()</code></a>.</p><p>The <a 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 href="glib-Dynamic-Loading-of-Modules.html#GModuleCheckInit"><span class="type">GModuleCheckInit</span></a> and <a 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 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="id2899217"></a><p class="title"><b>Example 12. Calling a function defined in a <span class="structname">GModule</span></b></p><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; } /* 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><p></p></div><div class="refsect1" lang="en"><a name="id2899261"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id2899266"></a><h3><a name="GModule"></a>GModule</h3><a class="indexterm" name="id2899275"></a><pre class="programlisting">typedef struct _GModule GModule;</pre><p>The <a href="glib-Dynamic-Loading-of-Modules.html#GModule"><span class="type">GModule</span></a> struct is an opaque data structure to represent a<a 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="id2899311"></a><h3><a name="g-module-supported"></a>g_module_supported ()</h3><a class="indexterm" name="id2899322"></a><pre class="programlisting"><a 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><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td><code class="literal">TRUE</code> if modules are supported.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2899363"></a><h3><a name="g-module-build-path"></a>g_module_build_path ()</h3><a class="indexterm" name="id2899374"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gchar">gchar</a>* g_module_build_path (const <a href="glib-Basic-Types.html#gchar">gchar</a> *directory, const <a 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 <code class="literal">NULL</code> 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 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><span class="term"><em class="parameter"><code>directory</code></em> :</span></td><td>the directory where the module is. This can be <code class="literal">NULL</code> or the emptystring to indicate that the standard platform-specific directories will be used, though that is not recommended.</td></tr><tr><td><span class="term"><em class="parameter"><code>module_name</code></em> :</span></td><td>the name of the module.</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></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="id2899533"></a><h3><a name="g-module-open"></a>g_module_open ()</h3><a class="indexterm" name="id2899544"></a><pre class="programlisting"><a href="glib-Dynamic-Loading-of-Modules.html#GModule">GModule</a>* g_module_open (const <a href="glib-Basic-Types.html#gchar">gchar</a> *file_name, <a 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 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 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 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, <code class="literal">NULL</code> is returned.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>file_name</code></em> :</span></td><td>the name of the file containing the module.</td></tr><tr><td><span class="term"><em class="parameter"><code>flags</code></em> :</span></td><td>the flags used for opening the module. This can be the logicalOR of any of the <a href="glib-Dynamic-Loading-of-Modules.html#GModuleFlags"><span class="type">GModuleFlags</span></a>.</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td>a <a href="glib-Dynamic-Loading-of-Modules.html#GModule"><span class="type">GModule</span></a> on success, or <code class="literal">NULL</code> on failure.</td></tr></tbody></table></div></div><hr>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -