applied patch from Andreas Persenius <ndap@swipnet.se> that updates the
[platform/upstream/glib.git] / gmodule / gmodule-win32.c
1 /* GMODULE - GLIB wrapper code for dynamic module loading
2  * Copyright (C) 1998, 2000 Tim Janik
3  *
4  * Win32 GMODULE implementation
5  * Copyright (C) 1998 Tor Lillqvist
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /*
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/. 
28  */
29
30 /* 
31  * MT safe
32  */
33
34 #include <stdio.h>
35 #include <windows.h>
36
37 #ifdef __MSVC__
38 #include <tlhelp32.h>
39 #else
40
41 /* The w32api headers supplied with the mingw gcc don't have
42  * tlhelp32.h. We really only need the MODULEENTRY32 struct and the
43  * TH32CS_SNAPMODULE value, so provide them here.
44  */
45
46 #define MAX_MODULE_NAME32 255
47
48 typedef struct
49 {
50   DWORD dwSize;
51   DWORD th32ModuleID;
52   DWORD th32ProcessID;
53   DWORD GlblcntUsage;
54   DWORD ProccntUsage;
55   BYTE  *modBaseAddr; 
56   DWORD modBaseSize; 
57   HMODULE hModule;     
58   char szModule[MAX_MODULE_NAME32 + 1];
59   char szExePath[MAX_PATH];
60 } MODULEENTRY32;
61 #define TH32CS_SNAPMODULE 8
62
63 #endif
64
65 static void
66 set_error (void)
67 {
68   gchar *error = g_win32_error_message (GetLastError ());
69
70   g_module_set_error (error);
71   g_free (error);
72 }
73
74 /* --- functions --- */
75 static gpointer
76 _g_module_open (const gchar *file_name,
77                 gboolean     bind_lazy)
78 {
79   HINSTANCE handle;
80   
81   handle = LoadLibrary (file_name);
82   if (!handle)
83     set_error ();
84
85   return handle;
86 }
87
88 static gint dummy;
89 static gpointer null_module_handle = &dummy;
90   
91 static gpointer
92 _g_module_self (void)
93 {
94   return null_module_handle;
95 }
96
97 static void
98 _g_module_close (gpointer handle,
99                  gboolean is_unref)
100 {
101   if (handle != null_module_handle)
102     if (!FreeLibrary (handle))
103       set_error ();
104 }
105
106 static gpointer
107 find_in_any_module_using_toolhelp (const gchar *symbol_name)
108 {
109   typedef HANDLE (WINAPI *PFNCREATETOOLHELP32SNAPSHOT)(DWORD, DWORD);
110   static PFNCREATETOOLHELP32SNAPSHOT pfnCreateToolhelp32Snapshot = NULL;
111
112   typedef BOOL (WINAPI *PFNMODULE32FIRST)(HANDLE, MODULEENTRY32*);
113   static PFNMODULE32FIRST pfnModule32First= NULL;
114
115   typedef BOOL (WINAPI *PFNMODULE32NEXT)(HANDLE, MODULEENTRY32*);
116   static PFNMODULE32NEXT pfnModule32Next = NULL;
117
118   static HMODULE kernel32;
119
120   HANDLE snapshot; 
121   MODULEENTRY32 me32;
122
123   gpointer p;
124
125   if (!pfnCreateToolhelp32Snapshot || !pfnModule32First || !pfnModule32Next)
126     {
127       if (!kernel32)
128         if (!(kernel32 = GetModuleHandle ("kernel32.dll")))
129           return NULL;
130
131       if (!(pfnCreateToolhelp32Snapshot = (PFNCREATETOOLHELP32SNAPSHOT) GetProcAddress (kernel32, "CreateToolhelp32Snapshot"))
132           || !(pfnModule32First = (PFNMODULE32FIRST) GetProcAddress (kernel32, "Module32First"))
133           || !(pfnModule32Next = (PFNMODULE32NEXT) GetProcAddress (kernel32, "Module32Next")))
134         return NULL;
135     }
136
137   if ((snapshot = (*pfnCreateToolhelp32Snapshot) (TH32CS_SNAPMODULE, 0)) == (HANDLE) -1)
138     return NULL;
139
140   me32.dwSize = sizeof (me32);
141   p = NULL;
142   if ((*pfnModule32First) (snapshot, &me32))
143     {
144       do {
145         if ((p = GetProcAddress (me32.hModule, symbol_name)) != NULL)
146           break;
147       } while ((*pfnModule32Next) (snapshot, &me32));
148     }
149
150   CloseHandle (snapshot);
151
152   return p;
153 }
154
155 static gpointer
156 find_in_any_module_using_psapi (const gchar *symbol_name)
157 {
158   static HMODULE psapi = NULL;
159
160   typedef BOOL (WINAPI *PFNENUMPROCESSMODULES) (HANDLE, HMODULE *, DWORD, LPDWORD) ;
161   static PFNENUMPROCESSMODULES pfnEnumProcessModules = NULL;
162
163   HMODULE *modules;
164   HMODULE dummy;
165   gint i, size;
166   DWORD needed;
167   
168   gpointer p;
169
170   if (!pfnEnumProcessModules)
171     {
172       if (!psapi)
173         if ((psapi = LoadLibrary ("psapi.dll")) == NULL)
174           return NULL;
175
176       if (!(pfnEnumProcessModules = (PFNENUMPROCESSMODULES) GetProcAddress (psapi, "EnumProcessModules")))
177         return NULL;
178     }
179
180   if (!(*pfnEnumProcessModules) (GetCurrentProcess (), &dummy,
181                                  sizeof (HMODULE), &needed))
182     return NULL;
183
184   size = needed + 10 * sizeof (HMODULE);
185   modules = g_malloc (size);
186
187   if (!(*pfnEnumProcessModules) (GetCurrentProcess (), modules,
188                                  size, &needed)
189       || needed > size)
190     {
191       g_free (modules);
192       return NULL;
193     }
194   
195   p = NULL;
196   for (i = 0; i < needed / sizeof (HMODULE); i++)
197     if ((p = GetProcAddress (modules[i], symbol_name)) != NULL)
198       break;
199
200   g_free (modules);
201
202   return p;
203 }
204
205 static gpointer
206 find_in_any_module (const gchar *symbol_name)
207 {
208   gpointer result;
209
210   if ((result = find_in_any_module_using_toolhelp (symbol_name)) == NULL
211       && (result = find_in_any_module_using_psapi (symbol_name)) == NULL)
212     return NULL;
213   else
214     return result;
215 }
216
217 static gpointer
218 _g_module_symbol (gpointer     handle,
219                   const gchar *symbol_name)
220 {
221   gpointer p;
222   
223   if (handle == null_module_handle)
224     {
225       if ((p = GetProcAddress (GetModuleHandle (NULL), symbol_name)) == NULL)
226         p = find_in_any_module (symbol_name);
227     }
228   else
229     p = GetProcAddress (handle, symbol_name);
230
231   if (!p)
232     set_error ();
233
234   return p;
235 }
236
237 static gchar*
238 _g_module_build_path (const gchar *directory,
239                       const gchar *module_name)
240 {
241   gint k;
242   
243   k = strlen (module_name);
244   if (directory && *directory)
245     if (k > 4 && g_strcasecmp (module_name + k - 4, ".dll") == 0)
246       return g_strconcat (directory, "\\", module_name, NULL);
247     else
248       return g_strconcat (directory, "\\", module_name, ".dll", NULL);
249   else if (k > 4 && g_strcasecmp (module_name + k - 4, ".dll") == 0)
250     return g_strdup (module_name);
251   else
252     return g_strconcat (module_name, ".dll", NULL);
253 }