Document the members of GOnce.
[glib.git] / gmodule / gmodule-win32.c
blobebf0cbb55e75850f0a378249d3a6a71481fa1970
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)
57 HINSTANCE handle;
58 #ifdef G_WITH_CYGWIN
59 gchar tmp[MAX_PATH];
61 cygwin_conv_to_win32_path(file_name, tmp);
62 file_name = tmp;
63 #endif
65 handle = LoadLibrary (file_name);
66 if (!handle)
67 set_error ();
69 return handle;
72 static gint dummy;
73 static gpointer null_module_handle = &dummy;
75 static gpointer
76 _g_module_self (void)
78 return null_module_handle;
81 static void
82 _g_module_close (gpointer handle,
83 gboolean is_unref)
85 if (handle != null_module_handle)
86 if (!FreeLibrary (handle))
87 set_error ();
90 static gpointer
91 find_in_any_module_using_toolhelp (const gchar *symbol_name)
93 typedef HANDLE (WINAPI *PFNCREATETOOLHELP32SNAPSHOT)(DWORD, DWORD);
94 static PFNCREATETOOLHELP32SNAPSHOT pfnCreateToolhelp32Snapshot = NULL;
96 typedef BOOL (WINAPI *PFNMODULE32FIRST)(HANDLE, MODULEENTRY32*);
97 static PFNMODULE32FIRST pfnModule32First= NULL;
99 typedef BOOL (WINAPI *PFNMODULE32NEXT)(HANDLE, MODULEENTRY32*);
100 static PFNMODULE32NEXT pfnModule32Next = NULL;
102 static HMODULE kernel32;
104 HANDLE snapshot;
105 MODULEENTRY32 me32;
107 gpointer p;
109 if (!pfnCreateToolhelp32Snapshot || !pfnModule32First || !pfnModule32Next)
111 if (!kernel32)
112 if (!(kernel32 = GetModuleHandle ("kernel32.dll")))
113 return NULL;
115 if (!(pfnCreateToolhelp32Snapshot = (PFNCREATETOOLHELP32SNAPSHOT) GetProcAddress (kernel32, "CreateToolhelp32Snapshot"))
116 || !(pfnModule32First = (PFNMODULE32FIRST) GetProcAddress (kernel32, "Module32First"))
117 || !(pfnModule32Next = (PFNMODULE32NEXT) GetProcAddress (kernel32, "Module32Next")))
118 return NULL;
121 if ((snapshot = (*pfnCreateToolhelp32Snapshot) (TH32CS_SNAPMODULE, 0)) == (HANDLE) -1)
122 return NULL;
124 me32.dwSize = sizeof (me32);
125 p = NULL;
126 if ((*pfnModule32First) (snapshot, &me32))
128 do {
129 if ((p = GetProcAddress (me32.hModule, symbol_name)) != NULL)
130 break;
131 } while ((*pfnModule32Next) (snapshot, &me32));
134 CloseHandle (snapshot);
136 return p;
139 static gpointer
140 find_in_any_module_using_psapi (const gchar *symbol_name)
142 static HMODULE psapi = NULL;
144 typedef BOOL (WINAPI *PFNENUMPROCESSMODULES) (HANDLE, HMODULE *, DWORD, LPDWORD) ;
145 static PFNENUMPROCESSMODULES pfnEnumProcessModules = NULL;
147 HMODULE *modules;
148 HMODULE dummy;
149 gint i, size;
150 DWORD needed;
152 gpointer p;
154 if (!pfnEnumProcessModules)
156 if (!psapi)
157 if ((psapi = LoadLibrary ("psapi.dll")) == NULL)
158 return NULL;
160 if (!(pfnEnumProcessModules = (PFNENUMPROCESSMODULES) GetProcAddress (psapi, "EnumProcessModules")))
161 return NULL;
164 if (!(*pfnEnumProcessModules) (GetCurrentProcess (), &dummy,
165 sizeof (HMODULE), &needed))
166 return NULL;
168 size = needed + 10 * sizeof (HMODULE);
169 modules = g_malloc (size);
171 if (!(*pfnEnumProcessModules) (GetCurrentProcess (), modules,
172 size, &needed)
173 || needed > size)
175 g_free (modules);
176 return NULL;
179 p = NULL;
180 for (i = 0; i < needed / sizeof (HMODULE); i++)
181 if ((p = GetProcAddress (modules[i], symbol_name)) != NULL)
182 break;
184 g_free (modules);
186 return p;
189 static gpointer
190 find_in_any_module (const gchar *symbol_name)
192 gpointer result;
194 if ((result = find_in_any_module_using_toolhelp (symbol_name)) == NULL
195 && (result = find_in_any_module_using_psapi (symbol_name)) == NULL)
196 return NULL;
197 else
198 return result;
201 static gpointer
202 _g_module_symbol (gpointer handle,
203 const gchar *symbol_name)
205 gpointer p;
207 if (handle == null_module_handle)
209 if ((p = GetProcAddress (GetModuleHandle (NULL), symbol_name)) == NULL)
210 p = find_in_any_module (symbol_name);
212 else
213 p = GetProcAddress (handle, symbol_name);
215 if (!p)
216 set_error ();
218 return p;
221 static gchar*
222 _g_module_build_path (const gchar *directory,
223 const gchar *module_name)
225 gint k;
227 k = strlen (module_name);
229 if (directory && *directory)
230 if (k > 4 && g_ascii_strcasecmp (module_name + k - 4, ".dll") == 0)
231 return g_strconcat (directory, G_DIR_SEPARATOR_S, module_name, NULL);
232 else if (strncmp (module_name, "lib", 3) == 0)
233 return g_strconcat (directory, G_DIR_SEPARATOR_S, module_name, ".dll", NULL);
234 else
235 return g_strconcat (directory, G_DIR_SEPARATOR_S, "lib", module_name, ".dll", NULL);
236 else if (k > 4 && g_ascii_strcasecmp (module_name + k - 4, ".dll") == 0)
237 return g_strdup (module_name);
238 else if (strncmp (module_name, "lib", 3) == 0)
239 return g_strconcat (module_name, ".dll", NULL);
240 else
241 return g_strconcat ("lib", module_name, ".dll", NULL);