Remove support for Windows 9x/ME, as will be done also in Pango and GTK+.
[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 #include <tlhelp32.h>
38
39 #ifdef G_WITH_CYGWIN
40 #include <sys/cygwin.h>
41 #endif
42
43 static void
44 set_error (void)
45 {
46   gchar *error = g_win32_error_message (GetLastError ());
47
48   g_module_set_error (error);
49   g_free (error);
50 }
51
52 /* --- functions --- */
53 static gpointer
54 _g_module_open (const gchar *file_name,
55                 gboolean     bind_lazy,
56                 gboolean     bind_local)
57 {
58   HINSTANCE handle;
59   wchar_t *wfilename;
60 #ifdef G_WITH_CYGWIN
61   gchar tmp[MAX_PATH];
62
63   cygwin_conv_to_win32_path(file_name, tmp);
64   file_name = tmp;
65 #endif
66   wfilename = g_utf8_to_utf16 (file_name, -1, NULL, NULL, NULL);
67
68   handle = LoadLibraryW (wfilename);
69   g_free (wfilename);
70       
71   if (!handle)
72     set_error ();
73
74   return handle;
75 }
76
77 static gint dummy;
78 static gpointer null_module_handle = &dummy;
79   
80 static gpointer
81 _g_module_self (void)
82 {
83   return null_module_handle;
84 }
85
86 static void
87 _g_module_close (gpointer handle,
88                  gboolean is_unref)
89 {
90   if (handle != null_module_handle)
91     if (!FreeLibrary (handle))
92       set_error ();
93 }
94
95 static gpointer
96 find_in_any_module_using_toolhelp (const gchar *symbol_name)
97 {
98   typedef HANDLE (WINAPI *PFNCREATETOOLHELP32SNAPSHOT)(DWORD, DWORD);
99   static PFNCREATETOOLHELP32SNAPSHOT pfnCreateToolhelp32Snapshot = NULL;
100
101   typedef BOOL (WINAPI *PFNMODULE32FIRST)(HANDLE, MODULEENTRY32*);
102   static PFNMODULE32FIRST pfnModule32First= NULL;
103
104   typedef BOOL (WINAPI *PFNMODULE32NEXT)(HANDLE, MODULEENTRY32*);
105   static PFNMODULE32NEXT pfnModule32Next = NULL;
106
107   static HMODULE kernel32;
108
109   HANDLE snapshot; 
110   MODULEENTRY32 me32;
111
112   gpointer p;
113
114   if (!pfnCreateToolhelp32Snapshot || !pfnModule32First || !pfnModule32Next)
115     {
116       if (!kernel32)
117         if (!(kernel32 = GetModuleHandle ("kernel32.dll")))
118           return NULL;
119
120       if (!(pfnCreateToolhelp32Snapshot = (PFNCREATETOOLHELP32SNAPSHOT) GetProcAddress (kernel32, "CreateToolhelp32Snapshot"))
121           || !(pfnModule32First = (PFNMODULE32FIRST) GetProcAddress (kernel32, "Module32First"))
122           || !(pfnModule32Next = (PFNMODULE32NEXT) GetProcAddress (kernel32, "Module32Next")))
123         return NULL;
124     }
125
126   if ((snapshot = (*pfnCreateToolhelp32Snapshot) (TH32CS_SNAPMODULE, 0)) == (HANDLE) -1)
127     return NULL;
128
129   me32.dwSize = sizeof (me32);
130   p = NULL;
131   if ((*pfnModule32First) (snapshot, &me32))
132     {
133       do {
134         if ((p = GetProcAddress (me32.hModule, symbol_name)) != NULL)
135           break;
136       } while ((*pfnModule32Next) (snapshot, &me32));
137     }
138
139   CloseHandle (snapshot);
140
141   return p;
142 }
143
144 static gpointer
145 find_in_any_module_using_psapi (const gchar *symbol_name)
146 {
147   static HMODULE psapi = NULL;
148
149   typedef BOOL (WINAPI *PFNENUMPROCESSMODULES) (HANDLE, HMODULE *, DWORD, LPDWORD) ;
150   static PFNENUMPROCESSMODULES pfnEnumProcessModules = NULL;
151
152   HMODULE *modules;
153   HMODULE dummy;
154   gint i, size;
155   DWORD needed;
156   
157   gpointer p;
158
159   if (!pfnEnumProcessModules)
160     {
161       if (!psapi)
162         if ((psapi = LoadLibrary ("psapi.dll")) == NULL)
163           return NULL;
164
165       if (!(pfnEnumProcessModules = (PFNENUMPROCESSMODULES) GetProcAddress (psapi, "EnumProcessModules")))
166         return NULL;
167     }
168
169   if (!(*pfnEnumProcessModules) (GetCurrentProcess (), &dummy,
170                                  sizeof (HMODULE), &needed))
171     return NULL;
172
173   size = needed + 10 * sizeof (HMODULE);
174   modules = g_malloc (size);
175
176   if (!(*pfnEnumProcessModules) (GetCurrentProcess (), modules,
177                                  size, &needed)
178       || needed > size)
179     {
180       g_free (modules);
181       return NULL;
182     }
183   
184   p = NULL;
185   for (i = 0; i < needed / sizeof (HMODULE); i++)
186     if ((p = GetProcAddress (modules[i], symbol_name)) != NULL)
187       break;
188
189   g_free (modules);
190
191   return p;
192 }
193
194 static gpointer
195 find_in_any_module (const gchar *symbol_name)
196 {
197   gpointer result;
198
199   if ((result = find_in_any_module_using_toolhelp (symbol_name)) == NULL
200       && (result = find_in_any_module_using_psapi (symbol_name)) == NULL)
201     return NULL;
202   else
203     return result;
204 }
205
206 static gpointer
207 _g_module_symbol (gpointer     handle,
208                   const gchar *symbol_name)
209 {
210   gpointer p;
211   
212   if (handle == null_module_handle)
213     {
214       if ((p = GetProcAddress (GetModuleHandle (NULL), symbol_name)) == NULL)
215         p = find_in_any_module (symbol_name);
216     }
217   else
218     p = GetProcAddress (handle, symbol_name);
219
220   if (!p)
221     set_error ();
222
223   return p;
224 }
225
226 static gchar*
227 _g_module_build_path (const gchar *directory,
228                       const gchar *module_name)
229 {
230   gint k;
231
232   k = strlen (module_name);
233     
234   if (directory && *directory)
235     if (k > 4 && g_ascii_strcasecmp (module_name + k - 4, ".dll") == 0)
236       return g_strconcat (directory, G_DIR_SEPARATOR_S, module_name, NULL);
237 #ifdef G_WITH_CYGWIN
238     else if (strncmp (module_name, "lib", 3) == 0 || strncmp (module_name, "cyg", 3) == 0)
239       return g_strconcat (directory, G_DIR_SEPARATOR_S, module_name, ".dll", NULL);
240     else
241       return g_strconcat (directory, G_DIR_SEPARATOR_S, "cyg", module_name, ".dll", NULL);
242 #else
243     else if (strncmp (module_name, "lib", 3) == 0)
244       return g_strconcat (directory, G_DIR_SEPARATOR_S, module_name, ".dll", NULL);
245     else
246       return g_strconcat (directory, G_DIR_SEPARATOR_S, "lib", module_name, ".dll", NULL);
247 #endif
248   else if (k > 4 && g_ascii_strcasecmp (module_name + k - 4, ".dll") == 0)
249     return g_strdup (module_name);
250 #ifdef G_WITH_CYGWIN
251   else if (strncmp (module_name, "lib", 3) == 0 || strncmp (module_name, "cyg", 3) == 0)
252     return g_strconcat (module_name, ".dll", NULL);
253   else
254     return g_strconcat ("cyg", module_name, ".dll", NULL);
255 #else
256   else if (strncmp (module_name, "lib", 3) == 0)
257     return g_strconcat (module_name, ".dll", NULL);
258   else
259     return g_strconcat ("lib", module_name, ".dll", NULL);
260 #endif
261 }