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