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"
20 #include "mono/metadata/w32subset.h"
32 mono_dl_get_so_prefix (void)
38 mono_dl_get_so_suffixes (void)
40 static const char *suffixes
[] = {
48 mono_dl_open_file (const char *file
, int flags
)
50 gpointer hModule
= NULL
;
52 gunichar2
* file_utf16
= g_utf8_to_utf16 (file
, strlen (file
), NULL
, NULL
, NULL
);
54 #if HAVE_API_SUPPORT_WIN32_SET_ERROR_MODE
55 guint last_sem
= SetErrorMode (SEM_FAILCRITICALERRORS
);
57 guint32 last_error
= 0;
59 #if HAVE_API_SUPPORT_WIN32_LOAD_LIBRARY
60 hModule
= LoadLibraryW (file_utf16
);
61 #elif HAVE_API_SUPPORT_WIN32_LOAD_PACKAGED_LIBRARY
62 hModule
= LoadPackagedLibrary (file_utf16
, NULL
);
64 g_assert_not_reached ();
67 last_error
= GetLastError ();
69 #if HAVE_API_SUPPORT_WIN32_SET_ERROR_MODE
70 SetErrorMode (last_sem
);
76 SetLastError (last_error
);
78 #if HAVE_API_SUPPORT_WIN32_GET_MODULE_HANDLE
79 hModule
= GetModuleHandleW (NULL
);
81 g_assert_not_reached ();
88 mono_dl_close_handle (MonoDl
*module
)
90 if (!module
->main_module
)
91 FreeLibrary ((HMODULE
)module
->handle
);
94 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
96 mono_dl_lookup_symbol_in_process (const char *symbol_name
)
99 DWORD buffer_size
= sizeof (HMODULE
) * 1024;
101 gpointer proc
= NULL
;
103 /* get the symbol from the loaded DLLs */
104 modules
= (HMODULE
*) g_malloc (buffer_size
);
108 if (!EnumProcessModules (GetCurrentProcess (), modules
,
109 buffer_size
, &needed
)) {
114 /* check whether the supplied buffer was too small, realloc, retry */
115 if (needed
> buffer_size
) {
118 buffer_size
= needed
;
119 modules
= (HMODULE
*) g_malloc (buffer_size
);
124 if (!EnumProcessModules (GetCurrentProcess (), modules
,
125 buffer_size
, &needed
)) {
131 for (i
= 0; i
< needed
/ sizeof (HANDLE
); i
++) {
132 proc
= (gpointer
)GetProcAddress (modules
[i
], symbol_name
);
142 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
145 mono_dl_lookup_symbol (MonoDl
*module
, const char *symbol_name
)
147 gpointer proc
= NULL
;
149 /* get the symbol directly from the specified module */
150 if (!module
->main_module
)
151 return (void*)GetProcAddress ((HMODULE
)module
->handle
, symbol_name
);
153 /* get the symbol from the main module */
154 proc
= (gpointer
)GetProcAddress ((HMODULE
)module
->handle
, symbol_name
);
158 /* get the symbol from the loaded DLLs */
159 return mono_dl_lookup_symbol_in_process (symbol_name
);
163 mono_dl_convert_flags (int flags
)
168 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
170 mono_dl_current_error_string (void)
174 DWORD code
= GetLastError ();
176 if (FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_IGNORE_INSERTS
, NULL
,
177 code
, MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), (LPTSTR
)&buf
, 0, NULL
))
179 ret
= g_utf16_to_utf8 (buf
, wcslen(buf
), NULL
, NULL
, NULL
);
182 g_assert_not_reached ();
186 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
189 mono_dl_get_executable_path (char *buf
, int buflen
)
195 mono_dl_get_system_dir (void)
202 #include <mono/utils/mono-compiler.h>
204 MONO_EMPTY_SOURCE_FILE (mono_dl_windows
);