Clarify the documentation of source ids a bit. (#325874, Dan Williams)
[glib.git] / gmodule / gmodule-win32.c
blobbf6309138bf6b7306d4488fe9be793d23335faed
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 Lesser 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 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser 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-2000. 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/.
30 /*
31 * MT safe
34 #include <stdio.h>
35 #include <windows.h>
37 #include <tlhelp32.h>
39 #ifdef G_WITH_CYGWIN
40 #include <sys/cygwin.h>
41 #endif
43 static void
44 set_error (void)
46 gchar *error = g_win32_error_message (GetLastError ());
48 g_module_set_error (error);
49 g_free (error);
52 /* --- functions --- */
53 static gpointer
54 _g_module_open (const gchar *file_name,
55 gboolean bind_lazy,
56 gboolean bind_local)
58 HINSTANCE handle;
59 #ifdef G_WITH_CYGWIN
60 gchar tmp[MAX_PATH];
62 cygwin_conv_to_win32_path(file_name, tmp);
63 file_name = tmp;
64 #endif
65 if (G_WIN32_HAVE_WIDECHAR_API ())
67 wchar_t *wfilename = g_utf8_to_utf16 (file_name, -1, NULL, NULL, NULL);
69 handle = LoadLibraryW (wfilename);
70 g_free (wfilename);
72 else
74 gchar *cp_filename = g_locale_from_utf8 (file_name, -1, NULL, NULL, NULL);
76 handle = LoadLibraryA (cp_filename);
77 g_free (cp_filename);
80 if (!handle)
81 set_error ();
83 return handle;
86 static gint dummy;
87 static gpointer null_module_handle = &dummy;
89 static gpointer
90 _g_module_self (void)
92 return null_module_handle;
95 static void
96 _g_module_close (gpointer handle,
97 gboolean is_unref)
99 if (handle != null_module_handle)
100 if (!FreeLibrary (handle))
101 set_error ();
104 static gpointer
105 find_in_any_module_using_toolhelp (const gchar *symbol_name)
107 typedef HANDLE (WINAPI *PFNCREATETOOLHELP32SNAPSHOT)(DWORD, DWORD);
108 static PFNCREATETOOLHELP32SNAPSHOT pfnCreateToolhelp32Snapshot = NULL;
110 typedef BOOL (WINAPI *PFNMODULE32FIRST)(HANDLE, MODULEENTRY32*);
111 static PFNMODULE32FIRST pfnModule32First= NULL;
113 typedef BOOL (WINAPI *PFNMODULE32NEXT)(HANDLE, MODULEENTRY32*);
114 static PFNMODULE32NEXT pfnModule32Next = NULL;
116 static HMODULE kernel32;
118 HANDLE snapshot;
119 MODULEENTRY32 me32;
121 gpointer p;
123 if (!pfnCreateToolhelp32Snapshot || !pfnModule32First || !pfnModule32Next)
125 if (!kernel32)
126 if (!(kernel32 = GetModuleHandle ("kernel32.dll")))
127 return NULL;
129 if (!(pfnCreateToolhelp32Snapshot = (PFNCREATETOOLHELP32SNAPSHOT) GetProcAddress (kernel32, "CreateToolhelp32Snapshot"))
130 || !(pfnModule32First = (PFNMODULE32FIRST) GetProcAddress (kernel32, "Module32First"))
131 || !(pfnModule32Next = (PFNMODULE32NEXT) GetProcAddress (kernel32, "Module32Next")))
132 return NULL;
135 if ((snapshot = (*pfnCreateToolhelp32Snapshot) (TH32CS_SNAPMODULE, 0)) == (HANDLE) -1)
136 return NULL;
138 me32.dwSize = sizeof (me32);
139 p = NULL;
140 if ((*pfnModule32First) (snapshot, &me32))
142 do {
143 if ((p = GetProcAddress (me32.hModule, symbol_name)) != NULL)
144 break;
145 } while ((*pfnModule32Next) (snapshot, &me32));
148 CloseHandle (snapshot);
150 return p;
153 static gpointer
154 find_in_any_module_using_psapi (const gchar *symbol_name)
156 static HMODULE psapi = NULL;
158 typedef BOOL (WINAPI *PFNENUMPROCESSMODULES) (HANDLE, HMODULE *, DWORD, LPDWORD) ;
159 static PFNENUMPROCESSMODULES pfnEnumProcessModules = NULL;
161 HMODULE *modules;
162 HMODULE dummy;
163 gint i, size;
164 DWORD needed;
166 gpointer p;
168 if (!pfnEnumProcessModules)
170 if (!psapi)
171 if ((psapi = LoadLibrary ("psapi.dll")) == NULL)
172 return NULL;
174 if (!(pfnEnumProcessModules = (PFNENUMPROCESSMODULES) GetProcAddress (psapi, "EnumProcessModules")))
175 return NULL;
178 if (!(*pfnEnumProcessModules) (GetCurrentProcess (), &dummy,
179 sizeof (HMODULE), &needed))
180 return NULL;
182 size = needed + 10 * sizeof (HMODULE);
183 modules = g_malloc (size);
185 if (!(*pfnEnumProcessModules) (GetCurrentProcess (), modules,
186 size, &needed)
187 || needed > size)
189 g_free (modules);
190 return NULL;
193 p = NULL;
194 for (i = 0; i < needed / sizeof (HMODULE); i++)
195 if ((p = GetProcAddress (modules[i], symbol_name)) != NULL)
196 break;
198 g_free (modules);
200 return p;
203 static gpointer
204 find_in_any_module (const gchar *symbol_name)
206 gpointer result;
208 if ((result = find_in_any_module_using_toolhelp (symbol_name)) == NULL
209 && (result = find_in_any_module_using_psapi (symbol_name)) == NULL)
210 return NULL;
211 else
212 return result;
215 static gpointer
216 _g_module_symbol (gpointer handle,
217 const gchar *symbol_name)
219 gpointer p;
221 if (handle == null_module_handle)
223 if ((p = GetProcAddress (GetModuleHandle (NULL), symbol_name)) == NULL)
224 p = find_in_any_module (symbol_name);
226 else
227 p = GetProcAddress (handle, symbol_name);
229 if (!p)
230 set_error ();
232 return p;
235 static gchar*
236 _g_module_build_path (const gchar *directory,
237 const gchar *module_name)
239 gint k;
241 k = strlen (module_name);
243 if (directory && *directory)
244 if (k > 4 && g_ascii_strcasecmp (module_name + k - 4, ".dll") == 0)
245 return g_strconcat (directory, G_DIR_SEPARATOR_S, module_name, NULL);
246 #ifdef G_WITH_CYGWIN
247 else if (strncmp (module_name, "lib", 3) == 0 || strncmp (module_name, "cyg", 3) == 0)
248 return g_strconcat (directory, G_DIR_SEPARATOR_S, module_name, ".dll", NULL);
249 else
250 return g_strconcat (directory, G_DIR_SEPARATOR_S, "cyg", module_name, ".dll", NULL);
251 #else
252 else if (strncmp (module_name, "lib", 3) == 0)
253 return g_strconcat (directory, G_DIR_SEPARATOR_S, module_name, ".dll", NULL);
254 else
255 return g_strconcat (directory, G_DIR_SEPARATOR_S, "lib", module_name, ".dll", NULL);
256 #endif
257 else if (k > 4 && g_ascii_strcasecmp (module_name + k - 4, ".dll") == 0)
258 return g_strdup (module_name);
259 #ifdef G_WITH_CYGWIN
260 else if (strncmp (module_name, "lib", 3) == 0 || strncmp (module_name, "cyg", 3) == 0)
261 return g_strconcat (module_name, ".dll", NULL);
262 else
263 return g_strconcat ("cyg", module_name, ".dll", NULL);
264 #else
265 else if (strncmp (module_name, "lib", 3) == 0)
266 return g_strconcat (module_name, ".dll", NULL);
267 else
268 return g_strconcat ("lib", module_name, ".dll", NULL);
269 #endif