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"
38 #include "wine/list.h"
39 #include "mscoree_private.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL( mscoree );
45 static const WCHAR net_11_subdir[] = {'1','.','0',0};
46 static const WCHAR net_20_subdir[] = {'2','.','0',0};
47 static const WCHAR net_40_subdir[] = {'4','.','0',0};
49 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl;
51 #define NUM_RUNTIMES 3
53 static struct CLRRuntimeInfo runtimes[NUM_RUNTIMES] = {
54 {&CLRRuntimeInfoVtbl, net_11_subdir, 1, 1, 4322, 0},
55 {&CLRRuntimeInfoVtbl, net_20_subdir, 2, 0, 50727, 0},
56 {&CLRRuntimeInfoVtbl, net_40_subdir, 4, 0, 30319, 0}
59 static int runtimes_initialized;
61 static CRITICAL_SECTION runtime_list_cs;
62 static CRITICAL_SECTION_DEBUG runtime_list_cs_debug =
64 0, 0, &runtime_list_cs,
65 { &runtime_list_cs_debug.ProcessLocksList,
66 &runtime_list_cs_debug.ProcessLocksList },
67 0, 0, { (DWORD_PTR)(__FILE__ ": runtime_list_cs") }
69 static CRITICAL_SECTION runtime_list_cs = { &runtime_list_cs_debug, -1, 0, 0, 0, 0 };
71 #define NUM_ABI_VERSIONS 2
73 static loaded_mono loaded_monos[NUM_ABI_VERSIONS];
75 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version);
77 static MonoAssembly* mono_assembly_search_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data);
79 static void mono_shutdown_callback_fn(MonoProfiler *prof);
81 static void set_environment(LPCWSTR bin_path)
83 WCHAR path_env[MAX_PATH];
86 static const WCHAR pathW[] = {'P','A','T','H',0};
88 /* We have to modify PATH as Mono loads other DLLs from this directory. */
89 GetEnvironmentVariableW(pathW, path_env, sizeof(path_env)/sizeof(WCHAR));
90 len = strlenW(path_env);
91 path_env[len++] = ';';
92 strcpyW(path_env+len, bin_path);
93 SetEnvironmentVariableW(pathW, path_env);
96 static void CDECL do_nothing(void)
100 static void missing_runtime_message(const CLRRuntimeInfo *This)
102 if (This->major == 1)
103 MESSAGE("wine: Install Mono 2.6 for Windows to run .NET 1.1 applications.\n");
104 else if (This->major == 2)
105 MESSAGE("wine: Install Mono for Windows to run .NET 2.0 applications.\n");
106 else if (This->major == 4)
107 MESSAGE("wine: Install Mono 2.8 or greater for Windows to run .NET 4.0 applications.\n");
110 static HRESULT load_mono(CLRRuntimeInfo *This, loaded_mono **result)
112 static const WCHAR bin[] = {'\\','b','i','n',0};
113 static const WCHAR lib[] = {'\\','l','i','b',0};
114 static const WCHAR etc[] = {'\\','e','t','c',0};
115 static const WCHAR glibdll[] = {'l','i','b','g','l','i','b','-','2','.','0','-','0','.','d','l','l',0};
116 WCHAR mono_dll_path[MAX_PATH+16], mono_bin_path[MAX_PATH+4];
117 WCHAR mono_lib_path[MAX_PATH+4], mono_etc_path[MAX_PATH+4];
118 char mono_lib_path_a[MAX_PATH], mono_etc_path_a[MAX_PATH];
120 char trace_setting[256];
122 if (This->mono_abi_version <= 0 || This->mono_abi_version > NUM_ABI_VERSIONS)
124 missing_runtime_message(This);
128 *result = &loaded_monos[This->mono_abi_version-1];
130 if ((*result)->is_shutdown)
132 ERR("Cannot load Mono after it has been shut down.\n");
137 if (!(*result)->mono_handle)
139 strcpyW(mono_bin_path, This->mono_path);
140 strcatW(mono_bin_path, bin);
141 set_environment(mono_bin_path);
143 strcpyW(mono_lib_path, This->mono_path);
144 strcatW(mono_lib_path, lib);
145 WideCharToMultiByte(CP_UTF8, 0, mono_lib_path, -1, mono_lib_path_a, MAX_PATH, NULL, NULL);
147 strcpyW(mono_etc_path, This->mono_path);
148 strcatW(mono_etc_path, etc);
149 WideCharToMultiByte(CP_UTF8, 0, mono_etc_path, -1, mono_etc_path_a, MAX_PATH, NULL, NULL);
151 if (!find_mono_dll(This->mono_path, mono_dll_path, This->mono_abi_version)) goto fail;
153 (*result)->mono_handle = LoadLibraryW(mono_dll_path);
155 if (!(*result)->mono_handle) goto fail;
157 #define LOAD_MONO_FUNCTION(x) do { \
158 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
159 if (!(*result)->x) { \
164 LOAD_MONO_FUNCTION(mono_assembly_get_image);
165 LOAD_MONO_FUNCTION(mono_assembly_open);
166 LOAD_MONO_FUNCTION(mono_config_parse);
167 LOAD_MONO_FUNCTION(mono_class_from_mono_type);
168 LOAD_MONO_FUNCTION(mono_class_from_name);
169 LOAD_MONO_FUNCTION(mono_class_get_method_from_name);
170 LOAD_MONO_FUNCTION(mono_domain_assembly_open);
171 LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook);
172 LOAD_MONO_FUNCTION(mono_jit_exec);
173 LOAD_MONO_FUNCTION(mono_jit_init);
174 LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
175 LOAD_MONO_FUNCTION(mono_object_get_domain);
176 LOAD_MONO_FUNCTION(mono_object_new);
177 LOAD_MONO_FUNCTION(mono_object_unbox);
178 LOAD_MONO_FUNCTION(mono_profiler_install);
179 LOAD_MONO_FUNCTION(mono_reflection_type_from_name);
180 LOAD_MONO_FUNCTION(mono_runtime_invoke);
181 LOAD_MONO_FUNCTION(mono_runtime_object_init);
182 LOAD_MONO_FUNCTION(mono_runtime_quit);
183 LOAD_MONO_FUNCTION(mono_set_dirs);
184 LOAD_MONO_FUNCTION(mono_stringify_assembly_name);
186 /* GLib imports obsoleted by the 2.0 ABI */
187 if (This->mono_abi_version == 1)
189 (*result)->glib_handle = LoadLibraryW(glibdll);
190 if (!(*result)->glib_handle) goto fail;
192 (*result)->mono_free = (void*)GetProcAddress((*result)->glib_handle, "g_free");
193 if (!(*result)->mono_free) goto fail;
197 LOAD_MONO_FUNCTION(mono_free);
200 #undef LOAD_MONO_FUNCTION
202 #define LOAD_OPT_VOID_MONO_FUNCTION(x) do { \
203 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
204 if (!(*result)->x) { \
205 (*result)->x = do_nothing; \
209 LOAD_OPT_VOID_MONO_FUNCTION(mono_runtime_set_shutting_down);
210 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_pool_cleanup);
211 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_suspend_all_other_threads);
212 LOAD_OPT_VOID_MONO_FUNCTION(mono_threads_set_shutting_down);
214 #undef LOAD_OPT_VOID_MONO_FUNCTION
216 (*result)->mono_profiler_install((MonoProfiler*)*result, mono_shutdown_callback_fn);
218 (*result)->mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
220 (*result)->mono_config_parse(NULL);
222 (*result)->mono_install_assembly_preload_hook(mono_assembly_search_hook_fn, *result);
224 trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
228 (*result)->mono_jit_set_trace_options(trace_setting);
235 ERR("Could not load Mono into this process\n");
236 FreeLibrary((*result)->mono_handle);
237 FreeLibrary((*result)->glib_handle);
238 (*result)->mono_handle = NULL;
239 (*result)->glib_handle = NULL;
243 static void mono_shutdown_callback_fn(MonoProfiler *prof)
245 loaded_mono *mono = (loaded_mono*)prof;
247 mono->is_shutdown = TRUE;
250 static HRESULT CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo *This, RuntimeHost **result)
253 loaded_mono *ploaded_mono;
255 if (This->loaded_runtime)
257 *result = This->loaded_runtime;
261 EnterCriticalSection(&runtime_list_cs);
263 hr = load_mono(This, &ploaded_mono);
266 hr = RuntimeHost_Construct(This, ploaded_mono, &This->loaded_runtime);
268 LeaveCriticalSection(&runtime_list_cs);
271 *result = This->loaded_runtime;
276 void unload_all_runtimes(void)
280 for (i=0; i<NUM_ABI_VERSIONS; i++)
282 loaded_mono *mono = &loaded_monos[i];
283 if (mono->mono_handle && mono->is_started && !mono->is_shutdown)
285 /* Copied from Mono's ves_icall_System_Environment_Exit */
286 mono->mono_threads_set_shutting_down();
287 mono->mono_runtime_set_shutting_down();
288 mono->mono_thread_pool_cleanup();
289 mono->mono_thread_suspend_all_other_threads();
290 mono->mono_runtime_quit();
294 for (i=0; i<NUM_RUNTIMES; i++)
295 if (runtimes[i].loaded_runtime)
296 RuntimeHost_Destroy(runtimes[i].loaded_runtime);
299 void expect_no_runtimes(void)
303 for (i=0; i<NUM_ABI_VERSIONS; i++)
305 loaded_mono *mono = &loaded_monos[i];
306 if (mono->mono_handle && mono->is_started && !mono->is_shutdown)
308 ERR("Process exited with a Mono runtime loaded.\n");
314 static HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
318 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
320 if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
321 IsEqualGUID( riid, &IID_IUnknown ) )
327 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
328 return E_NOINTERFACE;
331 ICLRRuntimeInfo_AddRef( iface );
336 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
341 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
346 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
347 LPWSTR pwzBuffer, DWORD *pcchBuffer)
349 struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
350 DWORD buffer_size = *pcchBuffer;
355 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
357 size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
359 assert(size <= sizeof(version));
361 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
365 if (buffer_size >= *pcchBuffer)
366 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
368 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
374 static BOOL get_install_root(LPWSTR install_dir)
376 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};
377 const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
382 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
386 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
396 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
397 LPWSTR pwzBuffer, DWORD *pcchBuffer)
399 static const WCHAR slash[] = {'\\',0};
400 DWORD buffer_size = *pcchBuffer;
401 WCHAR system_dir[MAX_PATH];
402 WCHAR version[MAX_PATH];
403 DWORD version_size, size;
406 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
408 if (!get_install_root(system_dir))
410 ERR("error reading registry key for installroot\n");
415 version_size = MAX_PATH;
416 ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
417 lstrcatW(system_dir, version);
418 lstrcatW(system_dir, slash);
419 size = lstrlenW(system_dir) + 1;
426 if (buffer_size >= size)
427 strcpyW(pwzBuffer, system_dir);
429 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
435 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
436 HANDLE hndProcess, BOOL *pbLoaded)
438 FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
443 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
444 UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
446 FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
451 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
452 LPCWSTR pwzDllName, HMODULE *phndModule)
454 WCHAR version[MAX_PATH];
458 TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
460 cchBuffer = MAX_PATH;
461 hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
462 if (FAILED(hr)) return hr;
464 return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
467 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
468 LPCSTR pszProcName, LPVOID *ppProc)
470 FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
475 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
476 REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
478 struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
482 TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
484 hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
487 hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
492 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
495 FIXME("%p %p\n", iface, pbLoadable);
500 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
501 DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
503 FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
508 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
509 DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
511 FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
516 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
518 FIXME("%p\n", iface);
523 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
524 BOOL *pbStarted, DWORD *pdwStartupFlags)
526 FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
531 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
532 CLRRuntimeInfo_QueryInterface,
533 CLRRuntimeInfo_AddRef,
534 CLRRuntimeInfo_Release,
535 CLRRuntimeInfo_GetVersionString,
536 CLRRuntimeInfo_GetRuntimeDirectory,
537 CLRRuntimeInfo_IsLoaded,
538 CLRRuntimeInfo_LoadErrorString,
539 CLRRuntimeInfo_LoadLibrary,
540 CLRRuntimeInfo_GetProcAddress,
541 CLRRuntimeInfo_GetInterface,
542 CLRRuntimeInfo_IsLoadable,
543 CLRRuntimeInfo_SetDefaultStartupFlags,
544 CLRRuntimeInfo_GetDefaultStartupFlags,
545 CLRRuntimeInfo_BindAsLegacyV2Runtime,
546 CLRRuntimeInfo_IsStarted
549 HRESULT ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo *iface, RuntimeHost **result)
551 struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
553 assert(This->ICLRRuntimeInfo_vtbl == &CLRRuntimeInfoVtbl);
555 return CLRRuntimeInfo_GetRuntimeHost(This, result);
558 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version)
560 static const WCHAR mono_dll[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
561 static const WCHAR libmono_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
562 static const WCHAR mono2_dll[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
563 static const WCHAR libmono2_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
564 DWORD attributes=INVALID_FILE_ATTRIBUTES;
566 if (abi_version == 1)
568 strcpyW(dll_path, path);
569 strcatW(dll_path, mono_dll);
570 attributes = GetFileAttributesW(dll_path);
572 if (attributes == INVALID_FILE_ATTRIBUTES)
574 strcpyW(dll_path, path);
575 strcatW(dll_path, libmono_dll);
576 attributes = GetFileAttributesW(dll_path);
579 else if (abi_version == 2)
581 strcpyW(dll_path, path);
582 strcatW(dll_path, mono2_dll);
583 attributes = GetFileAttributesW(dll_path);
585 if (attributes == INVALID_FILE_ATTRIBUTES)
587 strcpyW(dll_path, path);
588 strcatW(dll_path, libmono2_dll);
589 attributes = GetFileAttributesW(dll_path);
593 return (attributes != INVALID_FILE_ATTRIBUTES);
596 static BOOL get_mono_path_from_registry(LPWSTR path, int abi_version)
598 static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
599 static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
600 static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
601 static const WCHAR slash[] = {'\\',0};
603 WCHAR version[64], version_key[MAX_PATH];
606 WCHAR dll_path[MAX_PATH];
608 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
611 len = sizeof(version);
612 if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
619 lstrcpyW(version_key, mono_key);
620 lstrcatW(version_key, slash);
621 lstrcatW(version_key, version);
623 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
626 len = sizeof(WCHAR) * MAX_PATH;
627 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
634 return find_mono_dll(path, dll_path, abi_version);
637 static BOOL get_mono_path_from_folder(LPCWSTR folder, LPWSTR mono_path, int abi_version)
639 static const WCHAR mono_one_dot_zero[] = {'\\','m','o','n','o','-','1','.','0', 0};
640 static const WCHAR mono_two_dot_zero[] = {'\\','m','o','n','o','-','2','.','0', 0};
641 WCHAR mono_dll_path[MAX_PATH];
644 strcpyW(mono_path, folder);
646 if (abi_version == 1)
647 strcatW(mono_path, mono_one_dot_zero);
648 else if (abi_version == 2)
649 strcatW(mono_path, mono_two_dot_zero);
651 found = find_mono_dll(mono_path, mono_dll_path, abi_version);
656 static BOOL get_mono_path(LPWSTR path, int abi_version)
658 static const WCHAR subdir_mono[] = {'\\','m','o','n','o',0};
659 static const WCHAR sibling_mono[] = {'\\','.','.','\\','m','o','n','o',0};
660 WCHAR base_path[MAX_PATH];
661 const char *unix_data_dir;
664 static WCHAR* (CDECL *wine_get_dos_file_name)(const char*);
666 /* First try c:\windows\mono */
667 GetWindowsDirectoryW(base_path, MAX_PATH);
668 strcatW(base_path, subdir_mono);
670 if (get_mono_path_from_folder(base_path, path, abi_version))
673 /* Next: /usr/share/wine/mono */
674 unix_data_dir = wine_get_data_dir();
678 unix_data_dir = wine_get_build_dir();
684 if (!wine_get_dos_file_name)
685 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
687 if (wine_get_dos_file_name)
689 dos_data_dir = wine_get_dos_file_name(unix_data_dir);
693 strcpyW(base_path, dos_data_dir);
694 strcatW(base_path, build_tree ? sibling_mono : subdir_mono);
696 HeapFree(GetProcessHeap(), 0, dos_data_dir);
698 if (get_mono_path_from_folder(base_path, path, abi_version))
704 /* Last: the registry */
705 return get_mono_path_from_registry(path, abi_version);
708 static void find_runtimes(void)
711 static const WCHAR libmono[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
712 static const WCHAR mscorlib[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
713 WCHAR mono_path[MAX_PATH], lib_path[MAX_PATH];
714 BOOL any_runtimes_found = FALSE;
716 if (runtimes_initialized) return;
718 EnterCriticalSection(&runtime_list_cs);
720 if (runtimes_initialized) goto end;
722 for (abi_version=NUM_ABI_VERSIONS; abi_version>0; abi_version--)
724 if (!get_mono_path(mono_path, abi_version))
727 for (i=0; i<NUM_RUNTIMES; i++)
729 if (runtimes[i].mono_abi_version == 0)
731 strcpyW(lib_path, mono_path);
732 strcatW(lib_path, libmono);
733 strcatW(lib_path, runtimes[i].mono_libdir);
734 strcatW(lib_path, mscorlib);
736 if (GetFileAttributesW(lib_path) != INVALID_FILE_ATTRIBUTES)
738 runtimes[i].mono_abi_version = abi_version;
740 strcpyW(runtimes[i].mono_path, mono_path);
741 strcpyW(runtimes[i].mscorlib_path, lib_path);
743 any_runtimes_found = TRUE;
749 if (!any_runtimes_found)
751 /* Report all runtimes are available if Mono isn't installed.
752 * FIXME: Remove this when Mono is properly packaged. */
753 for (i=0; i<NUM_RUNTIMES; i++)
754 runtimes[i].mono_abi_version = -1;
757 runtimes_initialized = 1;
760 LeaveCriticalSection(&runtime_list_cs);
763 struct InstalledRuntimeEnum
765 IEnumUnknown IEnumUnknown_iface;
770 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
772 static inline struct InstalledRuntimeEnum *impl_from_IEnumUnknown(IEnumUnknown *iface)
774 return CONTAINING_RECORD(iface, struct InstalledRuntimeEnum, IEnumUnknown_iface);
777 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface, REFIID riid,
780 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
782 if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
783 IsEqualGUID( riid, &IID_IUnknown ) )
789 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
790 return E_NOINTERFACE;
793 IEnumUnknown_AddRef( iface );
798 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
800 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
801 ULONG ref = InterlockedIncrement(&This->ref);
803 TRACE("(%p) refcount=%u\n", iface, ref);
808 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
810 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
811 ULONG ref = InterlockedDecrement(&This->ref);
813 TRACE("(%p) refcount=%u\n", iface, ref);
817 HeapFree(GetProcessHeap(), 0, This);
823 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
824 IUnknown **rgelt, ULONG *pceltFetched)
826 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
831 TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
833 while (num_fetched < celt)
835 if (This->pos >= NUM_RUNTIMES)
840 if (runtimes[This->pos].mono_abi_version)
842 item = (IUnknown*)&runtimes[This->pos];
843 IUnknown_AddRef(item);
844 rgelt[num_fetched] = item;
851 *pceltFetched = num_fetched;
856 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
858 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
862 TRACE("(%p,%u)\n", iface, celt);
864 while (num_fetched < celt)
866 if (This->pos >= NUM_RUNTIMES)
871 if (runtimes[This->pos].mono_abi_version)
881 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
883 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
885 TRACE("(%p)\n", iface);
892 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
894 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
895 struct InstalledRuntimeEnum *new_enum;
897 TRACE("(%p)\n", iface);
899 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
901 return E_OUTOFMEMORY;
903 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
905 new_enum->pos = This->pos;
907 *ppenum = &new_enum->IEnumUnknown_iface;
912 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
913 InstalledRuntimeEnum_QueryInterface,
914 InstalledRuntimeEnum_AddRef,
915 InstalledRuntimeEnum_Release,
916 InstalledRuntimeEnum_Next,
917 InstalledRuntimeEnum_Skip,
918 InstalledRuntimeEnum_Reset,
919 InstalledRuntimeEnum_Clone
924 ICLRMetaHost ICLRMetaHost_iface;
927 static struct CLRMetaHost GlobalCLRMetaHost;
929 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
933 TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
935 if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
936 IsEqualGUID( riid, &IID_IUnknown ) )
942 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
943 return E_NOINTERFACE;
946 ICLRMetaHost_AddRef( iface );
951 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
956 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
961 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
967 if (version[0] == 'v')
970 if (!isdigit(*version))
973 while (isdigit(*version))
974 *major = *major * 10 + (*version++ - '0');
979 if (*version++ != '.' || !isdigit(*version))
982 while (isdigit(*version))
983 *minor = *minor * 10 + (*version++ - '0');
988 if (*version++ != '.' || !isdigit(*version))
991 while (isdigit(*version))
992 *build = *build * 10 + (*version++ - '0');
994 return *version == 0;
1000 static HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
1001 LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
1004 DWORD major, minor, build;
1006 TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
1011 if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
1013 ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
1014 return CLR_E_SHIM_RUNTIME;
1019 for (i=0; i<NUM_RUNTIMES; i++)
1021 if (runtimes[i].major == major && runtimes[i].minor == minor &&
1022 runtimes[i].build == build)
1024 if (runtimes[i].mono_abi_version)
1025 return IUnknown_QueryInterface((IUnknown*)&runtimes[i], iid, ppRuntime);
1028 missing_runtime_message(&runtimes[i]);
1029 return CLR_E_SHIM_RUNTIME;
1034 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
1035 return CLR_E_SHIM_RUNTIME;
1038 HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
1039 LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
1044 ULONG buffer_size=*pcchBuffer;
1046 TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
1048 hr = assembly_create(&assembly, pwzFilePath);
1052 hr = assembly_get_runtime_version(assembly, &version);
1056 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
1060 if (buffer_size >= *pcchBuffer)
1061 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
1063 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
1067 assembly_release(assembly);
1073 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
1074 IEnumUnknown **ppEnumerator)
1076 struct InstalledRuntimeEnum *new_enum;
1078 TRACE("%p\n", ppEnumerator);
1082 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
1084 return E_OUTOFMEMORY;
1086 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
1090 *ppEnumerator = &new_enum->IEnumUnknown_iface;
1095 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
1096 HANDLE hndProcess, IEnumUnknown **ppEnumerator)
1098 FIXME("%p %p\n", hndProcess, ppEnumerator);
1103 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
1104 RuntimeLoadedCallbackFnPtr pCallbackFunction)
1106 FIXME("%p\n", pCallbackFunction);
1111 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
1112 REFIID riid, LPVOID *ppUnk)
1114 FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
1119 static HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
1121 FIXME("%i: stub\n", iExitCode);
1123 ExitProcess(iExitCode);
1126 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
1128 CLRMetaHost_QueryInterface,
1130 CLRMetaHost_Release,
1131 CLRMetaHost_GetRuntime,
1132 CLRMetaHost_GetVersionFromFile,
1133 CLRMetaHost_EnumerateInstalledRuntimes,
1134 CLRMetaHost_EnumerateLoadedRuntimes,
1135 CLRMetaHost_RequestRuntimeLoadedNotification,
1136 CLRMetaHost_QueryLegacyV2RuntimeBinding,
1137 CLRMetaHost_ExitProcess
1140 static struct CLRMetaHost GlobalCLRMetaHost = {
1141 { &CLRMetaHost_vtbl }
1144 extern HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1146 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost.ICLRMetaHost_iface, riid, ppobj);
1149 static MonoAssembly* mono_assembly_search_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data)
1151 loaded_mono *mono = user_data;
1153 MonoAssembly *result=NULL;
1154 char *stringname=NULL;
1156 int stringnameW_size;
1157 IAssemblyCache *asmcache;
1159 WCHAR path[MAX_PATH];
1161 MonoImageOpenStatus stat;
1162 static WCHAR fusiondll[] = {'f','u','s','i','o','n',0};
1163 HMODULE hfusion=NULL;
1164 static HRESULT WINAPI (*pCreateAssemblyCache)(IAssemblyCache**,DWORD);
1166 stringname = mono->mono_stringify_assembly_name(aname);
1168 TRACE("%s\n", debugstr_a(stringname));
1170 if (!stringname) return NULL;
1172 /* FIXME: We should search the given paths before the GAC. */
1174 if (!pCreateAssemblyCache)
1176 hr = LoadLibraryShim(fusiondll, NULL, NULL, &hfusion);
1180 pCreateAssemblyCache = (void*)GetProcAddress(hfusion, "CreateAssemblyCache");
1181 if (!pCreateAssemblyCache)
1187 hr = pCreateAssemblyCache(&asmcache, 0);
1191 stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, stringname, -1, NULL, 0);
1193 stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1195 MultiByteToWideChar(CP_UTF8, 0, stringname, -1, stringnameW, stringnameW_size);
1201 info.cbAssemblyInfo = sizeof(info);
1202 info.pszCurrentAssemblyPathBuf = path;
1203 info.cchBuf = MAX_PATH;
1206 hr = IAssemblyCache_QueryAssemblyInfo(asmcache, 0, stringnameW, &info);
1209 HeapFree(GetProcessHeap(), 0, stringnameW);
1211 IAssemblyCache_Release(asmcache);
1216 TRACE("found: %s\n", debugstr_w(path));
1222 result = mono->mono_assembly_open(pathA, &stat);
1225 ERR("Failed to load %s, status=%u\n", debugstr_w(path), stat);
1227 HeapFree(GetProcessHeap(), 0, pathA);
1231 mono->mono_free(stringname);
1236 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1237 DWORD startup_flags, DWORD runtimeinfo_flags, BOOL legacy, ICLRRuntimeInfo **result)
1239 static const WCHAR dotconfig[] = {'.','c','o','n','f','i','g',0};
1240 static const DWORD supported_startup_flags = 0;
1241 static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1243 WCHAR local_version[MAX_PATH];
1244 ULONG local_version_size = MAX_PATH;
1245 WCHAR local_config_file[MAX_PATH];
1247 parsed_config_file parsed_config;
1249 if (startup_flags & ~supported_startup_flags)
1250 FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
1252 if (runtimeinfo_flags & ~supported_runtime_flags)
1253 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
1255 if (exefile && !config_file)
1257 strcpyW(local_config_file, exefile);
1258 strcatW(local_config_file, dotconfig);
1260 config_file = local_config_file;
1266 hr = parse_config_file(config_file, &parsed_config);
1270 supported_runtime *entry;
1271 LIST_FOR_EACH_ENTRY(entry, &parsed_config.supported_runtimes, supported_runtime, entry)
1273 hr = CLRMetaHost_GetRuntime(0, entry->version, &IID_ICLRRuntimeInfo, (void**)result);
1283 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file), hr);
1286 free_parsed_config_file(&parsed_config);
1292 if (exefile && !version)
1294 hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1296 version = local_version;
1298 if (FAILED(hr)) return hr;
1303 return CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1306 if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1317 if (runtimes[i].mono_abi_version)
1318 return IUnknown_QueryInterface((IUnknown*)&runtimes[i],
1319 &IID_ICLRRuntimeInfo, (void**)result);
1323 missing_runtime_message(&runtimes[1]);
1325 missing_runtime_message(&runtimes[NUM_RUNTIMES-1]);
1327 return CLR_E_SHIM_RUNTIME;
1330 return CLR_E_SHIM_RUNTIME;