2 * gmodule.c: dl* functions, glib style
5 * Gonzalo Paniagua Javier (gonzalo@novell.com)
6 * Jonathan Chambers (joncham@gmail.com)
8 * (C) 2006 Novell, Inc.
9 * (C) 2006 Jonathan Chambers
11 * Permission is hereby granted, free of charge, to any person obtaining
12 * a copy of this software and associated documentation files (the
13 * "Software"), to deal in the Software without restriction, including
14 * without limitation the rights to use, copy, modify, merge, publish,
15 * distribute, sublicense, and/or sell copies of the Software, and to
16 * permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
19 * The above copyright notice and this permission notice shall be
20 * included in all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 /* For Linux and Solaris, need to add others as we port this */
36 #define LIBSUFFIX "lib"
37 #define LIBPREFIX ".so"
44 g_module_open (const gchar *file, GModuleFlags flags)
49 flags &= G_MODULE_BIND_MASK;
50 if ((flags & G_MODULE_BIND_LAZY) != 0)
52 if ((flags & G_MODULE_BIND_LOCAL) != 0)
55 module = g_malloc (sizeof (GModule));
59 module->handle = dlopen (file, f);
64 g_module_symbol (GModule *module, const gchar *symbol_name, gpointer *symbol)
66 if (symbol_name == NULL || symbol == NULL)
69 if (module == NULL || module->handle == NULL)
72 *symbol = dlsym (module->handle, symbol_name);
73 return (*symbol != NULL);
83 g_module_close (GModule *module)
86 if (module == NULL || module->handle == NULL)
89 handle = module->handle;
90 module->handle = NULL;
92 return (0 == dlclose (handle));
95 #elif defined (G_OS_WIN32)
98 #define LIBSUFFIX ".dll"
106 g_module_open (const gchar *file, GModuleFlags flags)
109 module = g_malloc (sizeof (GModule));
113 module->handle = LoadLibrary (file);
118 g_module_symbol (GModule *module, const gchar *symbol_name, gpointer *symbol)
120 if (symbol_name == NULL || symbol == NULL)
123 if (module == NULL || module->handle == NULL)
126 *symbol = GetProcAddress (module->handle, symbol_name);
127 return (*symbol != NULL);
131 g_module_error (void)
135 DWORD code = GetLastError ();
137 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL,
138 code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, 0, NULL);
140 ret = g_strdup (buf);
147 g_module_close (GModule *module)
150 if (module == NULL || module->handle == NULL)
153 handle = module->handle;
154 module->handle = NULL;
156 return (0 == FreeLibrary (handle));
165 g_module_open (const gchar *file, GModuleFlags flags)
167 g_error ("g_module_open not implemented on this platform");
172 g_module_symbol (GModule *module, const gchar *symbol_name, gpointer *symbol)
174 g_error ("g_module_open not implemented on this platform");
179 g_module_error (void)
181 g_error ("g_module_open not implemented on this platform");
186 g_module_close (GModule *module)
188 g_error ("g_module_open not implemented on this platform");
194 g_module_build_path (const gchar *directory, const gchar *module_name)
196 if (module_name == NULL)
200 return g_strdup_printf ("%s/" LIBPREFIX "%s" LIBSUFFIX, directory, module_name);
201 return g_strdup_printf (LIBPREFIX "%s" LIBSUFFIX, module_name);