2 * ICLRMetaHost - discovery and management of available .NET runtimes
4 * Copyright 2010 Vincent Povirk for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wine/unicode.h"
28 #include "wine/library.h"
41 #include "wine/list.h"
42 #include "mscoree_private.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL( mscoree );
48 static const WCHAR net_11_subdir[] = {'1','.','0',0};
49 static const WCHAR net_20_subdir[] = {'2','.','0',0};
50 static const WCHAR net_40_subdir[] = {'4','.','0',0};
52 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl;
54 #define NUM_RUNTIMES 3
56 static struct CLRRuntimeInfo runtimes[NUM_RUNTIMES] = {
57 {{&CLRRuntimeInfoVtbl}, net_11_subdir, 1, 1, 4322, 0},
58 {{&CLRRuntimeInfoVtbl}, net_20_subdir, 2, 0, 50727, 0},
59 {{&CLRRuntimeInfoVtbl}, net_40_subdir, 4, 0, 30319, 0}
62 static int runtimes_initialized;
64 static CRITICAL_SECTION runtime_list_cs;
65 static CRITICAL_SECTION_DEBUG runtime_list_cs_debug =
67 0, 0, &runtime_list_cs,
68 { &runtime_list_cs_debug.ProcessLocksList,
69 &runtime_list_cs_debug.ProcessLocksList },
70 0, 0, { (DWORD_PTR)(__FILE__ ": runtime_list_cs") }
72 static CRITICAL_SECTION runtime_list_cs = { &runtime_list_cs_debug, -1, 0, 0, 0, 0 };
74 #define NUM_ABI_VERSIONS 2
76 static loaded_mono loaded_monos[NUM_ABI_VERSIONS];
78 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version);
80 static MonoAssembly* mono_assembly_search_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data);
82 static void mono_shutdown_callback_fn(MonoProfiler *prof);
84 static void set_environment(LPCWSTR bin_path)
86 WCHAR path_env[MAX_PATH];
89 static const WCHAR pathW[] = {'P','A','T','H',0};
91 /* We have to modify PATH as Mono loads other DLLs from this directory. */
92 GetEnvironmentVariableW(pathW, path_env, sizeof(path_env)/sizeof(WCHAR));
93 len = strlenW(path_env);
94 path_env[len++] = ';';
95 strcpyW(path_env+len, bin_path);
96 SetEnvironmentVariableW(pathW, path_env);
99 static void CDECL do_nothing(void)
103 static MonoImage* image_open_module_handle_dummy(HMODULE module_handle,
104 char* fname, UINT has_entry_point, MonoImageOpenStatus* status, int abi_version)
106 return loaded_monos[abi_version-1].mono_image_open(fname, status);
109 static CDECL MonoImage* image_open_module_handle_dummy_1(HMODULE module_handle,
110 char* fname, UINT has_entry_point, MonoImageOpenStatus* status)
112 return image_open_module_handle_dummy(module_handle, fname, has_entry_point, status, 1);
115 static CDECL MonoImage* image_open_module_handle_dummy_2(HMODULE module_handle,
116 char* fname, UINT has_entry_point, MonoImageOpenStatus* status)
118 return image_open_module_handle_dummy(module_handle, fname, has_entry_point, status, 2);
121 MonoImage* (CDECL * const image_from_handle_fn[NUM_ABI_VERSIONS])(HMODULE module_handle, char* fname, UINT has_entry_point, MonoImageOpenStatus* status) = {
122 image_open_module_handle_dummy_1,
123 image_open_module_handle_dummy_2
126 static void missing_runtime_message(const CLRRuntimeInfo *This)
128 if (This->major == 1)
129 MESSAGE("wine: Install Mono 2.6 for Windows to run .NET 1.1 applications.\n");
130 else if (This->major == 2)
131 MESSAGE("wine: Install Mono for Windows to run .NET 2.0 applications.\n");
132 else if (This->major == 4)
133 MESSAGE("wine: Install Mono 2.8 or greater for Windows to run .NET 4.0 applications.\n");
136 static HRESULT load_mono(CLRRuntimeInfo *This, loaded_mono **result)
138 static const WCHAR bin[] = {'\\','b','i','n',0};
139 static const WCHAR lib[] = {'\\','l','i','b',0};
140 static const WCHAR etc[] = {'\\','e','t','c',0};
141 static const WCHAR glibdll[] = {'l','i','b','g','l','i','b','-','2','.','0','-','0','.','d','l','l',0};
142 WCHAR mono_dll_path[MAX_PATH+16], mono_bin_path[MAX_PATH+4];
143 WCHAR mono_lib_path[MAX_PATH+4], mono_etc_path[MAX_PATH+4];
144 char mono_lib_path_a[MAX_PATH], mono_etc_path_a[MAX_PATH];
146 char trace_setting[256];
148 if (This->mono_abi_version <= 0 || This->mono_abi_version > NUM_ABI_VERSIONS)
150 missing_runtime_message(This);
154 *result = &loaded_monos[This->mono_abi_version-1];
156 if ((*result)->is_shutdown)
158 ERR("Cannot load Mono after it has been shut down.\n");
163 if (!(*result)->mono_handle)
165 strcpyW(mono_bin_path, This->mono_path);
166 strcatW(mono_bin_path, bin);
167 set_environment(mono_bin_path);
169 strcpyW(mono_lib_path, This->mono_path);
170 strcatW(mono_lib_path, lib);
171 WideCharToMultiByte(CP_UTF8, 0, mono_lib_path, -1, mono_lib_path_a, MAX_PATH, NULL, NULL);
173 strcpyW(mono_etc_path, This->mono_path);
174 strcatW(mono_etc_path, etc);
175 WideCharToMultiByte(CP_UTF8, 0, mono_etc_path, -1, mono_etc_path_a, MAX_PATH, NULL, NULL);
177 if (!find_mono_dll(This->mono_path, mono_dll_path, This->mono_abi_version)) goto fail;
179 (*result)->mono_handle = LoadLibraryW(mono_dll_path);
181 if (!(*result)->mono_handle) goto fail;
183 #define LOAD_MONO_FUNCTION(x) do { \
184 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
185 if (!(*result)->x) { \
190 LOAD_MONO_FUNCTION(mono_assembly_get_image);
191 LOAD_MONO_FUNCTION(mono_assembly_load_from);
192 LOAD_MONO_FUNCTION(mono_assembly_open);
193 LOAD_MONO_FUNCTION(mono_config_parse);
194 LOAD_MONO_FUNCTION(mono_class_from_mono_type);
195 LOAD_MONO_FUNCTION(mono_class_from_name);
196 LOAD_MONO_FUNCTION(mono_class_get_method_from_name);
197 LOAD_MONO_FUNCTION(mono_domain_assembly_open);
198 LOAD_MONO_FUNCTION(mono_image_open);
199 LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook);
200 LOAD_MONO_FUNCTION(mono_jit_exec);
201 LOAD_MONO_FUNCTION(mono_jit_init);
202 LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
203 LOAD_MONO_FUNCTION(mono_marshal_get_vtfixup_ftnptr);
204 LOAD_MONO_FUNCTION(mono_object_get_domain);
205 LOAD_MONO_FUNCTION(mono_object_new);
206 LOAD_MONO_FUNCTION(mono_object_unbox);
207 LOAD_MONO_FUNCTION(mono_profiler_install);
208 LOAD_MONO_FUNCTION(mono_reflection_type_from_name);
209 LOAD_MONO_FUNCTION(mono_runtime_invoke);
210 LOAD_MONO_FUNCTION(mono_runtime_object_init);
211 LOAD_MONO_FUNCTION(mono_runtime_quit);
212 LOAD_MONO_FUNCTION(mono_set_dirs);
213 LOAD_MONO_FUNCTION(mono_stringify_assembly_name);
214 LOAD_MONO_FUNCTION(mono_string_new);
215 LOAD_MONO_FUNCTION(mono_thread_attach);
217 /* GLib imports obsoleted by the 2.0 ABI */
218 if (This->mono_abi_version == 1)
220 (*result)->glib_handle = LoadLibraryW(glibdll);
221 if (!(*result)->glib_handle) goto fail;
223 (*result)->mono_free = (void*)GetProcAddress((*result)->glib_handle, "g_free");
224 if (!(*result)->mono_free) goto fail;
228 LOAD_MONO_FUNCTION(mono_free);
231 #undef LOAD_MONO_FUNCTION
233 #define LOAD_OPT_MONO_FUNCTION(x, default) do { \
234 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
235 if (!(*result)->x) { \
236 (*result)->x = default; \
240 LOAD_OPT_MONO_FUNCTION(mono_image_open_from_module_handle, image_from_handle_fn[This->mono_abi_version-1]);
241 LOAD_OPT_MONO_FUNCTION(mono_runtime_set_shutting_down, do_nothing);
242 LOAD_OPT_MONO_FUNCTION(mono_thread_pool_cleanup, do_nothing);
243 LOAD_OPT_MONO_FUNCTION(mono_thread_suspend_all_other_threads, do_nothing);
244 LOAD_OPT_MONO_FUNCTION(mono_threads_set_shutting_down, do_nothing);
246 #undef LOAD_OPT_MONO_FUNCTION
248 (*result)->mono_profiler_install((MonoProfiler*)*result, mono_shutdown_callback_fn);
250 (*result)->mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
252 (*result)->mono_config_parse(NULL);
254 (*result)->mono_install_assembly_preload_hook(mono_assembly_search_hook_fn, *result);
256 trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
260 (*result)->mono_jit_set_trace_options(trace_setting);
267 ERR("Could not load Mono into this process\n");
268 FreeLibrary((*result)->mono_handle);
269 FreeLibrary((*result)->glib_handle);
270 (*result)->mono_handle = NULL;
271 (*result)->glib_handle = NULL;
275 static void mono_shutdown_callback_fn(MonoProfiler *prof)
277 loaded_mono *mono = (loaded_mono*)prof;
279 mono->is_shutdown = TRUE;
282 static HRESULT CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo *This, RuntimeHost **result)
285 loaded_mono *ploaded_mono;
287 if (This->loaded_runtime)
289 *result = This->loaded_runtime;
293 EnterCriticalSection(&runtime_list_cs);
295 hr = load_mono(This, &ploaded_mono);
298 hr = RuntimeHost_Construct(This, ploaded_mono, &This->loaded_runtime);
300 LeaveCriticalSection(&runtime_list_cs);
303 *result = This->loaded_runtime;
308 void unload_all_runtimes(void)
312 for (i=0; i<NUM_ABI_VERSIONS; i++)
314 loaded_mono *mono = &loaded_monos[i];
315 if (mono->mono_handle && mono->is_started && !mono->is_shutdown)
317 /* Copied from Mono's ves_icall_System_Environment_Exit */
318 mono->mono_threads_set_shutting_down();
319 mono->mono_runtime_set_shutting_down();
320 mono->mono_thread_pool_cleanup();
321 mono->mono_thread_suspend_all_other_threads();
322 mono->mono_runtime_quit();
326 for (i=0; i<NUM_RUNTIMES; i++)
327 if (runtimes[i].loaded_runtime)
328 RuntimeHost_Destroy(runtimes[i].loaded_runtime);
331 void expect_no_runtimes(void)
335 for (i=0; i<NUM_ABI_VERSIONS; i++)
337 loaded_mono *mono = &loaded_monos[i];
338 if (mono->mono_handle && mono->is_started && !mono->is_shutdown)
340 ERR("Process exited with a Mono runtime loaded.\n");
346 static inline CLRRuntimeInfo *impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo *iface)
348 return CONTAINING_RECORD(iface, CLRRuntimeInfo, ICLRRuntimeInfo_iface);
351 static HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
355 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
357 if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
358 IsEqualGUID( riid, &IID_IUnknown ) )
364 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
365 return E_NOINTERFACE;
368 ICLRRuntimeInfo_AddRef( iface );
373 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
378 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
383 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
384 LPWSTR pwzBuffer, DWORD *pcchBuffer)
386 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
387 DWORD buffer_size = *pcchBuffer;
392 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
394 size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
396 assert(size <= sizeof(version));
398 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
402 if (buffer_size >= *pcchBuffer)
403 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
405 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
411 static BOOL get_install_root(LPWSTR install_dir)
413 const WCHAR dotnet_key[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t','\\','.','N','E','T','F','r','a','m','e','w','o','r','k','\\',0};
414 const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
419 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
423 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
433 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
434 LPWSTR pwzBuffer, DWORD *pcchBuffer)
436 static const WCHAR slash[] = {'\\',0};
437 DWORD buffer_size = *pcchBuffer;
438 WCHAR system_dir[MAX_PATH];
439 WCHAR version[MAX_PATH];
440 DWORD version_size, size;
443 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
445 if (!get_install_root(system_dir))
447 ERR("error reading registry key for installroot\n");
452 version_size = MAX_PATH;
453 ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
454 lstrcatW(system_dir, version);
455 lstrcatW(system_dir, slash);
456 size = lstrlenW(system_dir) + 1;
463 if (buffer_size >= size)
464 strcpyW(pwzBuffer, system_dir);
466 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
472 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
473 HANDLE hndProcess, BOOL *pbLoaded)
475 FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
480 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
481 UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
483 FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
488 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
489 LPCWSTR pwzDllName, HMODULE *phndModule)
491 WCHAR version[MAX_PATH];
495 TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
497 cchBuffer = MAX_PATH;
498 hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
499 if (FAILED(hr)) return hr;
501 return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
504 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
505 LPCSTR pszProcName, LPVOID *ppProc)
507 FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
512 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
513 REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
515 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
519 TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
521 hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
524 hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
529 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
532 FIXME("%p %p\n", iface, pbLoadable);
537 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
538 DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
540 FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
545 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
546 DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
548 FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
553 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
555 FIXME("%p\n", iface);
560 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
561 BOOL *pbStarted, DWORD *pdwStartupFlags)
563 FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
568 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
569 CLRRuntimeInfo_QueryInterface,
570 CLRRuntimeInfo_AddRef,
571 CLRRuntimeInfo_Release,
572 CLRRuntimeInfo_GetVersionString,
573 CLRRuntimeInfo_GetRuntimeDirectory,
574 CLRRuntimeInfo_IsLoaded,
575 CLRRuntimeInfo_LoadErrorString,
576 CLRRuntimeInfo_LoadLibrary,
577 CLRRuntimeInfo_GetProcAddress,
578 CLRRuntimeInfo_GetInterface,
579 CLRRuntimeInfo_IsLoadable,
580 CLRRuntimeInfo_SetDefaultStartupFlags,
581 CLRRuntimeInfo_GetDefaultStartupFlags,
582 CLRRuntimeInfo_BindAsLegacyV2Runtime,
583 CLRRuntimeInfo_IsStarted
586 HRESULT ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo *iface, RuntimeHost **result)
588 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
590 assert(This->ICLRRuntimeInfo_iface.lpVtbl == &CLRRuntimeInfoVtbl);
592 return CLRRuntimeInfo_GetRuntimeHost(This, result);
596 static const WCHAR libmono2_arch_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','-','x','8','6','.','d','l','l',0};
597 #elif defined(__x86_64__)
598 static const WCHAR libmono2_arch_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','-','x','8','6','_','6','4','.','d','l','l',0};
600 static const WCHAR libmono2_arch_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
603 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version)
605 static const WCHAR mono_dll[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
606 static const WCHAR libmono_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
607 static const WCHAR mono2_dll[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
608 static const WCHAR libmono2_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
609 DWORD attributes=INVALID_FILE_ATTRIBUTES;
611 if (abi_version == 1)
613 strcpyW(dll_path, path);
614 strcatW(dll_path, mono_dll);
615 attributes = GetFileAttributesW(dll_path);
617 if (attributes == INVALID_FILE_ATTRIBUTES)
619 strcpyW(dll_path, path);
620 strcatW(dll_path, libmono_dll);
621 attributes = GetFileAttributesW(dll_path);
624 else if (abi_version == 2)
626 strcpyW(dll_path, path);
627 strcatW(dll_path, libmono2_arch_dll);
628 attributes = GetFileAttributesW(dll_path);
630 if (attributes == INVALID_FILE_ATTRIBUTES)
632 strcpyW(dll_path, path);
633 strcatW(dll_path, mono2_dll);
634 attributes = GetFileAttributesW(dll_path);
637 if (attributes == INVALID_FILE_ATTRIBUTES)
639 strcpyW(dll_path, path);
640 strcatW(dll_path, libmono2_dll);
641 attributes = GetFileAttributesW(dll_path);
645 return (attributes != INVALID_FILE_ATTRIBUTES);
648 static BOOL get_mono_path_from_registry(LPWSTR path, int abi_version)
650 static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
651 static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
652 static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
653 static const WCHAR slash[] = {'\\',0};
655 WCHAR version[64], version_key[MAX_PATH];
658 WCHAR dll_path[MAX_PATH];
660 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
663 len = sizeof(version);
664 if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
671 lstrcpyW(version_key, mono_key);
672 lstrcatW(version_key, slash);
673 lstrcatW(version_key, version);
675 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
678 len = sizeof(WCHAR) * MAX_PATH;
679 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
686 return find_mono_dll(path, dll_path, abi_version);
689 static BOOL get_mono_path_from_folder(LPCWSTR folder, LPWSTR mono_path, int abi_version)
691 static const WCHAR mono_one_dot_zero[] = {'\\','m','o','n','o','-','1','.','0', 0};
692 static const WCHAR mono_two_dot_zero[] = {'\\','m','o','n','o','-','2','.','0', 0};
693 WCHAR mono_dll_path[MAX_PATH];
696 strcpyW(mono_path, folder);
698 if (abi_version == 1)
699 strcatW(mono_path, mono_one_dot_zero);
700 else if (abi_version == 2)
701 strcatW(mono_path, mono_two_dot_zero);
703 found = find_mono_dll(mono_path, mono_dll_path, abi_version);
708 static BOOL get_mono_path(LPWSTR path, int abi_version)
710 static const WCHAR subdir_mono[] = {'\\','m','o','n','o',0};
711 static const WCHAR sibling_mono[] = {'\\','.','.','\\','m','o','n','o',0};
712 WCHAR base_path[MAX_PATH];
713 const char *unix_data_dir;
716 static WCHAR* (CDECL *wine_get_dos_file_name)(const char*);
718 /* First try c:\windows\mono */
719 GetWindowsDirectoryW(base_path, MAX_PATH);
720 strcatW(base_path, subdir_mono);
722 if (get_mono_path_from_folder(base_path, path, abi_version))
725 /* Next: /usr/share/wine/mono */
726 unix_data_dir = wine_get_data_dir();
730 unix_data_dir = wine_get_build_dir();
736 if (!wine_get_dos_file_name)
737 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
739 if (wine_get_dos_file_name)
741 dos_data_dir = wine_get_dos_file_name(unix_data_dir);
745 strcpyW(base_path, dos_data_dir);
746 strcatW(base_path, build_tree ? sibling_mono : subdir_mono);
748 HeapFree(GetProcessHeap(), 0, dos_data_dir);
750 if (get_mono_path_from_folder(base_path, path, abi_version))
756 /* Last: the registry */
757 return get_mono_path_from_registry(path, abi_version);
760 static void find_runtimes(void)
763 static const WCHAR libmono[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
764 static const WCHAR mscorlib[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
765 WCHAR mono_path[MAX_PATH], lib_path[MAX_PATH];
766 BOOL any_runtimes_found = FALSE;
768 if (runtimes_initialized) return;
770 EnterCriticalSection(&runtime_list_cs);
772 if (runtimes_initialized) goto end;
774 for (abi_version=NUM_ABI_VERSIONS; abi_version>0; abi_version--)
776 if (!get_mono_path(mono_path, abi_version))
779 for (i=0; i<NUM_RUNTIMES; i++)
781 if (runtimes[i].mono_abi_version == 0)
783 strcpyW(lib_path, mono_path);
784 strcatW(lib_path, libmono);
785 strcatW(lib_path, runtimes[i].mono_libdir);
786 strcatW(lib_path, mscorlib);
788 if (GetFileAttributesW(lib_path) != INVALID_FILE_ATTRIBUTES)
790 runtimes[i].mono_abi_version = abi_version;
792 strcpyW(runtimes[i].mono_path, mono_path);
793 strcpyW(runtimes[i].mscorlib_path, lib_path);
795 any_runtimes_found = TRUE;
801 if (!any_runtimes_found)
803 /* Report all runtimes are available if Mono isn't installed.
804 * FIXME: Remove this when Mono is properly packaged. */
805 for (i=0; i<NUM_RUNTIMES; i++)
806 runtimes[i].mono_abi_version = -1;
809 runtimes_initialized = 1;
812 LeaveCriticalSection(&runtime_list_cs);
815 struct InstalledRuntimeEnum
817 IEnumUnknown IEnumUnknown_iface;
822 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
824 static inline struct InstalledRuntimeEnum *impl_from_IEnumUnknown(IEnumUnknown *iface)
826 return CONTAINING_RECORD(iface, struct InstalledRuntimeEnum, IEnumUnknown_iface);
829 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface, REFIID riid,
832 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
834 if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
835 IsEqualGUID( riid, &IID_IUnknown ) )
841 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
842 return E_NOINTERFACE;
845 IEnumUnknown_AddRef( iface );
850 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
852 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
853 ULONG ref = InterlockedIncrement(&This->ref);
855 TRACE("(%p) refcount=%u\n", iface, ref);
860 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
862 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
863 ULONG ref = InterlockedDecrement(&This->ref);
865 TRACE("(%p) refcount=%u\n", iface, ref);
869 HeapFree(GetProcessHeap(), 0, This);
875 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
876 IUnknown **rgelt, ULONG *pceltFetched)
878 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
883 TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
885 while (num_fetched < celt)
887 if (This->pos >= NUM_RUNTIMES)
892 if (runtimes[This->pos].mono_abi_version)
894 item = (IUnknown*)&runtimes[This->pos].ICLRRuntimeInfo_iface;
895 IUnknown_AddRef(item);
896 rgelt[num_fetched] = item;
903 *pceltFetched = num_fetched;
908 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
910 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
914 TRACE("(%p,%u)\n", iface, celt);
916 while (num_fetched < celt)
918 if (This->pos >= NUM_RUNTIMES)
923 if (runtimes[This->pos].mono_abi_version)
933 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
935 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
937 TRACE("(%p)\n", iface);
944 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
946 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
947 struct InstalledRuntimeEnum *new_enum;
949 TRACE("(%p)\n", iface);
951 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
953 return E_OUTOFMEMORY;
955 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
957 new_enum->pos = This->pos;
959 *ppenum = &new_enum->IEnumUnknown_iface;
964 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
965 InstalledRuntimeEnum_QueryInterface,
966 InstalledRuntimeEnum_AddRef,
967 InstalledRuntimeEnum_Release,
968 InstalledRuntimeEnum_Next,
969 InstalledRuntimeEnum_Skip,
970 InstalledRuntimeEnum_Reset,
971 InstalledRuntimeEnum_Clone
976 ICLRMetaHost ICLRMetaHost_iface;
979 static struct CLRMetaHost GlobalCLRMetaHost;
981 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
985 TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
987 if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
988 IsEqualGUID( riid, &IID_IUnknown ) )
994 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
995 return E_NOINTERFACE;
998 ICLRMetaHost_AddRef( iface );
1003 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
1008 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
1013 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
1019 if (version[0] == 'v' || version[0] == 'V')
1022 if (!isdigit(*version))
1025 while (isdigit(*version))
1026 *major = *major * 10 + (*version++ - '0');
1031 if (*version++ != '.' || !isdigit(*version))
1034 while (isdigit(*version))
1035 *minor = *minor * 10 + (*version++ - '0');
1040 if (*version++ != '.' || !isdigit(*version))
1043 while (isdigit(*version))
1044 *build = *build * 10 + (*version++ - '0');
1046 return *version == 0;
1052 HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
1053 LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
1056 DWORD major, minor, build;
1058 TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
1063 if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
1065 ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
1066 return CLR_E_SHIM_RUNTIME;
1071 for (i=0; i<NUM_RUNTIMES; i++)
1073 if (runtimes[i].major == major && runtimes[i].minor == minor &&
1074 runtimes[i].build == build)
1076 if (runtimes[i].mono_abi_version)
1077 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface, iid,
1081 missing_runtime_message(&runtimes[i]);
1082 return CLR_E_SHIM_RUNTIME;
1087 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
1088 return CLR_E_SHIM_RUNTIME;
1091 HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
1092 LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
1097 ULONG buffer_size=*pcchBuffer;
1099 TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
1101 hr = assembly_create(&assembly, pwzFilePath);
1105 hr = assembly_get_runtime_version(assembly, &version);
1109 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
1113 if (buffer_size >= *pcchBuffer)
1114 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
1116 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
1120 assembly_release(assembly);
1126 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
1127 IEnumUnknown **ppEnumerator)
1129 struct InstalledRuntimeEnum *new_enum;
1131 TRACE("%p\n", ppEnumerator);
1135 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
1137 return E_OUTOFMEMORY;
1139 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
1143 *ppEnumerator = &new_enum->IEnumUnknown_iface;
1148 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
1149 HANDLE hndProcess, IEnumUnknown **ppEnumerator)
1151 FIXME("%p %p\n", hndProcess, ppEnumerator);
1156 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
1157 RuntimeLoadedCallbackFnPtr pCallbackFunction)
1159 FIXME("%p\n", pCallbackFunction);
1164 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
1165 REFIID riid, LPVOID *ppUnk)
1167 FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
1172 static HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
1174 FIXME("%i: stub\n", iExitCode);
1176 ExitProcess(iExitCode);
1179 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
1181 CLRMetaHost_QueryInterface,
1183 CLRMetaHost_Release,
1184 CLRMetaHost_GetRuntime,
1185 CLRMetaHost_GetVersionFromFile,
1186 CLRMetaHost_EnumerateInstalledRuntimes,
1187 CLRMetaHost_EnumerateLoadedRuntimes,
1188 CLRMetaHost_RequestRuntimeLoadedNotification,
1189 CLRMetaHost_QueryLegacyV2RuntimeBinding,
1190 CLRMetaHost_ExitProcess
1193 static struct CLRMetaHost GlobalCLRMetaHost = {
1194 { &CLRMetaHost_vtbl }
1197 HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1199 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost.ICLRMetaHost_iface, riid, ppobj);
1202 static MonoAssembly* mono_assembly_search_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data)
1204 loaded_mono *mono = user_data;
1206 MonoAssembly *result=NULL;
1207 char *stringname=NULL;
1209 int stringnameW_size;
1210 IAssemblyCache *asmcache;
1212 WCHAR path[MAX_PATH];
1214 MonoImageOpenStatus stat;
1215 static WCHAR fusiondll[] = {'f','u','s','i','o','n',0};
1216 HMODULE hfusion=NULL;
1217 static HRESULT (WINAPI *pCreateAssemblyCache)(IAssemblyCache**,DWORD);
1219 stringname = mono->mono_stringify_assembly_name(aname);
1221 TRACE("%s\n", debugstr_a(stringname));
1223 if (!stringname) return NULL;
1225 /* FIXME: We should search the given paths before the GAC. */
1227 if (!pCreateAssemblyCache)
1229 hr = LoadLibraryShim(fusiondll, NULL, NULL, &hfusion);
1233 pCreateAssemblyCache = (void*)GetProcAddress(hfusion, "CreateAssemblyCache");
1234 if (!pCreateAssemblyCache)
1240 hr = pCreateAssemblyCache(&asmcache, 0);
1244 stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, stringname, -1, NULL, 0);
1246 stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1248 MultiByteToWideChar(CP_UTF8, 0, stringname, -1, stringnameW, stringnameW_size);
1254 info.cbAssemblyInfo = sizeof(info);
1255 info.pszCurrentAssemblyPathBuf = path;
1256 info.cchBuf = MAX_PATH;
1259 hr = IAssemblyCache_QueryAssemblyInfo(asmcache, 0, stringnameW, &info);
1262 HeapFree(GetProcessHeap(), 0, stringnameW);
1264 IAssemblyCache_Release(asmcache);
1269 TRACE("found: %s\n", debugstr_w(path));
1275 result = mono->mono_assembly_open(pathA, &stat);
1278 ERR("Failed to load %s, status=%u\n", debugstr_w(path), stat);
1280 HeapFree(GetProcessHeap(), 0, pathA);
1284 mono->mono_free(stringname);
1289 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1290 DWORD startup_flags, DWORD runtimeinfo_flags, BOOL legacy, ICLRRuntimeInfo **result)
1292 static const WCHAR dotconfig[] = {'.','c','o','n','f','i','g',0};
1293 static const DWORD supported_startup_flags = 0;
1294 static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1296 WCHAR local_version[MAX_PATH];
1297 ULONG local_version_size = MAX_PATH;
1298 WCHAR local_config_file[MAX_PATH];
1300 parsed_config_file parsed_config;
1302 if (startup_flags & ~supported_startup_flags)
1303 FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
1305 if (runtimeinfo_flags & ~supported_runtime_flags)
1306 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
1308 if (exefile && !config_file)
1310 strcpyW(local_config_file, exefile);
1311 strcatW(local_config_file, dotconfig);
1313 config_file = local_config_file;
1319 hr = parse_config_file(config_file, &parsed_config);
1323 supported_runtime *entry;
1324 LIST_FOR_EACH_ENTRY(entry, &parsed_config.supported_runtimes, supported_runtime, entry)
1326 hr = CLRMetaHost_GetRuntime(0, entry->version, &IID_ICLRRuntimeInfo, (void**)result);
1336 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file), hr);
1339 free_parsed_config_file(&parsed_config);
1345 if (exefile && !version)
1347 hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1349 version = local_version;
1351 if (FAILED(hr)) return hr;
1356 hr = CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1361 if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1363 DWORD major, minor, build;
1365 if (version && !parse_runtime_version(version, &major, &minor, &build))
1367 ERR("Cannot parse %s\n", debugstr_w(version));
1368 return CLR_E_SHIM_RUNTIME;
1380 if (runtimes[i].mono_abi_version)
1382 /* Must be greater or equal to the version passed in. */
1383 if (!version || ((runtimes[i].major >= major && runtimes[i].minor >= minor && runtimes[i].build >= build) ||
1384 (runtimes[i].major >= major && runtimes[i].minor > minor) ||
1385 (runtimes[i].major > major)))
1387 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface,
1388 &IID_ICLRRuntimeInfo, (void **)result);
1394 missing_runtime_message(&runtimes[1]);
1396 missing_runtime_message(&runtimes[NUM_RUNTIMES-1]);
1398 return CLR_E_SHIM_RUNTIME;
1401 return CLR_E_SHIM_RUNTIME;