2 * mono-dl.c: Interface to the dynamic linker
5 * Mono Team (http://www.mono-project.com)
7 * Copyright 2001-2004 Ximian, Inc.
8 * Copyright 2004-2009 Novell, Inc.
9 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13 #if defined(HOST_WIN32)
15 #include "mono/utils/mono-dl.h"
16 #include "mono/utils/mono-dl-windows.h"
17 #include "mono/utils/mono-embed.h"
18 #include "mono/utils/mono-path.h"
30 mono_dl_get_so_prefix (void)
36 mono_dl_get_so_suffixes (void)
38 static const char *suffixes[] = {
46 mono_dl_open_file (const char *file, int flags)
48 gpointer hModule = NULL;
50 gunichar2* file_utf16 = g_utf8_to_utf16 (file, strlen (file), NULL, NULL, NULL);
52 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
53 guint last_sem = SetErrorMode (SEM_FAILCRITICALERRORS);
55 guint32 last_error = 0;
57 hModule = LoadLibrary (file_utf16);
59 last_error = GetLastError ();
61 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
62 SetErrorMode (last_sem);
68 SetLastError (last_error);
70 hModule = GetModuleHandle (NULL);
76 mono_dl_close_handle (MonoDl *module)
78 if (!module->main_module)
79 FreeLibrary (module->handle);
82 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
84 mono_dl_lookup_symbol_in_process (const char *symbol_name)
87 DWORD buffer_size = sizeof (HMODULE) * 1024;
91 /* get the symbol from the loaded DLLs */
92 modules = (HMODULE *) g_malloc (buffer_size);
96 if (!EnumProcessModules (GetCurrentProcess (), modules,
97 buffer_size, &needed)) {
102 /* check whether the supplied buffer was too small, realloc, retry */
103 if (needed > buffer_size) {
106 buffer_size = needed;
107 modules = (HMODULE *) g_malloc (buffer_size);
112 if (!EnumProcessModules (GetCurrentProcess (), modules,
113 buffer_size, &needed)) {
119 for (i = 0; i < needed / sizeof (HANDLE); i++) {
120 proc = GetProcAddress (modules [i], symbol_name);
130 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
133 mono_dl_lookup_symbol (MonoDl *module, const char *symbol_name)
135 gpointer proc = NULL;
137 /* get the symbol directly from the specified module */
138 if (!module->main_module)
139 return GetProcAddress (module->handle, symbol_name);
141 /* get the symbol from the main module */
142 proc = GetProcAddress (module->handle, symbol_name);
146 /* get the symbol from the loaded DLLs */
147 return mono_dl_lookup_symbol_in_process (symbol_name);
151 mono_dl_convert_flags (int flags)
156 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
158 mono_dl_current_error_string (void)
162 DWORD code = GetLastError ();
164 if (FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
165 code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&buf, 0, NULL))
167 ret = g_utf16_to_utf8 (buf, wcslen(buf), NULL, NULL, NULL);
170 g_assert_not_reached ();
174 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
177 mono_dl_get_executable_path (char *buf, int buflen)
183 mono_dl_get_system_dir (void)