1 /* GMODULE - GLIB wrapper code for dynamic module loading
2 * Copyright (C) 1998, 2000 Tim Janik
4 * Win32 GMODULE implementation
5 * Copyright (C) 1998 Tor Lillqvist
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GLib Team and others 1997-1999. See the AUTHORS
25 * file for a list of people on the GLib Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GLib at ftp://ftp.gtk.org/pub/gtk/.
41 gchar *error = g_win32_error_message (GetLastError ());
43 g_module_set_error (error);
47 /* --- functions --- */
49 _g_module_open (const gchar *file_name,
54 handle = LoadLibrary (file_name);
62 static gpointer null_module_handle = &dummy;
67 return null_module_handle;
71 _g_module_close (gpointer handle,
74 if (handle != null_module_handle)
75 if (!FreeLibrary (handle))
80 find_in_any_module_using_toolhelp (const gchar *symbol_name)
82 typedef HANDLE (WINAPI *PFNCREATETOOLHELP32SNAPSHOT)(DWORD, DWORD);
83 static PFNCREATETOOLHELP32SNAPSHOT pfnCreateToolhelp32Snapshot = NULL;
85 typedef BOOL (WINAPI *PFNMODULE32FIRST)(HANDLE, LPMODULEENTRY32);
86 static PFNMODULE32FIRST pfnModule32First= NULL;
88 typedef BOOL (WINAPI *PFNMODULE32NEXT)(HANDLE, LPMODULEENTRY32);
89 static PFNMODULE32NEXT pfnModule32Next = NULL;
91 static HMODULE kernel32;
98 if (!pfnCreateToolhelp32Snapshot || !pfnModule32First || !pfnModule32Next)
101 if (!(kernel32 = GetModuleHandle ("kernel32.dll")))
104 if (!(pfnCreateToolhelp32Snapshot = (PFNCREATETOOLHELP32SNAPSHOT) GetProcAddress (kernel32, "CreateToolhelp32Snapshot"))
105 || !(pfnModule32First = (PFNMODULE32FIRST) GetProcAddress (kernel32, "Module32First"))
106 || !(pfnModule32Next = (PFNMODULE32NEXT) GetProcAddress (kernel32, "Module32Next")))
110 if ((snapshot = (*pfnCreateToolhelp32Snapshot) (TH32CS_SNAPMODULE, 0)) == (HANDLE) -1)
113 me32.dwSize = sizeof (me32);
115 if ((*pfnModule32First) (snapshot, &me32))
118 if ((p = GetProcAddress (me32.hModule, symbol_name)) != NULL)
120 } while ((*pfnModule32Next) (snapshot, &me32));
123 CloseHandle (snapshot);
129 find_in_any_module_using_psapi (const gchar *symbol_name)
131 static HMODULE psapi = NULL;
133 typedef BOOL (WINAPI *PFNENUMPROCESSMODULES) (HANDLE, HMODULE *, DWORD, LPDWORD) ;
134 static PFNENUMPROCESSMODULES pfnEnumProcessModules = NULL;
143 if (!pfnEnumProcessModules)
146 if ((psapi = LoadLibrary ("psapi.dll")) == NULL)
149 if (!(pfnEnumProcessModules = (PFNENUMPROCESSMODULES) GetProcAddress (psapi, "EnumProcessModules")))
153 if (!(*pfnEnumProcessModules) (GetCurrentProcess (), &dummy,
154 sizeof (HMODULE), &needed))
157 size = needed + 10 * sizeof (HMODULE);
158 modules = g_malloc (size);
160 if (!(*pfnEnumProcessModules) (GetCurrentProcess (), modules,
169 for (i = 0; i < needed / sizeof (HMODULE); i++)
170 if ((p = GetProcAddress (modules[i], symbol_name)) != NULL)
179 find_in_any_module (const gchar *symbol_name)
183 if ((result = find_in_any_module_using_toolhelp (symbol_name)) == NULL
184 && (result = find_in_any_module_using_psapi (symbol_name)) == NULL)
191 _g_module_symbol (gpointer handle,
192 const gchar *symbol_name)
196 if (handle == null_module_handle)
198 if ((p = GetProcAddress (GetModuleHandle (NULL), symbol_name)) == NULL)
199 p = find_in_any_module (symbol_name);
202 p = GetProcAddress (handle, symbol_name);
211 _g_module_build_path (const gchar *directory,
212 const gchar *module_name)
216 k = strlen (module_name);
217 if (directory && *directory)
218 if (k > 4 && g_strcasecmp (module_name + k - 4, ".dll") == 0)
219 return g_strconcat (directory, "\\", module_name, NULL);
221 return g_strconcat (directory, "\\", module_name, ".dll", NULL);
222 else if (k > 4 && g_strcasecmp (module_name + k - 4, ".dll") == 0)
223 return g_strdup (module_name);
225 return g_strconcat (module_name, ".dll", NULL);