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