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.
12 #if defined(HOST_WIN32)
14 #include "mono/utils/mono-dl.h"
15 #include "mono/utils/mono-embed.h"
16 #include "mono/utils/mono-path.h"
29 mono_dl_get_so_prefix (void)
35 mono_dl_get_so_suffixes (void)
37 static const char *suffixes[] = {
45 mono_dl_open_file (const char *file, int flags)
47 gpointer hModule = NULL;
49 gunichar2* file_utf16 = g_utf8_to_utf16 (file, strlen (file), NULL, NULL, NULL);
50 guint last_sem = SetErrorMode (SEM_FAILCRITICALERRORS);
51 guint32 last_error = 0;
53 hModule = LoadLibrary (file_utf16);
55 last_error = GetLastError ();
57 SetErrorMode (last_sem);
61 SetLastError (last_error);
63 hModule = GetModuleHandle (NULL);
69 mono_dl_close_handle (MonoDl *module)
71 if (!module->main_module)
72 FreeLibrary (module->handle);
76 mono_dl_lookup_symbol (MonoDl *module, const char *symbol_name)
79 DWORD buffer_size = sizeof (HMODULE) * 1024;
83 /* get the symbol directly from the specified module */
84 if (!module->main_module)
85 return GetProcAddress (module->handle, symbol_name);
87 /* get the symbol from the main module */
88 proc = GetProcAddress (module->handle, symbol_name);
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);
133 mono_dl_convert_flags (int flags)
139 mono_dl_current_error_string (void)
143 DWORD code = GetLastError ();
145 if (FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
146 code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&buf, 0, NULL))
148 ret = g_utf16_to_utf8 (buf, wcslen(buf), NULL, NULL, NULL);
151 g_assert_not_reached ();
157 mono_dl_get_executable_path (char *buf, int buflen)
163 mono_dl_get_system_dir (void)