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