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 set_environment(LPCWSTR bin_path)
81 WCHAR path_env[MAX_PATH];
84 static const WCHAR pathW[] = {'P','A','T','H',0};
86 /* We have to modify PATH as Mono loads other DLLs from this directory. */
87 GetEnvironmentVariableW(pathW, path_env, sizeof(path_env)/sizeof(WCHAR));
88 len = strlenW(path_env);
89 path_env[len++] = ';';
90 strcpyW(path_env+len, bin_path);
91 SetEnvironmentVariableW(pathW, path_env);
94 static HRESULT load_mono(CLRRuntimeInfo *This, loaded_mono **result)
96 static const WCHAR bin[] = {'\\','b','i','n',0};
97 static const WCHAR lib[] = {'\\','l','i','b',0};
98 static const WCHAR etc[] = {'\\','e','t','c',0};
99 static const WCHAR glibdll[] = {'l','i','b','g','l','i','b','-','2','.','0','-','0','.','d','l','l',0};
100 WCHAR mono_dll_path[MAX_PATH+16], mono_bin_path[MAX_PATH+4];
101 WCHAR mono_lib_path[MAX_PATH+4], mono_etc_path[MAX_PATH+4];
102 char mono_lib_path_a[MAX_PATH], mono_etc_path_a[MAX_PATH];
104 char trace_setting[256];
106 if (This->mono_abi_version == -1)
107 MESSAGE("wine: Install the Windows version of Mono to run .NET executables\n");
109 if (This->mono_abi_version <= 0 || This->mono_abi_version > NUM_ABI_VERSIONS)
112 *result = &loaded_monos[This->mono_abi_version-1];
114 if (!(*result)->mono_handle)
116 strcpyW(mono_bin_path, This->mono_path);
117 strcatW(mono_bin_path, bin);
118 set_environment(mono_bin_path);
120 strcpyW(mono_lib_path, This->mono_path);
121 strcatW(mono_lib_path, lib);
122 WideCharToMultiByte(CP_UTF8, 0, mono_lib_path, -1, mono_lib_path_a, MAX_PATH, NULL, NULL);
124 strcpyW(mono_etc_path, This->mono_path);
125 strcatW(mono_etc_path, etc);
126 WideCharToMultiByte(CP_UTF8, 0, mono_etc_path, -1, mono_etc_path_a, MAX_PATH, NULL, NULL);
128 if (!find_mono_dll(This->mono_path, mono_dll_path, This->mono_abi_version)) goto fail;
130 (*result)->mono_handle = LoadLibraryW(mono_dll_path);
132 if (!(*result)->mono_handle) goto fail;
134 #define LOAD_MONO_FUNCTION(x) do { \
135 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
136 if (!(*result)->x) { \
141 LOAD_MONO_FUNCTION(mono_assembly_get_image);
142 LOAD_MONO_FUNCTION(mono_assembly_open);
143 LOAD_MONO_FUNCTION(mono_config_parse);
144 LOAD_MONO_FUNCTION(mono_class_from_mono_type);
145 LOAD_MONO_FUNCTION(mono_class_from_name);
146 LOAD_MONO_FUNCTION(mono_class_get_method_from_name);
147 LOAD_MONO_FUNCTION(mono_domain_assembly_open);
148 LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook);
149 LOAD_MONO_FUNCTION(mono_jit_cleanup);
150 LOAD_MONO_FUNCTION(mono_jit_exec);
151 LOAD_MONO_FUNCTION(mono_jit_init);
152 LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
153 LOAD_MONO_FUNCTION(mono_object_get_domain);
154 LOAD_MONO_FUNCTION(mono_object_new);
155 LOAD_MONO_FUNCTION(mono_object_unbox);
156 LOAD_MONO_FUNCTION(mono_reflection_type_from_name);
157 LOAD_MONO_FUNCTION(mono_runtime_invoke);
158 LOAD_MONO_FUNCTION(mono_runtime_object_init);
159 LOAD_MONO_FUNCTION(mono_set_dirs);
160 LOAD_MONO_FUNCTION(mono_stringify_assembly_name);
162 /* GLib imports obsoleted by the 2.0 ABI */
163 if (This->mono_abi_version == 1)
165 (*result)->glib_handle = LoadLibraryW(glibdll);
166 if (!(*result)->glib_handle) goto fail;
168 (*result)->mono_free = (void*)GetProcAddress((*result)->glib_handle, "g_free");
169 if (!(*result)->mono_free) goto fail;
173 LOAD_MONO_FUNCTION(mono_free);
176 #undef LOAD_MONO_FUNCTION
178 (*result)->mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
180 (*result)->mono_config_parse(NULL);
182 (*result)->mono_install_assembly_preload_hook(mono_assembly_search_hook_fn, *result);
184 trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
188 (*result)->mono_jit_set_trace_options(trace_setting);
195 ERR("Could not load Mono into this process\n");
196 FreeLibrary((*result)->mono_handle);
197 FreeLibrary((*result)->glib_handle);
198 (*result)->mono_handle = NULL;
199 (*result)->glib_handle = NULL;
203 static HRESULT CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo *This, RuntimeHost **result)
206 loaded_mono *ploaded_mono;
208 if (This->loaded_runtime)
210 *result = This->loaded_runtime;
214 EnterCriticalSection(&runtime_list_cs);
216 hr = load_mono(This, &ploaded_mono);
219 hr = RuntimeHost_Construct(This, ploaded_mono, &This->loaded_runtime);
221 LeaveCriticalSection(&runtime_list_cs);
224 *result = This->loaded_runtime;
229 void unload_all_runtimes(void)
233 for (i=0; i<NUM_RUNTIMES; i++)
234 if (runtimes[i].loaded_runtime)
235 RuntimeHost_Destroy(runtimes[i].loaded_runtime);
238 static HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
242 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
244 if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
245 IsEqualGUID( riid, &IID_IUnknown ) )
251 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
252 return E_NOINTERFACE;
255 ICLRRuntimeInfo_AddRef( iface );
260 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
262 MSCOREE_LockModule();
266 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
268 MSCOREE_UnlockModule();
272 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
273 LPWSTR pwzBuffer, DWORD *pcchBuffer)
275 struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
276 DWORD buffer_size = *pcchBuffer;
281 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
283 size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
285 assert(size <= sizeof(version));
287 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
291 if (buffer_size >= *pcchBuffer)
292 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
294 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
300 static BOOL get_install_root(LPWSTR install_dir)
302 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};
303 const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
308 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
312 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
322 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
323 LPWSTR pwzBuffer, DWORD *pcchBuffer)
325 static const WCHAR slash[] = {'\\',0};
326 DWORD buffer_size = *pcchBuffer;
327 WCHAR system_dir[MAX_PATH];
328 WCHAR version[MAX_PATH];
329 DWORD version_size, size;
332 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
334 if (!get_install_root(system_dir))
336 ERR("error reading registry key for installroot\n");
341 version_size = MAX_PATH;
342 ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
343 lstrcatW(system_dir, version);
344 lstrcatW(system_dir, slash);
345 size = lstrlenW(system_dir) + 1;
352 if (buffer_size >= size)
353 strcpyW(pwzBuffer, system_dir);
355 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
361 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
362 HANDLE hndProcess, BOOL *pbLoaded)
364 FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
369 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
370 UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
372 FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
377 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
378 LPCWSTR pwzDllName, HMODULE *phndModule)
380 WCHAR version[MAX_PATH];
384 TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
386 cchBuffer = MAX_PATH;
387 hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
388 if (FAILED(hr)) return hr;
390 return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
393 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
394 LPCSTR pszProcName, LPVOID *ppProc)
396 FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
401 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
402 REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
404 struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
408 TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
410 hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
413 hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
418 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
421 FIXME("%p %p\n", iface, pbLoadable);
426 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
427 DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
429 FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
434 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
435 DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
437 FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
442 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
444 FIXME("%p\n", iface);
449 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
450 BOOL *pbStarted, DWORD *pdwStartupFlags)
452 FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
457 const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
458 CLRRuntimeInfo_QueryInterface,
459 CLRRuntimeInfo_AddRef,
460 CLRRuntimeInfo_Release,
461 CLRRuntimeInfo_GetVersionString,
462 CLRRuntimeInfo_GetRuntimeDirectory,
463 CLRRuntimeInfo_IsLoaded,
464 CLRRuntimeInfo_LoadErrorString,
465 CLRRuntimeInfo_LoadLibrary,
466 CLRRuntimeInfo_GetProcAddress,
467 CLRRuntimeInfo_GetInterface,
468 CLRRuntimeInfo_IsLoadable,
469 CLRRuntimeInfo_SetDefaultStartupFlags,
470 CLRRuntimeInfo_GetDefaultStartupFlags,
471 CLRRuntimeInfo_BindAsLegacyV2Runtime,
472 CLRRuntimeInfo_IsStarted
475 HRESULT ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo *iface, RuntimeHost **result)
477 struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
479 assert(This->ICLRRuntimeInfo_vtbl == &CLRRuntimeInfoVtbl);
481 return CLRRuntimeInfo_GetRuntimeHost(This, result);
484 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version)
486 static const WCHAR mono_dll[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
487 static const WCHAR libmono_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
488 static const WCHAR mono2_dll[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
489 static const WCHAR libmono2_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
490 DWORD attributes=INVALID_FILE_ATTRIBUTES;
492 if (abi_version == 1)
494 strcpyW(dll_path, path);
495 strcatW(dll_path, mono_dll);
496 attributes = GetFileAttributesW(dll_path);
498 if (attributes == INVALID_FILE_ATTRIBUTES)
500 strcpyW(dll_path, path);
501 strcatW(dll_path, libmono_dll);
502 attributes = GetFileAttributesW(dll_path);
505 else if (abi_version == 2)
507 strcpyW(dll_path, path);
508 strcatW(dll_path, mono2_dll);
509 attributes = GetFileAttributesW(dll_path);
511 if (attributes == INVALID_FILE_ATTRIBUTES)
513 strcpyW(dll_path, path);
514 strcatW(dll_path, libmono2_dll);
515 attributes = GetFileAttributesW(dll_path);
519 return (attributes != INVALID_FILE_ATTRIBUTES);
522 static BOOL get_mono_path_from_registry(LPWSTR path, int abi_version)
524 static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
525 static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
526 static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
527 static const WCHAR slash[] = {'\\',0};
529 WCHAR version[64], version_key[MAX_PATH];
532 WCHAR dll_path[MAX_PATH];
534 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
537 len = sizeof(version);
538 if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
545 lstrcpyW(version_key, mono_key);
546 lstrcatW(version_key, slash);
547 lstrcatW(version_key, version);
549 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
552 len = sizeof(WCHAR) * MAX_PATH;
553 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
560 return find_mono_dll(path, dll_path, abi_version);
563 static BOOL get_mono_path_from_folder(LPCWSTR folder, LPWSTR mono_path, int abi_version)
565 static const WCHAR mono_one_dot_zero[] = {'\\','m','o','n','o','-','1','.','0', 0};
566 static const WCHAR mono_two_dot_zero[] = {'\\','m','o','n','o','-','2','.','0', 0};
567 WCHAR mono_dll_path[MAX_PATH];
570 strcpyW(mono_path, folder);
572 if (abi_version == 1)
573 strcatW(mono_path, mono_one_dot_zero);
574 else if (abi_version == 2)
575 strcatW(mono_path, mono_two_dot_zero);
577 found = find_mono_dll(mono_path, mono_dll_path, abi_version);
582 static BOOL get_mono_path(LPWSTR path, int abi_version)
584 static const WCHAR subdir_mono[] = {'\\','m','o','n','o',0};
585 static const WCHAR sibling_mono[] = {'\\','.','.','\\','m','o','n','o',0};
586 WCHAR base_path[MAX_PATH];
587 const char *unix_data_dir;
590 static WCHAR* (CDECL *wine_get_dos_file_name)(const char*);
592 /* First try c:\windows\mono */
593 GetWindowsDirectoryW(base_path, MAX_PATH);
594 strcatW(base_path, subdir_mono);
596 if (get_mono_path_from_folder(base_path, path, abi_version))
599 /* Next: /usr/share/wine/mono */
600 unix_data_dir = wine_get_data_dir();
604 unix_data_dir = wine_get_build_dir();
610 if (!wine_get_dos_file_name)
611 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
613 if (wine_get_dos_file_name)
615 dos_data_dir = wine_get_dos_file_name(unix_data_dir);
619 strcpyW(base_path, dos_data_dir);
620 strcatW(base_path, build_tree ? sibling_mono : subdir_mono);
622 HeapFree(GetProcessHeap(), 0, dos_data_dir);
624 if (get_mono_path_from_folder(base_path, path, abi_version))
630 /* Last: the registry */
631 return get_mono_path_from_registry(path, abi_version);
634 static void find_runtimes(void)
637 static const WCHAR libmono[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
638 static const WCHAR mscorlib[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
639 WCHAR mono_path[MAX_PATH], lib_path[MAX_PATH];
640 BOOL any_runtimes_found = FALSE;
642 if (runtimes_initialized) return;
644 EnterCriticalSection(&runtime_list_cs);
646 if (runtimes_initialized) goto end;
648 for (abi_version=NUM_ABI_VERSIONS; abi_version>0; abi_version--)
650 if (!get_mono_path(mono_path, abi_version))
653 for (i=0; i<NUM_RUNTIMES; i++)
655 if (runtimes[i].mono_abi_version == 0)
657 strcpyW(lib_path, mono_path);
658 strcatW(lib_path, libmono);
659 strcatW(lib_path, runtimes[i].mono_libdir);
660 strcatW(lib_path, mscorlib);
662 if (GetFileAttributesW(lib_path) != INVALID_FILE_ATTRIBUTES)
664 runtimes[i].mono_abi_version = abi_version;
666 strcpyW(runtimes[i].mono_path, mono_path);
667 strcpyW(runtimes[i].mscorlib_path, lib_path);
669 any_runtimes_found = TRUE;
675 if (!any_runtimes_found)
677 /* Report all runtimes are available if Mono isn't installed.
678 * FIXME: Remove this when Mono is properly packaged. */
679 for (i=0; i<NUM_RUNTIMES; i++)
680 runtimes[i].mono_abi_version = -1;
683 runtimes_initialized = 1;
686 LeaveCriticalSection(&runtime_list_cs);
689 struct InstalledRuntimeEnum
691 const struct IEnumUnknownVtbl *Vtbl;
696 const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
698 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface,
702 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
704 if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
705 IsEqualGUID( riid, &IID_IUnknown ) )
711 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
712 return E_NOINTERFACE;
715 IEnumUnknown_AddRef( iface );
720 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
722 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
723 ULONG ref = InterlockedIncrement(&This->ref);
725 MSCOREE_LockModule();
727 TRACE("(%p) refcount=%u\n", iface, ref);
732 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
734 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
735 ULONG ref = InterlockedDecrement(&This->ref);
737 MSCOREE_UnlockModule();
739 TRACE("(%p) refcount=%u\n", iface, ref);
743 HeapFree(GetProcessHeap(), 0, This);
749 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
750 IUnknown **rgelt, ULONG *pceltFetched)
752 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
757 TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
759 while (num_fetched < celt)
761 if (This->pos >= NUM_RUNTIMES)
766 if (runtimes[This->pos].mono_abi_version)
768 item = (IUnknown*)&runtimes[This->pos];
769 IUnknown_AddRef(item);
770 rgelt[num_fetched] = item;
777 *pceltFetched = num_fetched;
782 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
784 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
788 TRACE("(%p,%u)\n", iface, celt);
790 while (num_fetched < celt)
792 if (This->pos >= NUM_RUNTIMES)
797 if (runtimes[This->pos].mono_abi_version)
807 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
809 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
811 TRACE("(%p)\n", iface);
818 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
820 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
821 struct InstalledRuntimeEnum *new_enum;
823 TRACE("(%p)\n", iface);
825 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
827 return E_OUTOFMEMORY;
829 new_enum->Vtbl = &InstalledRuntimeEnum_Vtbl;
831 new_enum->pos = This->pos;
833 *ppenum = (IEnumUnknown*)new_enum;
838 const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
839 InstalledRuntimeEnum_QueryInterface,
840 InstalledRuntimeEnum_AddRef,
841 InstalledRuntimeEnum_Release,
842 InstalledRuntimeEnum_Next,
843 InstalledRuntimeEnum_Skip,
844 InstalledRuntimeEnum_Reset,
845 InstalledRuntimeEnum_Clone
850 const struct ICLRMetaHostVtbl *CLRMetaHost_vtbl;
853 static const struct CLRMetaHost GlobalCLRMetaHost;
855 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
859 TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
861 if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
862 IsEqualGUID( riid, &IID_IUnknown ) )
868 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
869 return E_NOINTERFACE;
872 ICLRMetaHost_AddRef( iface );
877 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
879 MSCOREE_LockModule();
883 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
885 MSCOREE_UnlockModule();
889 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
895 if (version[0] == 'v')
898 if (!isdigit(*version))
901 while (isdigit(*version))
902 *major = *major * 10 + (*version++ - '0');
907 if (*version++ != '.' || !isdigit(*version))
910 while (isdigit(*version))
911 *minor = *minor * 10 + (*version++ - '0');
916 if (*version++ != '.' || !isdigit(*version))
919 while (isdigit(*version))
920 *build = *build * 10 + (*version++ - '0');
922 return *version == 0;
928 static HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
929 LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
932 DWORD major, minor, build;
934 TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
939 if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
941 ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
942 return CLR_E_SHIM_RUNTIME;
947 for (i=0; i<NUM_RUNTIMES; i++)
949 if (runtimes[i].major == major && runtimes[i].minor == minor &&
950 runtimes[i].build == build)
952 if (runtimes[i].mono_abi_version)
953 return IUnknown_QueryInterface((IUnknown*)&runtimes[i], iid, ppRuntime);
956 ERR("Mono is missing %s runtime\n", debugstr_w(pwzVersion));
957 return CLR_E_SHIM_RUNTIME;
962 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
963 return CLR_E_SHIM_RUNTIME;
966 static HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
967 LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
972 ULONG buffer_size=*pcchBuffer;
974 TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
976 hr = assembly_create(&assembly, pwzFilePath);
980 hr = assembly_get_runtime_version(assembly, &version);
984 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
988 if (buffer_size >= *pcchBuffer)
989 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
991 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
995 assembly_release(assembly);
1001 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
1002 IEnumUnknown **ppEnumerator)
1004 struct InstalledRuntimeEnum *new_enum;
1006 TRACE("%p\n", ppEnumerator);
1010 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
1012 return E_OUTOFMEMORY;
1014 new_enum->Vtbl = &InstalledRuntimeEnum_Vtbl;
1018 *ppEnumerator = (IEnumUnknown*)new_enum;
1023 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
1024 HANDLE hndProcess, IEnumUnknown **ppEnumerator)
1026 FIXME("%p %p\n", hndProcess, ppEnumerator);
1031 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
1032 RuntimeLoadedCallbackFnPtr pCallbackFunction)
1034 FIXME("%p\n", pCallbackFunction);
1039 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
1040 REFIID riid, LPVOID *ppUnk)
1042 FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
1047 static HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
1049 FIXME("%i: stub\n", iExitCode);
1051 ExitProcess(iExitCode);
1054 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
1056 CLRMetaHost_QueryInterface,
1058 CLRMetaHost_Release,
1059 CLRMetaHost_GetRuntime,
1060 CLRMetaHost_GetVersionFromFile,
1061 CLRMetaHost_EnumerateInstalledRuntimes,
1062 CLRMetaHost_EnumerateLoadedRuntimes,
1063 CLRMetaHost_RequestRuntimeLoadedNotification,
1064 CLRMetaHost_QueryLegacyV2RuntimeBinding,
1065 CLRMetaHost_ExitProcess
1068 static const struct CLRMetaHost GlobalCLRMetaHost = {
1072 extern HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1074 return ICLRMetaHost_QueryInterface((ICLRMetaHost*)&GlobalCLRMetaHost, riid, ppobj);
1077 static MonoAssembly* mono_assembly_search_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data)
1079 loaded_mono *mono = user_data;
1081 MonoAssembly *result=NULL;
1082 char *stringname=NULL;
1084 int stringnameW_size;
1085 IAssemblyCache *asmcache;
1087 WCHAR path[MAX_PATH];
1089 MonoImageOpenStatus stat;
1090 static WCHAR fusiondll[] = {'f','u','s','i','o','n',0};
1091 HMODULE hfusion=NULL;
1092 static HRESULT WINAPI (*pCreateAssemblyCache)(IAssemblyCache**,DWORD);
1094 stringname = mono->mono_stringify_assembly_name(aname);
1096 TRACE("%s\n", debugstr_a(stringname));
1098 if (!stringname) return NULL;
1100 /* FIXME: We should search the given paths before the GAC. */
1102 if (!pCreateAssemblyCache)
1104 hr = LoadLibraryShim(fusiondll, NULL, NULL, &hfusion);
1108 pCreateAssemblyCache = (void*)GetProcAddress(hfusion, "CreateAssemblyCache");
1109 if (!pCreateAssemblyCache)
1115 hr = pCreateAssemblyCache(&asmcache, 0);
1119 stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, stringname, -1, NULL, 0);
1121 stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1123 MultiByteToWideChar(CP_UTF8, 0, stringname, -1, stringnameW, stringnameW_size);
1129 info.cbAssemblyInfo = sizeof(info);
1130 info.pszCurrentAssemblyPathBuf = path;
1131 info.cchBuf = MAX_PATH;
1134 hr = IAssemblyCache_QueryAssemblyInfo(asmcache, 0, stringnameW, &info);
1137 HeapFree(GetProcessHeap(), 0, stringnameW);
1139 IAssemblyCache_Release(asmcache);
1144 TRACE("found: %s\n", debugstr_w(path));
1150 result = mono->mono_assembly_open(pathA, &stat);
1153 ERR("Failed to load %s, status=%u\n", debugstr_w(path), stat);
1155 HeapFree(GetProcessHeap(), 0, pathA);
1159 mono->mono_free(stringname);
1164 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1165 DWORD startup_flags, DWORD runtimeinfo_flags, BOOL legacy, ICLRRuntimeInfo **result)
1167 static const WCHAR dotconfig[] = {'.','c','o','n','f','i','g',0};
1168 static const DWORD supported_startup_flags = 0;
1169 static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1171 WCHAR local_version[MAX_PATH];
1172 ULONG local_version_size = MAX_PATH;
1173 WCHAR local_config_file[MAX_PATH];
1175 parsed_config_file parsed_config;
1177 if (startup_flags & ~supported_startup_flags)
1178 FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
1180 if (runtimeinfo_flags & ~supported_runtime_flags)
1181 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
1183 if (exefile && !config_file)
1185 strcpyW(local_config_file, exefile);
1186 strcatW(local_config_file, dotconfig);
1188 config_file = local_config_file;
1194 hr = parse_config_file(config_file, &parsed_config);
1198 supported_runtime *entry;
1199 LIST_FOR_EACH_ENTRY(entry, &parsed_config.supported_runtimes, supported_runtime, entry)
1201 hr = CLRMetaHost_GetRuntime(0, entry->version, &IID_ICLRRuntimeInfo, (void**)result);
1211 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file), hr);
1214 free_parsed_config_file(&parsed_config);
1220 if (exefile && !version)
1222 hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1224 version = local_version;
1226 if (FAILED(hr)) return hr;
1231 return CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1234 if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1245 if (runtimes[i].mono_abi_version)
1246 return IUnknown_QueryInterface((IUnknown*)&runtimes[i],
1247 &IID_ICLRRuntimeInfo, (void**)result);
1250 ERR("No %s.NET runtime installed\n", legacy ? "legacy " : "");
1252 return CLR_E_SHIM_RUNTIME;
1255 return CLR_E_SHIM_RUNTIME;
1258 HRESULT force_get_runtime_info(ICLRRuntimeInfo **result)
1260 return IUnknown_QueryInterface((IUnknown*)&runtimes[0], &IID_ICLRRuntimeInfo, (void**)result);