More disambiguation of Python in makefiles (#18284)
[mono-project.git] / mono / utils / mono-dl-windows.c
blob88c4911ee2b600f324f6ded5274d835e915fdc11
1 /**
2 * \file
3 * Interface to the dynamic linker
5 * Author:
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.
12 #include <config.h>
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"
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <ctype.h>
25 #include <string.h>
26 #include <glib.h>
28 #include <windows.h>
29 #include <psapi.h>
31 const char*
32 mono_dl_get_so_prefix (void)
34 return "";
37 const char**
38 mono_dl_get_so_suffixes (void)
40 static const char *suffixes[] = {
41 ".dll",
42 "",
44 return suffixes;
47 void*
48 mono_dl_open_file (const char *file, int flags)
50 gpointer hModule = NULL;
51 if (file) {
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);
56 #endif
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);
63 #else
64 g_assert_not_reached ();
65 #endif
66 if (!hModule)
67 last_error = GetLastError ();
69 #if HAVE_API_SUPPORT_WIN32_SET_ERROR_MODE
70 SetErrorMode (last_sem);
71 #endif
73 g_free (file_utf16);
75 if (!hModule)
76 SetLastError (last_error);
77 } else {
78 #if HAVE_API_SUPPORT_WIN32_GET_MODULE_HANDLE
79 hModule = GetModuleHandleW (NULL);
80 #else
81 g_assert_not_reached ();
82 #endif
84 return hModule;
87 void
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)
95 void*
96 mono_dl_lookup_symbol_in_process (const char *symbol_name)
98 HMODULE *modules;
99 DWORD buffer_size = sizeof (HMODULE) * 1024;
100 DWORD needed, i;
101 gpointer proc = NULL;
103 /* get the symbol from the loaded DLLs */
104 modules = (HMODULE *) g_malloc (buffer_size);
105 if (modules == NULL)
106 return NULL;
108 if (!EnumProcessModules (GetCurrentProcess (), modules,
109 buffer_size, &needed)) {
110 g_free (modules);
111 return NULL;
114 /* check whether the supplied buffer was too small, realloc, retry */
115 if (needed > buffer_size) {
116 g_free (modules);
118 buffer_size = needed;
119 modules = (HMODULE *) g_malloc (buffer_size);
121 if (modules == NULL)
122 return NULL;
124 if (!EnumProcessModules (GetCurrentProcess (), modules,
125 buffer_size, &needed)) {
126 g_free (modules);
127 return NULL;
131 for (i = 0; i < needed / sizeof (HANDLE); i++) {
132 proc = (gpointer)GetProcAddress (modules [i], symbol_name);
133 if (proc != NULL) {
134 g_free (modules);
135 return proc;
139 g_free (modules);
140 return NULL;
142 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
144 void*
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);
155 if (proc != NULL)
156 return proc;
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)
165 return 0;
168 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
169 char*
170 mono_dl_current_error_string (void)
172 char* ret = NULL;
173 wchar_t* buf = NULL;
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);
180 LocalFree (buf);
181 } else {
182 g_assert_not_reached ();
184 return ret;
186 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
189 mono_dl_get_executable_path (char *buf, int buflen)
191 return -1; //TODO
194 const char*
195 mono_dl_get_system_dir (void)
197 return NULL;
200 #else
202 #include <mono/utils/mono-compiler.h>
204 MONO_EMPTY_SOURCE_FILE (mono_dl_windows);
206 #endif