2 * gmodule.c: dl* functions, glib style
5 * Gonzalo Paniagua Javier (gonzalo@novell.com)
6 * Jonathan Chambers (joncham@gmail.com)
7 * Robert Jordan (robertj@gmx.net)
9 * (C) 2006 Novell, Inc.
10 * (C) 2006 Jonathan Chambers
12 * Permission is hereby granted, free of charge, to any person obtaining
13 * a copy of this software and associated documentation files (the
14 * "Software"), to deal in the Software without restriction, including
15 * without limitation the rights to use, copy, modify, merge, publish,
16 * distribute, sublicense, and/or sell copies of the Software, and to
17 * permit persons to whom the Software is furnished to do so, subject to
18 * the following conditions:
20 * The above copyright notice and this permission notice shall be
21 * included in all copies or substantial portions of the Software.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 #include <gmodule-win32-internals.h>
37 #define LIBSUFFIX ".dll"
46 g_module_open (const gchar *file, GModuleFlags flags)
49 module = g_malloc (sizeof (GModule));
55 file16 = u8to16(file);
56 module->main_module = FALSE;
57 module->handle = LoadLibraryW (file16);
59 if (!module->handle) {
65 module->main_module = TRUE;
66 module->handle = GetModuleHandle (NULL);
72 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
74 w32_find_symbol (const gchar *symbol_name)
77 DWORD buffer_size = sizeof (HMODULE) * 1024;
80 modules = (HMODULE *) g_malloc (buffer_size);
85 if (!EnumProcessModules (GetCurrentProcess (), modules,
86 buffer_size, &needed)) {
91 /* check whether the supplied buffer was too small, realloc, retry */
92 if (needed > buffer_size) {
96 modules = (HMODULE *) g_malloc (buffer_size);
101 if (!EnumProcessModules (GetCurrentProcess (), modules,
102 buffer_size, &needed)) {
108 for (i = 0; i < needed / sizeof (HANDLE); i++) {
109 gpointer proc = (gpointer)(intptr_t)GetProcAddress (modules [i], symbol_name);
119 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
122 g_module_symbol (GModule *module, const gchar *symbol_name, gpointer *symbol)
124 if (module == NULL || symbol_name == NULL || symbol == NULL)
127 if (module->main_module) {
128 *symbol = (gpointer)(intptr_t)GetProcAddress (module->handle, symbol_name);
132 *symbol = w32_find_symbol (symbol_name);
133 return *symbol != NULL;
135 *symbol = (gpointer)(intptr_t)GetProcAddress (module->handle, symbol_name);
136 return *symbol != NULL;
140 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
142 g_module_error (void)
146 DWORD code = GetLastError ();
148 /* FIXME: buf must not be NULL! */
149 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL,
150 code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, 0, NULL);
157 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
160 g_module_close (GModule *module)
165 if (module == NULL || module->handle == NULL)
168 handle = module->handle;
169 main_module = module->main_module;
170 module->handle = NULL;
172 return (main_module ? 1 : (0 == FreeLibrary (handle)));
176 g_module_build_path (const gchar *directory, const gchar *module_name)
178 char *lib_prefix = "";
180 if (module_name == NULL)
183 if (strncmp (module_name, "lib", 3) != 0)
184 lib_prefix = LIBPREFIX;
186 if (directory && *directory){
188 return g_strdup_printf ("%s/%s%s" LIBSUFFIX, directory, lib_prefix, module_name);
190 return g_strdup_printf ("%s%s" LIBSUFFIX, lib_prefix, module_name);