3 * Interface to the dynamic linker
6 * Mono Team (http://www.mono-project.com)
8 * Copyright 2001-2004 Ximian, Inc.
9 * Copyright 2004-2009 Novell, Inc.
10 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
14 #if defined(HOST_WIN32)
16 #include "mono/utils/mono-dl.h"
17 #include "mono/utils/mono-dl-windows-internals.h"
18 #include "mono/utils/mono-embed.h"
19 #include "mono/utils/mono-path.h"
31 mono_dl_get_so_prefix (void)
37 mono_dl_get_so_suffixes (void)
39 static const char *suffixes[] = {
47 mono_dl_open_file (const char *file, int flags)
49 gpointer hModule = NULL;
51 gunichar2* file_utf16 = g_utf8_to_utf16 (file, strlen (file), NULL, NULL, NULL);
53 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
54 guint last_sem = SetErrorMode (SEM_FAILCRITICALERRORS);
56 guint32 last_error = 0;
58 hModule = LoadLibrary (file_utf16);
60 last_error = GetLastError ();
62 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
63 SetErrorMode (last_sem);
69 SetLastError (last_error);
71 hModule = GetModuleHandle (NULL);
77 mono_dl_close_handle (MonoDl *module)
79 if (!module->main_module)
80 FreeLibrary (module->handle);
83 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
85 mono_dl_lookup_symbol_in_process (const char *symbol_name)
88 DWORD buffer_size = sizeof (HMODULE) * 1024;
92 /* get the symbol from the loaded DLLs */
93 modules = (HMODULE *) g_malloc (buffer_size);
97 if (!EnumProcessModules (GetCurrentProcess (), modules,
98 buffer_size, &needed)) {
103 /* check whether the supplied buffer was too small, realloc, retry */
104 if (needed > buffer_size) {
107 buffer_size = needed;
108 modules = (HMODULE *) g_malloc (buffer_size);
113 if (!EnumProcessModules (GetCurrentProcess (), modules,
114 buffer_size, &needed)) {
120 for (i = 0; i < needed / sizeof (HANDLE); i++) {
121 proc = GetProcAddress (modules [i], symbol_name);
131 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
134 mono_dl_lookup_symbol (MonoDl *module, const char *symbol_name)
136 gpointer proc = NULL;
138 /* get the symbol directly from the specified module */
139 if (!module->main_module)
140 return GetProcAddress (module->handle, symbol_name);
142 /* get the symbol from the main module */
143 proc = GetProcAddress (module->handle, symbol_name);
147 /* get the symbol from the loaded DLLs */
148 return mono_dl_lookup_symbol_in_process (symbol_name);
152 mono_dl_convert_flags (int flags)
157 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
159 mono_dl_current_error_string (void)
163 DWORD code = GetLastError ();
165 if (FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
166 code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&buf, 0, NULL))
168 ret = g_utf16_to_utf8 (buf, wcslen(buf), NULL, NULL, NULL);
171 g_assert_not_reached ();
175 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
178 mono_dl_get_executable_path (char *buf, int buflen)
184 mono_dl_get_system_dir (void)