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 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 HRESULT load_mono(CLRRuntimeInfo *This, loaded_mono **result)
102 static const WCHAR bin[] = {'\\','b','i','n',0};
103 static const WCHAR lib[] = {'\\','l','i','b',0};
104 static const WCHAR etc[] = {'\\','e','t','c',0};
105 static const WCHAR glibdll[] = {'l','i','b','g','l','i','b','-','2','.','0','-','0','.','d','l','l',0};
106 WCHAR mono_dll_path[MAX_PATH+16], mono_bin_path[MAX_PATH+4];
107 WCHAR mono_lib_path[MAX_PATH+4], mono_etc_path[MAX_PATH+4];
108 char mono_lib_path_a[MAX_PATH], mono_etc_path_a[MAX_PATH];
110 char trace_setting[256];
112 if (This->mono_abi_version == -1)
113 MESSAGE("wine: Install the Windows version of Mono to run .NET executables\n");
115 if (This->mono_abi_version <= 0 || This->mono_abi_version > NUM_ABI_VERSIONS)
118 *result = &loaded_monos[This->mono_abi_version-1];
120 if ((*result)->is_shutdown)
122 ERR("Cannot load Mono after it has been shut down.");
127 if (!(*result)->mono_handle)
129 strcpyW(mono_bin_path, This->mono_path);
130 strcatW(mono_bin_path, bin);
131 set_environment(mono_bin_path);
133 strcpyW(mono_lib_path, This->mono_path);
134 strcatW(mono_lib_path, lib);
135 WideCharToMultiByte(CP_UTF8, 0, mono_lib_path, -1, mono_lib_path_a, MAX_PATH, NULL, NULL);
137 strcpyW(mono_etc_path, This->mono_path);
138 strcatW(mono_etc_path, etc);
139 WideCharToMultiByte(CP_UTF8, 0, mono_etc_path, -1, mono_etc_path_a, MAX_PATH, NULL, NULL);
141 if (!find_mono_dll(This->mono_path, mono_dll_path, This->mono_abi_version)) goto fail;
143 (*result)->mono_handle = LoadLibraryW(mono_dll_path);
145 if (!(*result)->mono_handle) goto fail;
147 #define LOAD_MONO_FUNCTION(x) do { \
148 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
149 if (!(*result)->x) { \
154 LOAD_MONO_FUNCTION(mono_assembly_get_image);
155 LOAD_MONO_FUNCTION(mono_assembly_open);
156 LOAD_MONO_FUNCTION(mono_config_parse);
157 LOAD_MONO_FUNCTION(mono_class_from_mono_type);
158 LOAD_MONO_FUNCTION(mono_class_from_name);
159 LOAD_MONO_FUNCTION(mono_class_get_method_from_name);
160 LOAD_MONO_FUNCTION(mono_domain_assembly_open);
161 LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook);
162 LOAD_MONO_FUNCTION(mono_jit_exec);
163 LOAD_MONO_FUNCTION(mono_jit_init);
164 LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
165 LOAD_MONO_FUNCTION(mono_object_get_domain);
166 LOAD_MONO_FUNCTION(mono_object_new);
167 LOAD_MONO_FUNCTION(mono_object_unbox);
168 LOAD_MONO_FUNCTION(mono_profiler_install);
169 LOAD_MONO_FUNCTION(mono_reflection_type_from_name);
170 LOAD_MONO_FUNCTION(mono_runtime_invoke);
171 LOAD_MONO_FUNCTION(mono_runtime_object_init);
172 LOAD_MONO_FUNCTION(mono_runtime_quit);
173 LOAD_MONO_FUNCTION(mono_set_dirs);
174 LOAD_MONO_FUNCTION(mono_stringify_assembly_name);
176 /* GLib imports obsoleted by the 2.0 ABI */
177 if (This->mono_abi_version == 1)
179 (*result)->glib_handle = LoadLibraryW(glibdll);
180 if (!(*result)->glib_handle) goto fail;
182 (*result)->mono_free = (void*)GetProcAddress((*result)->glib_handle, "g_free");
183 if (!(*result)->mono_free) goto fail;
187 LOAD_MONO_FUNCTION(mono_free);
190 #undef LOAD_MONO_FUNCTION
192 #define LOAD_OPT_VOID_MONO_FUNCTION(x) do { \
193 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
194 if (!(*result)->x) { \
195 (*result)->x = do_nothing; \
199 LOAD_OPT_VOID_MONO_FUNCTION(mono_runtime_set_shutting_down);
200 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_pool_cleanup);
201 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_suspend_all_other_threads);
202 LOAD_OPT_VOID_MONO_FUNCTION(mono_threads_set_shutting_down);
204 #undef LOAD_OPT_VOID_MONO_FUNCTION
206 (*result)->mono_profiler_install((MonoProfiler*)*result, mono_shutdown_callback_fn);
208 (*result)->mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
210 (*result)->mono_config_parse(NULL);
212 (*result)->mono_install_assembly_preload_hook(mono_assembly_search_hook_fn, *result);
214 trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
218 (*result)->mono_jit_set_trace_options(trace_setting);
225 ERR("Could not load Mono into this process\n");
226 FreeLibrary((*result)->mono_handle);
227 FreeLibrary((*result)->glib_handle);
228 (*result)->mono_handle = NULL;
229 (*result)->glib_handle = NULL;
233 static void mono_shutdown_callback_fn(MonoProfiler *prof)
235 loaded_mono *mono = (loaded_mono*)prof;
237 mono->is_shutdown = TRUE;
240 static HRESULT CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo *This, RuntimeHost **result)
243 loaded_mono *ploaded_mono;
245 if (This->loaded_runtime)
247 *result = This->loaded_runtime;
251 EnterCriticalSection(&runtime_list_cs);
253 hr = load_mono(This, &ploaded_mono);
256 hr = RuntimeHost_Construct(This, ploaded_mono, &This->loaded_runtime);
258 LeaveCriticalSection(&runtime_list_cs);
261 *result = This->loaded_runtime;
266 void unload_all_runtimes(void)
270 for (i=0; i<NUM_ABI_VERSIONS; i++)
272 loaded_mono *mono = &loaded_monos[i];
273 if (mono->mono_handle && mono->is_started && !mono->is_shutdown)
275 /* Copied from Mono's ves_icall_System_Environment_Exit */
276 mono->mono_threads_set_shutting_down();
277 mono->mono_runtime_set_shutting_down();
278 mono->mono_thread_pool_cleanup();
279 mono->mono_thread_suspend_all_other_threads();
280 mono->mono_runtime_quit();
284 for (i=0; i<NUM_RUNTIMES; i++)
285 if (runtimes[i].loaded_runtime)
286 RuntimeHost_Destroy(runtimes[i].loaded_runtime);
289 void expect_no_runtimes(void)
293 for (i=0; i<NUM_ABI_VERSIONS; i++)
295 loaded_mono *mono = &loaded_monos[i];
296 if (mono->mono_handle && mono->is_started && !mono->is_shutdown)
298 ERR("Process exited with a Mono runtime loaded.\n");
304 static HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
308 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
310 if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
311 IsEqualGUID( riid, &IID_IUnknown ) )
317 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
318 return E_NOINTERFACE;
321 ICLRRuntimeInfo_AddRef( iface );
326 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
331 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
336 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
337 LPWSTR pwzBuffer, DWORD *pcchBuffer)
339 struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
340 DWORD buffer_size = *pcchBuffer;
345 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
347 size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
349 assert(size <= sizeof(version));
351 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
355 if (buffer_size >= *pcchBuffer)
356 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
358 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
364 static BOOL get_install_root(LPWSTR install_dir)
366 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};
367 const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
372 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
376 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
386 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
387 LPWSTR pwzBuffer, DWORD *pcchBuffer)
389 static const WCHAR slash[] = {'\\',0};
390 DWORD buffer_size = *pcchBuffer;
391 WCHAR system_dir[MAX_PATH];
392 WCHAR version[MAX_PATH];
393 DWORD version_size, size;
396 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
398 if (!get_install_root(system_dir))
400 ERR("error reading registry key for installroot\n");
405 version_size = MAX_PATH;
406 ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
407 lstrcatW(system_dir, version);
408 lstrcatW(system_dir, slash);
409 size = lstrlenW(system_dir) + 1;
416 if (buffer_size >= size)
417 strcpyW(pwzBuffer, system_dir);
419 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
425 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
426 HANDLE hndProcess, BOOL *pbLoaded)
428 FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
433 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
434 UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
436 FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
441 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
442 LPCWSTR pwzDllName, HMODULE *phndModule)
444 WCHAR version[MAX_PATH];
448 TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
450 cchBuffer = MAX_PATH;
451 hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
452 if (FAILED(hr)) return hr;
454 return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
457 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
458 LPCSTR pszProcName, LPVOID *ppProc)
460 FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
465 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
466 REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
468 struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
472 TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
474 hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
477 hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
482 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
485 FIXME("%p %p\n", iface, pbLoadable);
490 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
491 DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
493 FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
498 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
499 DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
501 FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
506 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
508 FIXME("%p\n", iface);
513 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
514 BOOL *pbStarted, DWORD *pdwStartupFlags)
516 FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
521 const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
522 CLRRuntimeInfo_QueryInterface,
523 CLRRuntimeInfo_AddRef,
524 CLRRuntimeInfo_Release,
525 CLRRuntimeInfo_GetVersionString,
526 CLRRuntimeInfo_GetRuntimeDirectory,
527 CLRRuntimeInfo_IsLoaded,
528 CLRRuntimeInfo_LoadErrorString,
529 CLRRuntimeInfo_LoadLibrary,
530 CLRRuntimeInfo_GetProcAddress,
531 CLRRuntimeInfo_GetInterface,
532 CLRRuntimeInfo_IsLoadable,
533 CLRRuntimeInfo_SetDefaultStartupFlags,
534 CLRRuntimeInfo_GetDefaultStartupFlags,
535 CLRRuntimeInfo_BindAsLegacyV2Runtime,
536 CLRRuntimeInfo_IsStarted
539 HRESULT ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo *iface, RuntimeHost **result)
541 struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
543 assert(This->ICLRRuntimeInfo_vtbl == &CLRRuntimeInfoVtbl);
545 return CLRRuntimeInfo_GetRuntimeHost(This, result);
548 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version)
550 static const WCHAR mono_dll[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
551 static const WCHAR libmono_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
552 static const WCHAR mono2_dll[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
553 static const WCHAR libmono2_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
554 DWORD attributes=INVALID_FILE_ATTRIBUTES;
556 if (abi_version == 1)
558 strcpyW(dll_path, path);
559 strcatW(dll_path, mono_dll);
560 attributes = GetFileAttributesW(dll_path);
562 if (attributes == INVALID_FILE_ATTRIBUTES)
564 strcpyW(dll_path, path);
565 strcatW(dll_path, libmono_dll);
566 attributes = GetFileAttributesW(dll_path);
569 else if (abi_version == 2)
571 strcpyW(dll_path, path);
572 strcatW(dll_path, mono2_dll);
573 attributes = GetFileAttributesW(dll_path);
575 if (attributes == INVALID_FILE_ATTRIBUTES)
577 strcpyW(dll_path, path);
578 strcatW(dll_path, libmono2_dll);
579 attributes = GetFileAttributesW(dll_path);
583 return (attributes != INVALID_FILE_ATTRIBUTES);
586 static BOOL get_mono_path_from_registry(LPWSTR path, int abi_version)
588 static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
589 static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
590 static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
591 static const WCHAR slash[] = {'\\',0};
593 WCHAR version[64], version_key[MAX_PATH];
596 WCHAR dll_path[MAX_PATH];
598 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
601 len = sizeof(version);
602 if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
609 lstrcpyW(version_key, mono_key);
610 lstrcatW(version_key, slash);
611 lstrcatW(version_key, version);
613 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
616 len = sizeof(WCHAR) * MAX_PATH;
617 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
624 return find_mono_dll(path, dll_path, abi_version);
627 static BOOL get_mono_path_from_folder(LPCWSTR folder, LPWSTR mono_path, int abi_version)
629 static const WCHAR mono_one_dot_zero[] = {'\\','m','o','n','o','-','1','.','0', 0};
630 static const WCHAR mono_two_dot_zero[] = {'\\','m','o','n','o','-','2','.','0', 0};
631 WCHAR mono_dll_path[MAX_PATH];
634 strcpyW(mono_path, folder);
636 if (abi_version == 1)
637 strcatW(mono_path, mono_one_dot_zero);
638 else if (abi_version == 2)
639 strcatW(mono_path, mono_two_dot_zero);
641 found = find_mono_dll(mono_path, mono_dll_path, abi_version);
646 static BOOL get_mono_path(LPWSTR path, int abi_version)
648 static const WCHAR subdir_mono[] = {'\\','m','o','n','o',0};
649 static const WCHAR sibling_mono[] = {'\\','.','.','\\','m','o','n','o',0};
650 WCHAR base_path[MAX_PATH];
651 const char *unix_data_dir;
654 static WCHAR* (CDECL *wine_get_dos_file_name)(const char*);
656 /* First try c:\windows\mono */
657 GetWindowsDirectoryW(base_path, MAX_PATH);
658 strcatW(base_path, subdir_mono);
660 if (get_mono_path_from_folder(base_path, path, abi_version))
663 /* Next: /usr/share/wine/mono */
664 unix_data_dir = wine_get_data_dir();
668 unix_data_dir = wine_get_build_dir();
674 if (!wine_get_dos_file_name)
675 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
677 if (wine_get_dos_file_name)
679 dos_data_dir = wine_get_dos_file_name(unix_data_dir);
683 strcpyW(base_path, dos_data_dir);
684 strcatW(base_path, build_tree ? sibling_mono : subdir_mono);
686 HeapFree(GetProcessHeap(), 0, dos_data_dir);
688 if (get_mono_path_from_folder(base_path, path, abi_version))
694 /* Last: the registry */
695 return get_mono_path_from_registry(path, abi_version);
698 static void find_runtimes(void)
701 static const WCHAR libmono[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
702 static const WCHAR mscorlib[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
703 WCHAR mono_path[MAX_PATH], lib_path[MAX_PATH];
704 BOOL any_runtimes_found = FALSE;
706 if (runtimes_initialized) return;
708 EnterCriticalSection(&runtime_list_cs);
710 if (runtimes_initialized) goto end;
712 for (abi_version=NUM_ABI_VERSIONS; abi_version>0; abi_version--)
714 if (!get_mono_path(mono_path, abi_version))
717 for (i=0; i<NUM_RUNTIMES; i++)
719 if (runtimes[i].mono_abi_version == 0)
721 strcpyW(lib_path, mono_path);
722 strcatW(lib_path, libmono);
723 strcatW(lib_path, runtimes[i].mono_libdir);
724 strcatW(lib_path, mscorlib);
726 if (GetFileAttributesW(lib_path) != INVALID_FILE_ATTRIBUTES)
728 runtimes[i].mono_abi_version = abi_version;
730 strcpyW(runtimes[i].mono_path, mono_path);
731 strcpyW(runtimes[i].mscorlib_path, lib_path);
733 any_runtimes_found = TRUE;
739 if (!any_runtimes_found)
741 /* Report all runtimes are available if Mono isn't installed.
742 * FIXME: Remove this when Mono is properly packaged. */
743 for (i=0; i<NUM_RUNTIMES; i++)
744 runtimes[i].mono_abi_version = -1;
747 runtimes_initialized = 1;
750 LeaveCriticalSection(&runtime_list_cs);
753 struct InstalledRuntimeEnum
755 const struct IEnumUnknownVtbl *Vtbl;
760 const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
762 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface,
766 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
768 if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
769 IsEqualGUID( riid, &IID_IUnknown ) )
775 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
776 return E_NOINTERFACE;
779 IEnumUnknown_AddRef( iface );
784 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
786 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
787 ULONG ref = InterlockedIncrement(&This->ref);
789 TRACE("(%p) refcount=%u\n", iface, ref);
794 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
796 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
797 ULONG ref = InterlockedDecrement(&This->ref);
799 TRACE("(%p) refcount=%u\n", iface, ref);
803 HeapFree(GetProcessHeap(), 0, This);
809 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
810 IUnknown **rgelt, ULONG *pceltFetched)
812 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
817 TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
819 while (num_fetched < celt)
821 if (This->pos >= NUM_RUNTIMES)
826 if (runtimes[This->pos].mono_abi_version)
828 item = (IUnknown*)&runtimes[This->pos];
829 IUnknown_AddRef(item);
830 rgelt[num_fetched] = item;
837 *pceltFetched = num_fetched;
842 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
844 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
848 TRACE("(%p,%u)\n", iface, celt);
850 while (num_fetched < celt)
852 if (This->pos >= NUM_RUNTIMES)
857 if (runtimes[This->pos].mono_abi_version)
867 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
869 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
871 TRACE("(%p)\n", iface);
878 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
880 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
881 struct InstalledRuntimeEnum *new_enum;
883 TRACE("(%p)\n", iface);
885 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
887 return E_OUTOFMEMORY;
889 new_enum->Vtbl = &InstalledRuntimeEnum_Vtbl;
891 new_enum->pos = This->pos;
893 *ppenum = (IEnumUnknown*)new_enum;
898 const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
899 InstalledRuntimeEnum_QueryInterface,
900 InstalledRuntimeEnum_AddRef,
901 InstalledRuntimeEnum_Release,
902 InstalledRuntimeEnum_Next,
903 InstalledRuntimeEnum_Skip,
904 InstalledRuntimeEnum_Reset,
905 InstalledRuntimeEnum_Clone
910 const struct ICLRMetaHostVtbl *CLRMetaHost_vtbl;
913 static const struct CLRMetaHost GlobalCLRMetaHost;
915 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
919 TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
921 if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
922 IsEqualGUID( riid, &IID_IUnknown ) )
928 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
929 return E_NOINTERFACE;
932 ICLRMetaHost_AddRef( iface );
937 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
942 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
947 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
953 if (version[0] == 'v')
956 if (!isdigit(*version))
959 while (isdigit(*version))
960 *major = *major * 10 + (*version++ - '0');
965 if (*version++ != '.' || !isdigit(*version))
968 while (isdigit(*version))
969 *minor = *minor * 10 + (*version++ - '0');
974 if (*version++ != '.' || !isdigit(*version))
977 while (isdigit(*version))
978 *build = *build * 10 + (*version++ - '0');
980 return *version == 0;
986 static HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
987 LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
990 DWORD major, minor, build;
992 TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
997 if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
999 ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
1000 return CLR_E_SHIM_RUNTIME;
1005 for (i=0; i<NUM_RUNTIMES; i++)
1007 if (runtimes[i].major == major && runtimes[i].minor == minor &&
1008 runtimes[i].build == build)
1010 if (runtimes[i].mono_abi_version)
1011 return IUnknown_QueryInterface((IUnknown*)&runtimes[i], iid, ppRuntime);
1014 ERR("Mono is missing %s runtime\n", debugstr_w(pwzVersion));
1015 return CLR_E_SHIM_RUNTIME;
1020 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
1021 return CLR_E_SHIM_RUNTIME;
1024 HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
1025 LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
1030 ULONG buffer_size=*pcchBuffer;
1032 TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
1034 hr = assembly_create(&assembly, pwzFilePath);
1038 hr = assembly_get_runtime_version(assembly, &version);
1042 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
1046 if (buffer_size >= *pcchBuffer)
1047 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
1049 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
1053 assembly_release(assembly);
1059 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
1060 IEnumUnknown **ppEnumerator)
1062 struct InstalledRuntimeEnum *new_enum;
1064 TRACE("%p\n", ppEnumerator);
1068 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
1070 return E_OUTOFMEMORY;
1072 new_enum->Vtbl = &InstalledRuntimeEnum_Vtbl;
1076 *ppEnumerator = (IEnumUnknown*)new_enum;
1081 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
1082 HANDLE hndProcess, IEnumUnknown **ppEnumerator)
1084 FIXME("%p %p\n", hndProcess, ppEnumerator);
1089 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
1090 RuntimeLoadedCallbackFnPtr pCallbackFunction)
1092 FIXME("%p\n", pCallbackFunction);
1097 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
1098 REFIID riid, LPVOID *ppUnk)
1100 FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
1105 static HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
1107 FIXME("%i: stub\n", iExitCode);
1109 ExitProcess(iExitCode);
1112 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
1114 CLRMetaHost_QueryInterface,
1116 CLRMetaHost_Release,
1117 CLRMetaHost_GetRuntime,
1118 CLRMetaHost_GetVersionFromFile,
1119 CLRMetaHost_EnumerateInstalledRuntimes,
1120 CLRMetaHost_EnumerateLoadedRuntimes,
1121 CLRMetaHost_RequestRuntimeLoadedNotification,
1122 CLRMetaHost_QueryLegacyV2RuntimeBinding,
1123 CLRMetaHost_ExitProcess
1126 static const struct CLRMetaHost GlobalCLRMetaHost = {
1130 extern HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1132 return ICLRMetaHost_QueryInterface((ICLRMetaHost*)&GlobalCLRMetaHost, riid, ppobj);
1135 static MonoAssembly* mono_assembly_search_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data)
1137 loaded_mono *mono = user_data;
1139 MonoAssembly *result=NULL;
1140 char *stringname=NULL;
1142 int stringnameW_size;
1143 IAssemblyCache *asmcache;
1145 WCHAR path[MAX_PATH];
1147 MonoImageOpenStatus stat;
1148 static WCHAR fusiondll[] = {'f','u','s','i','o','n',0};
1149 HMODULE hfusion=NULL;
1150 static HRESULT WINAPI (*pCreateAssemblyCache)(IAssemblyCache**,DWORD);
1152 stringname = mono->mono_stringify_assembly_name(aname);
1154 TRACE("%s\n", debugstr_a(stringname));
1156 if (!stringname) return NULL;
1158 /* FIXME: We should search the given paths before the GAC. */
1160 if (!pCreateAssemblyCache)
1162 hr = LoadLibraryShim(fusiondll, NULL, NULL, &hfusion);
1166 pCreateAssemblyCache = (void*)GetProcAddress(hfusion, "CreateAssemblyCache");
1167 if (!pCreateAssemblyCache)
1173 hr = pCreateAssemblyCache(&asmcache, 0);
1177 stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, stringname, -1, NULL, 0);
1179 stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1181 MultiByteToWideChar(CP_UTF8, 0, stringname, -1, stringnameW, stringnameW_size);
1187 info.cbAssemblyInfo = sizeof(info);
1188 info.pszCurrentAssemblyPathBuf = path;
1189 info.cchBuf = MAX_PATH;
1192 hr = IAssemblyCache_QueryAssemblyInfo(asmcache, 0, stringnameW, &info);
1195 HeapFree(GetProcessHeap(), 0, stringnameW);
1197 IAssemblyCache_Release(asmcache);
1202 TRACE("found: %s\n", debugstr_w(path));
1208 result = mono->mono_assembly_open(pathA, &stat);
1211 ERR("Failed to load %s, status=%u\n", debugstr_w(path), stat);
1213 HeapFree(GetProcessHeap(), 0, pathA);
1217 mono->mono_free(stringname);
1222 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1223 DWORD startup_flags, DWORD runtimeinfo_flags, BOOL legacy, ICLRRuntimeInfo **result)
1225 static const WCHAR dotconfig[] = {'.','c','o','n','f','i','g',0};
1226 static const DWORD supported_startup_flags = 0;
1227 static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1229 WCHAR local_version[MAX_PATH];
1230 ULONG local_version_size = MAX_PATH;
1231 WCHAR local_config_file[MAX_PATH];
1233 parsed_config_file parsed_config;
1235 if (startup_flags & ~supported_startup_flags)
1236 FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
1238 if (runtimeinfo_flags & ~supported_runtime_flags)
1239 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
1241 if (exefile && !config_file)
1243 strcpyW(local_config_file, exefile);
1244 strcatW(local_config_file, dotconfig);
1246 config_file = local_config_file;
1252 hr = parse_config_file(config_file, &parsed_config);
1256 supported_runtime *entry;
1257 LIST_FOR_EACH_ENTRY(entry, &parsed_config.supported_runtimes, supported_runtime, entry)
1259 hr = CLRMetaHost_GetRuntime(0, entry->version, &IID_ICLRRuntimeInfo, (void**)result);
1269 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file), hr);
1272 free_parsed_config_file(&parsed_config);
1278 if (exefile && !version)
1280 hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1282 version = local_version;
1284 if (FAILED(hr)) return hr;
1289 return CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1292 if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1303 if (runtimes[i].mono_abi_version)
1304 return IUnknown_QueryInterface((IUnknown*)&runtimes[i],
1305 &IID_ICLRRuntimeInfo, (void**)result);
1308 ERR("No %s.NET runtime installed\n", legacy ? "legacy " : "");
1310 return CLR_E_SHIM_RUNTIME;
1313 return CLR_E_SHIM_RUNTIME;
1316 HRESULT force_get_runtime_info(ICLRRuntimeInfo **result)
1318 return IUnknown_QueryInterface((IUnknown*)&runtimes[0], &IID_ICLRRuntimeInfo, (void**)result);