In the "null" module case _g_module_symbol should still first search the main
[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 Library 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  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library 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-1999.  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 #include <psapi.h>
37 #include <tlhelp32.h>
38
39 static void
40 set_error (void)
41 {
42   gchar *error = g_win32_error_message (GetLastError ());
43
44   g_module_set_error (error);
45   g_free (error);
46 }
47
48 /* --- functions --- */
49 static gpointer
50 _g_module_open (const gchar *file_name,
51                 gboolean     bind_lazy)
52 {
53   HINSTANCE handle;
54   
55   handle = LoadLibrary (file_name);
56   if (!handle)
57     set_error ();
58
59   return handle;
60 }
61
62 static gint dummy;
63 static gpointer null_module_handle = &dummy;
64   
65 static gpointer
66 _g_module_self (void)
67 {
68   return null_module_handle;
69 }
70
71 static void
72 _g_module_close (gpointer handle,
73                  gboolean is_unref)
74 {
75   if (handle != null_module_handle)
76     if (!FreeLibrary (handle))
77       set_error ();
78 }
79
80 static gpointer
81 find_in_any_module_using_toolhelp (const gchar *symbol_name)
82 {
83   typedef HANDLE (WINAPI *PFNCREATETOOLHELP32SNAPSHOT)(DWORD, DWORD);
84   static PFNCREATETOOLHELP32SNAPSHOT pfnCreateToolhelp32Snapshot = NULL;
85
86   typedef BOOL (WINAPI *PFNMODULE32FIRST)(HANDLE, LPMODULEENTRY32);
87   static PFNMODULE32FIRST pfnModule32First= NULL;
88
89   typedef BOOL (WINAPI *PFNMODULE32NEXT)(HANDLE, LPMODULEENTRY32);
90   static PFNMODULE32NEXT pfnModule32Next = NULL;
91
92   static HMODULE kernel32;
93
94   HANDLE snapshot; 
95   MODULEENTRY32 me32;
96
97   gpointer p;
98
99   if (!pfnCreateToolhelp32Snapshot || !pfnModule32First || !pfnModule32Next)
100     {
101       if (!kernel32)
102         if (!(kernel32 = GetModuleHandle ("kernel32.dll")))
103           return NULL;
104
105       if (!(pfnCreateToolhelp32Snapshot = (PFNCREATETOOLHELP32SNAPSHOT) GetProcAddress (kernel32, "CreateToolhelp32Snapshot"))
106           || !(pfnModule32First = (PFNMODULE32FIRST) GetProcAddress (kernel32, "Module32First"))
107           || !(pfnModule32Next = (PFNMODULE32NEXT) GetProcAddress (kernel32, "Module32Next")))
108         return NULL;
109     }
110
111   if ((snapshot = (*pfnCreateToolhelp32Snapshot) (TH32CS_SNAPMODULE, 0)) == (HANDLE) -1)
112     return NULL;
113
114   me32.dwSize = sizeof (me32);
115   p = NULL;
116   if ((*pfnModule32First) (snapshot, &me32))
117     {
118       do {
119         if ((p = GetProcAddress (me32.hModule, symbol_name)) != NULL)
120           break;
121       } while ((*pfnModule32Next) (snapshot, &me32));
122     }
123
124   CloseHandle (snapshot);
125
126   return p;
127 }
128
129 static gpointer
130 find_in_any_module_using_psapi (const gchar *symbol_name)
131 {
132   static HMODULE psapi = NULL;
133
134   typedef BOOL (WINAPI *PFNENUMPROCESSMODULES) (HANDLE, HMODULE *, DWORD, LPDWORD) ;
135   static PFNENUMPROCESSMODULES pfnEnumProcessModules = NULL;
136
137   HMODULE *modules;
138   HMODULE dummy;
139   gint i, size;
140   DWORD needed;
141   
142   gpointer p;
143
144   if (!pfnEnumProcessModules)
145     {
146       if (!psapi)
147         if ((psapi = LoadLibrary ("psapi.dll")) == NULL)
148           return NULL;
149
150       if (!(pfnEnumProcessModules = (PFNENUMPROCESSMODULES) GetProcAddress (psapi, "EnumProcessModules")))
151         return NULL;
152     }
153
154   if (!(*pfnEnumProcessModules) (GetCurrentProcess (), &dummy,
155                                  sizeof (HMODULE), &needed))
156     return NULL;
157
158   size = needed + 10 * sizeof (HMODULE);
159   modules = g_malloc (size);
160
161   if (!(*pfnEnumProcessModules) (GetCurrentProcess (), modules,
162                                  size, &needed)
163       || needed > size)
164     {
165       g_free (modules);
166       return NULL;
167     }
168   
169   p = NULL;
170   for (i = 0; i < needed / sizeof (HMODULE); i++)
171     if ((p = GetProcAddress (modules[i], symbol_name)) != NULL)
172       break;
173
174   g_free (modules);
175
176   return p;
177 }
178
179 static gpointer
180 find_in_any_module (const gchar *symbol_name)
181 {
182   gpointer result;
183
184   if ((result = find_in_any_module_using_toolhelp (symbol_name)) == NULL
185       && (result = find_in_any_module_using_psapi (symbol_name)) == NULL)
186     return NULL;
187   else
188     return result;
189 }
190
191 static gpointer
192 _g_module_symbol (gpointer     handle,
193                   const gchar *symbol_name)
194 {
195   gpointer p;
196   
197   if (handle == null_module_handle)
198     {
199       if ((p = GetProcAddress (GetModuleHandle (NULL), symbol_name)) == NULL)
200         p = find_in_any_module (symbol_name);
201     }
202   else
203     p = GetProcAddress (handle, symbol_name);
204
205   if (!p)
206     set_error ();
207
208   return p;
209 }
210
211 static gchar*
212 _g_module_build_path (const gchar *directory,
213                       const gchar *module_name)
214 {
215   gint k;
216   
217   k = strlen (module_name);
218   if (directory && *directory)
219     if (k > 4 && g_strcasecmp (module_name + k - 4, ".dll") == 0)
220       return g_strconcat (directory, "\\", module_name, NULL);
221     else
222       return g_strconcat (directory, "\\", module_name, ".dll", NULL);
223   else if (k > 4 && g_strcasecmp (module_name + k - 4, ".dll") == 0)
224     return g_strdup (module_name);
225   else
226     return g_strconcat (module_name, ".dll", NULL);
227 }