msxml3/tests: Check the correct return value.
[wine/multimedia.git] / dlls / mscoree / metahost.c
blob3736dc0ace15abaf631d67750847806c4b992db5
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_load_from);
169 LOAD_MONO_FUNCTION(mono_assembly_open);
170 LOAD_MONO_FUNCTION(mono_config_parse);
171 LOAD_MONO_FUNCTION(mono_class_from_mono_type);
172 LOAD_MONO_FUNCTION(mono_class_from_name);
173 LOAD_MONO_FUNCTION(mono_class_get_method_from_name);
174 LOAD_MONO_FUNCTION(mono_domain_assembly_open);
175 LOAD_MONO_FUNCTION(mono_image_open_from_module_handle);
176 LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook);
177 LOAD_MONO_FUNCTION(mono_jit_exec);
178 LOAD_MONO_FUNCTION(mono_jit_init);
179 LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
180 LOAD_MONO_FUNCTION(mono_marshal_get_vtfixup_ftnptr);
181 LOAD_MONO_FUNCTION(mono_object_get_domain);
182 LOAD_MONO_FUNCTION(mono_object_new);
183 LOAD_MONO_FUNCTION(mono_object_unbox);
184 LOAD_MONO_FUNCTION(mono_profiler_install);
185 LOAD_MONO_FUNCTION(mono_reflection_type_from_name);
186 LOAD_MONO_FUNCTION(mono_runtime_invoke);
187 LOAD_MONO_FUNCTION(mono_runtime_object_init);
188 LOAD_MONO_FUNCTION(mono_runtime_quit);
189 LOAD_MONO_FUNCTION(mono_set_dirs);
190 LOAD_MONO_FUNCTION(mono_stringify_assembly_name);
191 LOAD_MONO_FUNCTION(mono_string_new);
192 LOAD_MONO_FUNCTION(mono_thread_attach);
194 /* GLib imports obsoleted by the 2.0 ABI */
195 if (This->mono_abi_version == 1)
197 (*result)->glib_handle = LoadLibraryW(glibdll);
198 if (!(*result)->glib_handle) goto fail;
200 (*result)->mono_free = (void*)GetProcAddress((*result)->glib_handle, "g_free");
201 if (!(*result)->mono_free) goto fail;
203 else
205 LOAD_MONO_FUNCTION(mono_free);
208 #undef LOAD_MONO_FUNCTION
210 #define LOAD_OPT_VOID_MONO_FUNCTION(x) do { \
211 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
212 if (!(*result)->x) { \
213 (*result)->x = do_nothing; \
215 } while (0);
217 LOAD_OPT_VOID_MONO_FUNCTION(mono_runtime_set_shutting_down);
218 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_pool_cleanup);
219 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_suspend_all_other_threads);
220 LOAD_OPT_VOID_MONO_FUNCTION(mono_threads_set_shutting_down);
222 #undef LOAD_OPT_VOID_MONO_FUNCTION
224 (*result)->mono_profiler_install((MonoProfiler*)*result, mono_shutdown_callback_fn);
226 (*result)->mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
228 (*result)->mono_config_parse(NULL);
230 (*result)->mono_install_assembly_preload_hook(mono_assembly_search_hook_fn, *result);
232 trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
234 if (trace_size)
236 (*result)->mono_jit_set_trace_options(trace_setting);
240 return S_OK;
242 fail:
243 ERR("Could not load Mono into this process\n");
244 FreeLibrary((*result)->mono_handle);
245 FreeLibrary((*result)->glib_handle);
246 (*result)->mono_handle = NULL;
247 (*result)->glib_handle = NULL;
248 return E_FAIL;
251 static void mono_shutdown_callback_fn(MonoProfiler *prof)
253 loaded_mono *mono = (loaded_mono*)prof;
255 mono->is_shutdown = TRUE;
258 static HRESULT CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo *This, RuntimeHost **result)
260 HRESULT hr = S_OK;
261 loaded_mono *ploaded_mono;
263 if (This->loaded_runtime)
265 *result = This->loaded_runtime;
266 return hr;
269 EnterCriticalSection(&runtime_list_cs);
271 hr = load_mono(This, &ploaded_mono);
273 if (SUCCEEDED(hr))
274 hr = RuntimeHost_Construct(This, ploaded_mono, &This->loaded_runtime);
276 LeaveCriticalSection(&runtime_list_cs);
278 if (SUCCEEDED(hr))
279 *result = This->loaded_runtime;
281 return hr;
284 void unload_all_runtimes(void)
286 int i;
288 for (i=0; i<NUM_ABI_VERSIONS; i++)
290 loaded_mono *mono = &loaded_monos[i];
291 if (mono->mono_handle && mono->is_started && !mono->is_shutdown)
293 /* Copied from Mono's ves_icall_System_Environment_Exit */
294 mono->mono_threads_set_shutting_down();
295 mono->mono_runtime_set_shutting_down();
296 mono->mono_thread_pool_cleanup();
297 mono->mono_thread_suspend_all_other_threads();
298 mono->mono_runtime_quit();
302 for (i=0; i<NUM_RUNTIMES; i++)
303 if (runtimes[i].loaded_runtime)
304 RuntimeHost_Destroy(runtimes[i].loaded_runtime);
307 void expect_no_runtimes(void)
309 int i;
311 for (i=0; i<NUM_ABI_VERSIONS; i++)
313 loaded_mono *mono = &loaded_monos[i];
314 if (mono->mono_handle && mono->is_started && !mono->is_shutdown)
316 ERR("Process exited with a Mono runtime loaded.\n");
317 return;
322 static inline CLRRuntimeInfo *impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo *iface)
324 return CONTAINING_RECORD(iface, CLRRuntimeInfo, ICLRRuntimeInfo_iface);
327 static HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
328 REFIID riid,
329 void **ppvObject)
331 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
333 if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
334 IsEqualGUID( riid, &IID_IUnknown ) )
336 *ppvObject = iface;
338 else
340 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
341 return E_NOINTERFACE;
344 ICLRRuntimeInfo_AddRef( iface );
346 return S_OK;
349 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
351 return 2;
354 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
356 return 1;
359 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
360 LPWSTR pwzBuffer, DWORD *pcchBuffer)
362 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
363 DWORD buffer_size = *pcchBuffer;
364 HRESULT hr = S_OK;
365 char version[11];
366 DWORD size;
368 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
370 size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
372 assert(size <= sizeof(version));
374 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
376 if (pwzBuffer)
378 if (buffer_size >= *pcchBuffer)
379 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
380 else
381 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
384 return hr;
387 static BOOL get_install_root(LPWSTR install_dir)
389 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};
390 const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
392 DWORD len;
393 HKEY key;
395 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
396 return FALSE;
398 len = MAX_PATH;
399 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
401 RegCloseKey(key);
402 return FALSE;
404 RegCloseKey(key);
406 return TRUE;
409 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
410 LPWSTR pwzBuffer, DWORD *pcchBuffer)
412 static const WCHAR slash[] = {'\\',0};
413 DWORD buffer_size = *pcchBuffer;
414 WCHAR system_dir[MAX_PATH];
415 WCHAR version[MAX_PATH];
416 DWORD version_size, size;
417 HRESULT hr = S_OK;
419 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
421 if (!get_install_root(system_dir))
423 ERR("error reading registry key for installroot\n");
424 return E_FAIL;
426 else
428 version_size = MAX_PATH;
429 ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
430 lstrcatW(system_dir, version);
431 lstrcatW(system_dir, slash);
432 size = lstrlenW(system_dir) + 1;
435 *pcchBuffer = size;
437 if (pwzBuffer)
439 if (buffer_size >= size)
440 strcpyW(pwzBuffer, system_dir);
441 else
442 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
445 return hr;
448 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
449 HANDLE hndProcess, BOOL *pbLoaded)
451 FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
453 return E_NOTIMPL;
456 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
457 UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
459 FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
461 return E_NOTIMPL;
464 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
465 LPCWSTR pwzDllName, HMODULE *phndModule)
467 WCHAR version[MAX_PATH];
468 HRESULT hr;
469 DWORD cchBuffer;
471 TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
473 cchBuffer = MAX_PATH;
474 hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
475 if (FAILED(hr)) return hr;
477 return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
480 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
481 LPCSTR pszProcName, LPVOID *ppProc)
483 FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
485 return E_NOTIMPL;
488 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
489 REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
491 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
492 RuntimeHost *host;
493 HRESULT hr;
495 TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
497 hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
499 if (SUCCEEDED(hr))
500 hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
502 return hr;
505 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
506 BOOL *pbLoadable)
508 FIXME("%p %p\n", iface, pbLoadable);
510 return E_NOTIMPL;
513 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
514 DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
516 FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
518 return E_NOTIMPL;
521 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
522 DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
524 FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
526 return E_NOTIMPL;
529 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
531 FIXME("%p\n", iface);
533 return E_NOTIMPL;
536 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
537 BOOL *pbStarted, DWORD *pdwStartupFlags)
539 FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
541 return E_NOTIMPL;
544 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
545 CLRRuntimeInfo_QueryInterface,
546 CLRRuntimeInfo_AddRef,
547 CLRRuntimeInfo_Release,
548 CLRRuntimeInfo_GetVersionString,
549 CLRRuntimeInfo_GetRuntimeDirectory,
550 CLRRuntimeInfo_IsLoaded,
551 CLRRuntimeInfo_LoadErrorString,
552 CLRRuntimeInfo_LoadLibrary,
553 CLRRuntimeInfo_GetProcAddress,
554 CLRRuntimeInfo_GetInterface,
555 CLRRuntimeInfo_IsLoadable,
556 CLRRuntimeInfo_SetDefaultStartupFlags,
557 CLRRuntimeInfo_GetDefaultStartupFlags,
558 CLRRuntimeInfo_BindAsLegacyV2Runtime,
559 CLRRuntimeInfo_IsStarted
562 HRESULT ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo *iface, RuntimeHost **result)
564 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
566 assert(This->ICLRRuntimeInfo_iface.lpVtbl == &CLRRuntimeInfoVtbl);
568 return CLRRuntimeInfo_GetRuntimeHost(This, result);
571 #ifdef __i386__
572 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};
573 #elif defined(__x86_64__)
574 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};
575 #else
576 static const WCHAR libmono2_arch_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
577 #endif
579 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version)
581 static const WCHAR mono_dll[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
582 static const WCHAR libmono_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
583 static const WCHAR mono2_dll[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
584 static const WCHAR libmono2_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
585 DWORD attributes=INVALID_FILE_ATTRIBUTES;
587 if (abi_version == 1)
589 strcpyW(dll_path, path);
590 strcatW(dll_path, mono_dll);
591 attributes = GetFileAttributesW(dll_path);
593 if (attributes == INVALID_FILE_ATTRIBUTES)
595 strcpyW(dll_path, path);
596 strcatW(dll_path, libmono_dll);
597 attributes = GetFileAttributesW(dll_path);
600 else if (abi_version == 2)
602 strcpyW(dll_path, path);
603 strcatW(dll_path, libmono2_arch_dll);
604 attributes = GetFileAttributesW(dll_path);
606 if (attributes == INVALID_FILE_ATTRIBUTES)
608 strcpyW(dll_path, path);
609 strcatW(dll_path, mono2_dll);
610 attributes = GetFileAttributesW(dll_path);
613 if (attributes == INVALID_FILE_ATTRIBUTES)
615 strcpyW(dll_path, path);
616 strcatW(dll_path, libmono2_dll);
617 attributes = GetFileAttributesW(dll_path);
621 return (attributes != INVALID_FILE_ATTRIBUTES);
624 static BOOL get_mono_path_from_registry(LPWSTR path, int abi_version)
626 static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
627 static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
628 static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
629 static const WCHAR slash[] = {'\\',0};
631 WCHAR version[64], version_key[MAX_PATH];
632 DWORD len;
633 HKEY key;
634 WCHAR dll_path[MAX_PATH];
636 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
637 return FALSE;
639 len = sizeof(version);
640 if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
642 RegCloseKey(key);
643 return FALSE;
645 RegCloseKey(key);
647 lstrcpyW(version_key, mono_key);
648 lstrcatW(version_key, slash);
649 lstrcatW(version_key, version);
651 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
652 return FALSE;
654 len = sizeof(WCHAR) * MAX_PATH;
655 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
657 RegCloseKey(key);
658 return FALSE;
660 RegCloseKey(key);
662 return find_mono_dll(path, dll_path, abi_version);
665 static BOOL get_mono_path_from_folder(LPCWSTR folder, LPWSTR mono_path, int abi_version)
667 static const WCHAR mono_one_dot_zero[] = {'\\','m','o','n','o','-','1','.','0', 0};
668 static const WCHAR mono_two_dot_zero[] = {'\\','m','o','n','o','-','2','.','0', 0};
669 WCHAR mono_dll_path[MAX_PATH];
670 BOOL found = FALSE;
672 strcpyW(mono_path, folder);
674 if (abi_version == 1)
675 strcatW(mono_path, mono_one_dot_zero);
676 else if (abi_version == 2)
677 strcatW(mono_path, mono_two_dot_zero);
679 found = find_mono_dll(mono_path, mono_dll_path, abi_version);
681 return found;
684 static BOOL get_mono_path(LPWSTR path, int abi_version)
686 static const WCHAR subdir_mono[] = {'\\','m','o','n','o',0};
687 static const WCHAR sibling_mono[] = {'\\','.','.','\\','m','o','n','o',0};
688 WCHAR base_path[MAX_PATH];
689 const char *unix_data_dir;
690 WCHAR *dos_data_dir;
691 int build_tree=0;
692 static WCHAR* (CDECL *wine_get_dos_file_name)(const char*);
694 /* First try c:\windows\mono */
695 GetWindowsDirectoryW(base_path, MAX_PATH);
696 strcatW(base_path, subdir_mono);
698 if (get_mono_path_from_folder(base_path, path, abi_version))
699 return TRUE;
701 /* Next: /usr/share/wine/mono */
702 unix_data_dir = wine_get_data_dir();
704 if (!unix_data_dir)
706 unix_data_dir = wine_get_build_dir();
707 build_tree = 1;
710 if (unix_data_dir)
712 if (!wine_get_dos_file_name)
713 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
715 if (wine_get_dos_file_name)
717 dos_data_dir = wine_get_dos_file_name(unix_data_dir);
719 if (dos_data_dir)
721 strcpyW(base_path, dos_data_dir);
722 strcatW(base_path, build_tree ? sibling_mono : subdir_mono);
724 HeapFree(GetProcessHeap(), 0, dos_data_dir);
726 if (get_mono_path_from_folder(base_path, path, abi_version))
727 return TRUE;
732 /* Last: the registry */
733 return get_mono_path_from_registry(path, abi_version);
736 static void find_runtimes(void)
738 int abi_version, i;
739 static const WCHAR libmono[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
740 static const WCHAR mscorlib[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
741 WCHAR mono_path[MAX_PATH], lib_path[MAX_PATH];
742 BOOL any_runtimes_found = FALSE;
744 if (runtimes_initialized) return;
746 EnterCriticalSection(&runtime_list_cs);
748 if (runtimes_initialized) goto end;
750 for (abi_version=NUM_ABI_VERSIONS; abi_version>0; abi_version--)
752 if (!get_mono_path(mono_path, abi_version))
753 continue;
755 for (i=0; i<NUM_RUNTIMES; i++)
757 if (runtimes[i].mono_abi_version == 0)
759 strcpyW(lib_path, mono_path);
760 strcatW(lib_path, libmono);
761 strcatW(lib_path, runtimes[i].mono_libdir);
762 strcatW(lib_path, mscorlib);
764 if (GetFileAttributesW(lib_path) != INVALID_FILE_ATTRIBUTES)
766 runtimes[i].mono_abi_version = abi_version;
768 strcpyW(runtimes[i].mono_path, mono_path);
769 strcpyW(runtimes[i].mscorlib_path, lib_path);
771 any_runtimes_found = TRUE;
777 if (!any_runtimes_found)
779 /* Report all runtimes are available if Mono isn't installed.
780 * FIXME: Remove this when Mono is properly packaged. */
781 for (i=0; i<NUM_RUNTIMES; i++)
782 runtimes[i].mono_abi_version = -1;
785 runtimes_initialized = 1;
787 end:
788 LeaveCriticalSection(&runtime_list_cs);
791 struct InstalledRuntimeEnum
793 IEnumUnknown IEnumUnknown_iface;
794 LONG ref;
795 ULONG pos;
798 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
800 static inline struct InstalledRuntimeEnum *impl_from_IEnumUnknown(IEnumUnknown *iface)
802 return CONTAINING_RECORD(iface, struct InstalledRuntimeEnum, IEnumUnknown_iface);
805 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface, REFIID riid,
806 void **ppvObject)
808 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
810 if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
811 IsEqualGUID( riid, &IID_IUnknown ) )
813 *ppvObject = iface;
815 else
817 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
818 return E_NOINTERFACE;
821 IEnumUnknown_AddRef( iface );
823 return S_OK;
826 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
828 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
829 ULONG ref = InterlockedIncrement(&This->ref);
831 TRACE("(%p) refcount=%u\n", iface, ref);
833 return ref;
836 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
838 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
839 ULONG ref = InterlockedDecrement(&This->ref);
841 TRACE("(%p) refcount=%u\n", iface, ref);
843 if (ref == 0)
845 HeapFree(GetProcessHeap(), 0, This);
848 return ref;
851 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
852 IUnknown **rgelt, ULONG *pceltFetched)
854 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
855 int num_fetched = 0;
856 HRESULT hr=S_OK;
857 IUnknown *item;
859 TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
861 while (num_fetched < celt)
863 if (This->pos >= NUM_RUNTIMES)
865 hr = S_FALSE;
866 break;
868 if (runtimes[This->pos].mono_abi_version)
870 item = (IUnknown*)&runtimes[This->pos].ICLRRuntimeInfo_iface;
871 IUnknown_AddRef(item);
872 rgelt[num_fetched] = item;
873 num_fetched++;
875 This->pos++;
878 if (pceltFetched)
879 *pceltFetched = num_fetched;
881 return hr;
884 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
886 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
887 int num_fetched = 0;
888 HRESULT hr=S_OK;
890 TRACE("(%p,%u)\n", iface, celt);
892 while (num_fetched < celt)
894 if (This->pos >= NUM_RUNTIMES)
896 hr = S_FALSE;
897 break;
899 if (runtimes[This->pos].mono_abi_version)
901 num_fetched++;
903 This->pos++;
906 return hr;
909 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
911 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
913 TRACE("(%p)\n", iface);
915 This->pos = 0;
917 return S_OK;
920 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
922 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
923 struct InstalledRuntimeEnum *new_enum;
925 TRACE("(%p)\n", iface);
927 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
928 if (!new_enum)
929 return E_OUTOFMEMORY;
931 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
932 new_enum->ref = 1;
933 new_enum->pos = This->pos;
935 *ppenum = &new_enum->IEnumUnknown_iface;
937 return S_OK;
940 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
941 InstalledRuntimeEnum_QueryInterface,
942 InstalledRuntimeEnum_AddRef,
943 InstalledRuntimeEnum_Release,
944 InstalledRuntimeEnum_Next,
945 InstalledRuntimeEnum_Skip,
946 InstalledRuntimeEnum_Reset,
947 InstalledRuntimeEnum_Clone
950 struct CLRMetaHost
952 ICLRMetaHost ICLRMetaHost_iface;
955 static struct CLRMetaHost GlobalCLRMetaHost;
957 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
958 REFIID riid,
959 void **ppvObject)
961 TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
963 if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
964 IsEqualGUID( riid, &IID_IUnknown ) )
966 *ppvObject = iface;
968 else
970 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
971 return E_NOINTERFACE;
974 ICLRMetaHost_AddRef( iface );
976 return S_OK;
979 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
981 return 2;
984 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
986 return 1;
989 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
991 *major = 0;
992 *minor = 0;
993 *build = 0;
995 if (version[0] == 'v' || version[0] == 'V')
997 version++;
998 if (!isdigit(*version))
999 return FALSE;
1001 while (isdigit(*version))
1002 *major = *major * 10 + (*version++ - '0');
1004 if (*version == 0)
1005 return TRUE;
1007 if (*version++ != '.' || !isdigit(*version))
1008 return FALSE;
1010 while (isdigit(*version))
1011 *minor = *minor * 10 + (*version++ - '0');
1013 if (*version == 0)
1014 return TRUE;
1016 if (*version++ != '.' || !isdigit(*version))
1017 return FALSE;
1019 while (isdigit(*version))
1020 *build = *build * 10 + (*version++ - '0');
1022 return *version == 0;
1024 else
1025 return FALSE;
1028 HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
1029 LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
1031 int i;
1032 DWORD major, minor, build;
1034 TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
1036 if (!pwzVersion)
1037 return E_POINTER;
1039 if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
1041 ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
1042 return CLR_E_SHIM_RUNTIME;
1045 find_runtimes();
1047 for (i=0; i<NUM_RUNTIMES; i++)
1049 if (runtimes[i].major == major && runtimes[i].minor == minor &&
1050 runtimes[i].build == build)
1052 if (runtimes[i].mono_abi_version)
1053 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface, iid,
1054 ppRuntime);
1055 else
1057 missing_runtime_message(&runtimes[i]);
1058 return CLR_E_SHIM_RUNTIME;
1063 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
1064 return CLR_E_SHIM_RUNTIME;
1067 HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
1068 LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
1070 ASSEMBLY *assembly;
1071 HRESULT hr;
1072 LPSTR version;
1073 ULONG buffer_size=*pcchBuffer;
1075 TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
1077 hr = assembly_create(&assembly, pwzFilePath);
1079 if (SUCCEEDED(hr))
1081 hr = assembly_get_runtime_version(assembly, &version);
1083 if (SUCCEEDED(hr))
1085 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
1087 if (pwzBuffer)
1089 if (buffer_size >= *pcchBuffer)
1090 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
1091 else
1092 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
1096 assembly_release(assembly);
1099 return hr;
1102 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
1103 IEnumUnknown **ppEnumerator)
1105 struct InstalledRuntimeEnum *new_enum;
1107 TRACE("%p\n", ppEnumerator);
1109 find_runtimes();
1111 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
1112 if (!new_enum)
1113 return E_OUTOFMEMORY;
1115 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
1116 new_enum->ref = 1;
1117 new_enum->pos = 0;
1119 *ppEnumerator = &new_enum->IEnumUnknown_iface;
1121 return S_OK;
1124 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
1125 HANDLE hndProcess, IEnumUnknown **ppEnumerator)
1127 FIXME("%p %p\n", hndProcess, ppEnumerator);
1129 return E_NOTIMPL;
1132 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
1133 RuntimeLoadedCallbackFnPtr pCallbackFunction)
1135 FIXME("%p\n", pCallbackFunction);
1137 return E_NOTIMPL;
1140 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
1141 REFIID riid, LPVOID *ppUnk)
1143 FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
1145 return E_NOTIMPL;
1148 static HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
1150 FIXME("%i: stub\n", iExitCode);
1152 ExitProcess(iExitCode);
1155 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
1157 CLRMetaHost_QueryInterface,
1158 CLRMetaHost_AddRef,
1159 CLRMetaHost_Release,
1160 CLRMetaHost_GetRuntime,
1161 CLRMetaHost_GetVersionFromFile,
1162 CLRMetaHost_EnumerateInstalledRuntimes,
1163 CLRMetaHost_EnumerateLoadedRuntimes,
1164 CLRMetaHost_RequestRuntimeLoadedNotification,
1165 CLRMetaHost_QueryLegacyV2RuntimeBinding,
1166 CLRMetaHost_ExitProcess
1169 static struct CLRMetaHost GlobalCLRMetaHost = {
1170 { &CLRMetaHost_vtbl }
1173 HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1175 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost.ICLRMetaHost_iface, riid, ppobj);
1178 static MonoAssembly* mono_assembly_search_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data)
1180 loaded_mono *mono = user_data;
1181 HRESULT hr=S_OK;
1182 MonoAssembly *result=NULL;
1183 char *stringname=NULL;
1184 LPWSTR stringnameW;
1185 int stringnameW_size;
1186 IAssemblyCache *asmcache;
1187 ASSEMBLY_INFO info;
1188 WCHAR path[MAX_PATH];
1189 char *pathA;
1190 MonoImageOpenStatus stat;
1191 static WCHAR fusiondll[] = {'f','u','s','i','o','n',0};
1192 HMODULE hfusion=NULL;
1193 static HRESULT (WINAPI *pCreateAssemblyCache)(IAssemblyCache**,DWORD);
1195 stringname = mono->mono_stringify_assembly_name(aname);
1197 TRACE("%s\n", debugstr_a(stringname));
1199 if (!stringname) return NULL;
1201 /* FIXME: We should search the given paths before the GAC. */
1203 if (!pCreateAssemblyCache)
1205 hr = LoadLibraryShim(fusiondll, NULL, NULL, &hfusion);
1207 if (SUCCEEDED(hr))
1209 pCreateAssemblyCache = (void*)GetProcAddress(hfusion, "CreateAssemblyCache");
1210 if (!pCreateAssemblyCache)
1211 hr = E_FAIL;
1215 if (SUCCEEDED(hr))
1216 hr = pCreateAssemblyCache(&asmcache, 0);
1218 if (SUCCEEDED(hr))
1220 stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, stringname, -1, NULL, 0);
1222 stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1223 if (stringnameW)
1224 MultiByteToWideChar(CP_UTF8, 0, stringname, -1, stringnameW, stringnameW_size);
1225 else
1226 hr = E_OUTOFMEMORY;
1228 if (SUCCEEDED(hr))
1230 info.cbAssemblyInfo = sizeof(info);
1231 info.pszCurrentAssemblyPathBuf = path;
1232 info.cchBuf = MAX_PATH;
1233 path[0] = 0;
1235 hr = IAssemblyCache_QueryAssemblyInfo(asmcache, 0, stringnameW, &info);
1238 HeapFree(GetProcessHeap(), 0, stringnameW);
1240 IAssemblyCache_Release(asmcache);
1243 if (SUCCEEDED(hr))
1245 TRACE("found: %s\n", debugstr_w(path));
1247 pathA = WtoA(path);
1249 if (pathA)
1251 result = mono->mono_assembly_open(pathA, &stat);
1253 if (!result)
1254 ERR("Failed to load %s, status=%u\n", debugstr_w(path), stat);
1256 HeapFree(GetProcessHeap(), 0, pathA);
1260 mono->mono_free(stringname);
1262 return result;
1265 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1266 DWORD startup_flags, DWORD runtimeinfo_flags, BOOL legacy, ICLRRuntimeInfo **result)
1268 static const WCHAR dotconfig[] = {'.','c','o','n','f','i','g',0};
1269 static const DWORD supported_startup_flags = 0;
1270 static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1271 int i;
1272 WCHAR local_version[MAX_PATH];
1273 ULONG local_version_size = MAX_PATH;
1274 WCHAR local_config_file[MAX_PATH];
1275 HRESULT hr;
1276 parsed_config_file parsed_config;
1278 if (startup_flags & ~supported_startup_flags)
1279 FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
1281 if (runtimeinfo_flags & ~supported_runtime_flags)
1282 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
1284 if (exefile && !config_file)
1286 strcpyW(local_config_file, exefile);
1287 strcatW(local_config_file, dotconfig);
1289 config_file = local_config_file;
1292 if (config_file)
1294 int found=0;
1295 hr = parse_config_file(config_file, &parsed_config);
1297 if (SUCCEEDED(hr))
1299 supported_runtime *entry;
1300 LIST_FOR_EACH_ENTRY(entry, &parsed_config.supported_runtimes, supported_runtime, entry)
1302 hr = CLRMetaHost_GetRuntime(0, entry->version, &IID_ICLRRuntimeInfo, (void**)result);
1303 if (SUCCEEDED(hr))
1305 found = 1;
1306 break;
1310 else
1312 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file), hr);
1315 free_parsed_config_file(&parsed_config);
1317 if (found)
1318 return S_OK;
1321 if (exefile && !version)
1323 hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1325 version = local_version;
1327 if (FAILED(hr)) return hr;
1330 if (version)
1332 hr = CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1333 if(SUCCEEDED(hr))
1334 return hr;
1337 if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1339 DWORD major, minor, build;
1341 if (version && !parse_runtime_version(version, &major, &minor, &build))
1343 ERR("Cannot parse %s\n", debugstr_w(version));
1344 return CLR_E_SHIM_RUNTIME;
1347 find_runtimes();
1349 if (legacy)
1350 i = 2;
1351 else
1352 i = NUM_RUNTIMES;
1354 while (i--)
1356 if (runtimes[i].mono_abi_version)
1358 /* Must be greater or equal to the version passed in. */
1359 if (!version || ((runtimes[i].major >= major && runtimes[i].minor >= minor && runtimes[i].build >= build) ||
1360 (runtimes[i].major >= major && runtimes[i].minor > minor) ||
1361 (runtimes[i].major > major)))
1363 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface,
1364 &IID_ICLRRuntimeInfo, (void **)result);
1369 if (legacy)
1370 missing_runtime_message(&runtimes[1]);
1371 else
1372 missing_runtime_message(&runtimes[NUM_RUNTIMES-1]);
1374 return CLR_E_SHIM_RUNTIME;
1377 return CLR_E_SHIM_RUNTIME;