mscoree: Allow capital for version number.
[wine/multimedia.git] / dlls / mscoree / metahost.c
blobf877b1eeddf09ac4cea0aad421de9d58311a5752
1 /*
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
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <assert.h>
25 #define COBJMACROS
27 #include "wine/unicode.h"
28 #include "wine/library.h"
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winreg.h"
32 #include "ole2.h"
34 #include "corerror.h"
35 #include "cor.h"
36 #include "mscoree.h"
37 #include "corhdr.h"
38 #include "cordebug.h"
39 #include "metahost.h"
40 #include "fusion.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];
87 int len;
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 void missing_runtime_message(const CLRRuntimeInfo *This)
105 if (This->major == 1)
106 MESSAGE("wine: Install Mono 2.6 for Windows to run .NET 1.1 applications.\n");
107 else if (This->major == 2)
108 MESSAGE("wine: Install Mono for Windows to run .NET 2.0 applications.\n");
109 else if (This->major == 4)
110 MESSAGE("wine: Install Mono 2.8 or greater for Windows to run .NET 4.0 applications.\n");
113 static HRESULT load_mono(CLRRuntimeInfo *This, loaded_mono **result)
115 static const WCHAR bin[] = {'\\','b','i','n',0};
116 static const WCHAR lib[] = {'\\','l','i','b',0};
117 static const WCHAR etc[] = {'\\','e','t','c',0};
118 static const WCHAR glibdll[] = {'l','i','b','g','l','i','b','-','2','.','0','-','0','.','d','l','l',0};
119 WCHAR mono_dll_path[MAX_PATH+16], mono_bin_path[MAX_PATH+4];
120 WCHAR mono_lib_path[MAX_PATH+4], mono_etc_path[MAX_PATH+4];
121 char mono_lib_path_a[MAX_PATH], mono_etc_path_a[MAX_PATH];
122 int trace_size;
123 char trace_setting[256];
125 if (This->mono_abi_version <= 0 || This->mono_abi_version > NUM_ABI_VERSIONS)
127 missing_runtime_message(This);
128 return E_FAIL;
131 *result = &loaded_monos[This->mono_abi_version-1];
133 if ((*result)->is_shutdown)
135 ERR("Cannot load Mono after it has been shut down.\n");
136 *result = NULL;
137 return E_FAIL;
140 if (!(*result)->mono_handle)
142 strcpyW(mono_bin_path, This->mono_path);
143 strcatW(mono_bin_path, bin);
144 set_environment(mono_bin_path);
146 strcpyW(mono_lib_path, This->mono_path);
147 strcatW(mono_lib_path, lib);
148 WideCharToMultiByte(CP_UTF8, 0, mono_lib_path, -1, mono_lib_path_a, MAX_PATH, NULL, NULL);
150 strcpyW(mono_etc_path, This->mono_path);
151 strcatW(mono_etc_path, etc);
152 WideCharToMultiByte(CP_UTF8, 0, mono_etc_path, -1, mono_etc_path_a, MAX_PATH, NULL, NULL);
154 if (!find_mono_dll(This->mono_path, mono_dll_path, This->mono_abi_version)) goto fail;
156 (*result)->mono_handle = LoadLibraryW(mono_dll_path);
158 if (!(*result)->mono_handle) goto fail;
160 #define LOAD_MONO_FUNCTION(x) do { \
161 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
162 if (!(*result)->x) { \
163 goto fail; \
165 } while (0);
167 LOAD_MONO_FUNCTION(mono_assembly_get_image);
168 LOAD_MONO_FUNCTION(mono_assembly_open);
169 LOAD_MONO_FUNCTION(mono_config_parse);
170 LOAD_MONO_FUNCTION(mono_class_from_mono_type);
171 LOAD_MONO_FUNCTION(mono_class_from_name);
172 LOAD_MONO_FUNCTION(mono_class_get_method_from_name);
173 LOAD_MONO_FUNCTION(mono_domain_assembly_open);
174 LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook);
175 LOAD_MONO_FUNCTION(mono_jit_exec);
176 LOAD_MONO_FUNCTION(mono_jit_init);
177 LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
178 LOAD_MONO_FUNCTION(mono_object_get_domain);
179 LOAD_MONO_FUNCTION(mono_object_new);
180 LOAD_MONO_FUNCTION(mono_object_unbox);
181 LOAD_MONO_FUNCTION(mono_profiler_install);
182 LOAD_MONO_FUNCTION(mono_reflection_type_from_name);
183 LOAD_MONO_FUNCTION(mono_runtime_invoke);
184 LOAD_MONO_FUNCTION(mono_runtime_object_init);
185 LOAD_MONO_FUNCTION(mono_runtime_quit);
186 LOAD_MONO_FUNCTION(mono_set_dirs);
187 LOAD_MONO_FUNCTION(mono_stringify_assembly_name);
188 LOAD_MONO_FUNCTION(mono_string_new);
190 /* GLib imports obsoleted by the 2.0 ABI */
191 if (This->mono_abi_version == 1)
193 (*result)->glib_handle = LoadLibraryW(glibdll);
194 if (!(*result)->glib_handle) goto fail;
196 (*result)->mono_free = (void*)GetProcAddress((*result)->glib_handle, "g_free");
197 if (!(*result)->mono_free) goto fail;
199 else
201 LOAD_MONO_FUNCTION(mono_free);
204 #undef LOAD_MONO_FUNCTION
206 #define LOAD_OPT_VOID_MONO_FUNCTION(x) do { \
207 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
208 if (!(*result)->x) { \
209 (*result)->x = do_nothing; \
211 } while (0);
213 LOAD_OPT_VOID_MONO_FUNCTION(mono_runtime_set_shutting_down);
214 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_pool_cleanup);
215 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_suspend_all_other_threads);
216 LOAD_OPT_VOID_MONO_FUNCTION(mono_threads_set_shutting_down);
218 #undef LOAD_OPT_VOID_MONO_FUNCTION
220 (*result)->mono_profiler_install((MonoProfiler*)*result, mono_shutdown_callback_fn);
222 (*result)->mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
224 (*result)->mono_config_parse(NULL);
226 (*result)->mono_install_assembly_preload_hook(mono_assembly_search_hook_fn, *result);
228 trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
230 if (trace_size)
232 (*result)->mono_jit_set_trace_options(trace_setting);
236 return S_OK;
238 fail:
239 ERR("Could not load Mono into this process\n");
240 FreeLibrary((*result)->mono_handle);
241 FreeLibrary((*result)->glib_handle);
242 (*result)->mono_handle = NULL;
243 (*result)->glib_handle = NULL;
244 return E_FAIL;
247 static void mono_shutdown_callback_fn(MonoProfiler *prof)
249 loaded_mono *mono = (loaded_mono*)prof;
251 mono->is_shutdown = TRUE;
254 static HRESULT CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo *This, RuntimeHost **result)
256 HRESULT hr = S_OK;
257 loaded_mono *ploaded_mono;
259 if (This->loaded_runtime)
261 *result = This->loaded_runtime;
262 return hr;
265 EnterCriticalSection(&runtime_list_cs);
267 hr = load_mono(This, &ploaded_mono);
269 if (SUCCEEDED(hr))
270 hr = RuntimeHost_Construct(This, ploaded_mono, &This->loaded_runtime);
272 LeaveCriticalSection(&runtime_list_cs);
274 if (SUCCEEDED(hr))
275 *result = This->loaded_runtime;
277 return hr;
280 void unload_all_runtimes(void)
282 int i;
284 for (i=0; i<NUM_ABI_VERSIONS; i++)
286 loaded_mono *mono = &loaded_monos[i];
287 if (mono->mono_handle && mono->is_started && !mono->is_shutdown)
289 /* Copied from Mono's ves_icall_System_Environment_Exit */
290 mono->mono_threads_set_shutting_down();
291 mono->mono_runtime_set_shutting_down();
292 mono->mono_thread_pool_cleanup();
293 mono->mono_thread_suspend_all_other_threads();
294 mono->mono_runtime_quit();
298 for (i=0; i<NUM_RUNTIMES; i++)
299 if (runtimes[i].loaded_runtime)
300 RuntimeHost_Destroy(runtimes[i].loaded_runtime);
303 void expect_no_runtimes(void)
305 int i;
307 for (i=0; i<NUM_ABI_VERSIONS; i++)
309 loaded_mono *mono = &loaded_monos[i];
310 if (mono->mono_handle && mono->is_started && !mono->is_shutdown)
312 ERR("Process exited with a Mono runtime loaded.\n");
313 return;
318 static inline CLRRuntimeInfo *impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo *iface)
320 return CONTAINING_RECORD(iface, CLRRuntimeInfo, ICLRRuntimeInfo_iface);
323 static HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
324 REFIID riid,
325 void **ppvObject)
327 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
329 if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
330 IsEqualGUID( riid, &IID_IUnknown ) )
332 *ppvObject = iface;
334 else
336 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
337 return E_NOINTERFACE;
340 ICLRRuntimeInfo_AddRef( iface );
342 return S_OK;
345 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
347 return 2;
350 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
352 return 1;
355 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
356 LPWSTR pwzBuffer, DWORD *pcchBuffer)
358 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
359 DWORD buffer_size = *pcchBuffer;
360 HRESULT hr = S_OK;
361 char version[11];
362 DWORD size;
364 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
366 size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
368 assert(size <= sizeof(version));
370 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
372 if (pwzBuffer)
374 if (buffer_size >= *pcchBuffer)
375 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
376 else
377 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
380 return hr;
383 static BOOL get_install_root(LPWSTR install_dir)
385 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};
386 const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
388 DWORD len;
389 HKEY key;
391 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
392 return FALSE;
394 len = MAX_PATH;
395 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
397 RegCloseKey(key);
398 return FALSE;
400 RegCloseKey(key);
402 return TRUE;
405 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
406 LPWSTR pwzBuffer, DWORD *pcchBuffer)
408 static const WCHAR slash[] = {'\\',0};
409 DWORD buffer_size = *pcchBuffer;
410 WCHAR system_dir[MAX_PATH];
411 WCHAR version[MAX_PATH];
412 DWORD version_size, size;
413 HRESULT hr = S_OK;
415 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
417 if (!get_install_root(system_dir))
419 ERR("error reading registry key for installroot\n");
420 return E_FAIL;
422 else
424 version_size = MAX_PATH;
425 ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
426 lstrcatW(system_dir, version);
427 lstrcatW(system_dir, slash);
428 size = lstrlenW(system_dir) + 1;
431 *pcchBuffer = size;
433 if (pwzBuffer)
435 if (buffer_size >= size)
436 strcpyW(pwzBuffer, system_dir);
437 else
438 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
441 return hr;
444 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
445 HANDLE hndProcess, BOOL *pbLoaded)
447 FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
449 return E_NOTIMPL;
452 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
453 UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
455 FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
457 return E_NOTIMPL;
460 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
461 LPCWSTR pwzDllName, HMODULE *phndModule)
463 WCHAR version[MAX_PATH];
464 HRESULT hr;
465 DWORD cchBuffer;
467 TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
469 cchBuffer = MAX_PATH;
470 hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
471 if (FAILED(hr)) return hr;
473 return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
476 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
477 LPCSTR pszProcName, LPVOID *ppProc)
479 FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
481 return E_NOTIMPL;
484 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
485 REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
487 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
488 RuntimeHost *host;
489 HRESULT hr;
491 TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
493 hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
495 if (SUCCEEDED(hr))
496 hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
498 return hr;
501 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
502 BOOL *pbLoadable)
504 FIXME("%p %p\n", iface, pbLoadable);
506 return E_NOTIMPL;
509 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
510 DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
512 FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
514 return E_NOTIMPL;
517 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
518 DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
520 FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
522 return E_NOTIMPL;
525 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
527 FIXME("%p\n", iface);
529 return E_NOTIMPL;
532 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
533 BOOL *pbStarted, DWORD *pdwStartupFlags)
535 FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
537 return E_NOTIMPL;
540 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
541 CLRRuntimeInfo_QueryInterface,
542 CLRRuntimeInfo_AddRef,
543 CLRRuntimeInfo_Release,
544 CLRRuntimeInfo_GetVersionString,
545 CLRRuntimeInfo_GetRuntimeDirectory,
546 CLRRuntimeInfo_IsLoaded,
547 CLRRuntimeInfo_LoadErrorString,
548 CLRRuntimeInfo_LoadLibrary,
549 CLRRuntimeInfo_GetProcAddress,
550 CLRRuntimeInfo_GetInterface,
551 CLRRuntimeInfo_IsLoadable,
552 CLRRuntimeInfo_SetDefaultStartupFlags,
553 CLRRuntimeInfo_GetDefaultStartupFlags,
554 CLRRuntimeInfo_BindAsLegacyV2Runtime,
555 CLRRuntimeInfo_IsStarted
558 HRESULT ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo *iface, RuntimeHost **result)
560 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
562 assert(This->ICLRRuntimeInfo_iface.lpVtbl == &CLRRuntimeInfoVtbl);
564 return CLRRuntimeInfo_GetRuntimeHost(This, result);
567 #ifdef __i386__
568 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};
569 #elif defined(__x86_64__)
570 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};
571 #else
572 static const WCHAR libmono2_arch_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
573 #endif
575 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version)
577 static const WCHAR mono_dll[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
578 static const WCHAR libmono_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
579 static const WCHAR mono2_dll[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
580 static const WCHAR libmono2_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
581 DWORD attributes=INVALID_FILE_ATTRIBUTES;
583 if (abi_version == 1)
585 strcpyW(dll_path, path);
586 strcatW(dll_path, mono_dll);
587 attributes = GetFileAttributesW(dll_path);
589 if (attributes == INVALID_FILE_ATTRIBUTES)
591 strcpyW(dll_path, path);
592 strcatW(dll_path, libmono_dll);
593 attributes = GetFileAttributesW(dll_path);
596 else if (abi_version == 2)
598 strcpyW(dll_path, path);
599 strcatW(dll_path, libmono2_arch_dll);
600 attributes = GetFileAttributesW(dll_path);
602 if (attributes == INVALID_FILE_ATTRIBUTES)
604 strcpyW(dll_path, path);
605 strcatW(dll_path, mono2_dll);
606 attributes = GetFileAttributesW(dll_path);
609 if (attributes == INVALID_FILE_ATTRIBUTES)
611 strcpyW(dll_path, path);
612 strcatW(dll_path, libmono2_dll);
613 attributes = GetFileAttributesW(dll_path);
617 return (attributes != INVALID_FILE_ATTRIBUTES);
620 static BOOL get_mono_path_from_registry(LPWSTR path, int abi_version)
622 static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
623 static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
624 static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
625 static const WCHAR slash[] = {'\\',0};
627 WCHAR version[64], version_key[MAX_PATH];
628 DWORD len;
629 HKEY key;
630 WCHAR dll_path[MAX_PATH];
632 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
633 return FALSE;
635 len = sizeof(version);
636 if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
638 RegCloseKey(key);
639 return FALSE;
641 RegCloseKey(key);
643 lstrcpyW(version_key, mono_key);
644 lstrcatW(version_key, slash);
645 lstrcatW(version_key, version);
647 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
648 return FALSE;
650 len = sizeof(WCHAR) * MAX_PATH;
651 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
653 RegCloseKey(key);
654 return FALSE;
656 RegCloseKey(key);
658 return find_mono_dll(path, dll_path, abi_version);
661 static BOOL get_mono_path_from_folder(LPCWSTR folder, LPWSTR mono_path, int abi_version)
663 static const WCHAR mono_one_dot_zero[] = {'\\','m','o','n','o','-','1','.','0', 0};
664 static const WCHAR mono_two_dot_zero[] = {'\\','m','o','n','o','-','2','.','0', 0};
665 WCHAR mono_dll_path[MAX_PATH];
666 BOOL found = FALSE;
668 strcpyW(mono_path, folder);
670 if (abi_version == 1)
671 strcatW(mono_path, mono_one_dot_zero);
672 else if (abi_version == 2)
673 strcatW(mono_path, mono_two_dot_zero);
675 found = find_mono_dll(mono_path, mono_dll_path, abi_version);
677 return found;
680 static BOOL get_mono_path(LPWSTR path, int abi_version)
682 static const WCHAR subdir_mono[] = {'\\','m','o','n','o',0};
683 static const WCHAR sibling_mono[] = {'\\','.','.','\\','m','o','n','o',0};
684 WCHAR base_path[MAX_PATH];
685 const char *unix_data_dir;
686 WCHAR *dos_data_dir;
687 int build_tree=0;
688 static WCHAR* (CDECL *wine_get_dos_file_name)(const char*);
690 /* First try c:\windows\mono */
691 GetWindowsDirectoryW(base_path, MAX_PATH);
692 strcatW(base_path, subdir_mono);
694 if (get_mono_path_from_folder(base_path, path, abi_version))
695 return TRUE;
697 /* Next: /usr/share/wine/mono */
698 unix_data_dir = wine_get_data_dir();
700 if (!unix_data_dir)
702 unix_data_dir = wine_get_build_dir();
703 build_tree = 1;
706 if (unix_data_dir)
708 if (!wine_get_dos_file_name)
709 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
711 if (wine_get_dos_file_name)
713 dos_data_dir = wine_get_dos_file_name(unix_data_dir);
715 if (dos_data_dir)
717 strcpyW(base_path, dos_data_dir);
718 strcatW(base_path, build_tree ? sibling_mono : subdir_mono);
720 HeapFree(GetProcessHeap(), 0, dos_data_dir);
722 if (get_mono_path_from_folder(base_path, path, abi_version))
723 return TRUE;
728 /* Last: the registry */
729 return get_mono_path_from_registry(path, abi_version);
732 static void find_runtimes(void)
734 int abi_version, i;
735 static const WCHAR libmono[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
736 static const WCHAR mscorlib[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
737 WCHAR mono_path[MAX_PATH], lib_path[MAX_PATH];
738 BOOL any_runtimes_found = FALSE;
740 if (runtimes_initialized) return;
742 EnterCriticalSection(&runtime_list_cs);
744 if (runtimes_initialized) goto end;
746 for (abi_version=NUM_ABI_VERSIONS; abi_version>0; abi_version--)
748 if (!get_mono_path(mono_path, abi_version))
749 continue;
751 for (i=0; i<NUM_RUNTIMES; i++)
753 if (runtimes[i].mono_abi_version == 0)
755 strcpyW(lib_path, mono_path);
756 strcatW(lib_path, libmono);
757 strcatW(lib_path, runtimes[i].mono_libdir);
758 strcatW(lib_path, mscorlib);
760 if (GetFileAttributesW(lib_path) != INVALID_FILE_ATTRIBUTES)
762 runtimes[i].mono_abi_version = abi_version;
764 strcpyW(runtimes[i].mono_path, mono_path);
765 strcpyW(runtimes[i].mscorlib_path, lib_path);
767 any_runtimes_found = TRUE;
773 if (!any_runtimes_found)
775 /* Report all runtimes are available if Mono isn't installed.
776 * FIXME: Remove this when Mono is properly packaged. */
777 for (i=0; i<NUM_RUNTIMES; i++)
778 runtimes[i].mono_abi_version = -1;
781 runtimes_initialized = 1;
783 end:
784 LeaveCriticalSection(&runtime_list_cs);
787 struct InstalledRuntimeEnum
789 IEnumUnknown IEnumUnknown_iface;
790 LONG ref;
791 ULONG pos;
794 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
796 static inline struct InstalledRuntimeEnum *impl_from_IEnumUnknown(IEnumUnknown *iface)
798 return CONTAINING_RECORD(iface, struct InstalledRuntimeEnum, IEnumUnknown_iface);
801 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface, REFIID riid,
802 void **ppvObject)
804 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
806 if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
807 IsEqualGUID( riid, &IID_IUnknown ) )
809 *ppvObject = iface;
811 else
813 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
814 return E_NOINTERFACE;
817 IEnumUnknown_AddRef( iface );
819 return S_OK;
822 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
824 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
825 ULONG ref = InterlockedIncrement(&This->ref);
827 TRACE("(%p) refcount=%u\n", iface, ref);
829 return ref;
832 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
834 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
835 ULONG ref = InterlockedDecrement(&This->ref);
837 TRACE("(%p) refcount=%u\n", iface, ref);
839 if (ref == 0)
841 HeapFree(GetProcessHeap(), 0, This);
844 return ref;
847 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
848 IUnknown **rgelt, ULONG *pceltFetched)
850 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
851 int num_fetched = 0;
852 HRESULT hr=S_OK;
853 IUnknown *item;
855 TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
857 while (num_fetched < celt)
859 if (This->pos >= NUM_RUNTIMES)
861 hr = S_FALSE;
862 break;
864 if (runtimes[This->pos].mono_abi_version)
866 item = (IUnknown*)&runtimes[This->pos].ICLRRuntimeInfo_iface;
867 IUnknown_AddRef(item);
868 rgelt[num_fetched] = item;
869 num_fetched++;
871 This->pos++;
874 if (pceltFetched)
875 *pceltFetched = num_fetched;
877 return hr;
880 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
882 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
883 int num_fetched = 0;
884 HRESULT hr=S_OK;
886 TRACE("(%p,%u)\n", iface, celt);
888 while (num_fetched < celt)
890 if (This->pos >= NUM_RUNTIMES)
892 hr = S_FALSE;
893 break;
895 if (runtimes[This->pos].mono_abi_version)
897 num_fetched++;
899 This->pos++;
902 return hr;
905 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
907 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
909 TRACE("(%p)\n", iface);
911 This->pos = 0;
913 return S_OK;
916 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
918 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
919 struct InstalledRuntimeEnum *new_enum;
921 TRACE("(%p)\n", iface);
923 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
924 if (!new_enum)
925 return E_OUTOFMEMORY;
927 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
928 new_enum->ref = 1;
929 new_enum->pos = This->pos;
931 *ppenum = &new_enum->IEnumUnknown_iface;
933 return S_OK;
936 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
937 InstalledRuntimeEnum_QueryInterface,
938 InstalledRuntimeEnum_AddRef,
939 InstalledRuntimeEnum_Release,
940 InstalledRuntimeEnum_Next,
941 InstalledRuntimeEnum_Skip,
942 InstalledRuntimeEnum_Reset,
943 InstalledRuntimeEnum_Clone
946 struct CLRMetaHost
948 ICLRMetaHost ICLRMetaHost_iface;
951 static struct CLRMetaHost GlobalCLRMetaHost;
953 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
954 REFIID riid,
955 void **ppvObject)
957 TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
959 if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
960 IsEqualGUID( riid, &IID_IUnknown ) )
962 *ppvObject = iface;
964 else
966 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
967 return E_NOINTERFACE;
970 ICLRMetaHost_AddRef( iface );
972 return S_OK;
975 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
977 return 2;
980 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
982 return 1;
985 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
987 *major = 0;
988 *minor = 0;
989 *build = 0;
991 if (version[0] == 'v' || version[0] == 'V')
993 version++;
994 if (!isdigit(*version))
995 return FALSE;
997 while (isdigit(*version))
998 *major = *major * 10 + (*version++ - '0');
1000 if (*version == 0)
1001 return TRUE;
1003 if (*version++ != '.' || !isdigit(*version))
1004 return FALSE;
1006 while (isdigit(*version))
1007 *minor = *minor * 10 + (*version++ - '0');
1009 if (*version == 0)
1010 return TRUE;
1012 if (*version++ != '.' || !isdigit(*version))
1013 return FALSE;
1015 while (isdigit(*version))
1016 *build = *build * 10 + (*version++ - '0');
1018 return *version == 0;
1020 else
1021 return FALSE;
1024 HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
1025 LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
1027 int i;
1028 DWORD major, minor, build;
1030 TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
1032 if (!pwzVersion)
1033 return E_POINTER;
1035 if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
1037 ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
1038 return CLR_E_SHIM_RUNTIME;
1041 find_runtimes();
1043 for (i=0; i<NUM_RUNTIMES; i++)
1045 if (runtimes[i].major == major && runtimes[i].minor == minor &&
1046 runtimes[i].build == build)
1048 if (runtimes[i].mono_abi_version)
1049 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface, iid,
1050 ppRuntime);
1051 else
1053 missing_runtime_message(&runtimes[i]);
1054 return CLR_E_SHIM_RUNTIME;
1059 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
1060 return CLR_E_SHIM_RUNTIME;
1063 HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
1064 LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
1066 ASSEMBLY *assembly;
1067 HRESULT hr;
1068 LPSTR version;
1069 ULONG buffer_size=*pcchBuffer;
1071 TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
1073 hr = assembly_create(&assembly, pwzFilePath);
1075 if (SUCCEEDED(hr))
1077 hr = assembly_get_runtime_version(assembly, &version);
1079 if (SUCCEEDED(hr))
1081 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
1083 if (pwzBuffer)
1085 if (buffer_size >= *pcchBuffer)
1086 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
1087 else
1088 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
1092 assembly_release(assembly);
1095 return hr;
1098 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
1099 IEnumUnknown **ppEnumerator)
1101 struct InstalledRuntimeEnum *new_enum;
1103 TRACE("%p\n", ppEnumerator);
1105 find_runtimes();
1107 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
1108 if (!new_enum)
1109 return E_OUTOFMEMORY;
1111 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
1112 new_enum->ref = 1;
1113 new_enum->pos = 0;
1115 *ppEnumerator = &new_enum->IEnumUnknown_iface;
1117 return S_OK;
1120 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
1121 HANDLE hndProcess, IEnumUnknown **ppEnumerator)
1123 FIXME("%p %p\n", hndProcess, ppEnumerator);
1125 return E_NOTIMPL;
1128 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
1129 RuntimeLoadedCallbackFnPtr pCallbackFunction)
1131 FIXME("%p\n", pCallbackFunction);
1133 return E_NOTIMPL;
1136 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
1137 REFIID riid, LPVOID *ppUnk)
1139 FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
1141 return E_NOTIMPL;
1144 static HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
1146 FIXME("%i: stub\n", iExitCode);
1148 ExitProcess(iExitCode);
1151 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
1153 CLRMetaHost_QueryInterface,
1154 CLRMetaHost_AddRef,
1155 CLRMetaHost_Release,
1156 CLRMetaHost_GetRuntime,
1157 CLRMetaHost_GetVersionFromFile,
1158 CLRMetaHost_EnumerateInstalledRuntimes,
1159 CLRMetaHost_EnumerateLoadedRuntimes,
1160 CLRMetaHost_RequestRuntimeLoadedNotification,
1161 CLRMetaHost_QueryLegacyV2RuntimeBinding,
1162 CLRMetaHost_ExitProcess
1165 static struct CLRMetaHost GlobalCLRMetaHost = {
1166 { &CLRMetaHost_vtbl }
1169 HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1171 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost.ICLRMetaHost_iface, riid, ppobj);
1174 static MonoAssembly* mono_assembly_search_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data)
1176 loaded_mono *mono = user_data;
1177 HRESULT hr=S_OK;
1178 MonoAssembly *result=NULL;
1179 char *stringname=NULL;
1180 LPWSTR stringnameW;
1181 int stringnameW_size;
1182 IAssemblyCache *asmcache;
1183 ASSEMBLY_INFO info;
1184 WCHAR path[MAX_PATH];
1185 char *pathA;
1186 MonoImageOpenStatus stat;
1187 static WCHAR fusiondll[] = {'f','u','s','i','o','n',0};
1188 HMODULE hfusion=NULL;
1189 static HRESULT (WINAPI *pCreateAssemblyCache)(IAssemblyCache**,DWORD);
1191 stringname = mono->mono_stringify_assembly_name(aname);
1193 TRACE("%s\n", debugstr_a(stringname));
1195 if (!stringname) return NULL;
1197 /* FIXME: We should search the given paths before the GAC. */
1199 if (!pCreateAssemblyCache)
1201 hr = LoadLibraryShim(fusiondll, NULL, NULL, &hfusion);
1203 if (SUCCEEDED(hr))
1205 pCreateAssemblyCache = (void*)GetProcAddress(hfusion, "CreateAssemblyCache");
1206 if (!pCreateAssemblyCache)
1207 hr = E_FAIL;
1211 if (SUCCEEDED(hr))
1212 hr = pCreateAssemblyCache(&asmcache, 0);
1214 if (SUCCEEDED(hr))
1216 stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, stringname, -1, NULL, 0);
1218 stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1219 if (stringnameW)
1220 MultiByteToWideChar(CP_UTF8, 0, stringname, -1, stringnameW, stringnameW_size);
1221 else
1222 hr = E_OUTOFMEMORY;
1224 if (SUCCEEDED(hr))
1226 info.cbAssemblyInfo = sizeof(info);
1227 info.pszCurrentAssemblyPathBuf = path;
1228 info.cchBuf = MAX_PATH;
1229 path[0] = 0;
1231 hr = IAssemblyCache_QueryAssemblyInfo(asmcache, 0, stringnameW, &info);
1234 HeapFree(GetProcessHeap(), 0, stringnameW);
1236 IAssemblyCache_Release(asmcache);
1239 if (SUCCEEDED(hr))
1241 TRACE("found: %s\n", debugstr_w(path));
1243 pathA = WtoA(path);
1245 if (pathA)
1247 result = mono->mono_assembly_open(pathA, &stat);
1249 if (!result)
1250 ERR("Failed to load %s, status=%u\n", debugstr_w(path), stat);
1252 HeapFree(GetProcessHeap(), 0, pathA);
1256 mono->mono_free(stringname);
1258 return result;
1261 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1262 DWORD startup_flags, DWORD runtimeinfo_flags, BOOL legacy, ICLRRuntimeInfo **result)
1264 static const WCHAR dotconfig[] = {'.','c','o','n','f','i','g',0};
1265 static const DWORD supported_startup_flags = 0;
1266 static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1267 int i;
1268 WCHAR local_version[MAX_PATH];
1269 ULONG local_version_size = MAX_PATH;
1270 WCHAR local_config_file[MAX_PATH];
1271 HRESULT hr;
1272 parsed_config_file parsed_config;
1274 if (startup_flags & ~supported_startup_flags)
1275 FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
1277 if (runtimeinfo_flags & ~supported_runtime_flags)
1278 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
1280 if (exefile && !config_file)
1282 strcpyW(local_config_file, exefile);
1283 strcatW(local_config_file, dotconfig);
1285 config_file = local_config_file;
1288 if (config_file)
1290 int found=0;
1291 hr = parse_config_file(config_file, &parsed_config);
1293 if (SUCCEEDED(hr))
1295 supported_runtime *entry;
1296 LIST_FOR_EACH_ENTRY(entry, &parsed_config.supported_runtimes, supported_runtime, entry)
1298 hr = CLRMetaHost_GetRuntime(0, entry->version, &IID_ICLRRuntimeInfo, (void**)result);
1299 if (SUCCEEDED(hr))
1301 found = 1;
1302 break;
1306 else
1308 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file), hr);
1311 free_parsed_config_file(&parsed_config);
1313 if (found)
1314 return S_OK;
1317 if (exefile && !version)
1319 hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1321 version = local_version;
1323 if (FAILED(hr)) return hr;
1326 if (version)
1328 hr = CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1329 if(SUCCEEDED(hr))
1330 return hr;
1333 if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1335 DWORD major, minor, build;
1337 if (version && !parse_runtime_version(version, &major, &minor, &build))
1339 ERR("Cannot parse %s\n", debugstr_w(version));
1340 return CLR_E_SHIM_RUNTIME;
1343 find_runtimes();
1345 if (legacy)
1346 i = 2;
1347 else
1348 i = NUM_RUNTIMES;
1350 while (i--)
1352 if (runtimes[i].mono_abi_version)
1354 /* Must be greater or equal to the version passed in. */
1355 if (!version || ((runtimes[i].major >= major && runtimes[i].minor >= minor && runtimes[i].build >= build) ||
1356 (runtimes[i].major >= major && runtimes[i].minor > minor) ||
1357 (runtimes[i].major > major)))
1359 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface,
1360 &IID_ICLRRuntimeInfo, (void **)result);
1365 if (legacy)
1366 missing_runtime_message(&runtimes[1]);
1367 else
1368 missing_runtime_message(&runtimes[NUM_RUNTIMES-1]);
1370 return CLR_E_SHIM_RUNTIME;
1373 return CLR_E_SHIM_RUNTIME;