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