2 * gmodule.c: dl* functions, glib style
5 * Gonzalo Paniagua Javier (gonzalo@novell.com)
6 * Jonathan Chambers (joncham@gmail.com)
7 * Robert Jordan (robertj@gmx.net)
9 * (C) 2006 Novell, Inc.
10 * (C) 2006 Jonathan Chambers
12 * Permission is hereby granted, free of charge, to any person obtaining
13 * a copy of this software and associated documentation files (the
14 * "Software"), to deal in the Software without restriction, including
15 * without limitation the rights to use, copy, modify, merge, publish,
16 * distribute, sublicense, and/or sell copies of the Software, and to
17 * permit persons to whom the Software is furnished to do so, subject to
18 * the following conditions:
20 * The above copyright notice and this permission notice shall be
21 * included in all copies or substantial portions of the Software.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 #define PSAPI_VERSION 2 // Use the Windows 7 or newer version more directly.
38 #include <gmodule-win32-internals.h>
40 #define LIBSUFFIX ".dll"
49 g_module_open (const gchar
*file
, GModuleFlags flags
)
52 module
= g_malloc (sizeof (GModule
));
58 file16
= u8to16(file
);
59 module
->main_module
= FALSE
;
60 module
->handle
= LoadLibraryW (file16
);
62 if (!module
->handle
) {
68 module
->main_module
= TRUE
;
69 module
->handle
= GetModuleHandle (NULL
);
75 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
77 w32_find_symbol (const gchar
*symbol_name
)
80 DWORD buffer_size
= sizeof (HMODULE
) * 1024;
83 modules
= (HMODULE
*) g_malloc (buffer_size
);
88 if (!EnumProcessModules (GetCurrentProcess (), modules
,
89 buffer_size
, &needed
)) {
94 /* check whether the supplied buffer was too small, realloc, retry */
95 if (needed
> buffer_size
) {
99 modules
= (HMODULE
*) g_malloc (buffer_size
);
104 if (!EnumProcessModules (GetCurrentProcess (), modules
,
105 buffer_size
, &needed
)) {
111 for (i
= 0; i
< needed
/ sizeof (HANDLE
); i
++) {
112 gpointer proc
= (gpointer
)(intptr_t)GetProcAddress (modules
[i
], symbol_name
);
122 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
125 g_module_symbol (GModule
*module
, const gchar
*symbol_name
, gpointer
*symbol
)
127 if (module
== NULL
|| symbol_name
== NULL
|| symbol
== NULL
)
130 if (module
->main_module
) {
131 *symbol
= (gpointer
)(intptr_t)GetProcAddress (module
->handle
, symbol_name
);
135 *symbol
= w32_find_symbol (symbol_name
);
136 return *symbol
!= NULL
;
138 *symbol
= (gpointer
)(intptr_t)GetProcAddress (module
->handle
, symbol_name
);
139 return *symbol
!= NULL
;
144 g_module_address (void *addr
, char *file_name
, size_t file_name_len
,
145 void **file_base
, char *sym_name
, size_t sym_name_len
,
150 * We have to cast the address because usually this func works with strings,
151 * this being an exception.
153 BOOL ret
= GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
, (LPCWSTR
)addr
, &module
);
157 if (file_name
!= NULL
&& file_name_len
>= 1) {
158 /* sigh, non-const. AIX for POSIX is the same way. */
159 TCHAR
*fname
= (TCHAR
*)g_alloca(255);
160 DWORD bytes
= GetModuleFileName(module
, fname
, 255);
161 /* XXX: check for ERROR_INSUFFICIENT_BUFFER? */
163 /* Convert back to UTF-8 from wide for runtime */
164 *file_name
= '\0'; /* XXX */
169 /* XXX: implement the rest */
170 if (file_base
!= NULL
)
172 if (sym_name
!= NULL
&& sym_name_len
>= 1)
174 if (sym_addr
!= NULL
)
177 /* -1 reference count to avoid leaks; Ex variant does +1 refcount */
178 FreeLibrary (module
);
182 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
184 g_module_error (void)
188 DWORD code
= GetLastError ();
190 /* FIXME: buf must not be NULL! */
191 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ALLOCATE_BUFFER
, NULL
,
192 code
, MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), buf
, 0, NULL
);
199 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
202 g_module_close (GModule
*module
)
207 if (module
== NULL
|| module
->handle
== NULL
)
210 handle
= module
->handle
;
211 main_module
= module
->main_module
;
212 module
->handle
= NULL
;
214 return (main_module
? 1 : (0 == FreeLibrary (handle
)));
218 g_module_build_path (const gchar
*directory
, const gchar
*module_name
)
220 const char *lib_prefix
= "";
222 if (module_name
== NULL
)
225 if (strncmp (module_name
, "lib", 3) != 0)
226 lib_prefix
= LIBPREFIX
;
228 if (directory
&& *directory
){
230 return g_strdup_printf ("%s/%s%s" LIBSUFFIX
, directory
, lib_prefix
, module_name
);
232 return g_strdup_printf ("%s%s" LIBSUFFIX
, lib_prefix
, module_name
);
235 // This is not about GModule but is still a close fit.
236 // This is not named "g_" but that should be ok.
238 // No MAX_PATH limit.
240 // Prefer mono_get_module_filename over mono_get_module_filename_ex and mono_get_module_basename.
241 // Prefer not-ex, not-base.
244 mono_get_module_filename (gpointer mod
, gunichar2
**pstr
, guint32
*plength
)
246 gunichar2
*str
= NULL
;
247 guint32 capacity
= MAX_PATH
; // tunable
249 gboolean success
= FALSE
;
254 if (capacity
> (1 << 24))
256 str
= g_new (gunichar2
, capacity
);
259 length
= GetModuleFileNameW ((HMODULE
)mod
, str
, capacity
);
260 success
= length
&& length
< (capacity
- 1); // This function does not truncate, but - 1 anyway.
263 g_free (str
); // error or too small
265 if (!length
) // error
274 // This is not about GModule but is still a close fit.
275 // This is not named "g_" but that should be ok.
277 // No MAX_PATH limit.
279 // Prefer mono_get_module_filename over mono_get_module_filename_ex and mono_get_module_basename.
280 // Prefer not-ex, not-base.
283 mono_get_module_filename_ex (gpointer process
, gpointer mod
, gunichar2
**pstr
, guint32
*plength
)
285 gunichar2
*str
= NULL
;
286 guint32 capacity
= MAX_PATH
; // tunable
288 gboolean success
= FALSE
;
293 if (capacity
> (1 << 24))
295 str
= g_new (gunichar2
, capacity
);
298 length
= GetModuleFileNameExW (process
, (HMODULE
)mod
, str
, capacity
);
299 success
= length
&& length
< (capacity
- 1); // This function truncates, thus the - 1.
302 g_free (str
); // error or too small
304 if (!length
) // error
313 // This is not about GModule but is still a close fit.
314 // This is not named "g_" but that should be ok.
316 // No MAX_PATH limit.
318 // Prefer mono_get_module_filename over mono_get_module_filename_ex and mono_get_module_basename.
319 // Prefer not-ex, not-base.
322 mono_get_module_basename (gpointer process
, gpointer mod
, gunichar2
**pstr
, guint32
*plength
)
324 gunichar2
*str
= NULL
;
325 guint32 capacity
= MAX_PATH
; // tunable
327 gboolean success
= FALSE
;
332 if (capacity
> (1 << 24))
334 str
= g_new (gunichar2
, capacity
);
337 length
= GetModuleBaseNameW (process
, (HMODULE
)mod
, str
, capacity
);
338 success
= length
&& length
< (capacity
- 1); // This function truncates, thus the - 1.
341 g_free (str
); // error or too small
343 if (!length
) // error
353 // No MAX_PATH limit.
355 mono_get_current_directory (gunichar2
**pstr
, guint32
*plength
)
357 gunichar2
*str
= NULL
;
358 guint32 capacity
= MAX_PATH
; // tunable
360 gboolean success
= FALSE
;
365 if (capacity
> (1 << 24))
367 str
= g_new (gunichar2
, capacity
);
370 // Call in loop, not just twice, in case another thread is changing it.
371 // Result is transient in currentness and validity (can get deleted or become a file).
372 length
= GetCurrentDirectoryW (capacity
, str
);
373 success
= length
&& length
< (capacity
- 1);
376 g_free (str
); // error or too small
378 if (!length
) // error