kernel32: Add parentheses to clarify the precedence between '&' and '||'.
[wine/multimedia.git] / dlls / mscoree / metahost.c
blobd69bf1329b2140de93b95880cc8f42c0a816c3f1
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 "mscoree.h"
36 #include "fusion.h"
37 #include "metahost.h"
38 #include "wine/list.h"
39 #include "mscoree_private.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL( mscoree );
45 static const WCHAR net_11_subdir[] = {'1','.','0',0};
46 static const WCHAR net_20_subdir[] = {'2','.','0',0};
47 static const WCHAR net_40_subdir[] = {'4','.','0',0};
49 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl;
51 #define NUM_RUNTIMES 3
53 static struct CLRRuntimeInfo runtimes[NUM_RUNTIMES] = {
54 {{&CLRRuntimeInfoVtbl}, net_11_subdir, 1, 1, 4322, 0},
55 {{&CLRRuntimeInfoVtbl}, net_20_subdir, 2, 0, 50727, 0},
56 {{&CLRRuntimeInfoVtbl}, net_40_subdir, 4, 0, 30319, 0}
59 static int runtimes_initialized;
61 static CRITICAL_SECTION runtime_list_cs;
62 static CRITICAL_SECTION_DEBUG runtime_list_cs_debug =
64 0, 0, &runtime_list_cs,
65 { &runtime_list_cs_debug.ProcessLocksList,
66 &runtime_list_cs_debug.ProcessLocksList },
67 0, 0, { (DWORD_PTR)(__FILE__ ": runtime_list_cs") }
69 static CRITICAL_SECTION runtime_list_cs = { &runtime_list_cs_debug, -1, 0, 0, 0, 0 };
71 #define NUM_ABI_VERSIONS 2
73 static loaded_mono loaded_monos[NUM_ABI_VERSIONS];
75 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version);
77 static MonoAssembly* mono_assembly_search_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data);
79 static void mono_shutdown_callback_fn(MonoProfiler *prof);
81 static void set_environment(LPCWSTR bin_path)
83 WCHAR path_env[MAX_PATH];
84 int len;
86 static const WCHAR pathW[] = {'P','A','T','H',0};
88 /* We have to modify PATH as Mono loads other DLLs from this directory. */
89 GetEnvironmentVariableW(pathW, path_env, sizeof(path_env)/sizeof(WCHAR));
90 len = strlenW(path_env);
91 path_env[len++] = ';';
92 strcpyW(path_env+len, bin_path);
93 SetEnvironmentVariableW(pathW, path_env);
96 static void CDECL do_nothing(void)
100 static void missing_runtime_message(const CLRRuntimeInfo *This)
102 if (This->major == 1)
103 MESSAGE("wine: Install Mono 2.6 for Windows to run .NET 1.1 applications.\n");
104 else if (This->major == 2)
105 MESSAGE("wine: Install Mono for Windows to run .NET 2.0 applications.\n");
106 else if (This->major == 4)
107 MESSAGE("wine: Install Mono 2.8 or greater for Windows to run .NET 4.0 applications.\n");
110 static HRESULT load_mono(CLRRuntimeInfo *This, loaded_mono **result)
112 static const WCHAR bin[] = {'\\','b','i','n',0};
113 static const WCHAR lib[] = {'\\','l','i','b',0};
114 static const WCHAR etc[] = {'\\','e','t','c',0};
115 static const WCHAR glibdll[] = {'l','i','b','g','l','i','b','-','2','.','0','-','0','.','d','l','l',0};
116 WCHAR mono_dll_path[MAX_PATH+16], mono_bin_path[MAX_PATH+4];
117 WCHAR mono_lib_path[MAX_PATH+4], mono_etc_path[MAX_PATH+4];
118 char mono_lib_path_a[MAX_PATH], mono_etc_path_a[MAX_PATH];
119 int trace_size;
120 char trace_setting[256];
122 if (This->mono_abi_version <= 0 || This->mono_abi_version > NUM_ABI_VERSIONS)
124 missing_runtime_message(This);
125 return E_FAIL;
128 *result = &loaded_monos[This->mono_abi_version-1];
130 if ((*result)->is_shutdown)
132 ERR("Cannot load Mono after it has been shut down.\n");
133 *result = NULL;
134 return E_FAIL;
137 if (!(*result)->mono_handle)
139 strcpyW(mono_bin_path, This->mono_path);
140 strcatW(mono_bin_path, bin);
141 set_environment(mono_bin_path);
143 strcpyW(mono_lib_path, This->mono_path);
144 strcatW(mono_lib_path, lib);
145 WideCharToMultiByte(CP_UTF8, 0, mono_lib_path, -1, mono_lib_path_a, MAX_PATH, NULL, NULL);
147 strcpyW(mono_etc_path, This->mono_path);
148 strcatW(mono_etc_path, etc);
149 WideCharToMultiByte(CP_UTF8, 0, mono_etc_path, -1, mono_etc_path_a, MAX_PATH, NULL, NULL);
151 if (!find_mono_dll(This->mono_path, mono_dll_path, This->mono_abi_version)) goto fail;
153 (*result)->mono_handle = LoadLibraryW(mono_dll_path);
155 if (!(*result)->mono_handle) goto fail;
157 #define LOAD_MONO_FUNCTION(x) do { \
158 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
159 if (!(*result)->x) { \
160 goto fail; \
162 } while (0);
164 LOAD_MONO_FUNCTION(mono_assembly_get_image);
165 LOAD_MONO_FUNCTION(mono_assembly_open);
166 LOAD_MONO_FUNCTION(mono_config_parse);
167 LOAD_MONO_FUNCTION(mono_class_from_mono_type);
168 LOAD_MONO_FUNCTION(mono_class_from_name);
169 LOAD_MONO_FUNCTION(mono_class_get_method_from_name);
170 LOAD_MONO_FUNCTION(mono_domain_assembly_open);
171 LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook);
172 LOAD_MONO_FUNCTION(mono_jit_exec);
173 LOAD_MONO_FUNCTION(mono_jit_init);
174 LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
175 LOAD_MONO_FUNCTION(mono_object_get_domain);
176 LOAD_MONO_FUNCTION(mono_object_new);
177 LOAD_MONO_FUNCTION(mono_object_unbox);
178 LOAD_MONO_FUNCTION(mono_profiler_install);
179 LOAD_MONO_FUNCTION(mono_reflection_type_from_name);
180 LOAD_MONO_FUNCTION(mono_runtime_invoke);
181 LOAD_MONO_FUNCTION(mono_runtime_object_init);
182 LOAD_MONO_FUNCTION(mono_runtime_quit);
183 LOAD_MONO_FUNCTION(mono_set_dirs);
184 LOAD_MONO_FUNCTION(mono_stringify_assembly_name);
186 /* GLib imports obsoleted by the 2.0 ABI */
187 if (This->mono_abi_version == 1)
189 (*result)->glib_handle = LoadLibraryW(glibdll);
190 if (!(*result)->glib_handle) goto fail;
192 (*result)->mono_free = (void*)GetProcAddress((*result)->glib_handle, "g_free");
193 if (!(*result)->mono_free) goto fail;
195 else
197 LOAD_MONO_FUNCTION(mono_free);
200 #undef LOAD_MONO_FUNCTION
202 #define LOAD_OPT_VOID_MONO_FUNCTION(x) do { \
203 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
204 if (!(*result)->x) { \
205 (*result)->x = do_nothing; \
207 } while (0);
209 LOAD_OPT_VOID_MONO_FUNCTION(mono_runtime_set_shutting_down);
210 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_pool_cleanup);
211 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_suspend_all_other_threads);
212 LOAD_OPT_VOID_MONO_FUNCTION(mono_threads_set_shutting_down);
214 #undef LOAD_OPT_VOID_MONO_FUNCTION
216 (*result)->mono_profiler_install((MonoProfiler*)*result, mono_shutdown_callback_fn);
218 (*result)->mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
220 (*result)->mono_config_parse(NULL);
222 (*result)->mono_install_assembly_preload_hook(mono_assembly_search_hook_fn, *result);
224 trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
226 if (trace_size)
228 (*result)->mono_jit_set_trace_options(trace_setting);
232 return S_OK;
234 fail:
235 ERR("Could not load Mono into this process\n");
236 FreeLibrary((*result)->mono_handle);
237 FreeLibrary((*result)->glib_handle);
238 (*result)->mono_handle = NULL;
239 (*result)->glib_handle = NULL;
240 return E_FAIL;
243 static void mono_shutdown_callback_fn(MonoProfiler *prof)
245 loaded_mono *mono = (loaded_mono*)prof;
247 mono->is_shutdown = TRUE;
250 static HRESULT CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo *This, RuntimeHost **result)
252 HRESULT hr = S_OK;
253 loaded_mono *ploaded_mono;
255 if (This->loaded_runtime)
257 *result = This->loaded_runtime;
258 return hr;
261 EnterCriticalSection(&runtime_list_cs);
263 hr = load_mono(This, &ploaded_mono);
265 if (SUCCEEDED(hr))
266 hr = RuntimeHost_Construct(This, ploaded_mono, &This->loaded_runtime);
268 LeaveCriticalSection(&runtime_list_cs);
270 if (SUCCEEDED(hr))
271 *result = This->loaded_runtime;
273 return hr;
276 void unload_all_runtimes(void)
278 int i;
280 for (i=0; i<NUM_ABI_VERSIONS; i++)
282 loaded_mono *mono = &loaded_monos[i];
283 if (mono->mono_handle && mono->is_started && !mono->is_shutdown)
285 /* Copied from Mono's ves_icall_System_Environment_Exit */
286 mono->mono_threads_set_shutting_down();
287 mono->mono_runtime_set_shutting_down();
288 mono->mono_thread_pool_cleanup();
289 mono->mono_thread_suspend_all_other_threads();
290 mono->mono_runtime_quit();
294 for (i=0; i<NUM_RUNTIMES; i++)
295 if (runtimes[i].loaded_runtime)
296 RuntimeHost_Destroy(runtimes[i].loaded_runtime);
299 void expect_no_runtimes(void)
301 int i;
303 for (i=0; i<NUM_ABI_VERSIONS; i++)
305 loaded_mono *mono = &loaded_monos[i];
306 if (mono->mono_handle && mono->is_started && !mono->is_shutdown)
308 ERR("Process exited with a Mono runtime loaded.\n");
309 return;
314 static inline CLRRuntimeInfo *impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo *iface)
316 return CONTAINING_RECORD(iface, CLRRuntimeInfo, ICLRRuntimeInfo_iface);
319 static HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
320 REFIID riid,
321 void **ppvObject)
323 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
325 if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
326 IsEqualGUID( riid, &IID_IUnknown ) )
328 *ppvObject = iface;
330 else
332 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
333 return E_NOINTERFACE;
336 ICLRRuntimeInfo_AddRef( iface );
338 return S_OK;
341 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
343 return 2;
346 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
348 return 1;
351 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
352 LPWSTR pwzBuffer, DWORD *pcchBuffer)
354 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
355 DWORD buffer_size = *pcchBuffer;
356 HRESULT hr = S_OK;
357 char version[11];
358 DWORD size;
360 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
362 size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
364 assert(size <= sizeof(version));
366 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
368 if (pwzBuffer)
370 if (buffer_size >= *pcchBuffer)
371 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
372 else
373 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
376 return hr;
379 static BOOL get_install_root(LPWSTR install_dir)
381 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};
382 const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
384 DWORD len;
385 HKEY key;
387 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
388 return FALSE;
390 len = MAX_PATH;
391 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
393 RegCloseKey(key);
394 return FALSE;
396 RegCloseKey(key);
398 return TRUE;
401 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
402 LPWSTR pwzBuffer, DWORD *pcchBuffer)
404 static const WCHAR slash[] = {'\\',0};
405 DWORD buffer_size = *pcchBuffer;
406 WCHAR system_dir[MAX_PATH];
407 WCHAR version[MAX_PATH];
408 DWORD version_size, size;
409 HRESULT hr = S_OK;
411 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
413 if (!get_install_root(system_dir))
415 ERR("error reading registry key for installroot\n");
416 return E_FAIL;
418 else
420 version_size = MAX_PATH;
421 ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
422 lstrcatW(system_dir, version);
423 lstrcatW(system_dir, slash);
424 size = lstrlenW(system_dir) + 1;
427 *pcchBuffer = size;
429 if (pwzBuffer)
431 if (buffer_size >= size)
432 strcpyW(pwzBuffer, system_dir);
433 else
434 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
437 return hr;
440 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
441 HANDLE hndProcess, BOOL *pbLoaded)
443 FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
445 return E_NOTIMPL;
448 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
449 UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
451 FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
453 return E_NOTIMPL;
456 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
457 LPCWSTR pwzDllName, HMODULE *phndModule)
459 WCHAR version[MAX_PATH];
460 HRESULT hr;
461 DWORD cchBuffer;
463 TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
465 cchBuffer = MAX_PATH;
466 hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
467 if (FAILED(hr)) return hr;
469 return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
472 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
473 LPCSTR pszProcName, LPVOID *ppProc)
475 FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
477 return E_NOTIMPL;
480 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
481 REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
483 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
484 RuntimeHost *host;
485 HRESULT hr;
487 TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
489 hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
491 if (SUCCEEDED(hr))
492 hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
494 return hr;
497 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
498 BOOL *pbLoadable)
500 FIXME("%p %p\n", iface, pbLoadable);
502 return E_NOTIMPL;
505 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
506 DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
508 FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
510 return E_NOTIMPL;
513 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
514 DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
516 FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
518 return E_NOTIMPL;
521 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
523 FIXME("%p\n", iface);
525 return E_NOTIMPL;
528 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
529 BOOL *pbStarted, DWORD *pdwStartupFlags)
531 FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
533 return E_NOTIMPL;
536 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
537 CLRRuntimeInfo_QueryInterface,
538 CLRRuntimeInfo_AddRef,
539 CLRRuntimeInfo_Release,
540 CLRRuntimeInfo_GetVersionString,
541 CLRRuntimeInfo_GetRuntimeDirectory,
542 CLRRuntimeInfo_IsLoaded,
543 CLRRuntimeInfo_LoadErrorString,
544 CLRRuntimeInfo_LoadLibrary,
545 CLRRuntimeInfo_GetProcAddress,
546 CLRRuntimeInfo_GetInterface,
547 CLRRuntimeInfo_IsLoadable,
548 CLRRuntimeInfo_SetDefaultStartupFlags,
549 CLRRuntimeInfo_GetDefaultStartupFlags,
550 CLRRuntimeInfo_BindAsLegacyV2Runtime,
551 CLRRuntimeInfo_IsStarted
554 HRESULT ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo *iface, RuntimeHost **result)
556 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
558 assert(This->ICLRRuntimeInfo_iface.lpVtbl == &CLRRuntimeInfoVtbl);
560 return CLRRuntimeInfo_GetRuntimeHost(This, result);
563 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version)
565 static const WCHAR mono_dll[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
566 static const WCHAR libmono_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
567 static const WCHAR mono2_dll[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
568 static const WCHAR libmono2_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
569 DWORD attributes=INVALID_FILE_ATTRIBUTES;
571 if (abi_version == 1)
573 strcpyW(dll_path, path);
574 strcatW(dll_path, mono_dll);
575 attributes = GetFileAttributesW(dll_path);
577 if (attributes == INVALID_FILE_ATTRIBUTES)
579 strcpyW(dll_path, path);
580 strcatW(dll_path, libmono_dll);
581 attributes = GetFileAttributesW(dll_path);
584 else if (abi_version == 2)
586 strcpyW(dll_path, path);
587 strcatW(dll_path, mono2_dll);
588 attributes = GetFileAttributesW(dll_path);
590 if (attributes == INVALID_FILE_ATTRIBUTES)
592 strcpyW(dll_path, path);
593 strcatW(dll_path, libmono2_dll);
594 attributes = GetFileAttributesW(dll_path);
598 return (attributes != INVALID_FILE_ATTRIBUTES);
601 static BOOL get_mono_path_from_registry(LPWSTR path, int abi_version)
603 static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
604 static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
605 static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
606 static const WCHAR slash[] = {'\\',0};
608 WCHAR version[64], version_key[MAX_PATH];
609 DWORD len;
610 HKEY key;
611 WCHAR dll_path[MAX_PATH];
613 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
614 return FALSE;
616 len = sizeof(version);
617 if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
619 RegCloseKey(key);
620 return FALSE;
622 RegCloseKey(key);
624 lstrcpyW(version_key, mono_key);
625 lstrcatW(version_key, slash);
626 lstrcatW(version_key, version);
628 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
629 return FALSE;
631 len = sizeof(WCHAR) * MAX_PATH;
632 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
634 RegCloseKey(key);
635 return FALSE;
637 RegCloseKey(key);
639 return find_mono_dll(path, dll_path, abi_version);
642 static BOOL get_mono_path_from_folder(LPCWSTR folder, LPWSTR mono_path, int abi_version)
644 static const WCHAR mono_one_dot_zero[] = {'\\','m','o','n','o','-','1','.','0', 0};
645 static const WCHAR mono_two_dot_zero[] = {'\\','m','o','n','o','-','2','.','0', 0};
646 WCHAR mono_dll_path[MAX_PATH];
647 BOOL found = FALSE;
649 strcpyW(mono_path, folder);
651 if (abi_version == 1)
652 strcatW(mono_path, mono_one_dot_zero);
653 else if (abi_version == 2)
654 strcatW(mono_path, mono_two_dot_zero);
656 found = find_mono_dll(mono_path, mono_dll_path, abi_version);
658 return found;
661 static BOOL get_mono_path(LPWSTR path, int abi_version)
663 static const WCHAR subdir_mono[] = {'\\','m','o','n','o',0};
664 static const WCHAR sibling_mono[] = {'\\','.','.','\\','m','o','n','o',0};
665 WCHAR base_path[MAX_PATH];
666 const char *unix_data_dir;
667 WCHAR *dos_data_dir;
668 int build_tree=0;
669 static WCHAR* (CDECL *wine_get_dos_file_name)(const char*);
671 /* First try c:\windows\mono */
672 GetWindowsDirectoryW(base_path, MAX_PATH);
673 strcatW(base_path, subdir_mono);
675 if (get_mono_path_from_folder(base_path, path, abi_version))
676 return TRUE;
678 /* Next: /usr/share/wine/mono */
679 unix_data_dir = wine_get_data_dir();
681 if (!unix_data_dir)
683 unix_data_dir = wine_get_build_dir();
684 build_tree = 1;
687 if (unix_data_dir)
689 if (!wine_get_dos_file_name)
690 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
692 if (wine_get_dos_file_name)
694 dos_data_dir = wine_get_dos_file_name(unix_data_dir);
696 if (dos_data_dir)
698 strcpyW(base_path, dos_data_dir);
699 strcatW(base_path, build_tree ? sibling_mono : subdir_mono);
701 HeapFree(GetProcessHeap(), 0, dos_data_dir);
703 if (get_mono_path_from_folder(base_path, path, abi_version))
704 return TRUE;
709 /* Last: the registry */
710 return get_mono_path_from_registry(path, abi_version);
713 static void find_runtimes(void)
715 int abi_version, i;
716 static const WCHAR libmono[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
717 static const WCHAR mscorlib[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
718 WCHAR mono_path[MAX_PATH], lib_path[MAX_PATH];
719 BOOL any_runtimes_found = FALSE;
721 if (runtimes_initialized) return;
723 EnterCriticalSection(&runtime_list_cs);
725 if (runtimes_initialized) goto end;
727 for (abi_version=NUM_ABI_VERSIONS; abi_version>0; abi_version--)
729 if (!get_mono_path(mono_path, abi_version))
730 continue;
732 for (i=0; i<NUM_RUNTIMES; i++)
734 if (runtimes[i].mono_abi_version == 0)
736 strcpyW(lib_path, mono_path);
737 strcatW(lib_path, libmono);
738 strcatW(lib_path, runtimes[i].mono_libdir);
739 strcatW(lib_path, mscorlib);
741 if (GetFileAttributesW(lib_path) != INVALID_FILE_ATTRIBUTES)
743 runtimes[i].mono_abi_version = abi_version;
745 strcpyW(runtimes[i].mono_path, mono_path);
746 strcpyW(runtimes[i].mscorlib_path, lib_path);
748 any_runtimes_found = TRUE;
754 if (!any_runtimes_found)
756 /* Report all runtimes are available if Mono isn't installed.
757 * FIXME: Remove this when Mono is properly packaged. */
758 for (i=0; i<NUM_RUNTIMES; i++)
759 runtimes[i].mono_abi_version = -1;
762 runtimes_initialized = 1;
764 end:
765 LeaveCriticalSection(&runtime_list_cs);
768 struct InstalledRuntimeEnum
770 IEnumUnknown IEnumUnknown_iface;
771 LONG ref;
772 ULONG pos;
775 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
777 static inline struct InstalledRuntimeEnum *impl_from_IEnumUnknown(IEnumUnknown *iface)
779 return CONTAINING_RECORD(iface, struct InstalledRuntimeEnum, IEnumUnknown_iface);
782 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface, REFIID riid,
783 void **ppvObject)
785 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
787 if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
788 IsEqualGUID( riid, &IID_IUnknown ) )
790 *ppvObject = iface;
792 else
794 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
795 return E_NOINTERFACE;
798 IEnumUnknown_AddRef( iface );
800 return S_OK;
803 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
805 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
806 ULONG ref = InterlockedIncrement(&This->ref);
808 TRACE("(%p) refcount=%u\n", iface, ref);
810 return ref;
813 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
815 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
816 ULONG ref = InterlockedDecrement(&This->ref);
818 TRACE("(%p) refcount=%u\n", iface, ref);
820 if (ref == 0)
822 HeapFree(GetProcessHeap(), 0, This);
825 return ref;
828 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
829 IUnknown **rgelt, ULONG *pceltFetched)
831 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
832 int num_fetched = 0;
833 HRESULT hr=S_OK;
834 IUnknown *item;
836 TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
838 while (num_fetched < celt)
840 if (This->pos >= NUM_RUNTIMES)
842 hr = S_FALSE;
843 break;
845 if (runtimes[This->pos].mono_abi_version)
847 item = (IUnknown*)&runtimes[This->pos].ICLRRuntimeInfo_iface;
848 IUnknown_AddRef(item);
849 rgelt[num_fetched] = item;
850 num_fetched++;
852 This->pos++;
855 if (pceltFetched)
856 *pceltFetched = num_fetched;
858 return hr;
861 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
863 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
864 int num_fetched = 0;
865 HRESULT hr=S_OK;
867 TRACE("(%p,%u)\n", iface, celt);
869 while (num_fetched < celt)
871 if (This->pos >= NUM_RUNTIMES)
873 hr = S_FALSE;
874 break;
876 if (runtimes[This->pos].mono_abi_version)
878 num_fetched++;
880 This->pos++;
883 return hr;
886 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
888 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
890 TRACE("(%p)\n", iface);
892 This->pos = 0;
894 return S_OK;
897 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
899 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
900 struct InstalledRuntimeEnum *new_enum;
902 TRACE("(%p)\n", iface);
904 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
905 if (!new_enum)
906 return E_OUTOFMEMORY;
908 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
909 new_enum->ref = 1;
910 new_enum->pos = This->pos;
912 *ppenum = &new_enum->IEnumUnknown_iface;
914 return S_OK;
917 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
918 InstalledRuntimeEnum_QueryInterface,
919 InstalledRuntimeEnum_AddRef,
920 InstalledRuntimeEnum_Release,
921 InstalledRuntimeEnum_Next,
922 InstalledRuntimeEnum_Skip,
923 InstalledRuntimeEnum_Reset,
924 InstalledRuntimeEnum_Clone
927 struct CLRMetaHost
929 ICLRMetaHost ICLRMetaHost_iface;
932 static struct CLRMetaHost GlobalCLRMetaHost;
934 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
935 REFIID riid,
936 void **ppvObject)
938 TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
940 if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
941 IsEqualGUID( riid, &IID_IUnknown ) )
943 *ppvObject = iface;
945 else
947 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
948 return E_NOINTERFACE;
951 ICLRMetaHost_AddRef( iface );
953 return S_OK;
956 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
958 return 2;
961 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
963 return 1;
966 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
968 *major = 0;
969 *minor = 0;
970 *build = 0;
972 if (version[0] == 'v')
974 version++;
975 if (!isdigit(*version))
976 return FALSE;
978 while (isdigit(*version))
979 *major = *major * 10 + (*version++ - '0');
981 if (*version == 0)
982 return TRUE;
984 if (*version++ != '.' || !isdigit(*version))
985 return FALSE;
987 while (isdigit(*version))
988 *minor = *minor * 10 + (*version++ - '0');
990 if (*version == 0)
991 return TRUE;
993 if (*version++ != '.' || !isdigit(*version))
994 return FALSE;
996 while (isdigit(*version))
997 *build = *build * 10 + (*version++ - '0');
999 return *version == 0;
1001 else
1002 return FALSE;
1005 static HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
1006 LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
1008 int i;
1009 DWORD major, minor, build;
1011 TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
1013 if (!pwzVersion)
1014 return E_POINTER;
1016 if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
1018 ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
1019 return CLR_E_SHIM_RUNTIME;
1022 find_runtimes();
1024 for (i=0; i<NUM_RUNTIMES; i++)
1026 if (runtimes[i].major == major && runtimes[i].minor == minor &&
1027 runtimes[i].build == build)
1029 if (runtimes[i].mono_abi_version)
1030 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface, iid,
1031 ppRuntime);
1032 else
1034 missing_runtime_message(&runtimes[i]);
1035 return CLR_E_SHIM_RUNTIME;
1040 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
1041 return CLR_E_SHIM_RUNTIME;
1044 HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
1045 LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
1047 ASSEMBLY *assembly;
1048 HRESULT hr;
1049 LPSTR version;
1050 ULONG buffer_size=*pcchBuffer;
1052 TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
1054 hr = assembly_create(&assembly, pwzFilePath);
1056 if (SUCCEEDED(hr))
1058 hr = assembly_get_runtime_version(assembly, &version);
1060 if (SUCCEEDED(hr))
1062 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
1064 if (pwzBuffer)
1066 if (buffer_size >= *pcchBuffer)
1067 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
1068 else
1069 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
1073 assembly_release(assembly);
1076 return hr;
1079 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
1080 IEnumUnknown **ppEnumerator)
1082 struct InstalledRuntimeEnum *new_enum;
1084 TRACE("%p\n", ppEnumerator);
1086 find_runtimes();
1088 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
1089 if (!new_enum)
1090 return E_OUTOFMEMORY;
1092 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
1093 new_enum->ref = 1;
1094 new_enum->pos = 0;
1096 *ppEnumerator = &new_enum->IEnumUnknown_iface;
1098 return S_OK;
1101 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
1102 HANDLE hndProcess, IEnumUnknown **ppEnumerator)
1104 FIXME("%p %p\n", hndProcess, ppEnumerator);
1106 return E_NOTIMPL;
1109 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
1110 RuntimeLoadedCallbackFnPtr pCallbackFunction)
1112 FIXME("%p\n", pCallbackFunction);
1114 return E_NOTIMPL;
1117 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
1118 REFIID riid, LPVOID *ppUnk)
1120 FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
1122 return E_NOTIMPL;
1125 static HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
1127 FIXME("%i: stub\n", iExitCode);
1129 ExitProcess(iExitCode);
1132 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
1134 CLRMetaHost_QueryInterface,
1135 CLRMetaHost_AddRef,
1136 CLRMetaHost_Release,
1137 CLRMetaHost_GetRuntime,
1138 CLRMetaHost_GetVersionFromFile,
1139 CLRMetaHost_EnumerateInstalledRuntimes,
1140 CLRMetaHost_EnumerateLoadedRuntimes,
1141 CLRMetaHost_RequestRuntimeLoadedNotification,
1142 CLRMetaHost_QueryLegacyV2RuntimeBinding,
1143 CLRMetaHost_ExitProcess
1146 static struct CLRMetaHost GlobalCLRMetaHost = {
1147 { &CLRMetaHost_vtbl }
1150 HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1152 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost.ICLRMetaHost_iface, riid, ppobj);
1155 static MonoAssembly* mono_assembly_search_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data)
1157 loaded_mono *mono = user_data;
1158 HRESULT hr=S_OK;
1159 MonoAssembly *result=NULL;
1160 char *stringname=NULL;
1161 LPWSTR stringnameW;
1162 int stringnameW_size;
1163 IAssemblyCache *asmcache;
1164 ASSEMBLY_INFO info;
1165 WCHAR path[MAX_PATH];
1166 char *pathA;
1167 MonoImageOpenStatus stat;
1168 static WCHAR fusiondll[] = {'f','u','s','i','o','n',0};
1169 HMODULE hfusion=NULL;
1170 static HRESULT WINAPI (*pCreateAssemblyCache)(IAssemblyCache**,DWORD);
1172 stringname = mono->mono_stringify_assembly_name(aname);
1174 TRACE("%s\n", debugstr_a(stringname));
1176 if (!stringname) return NULL;
1178 /* FIXME: We should search the given paths before the GAC. */
1180 if (!pCreateAssemblyCache)
1182 hr = LoadLibraryShim(fusiondll, NULL, NULL, &hfusion);
1184 if (SUCCEEDED(hr))
1186 pCreateAssemblyCache = (void*)GetProcAddress(hfusion, "CreateAssemblyCache");
1187 if (!pCreateAssemblyCache)
1188 hr = E_FAIL;
1192 if (SUCCEEDED(hr))
1193 hr = pCreateAssemblyCache(&asmcache, 0);
1195 if (SUCCEEDED(hr))
1197 stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, stringname, -1, NULL, 0);
1199 stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1200 if (stringnameW)
1201 MultiByteToWideChar(CP_UTF8, 0, stringname, -1, stringnameW, stringnameW_size);
1202 else
1203 hr = E_OUTOFMEMORY;
1205 if (SUCCEEDED(hr))
1207 info.cbAssemblyInfo = sizeof(info);
1208 info.pszCurrentAssemblyPathBuf = path;
1209 info.cchBuf = MAX_PATH;
1210 path[0] = 0;
1212 hr = IAssemblyCache_QueryAssemblyInfo(asmcache, 0, stringnameW, &info);
1215 HeapFree(GetProcessHeap(), 0, stringnameW);
1217 IAssemblyCache_Release(asmcache);
1220 if (SUCCEEDED(hr))
1222 TRACE("found: %s\n", debugstr_w(path));
1224 pathA = WtoA(path);
1226 if (pathA)
1228 result = mono->mono_assembly_open(pathA, &stat);
1230 if (!result)
1231 ERR("Failed to load %s, status=%u\n", debugstr_w(path), stat);
1233 HeapFree(GetProcessHeap(), 0, pathA);
1237 mono->mono_free(stringname);
1239 return result;
1242 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1243 DWORD startup_flags, DWORD runtimeinfo_flags, BOOL legacy, ICLRRuntimeInfo **result)
1245 static const WCHAR dotconfig[] = {'.','c','o','n','f','i','g',0};
1246 static const DWORD supported_startup_flags = 0;
1247 static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1248 int i;
1249 WCHAR local_version[MAX_PATH];
1250 ULONG local_version_size = MAX_PATH;
1251 WCHAR local_config_file[MAX_PATH];
1252 HRESULT hr;
1253 parsed_config_file parsed_config;
1255 if (startup_flags & ~supported_startup_flags)
1256 FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
1258 if (runtimeinfo_flags & ~supported_runtime_flags)
1259 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
1261 if (exefile && !config_file)
1263 strcpyW(local_config_file, exefile);
1264 strcatW(local_config_file, dotconfig);
1266 config_file = local_config_file;
1269 if (config_file)
1271 int found=0;
1272 hr = parse_config_file(config_file, &parsed_config);
1274 if (SUCCEEDED(hr))
1276 supported_runtime *entry;
1277 LIST_FOR_EACH_ENTRY(entry, &parsed_config.supported_runtimes, supported_runtime, entry)
1279 hr = CLRMetaHost_GetRuntime(0, entry->version, &IID_ICLRRuntimeInfo, (void**)result);
1280 if (SUCCEEDED(hr))
1282 found = 1;
1283 break;
1287 else
1289 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file), hr);
1292 free_parsed_config_file(&parsed_config);
1294 if (found)
1295 return S_OK;
1298 if (exefile && !version)
1300 hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1302 version = local_version;
1304 if (FAILED(hr)) return hr;
1307 if (version)
1309 return CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1312 if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1314 find_runtimes();
1316 if (legacy)
1317 i = 2;
1318 else
1319 i = NUM_RUNTIMES;
1321 while (i--)
1323 if (runtimes[i].mono_abi_version)
1324 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface,
1325 &IID_ICLRRuntimeInfo, (void **)result);
1328 if (legacy)
1329 missing_runtime_message(&runtimes[1]);
1330 else
1331 missing_runtime_message(&runtimes[NUM_RUNTIMES-1]);
1333 return CLR_E_SHIM_RUNTIME;
1336 return CLR_E_SHIM_RUNTIME;