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-embed.h"
17 #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);
51 guint last_sem = SetErrorMode (SEM_FAILCRITICALERRORS);
52 guint32 last_error = 0;
54 hModule = LoadLibrary (file_utf16);
56 last_error = GetLastError ();
58 SetErrorMode (last_sem);
62 SetLastError (last_error);
64 hModule = GetModuleHandle (NULL);
70 mono_dl_close_handle (MonoDl *module)
72 if (!module->main_module)
73 FreeLibrary (module->handle);
77 mono_dl_lookup_symbol (MonoDl *module, const char *symbol_name)
80 DWORD buffer_size = sizeof (HMODULE) * 1024;
84 /* get the symbol directly from the specified module */
85 if (!module->main_module)
86 return GetProcAddress (module->handle, symbol_name);
88 /* get the symbol from the main module */
89 proc = GetProcAddress (module->handle, symbol_name);
93 /* get the symbol from the loaded DLLs */
94 modules = (HMODULE *) g_malloc (buffer_size);
98 if (!EnumProcessModules (GetCurrentProcess (), modules,
99 buffer_size, &needed)) {
104 /* check whether the supplied buffer was too small, realloc, retry */
105 if (needed > buffer_size) {
108 buffer_size = needed;
109 modules = (HMODULE *) g_malloc (buffer_size);
114 if (!EnumProcessModules (GetCurrentProcess (), modules,
115 buffer_size, &needed)) {
121 for (i = 0; i < needed / sizeof (HANDLE); i++) {
122 proc = GetProcAddress (modules [i], symbol_name);
134 mono_dl_convert_flags (int flags)
140 mono_dl_current_error_string (void)
144 DWORD code = GetLastError ();
146 if (FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
147 code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&buf, 0, NULL))
149 ret = g_utf16_to_utf8 (buf, wcslen(buf), NULL, NULL, NULL);
152 g_assert_not_reached ();
158 mono_dl_get_executable_path (char *buf, int buflen)
164 mono_dl_get_system_dir (void)