mscoree: Add ICorDebug interface support.
[wine/multimedia.git] / dlls / mscoree / metahost.c
blobd9e36a88d0f6d39b59e55e71cbfa0519bbd64239
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);
189 /* GLib imports obsoleted by the 2.0 ABI */
190 if (This->mono_abi_version == 1)
192 (*result)->glib_handle = LoadLibraryW(glibdll);
193 if (!(*result)->glib_handle) goto fail;
195 (*result)->mono_free = (void*)GetProcAddress((*result)->glib_handle, "g_free");
196 if (!(*result)->mono_free) goto fail;
198 else
200 LOAD_MONO_FUNCTION(mono_free);
203 #undef LOAD_MONO_FUNCTION
205 #define LOAD_OPT_VOID_MONO_FUNCTION(x) do { \
206 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
207 if (!(*result)->x) { \
208 (*result)->x = do_nothing; \
210 } while (0);
212 LOAD_OPT_VOID_MONO_FUNCTION(mono_runtime_set_shutting_down);
213 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_pool_cleanup);
214 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_suspend_all_other_threads);
215 LOAD_OPT_VOID_MONO_FUNCTION(mono_threads_set_shutting_down);
217 #undef LOAD_OPT_VOID_MONO_FUNCTION
219 (*result)->mono_profiler_install((MonoProfiler*)*result, mono_shutdown_callback_fn);
221 (*result)->mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
223 (*result)->mono_config_parse(NULL);
225 (*result)->mono_install_assembly_preload_hook(mono_assembly_search_hook_fn, *result);
227 trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
229 if (trace_size)
231 (*result)->mono_jit_set_trace_options(trace_setting);
235 return S_OK;
237 fail:
238 ERR("Could not load Mono into this process\n");
239 FreeLibrary((*result)->mono_handle);
240 FreeLibrary((*result)->glib_handle);
241 (*result)->mono_handle = NULL;
242 (*result)->glib_handle = NULL;
243 return E_FAIL;
246 static void mono_shutdown_callback_fn(MonoProfiler *prof)
248 loaded_mono *mono = (loaded_mono*)prof;
250 mono->is_shutdown = TRUE;
253 static HRESULT CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo *This, RuntimeHost **result)
255 HRESULT hr = S_OK;
256 loaded_mono *ploaded_mono;
258 if (This->loaded_runtime)
260 *result = This->loaded_runtime;
261 return hr;
264 EnterCriticalSection(&runtime_list_cs);
266 hr = load_mono(This, &ploaded_mono);
268 if (SUCCEEDED(hr))
269 hr = RuntimeHost_Construct(This, ploaded_mono, &This->loaded_runtime);
271 LeaveCriticalSection(&runtime_list_cs);
273 if (SUCCEEDED(hr))
274 *result = This->loaded_runtime;
276 return hr;
279 void unload_all_runtimes(void)
281 int i;
283 for (i=0; i<NUM_ABI_VERSIONS; i++)
285 loaded_mono *mono = &loaded_monos[i];
286 if (mono->mono_handle && mono->is_started && !mono->is_shutdown)
288 /* Copied from Mono's ves_icall_System_Environment_Exit */
289 mono->mono_threads_set_shutting_down();
290 mono->mono_runtime_set_shutting_down();
291 mono->mono_thread_pool_cleanup();
292 mono->mono_thread_suspend_all_other_threads();
293 mono->mono_runtime_quit();
297 for (i=0; i<NUM_RUNTIMES; i++)
298 if (runtimes[i].loaded_runtime)
299 RuntimeHost_Destroy(runtimes[i].loaded_runtime);
302 void expect_no_runtimes(void)
304 int i;
306 for (i=0; i<NUM_ABI_VERSIONS; i++)
308 loaded_mono *mono = &loaded_monos[i];
309 if (mono->mono_handle && mono->is_started && !mono->is_shutdown)
311 ERR("Process exited with a Mono runtime loaded.\n");
312 return;
317 static inline CLRRuntimeInfo *impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo *iface)
319 return CONTAINING_RECORD(iface, CLRRuntimeInfo, ICLRRuntimeInfo_iface);
322 static HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
323 REFIID riid,
324 void **ppvObject)
326 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
328 if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
329 IsEqualGUID( riid, &IID_IUnknown ) )
331 *ppvObject = iface;
333 else
335 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
336 return E_NOINTERFACE;
339 ICLRRuntimeInfo_AddRef( iface );
341 return S_OK;
344 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
346 return 2;
349 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
351 return 1;
354 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
355 LPWSTR pwzBuffer, DWORD *pcchBuffer)
357 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
358 DWORD buffer_size = *pcchBuffer;
359 HRESULT hr = S_OK;
360 char version[11];
361 DWORD size;
363 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
365 size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
367 assert(size <= sizeof(version));
369 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
371 if (pwzBuffer)
373 if (buffer_size >= *pcchBuffer)
374 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
375 else
376 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
379 return hr;
382 static BOOL get_install_root(LPWSTR install_dir)
384 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};
385 const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
387 DWORD len;
388 HKEY key;
390 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
391 return FALSE;
393 len = MAX_PATH;
394 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
396 RegCloseKey(key);
397 return FALSE;
399 RegCloseKey(key);
401 return TRUE;
404 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
405 LPWSTR pwzBuffer, DWORD *pcchBuffer)
407 static const WCHAR slash[] = {'\\',0};
408 DWORD buffer_size = *pcchBuffer;
409 WCHAR system_dir[MAX_PATH];
410 WCHAR version[MAX_PATH];
411 DWORD version_size, size;
412 HRESULT hr = S_OK;
414 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
416 if (!get_install_root(system_dir))
418 ERR("error reading registry key for installroot\n");
419 return E_FAIL;
421 else
423 version_size = MAX_PATH;
424 ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
425 lstrcatW(system_dir, version);
426 lstrcatW(system_dir, slash);
427 size = lstrlenW(system_dir) + 1;
430 *pcchBuffer = size;
432 if (pwzBuffer)
434 if (buffer_size >= size)
435 strcpyW(pwzBuffer, system_dir);
436 else
437 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
440 return hr;
443 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
444 HANDLE hndProcess, BOOL *pbLoaded)
446 FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
448 return E_NOTIMPL;
451 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
452 UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
454 FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
456 return E_NOTIMPL;
459 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
460 LPCWSTR pwzDllName, HMODULE *phndModule)
462 WCHAR version[MAX_PATH];
463 HRESULT hr;
464 DWORD cchBuffer;
466 TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
468 cchBuffer = MAX_PATH;
469 hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
470 if (FAILED(hr)) return hr;
472 return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
475 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
476 LPCSTR pszProcName, LPVOID *ppProc)
478 FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
480 return E_NOTIMPL;
483 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
484 REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
486 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
487 RuntimeHost *host;
488 HRESULT hr;
490 TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
492 hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
494 if (SUCCEEDED(hr))
495 hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
497 return hr;
500 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
501 BOOL *pbLoadable)
503 FIXME("%p %p\n", iface, pbLoadable);
505 return E_NOTIMPL;
508 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
509 DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
511 FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
513 return E_NOTIMPL;
516 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
517 DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
519 FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
521 return E_NOTIMPL;
524 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
526 FIXME("%p\n", iface);
528 return E_NOTIMPL;
531 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
532 BOOL *pbStarted, DWORD *pdwStartupFlags)
534 FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
536 return E_NOTIMPL;
539 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
540 CLRRuntimeInfo_QueryInterface,
541 CLRRuntimeInfo_AddRef,
542 CLRRuntimeInfo_Release,
543 CLRRuntimeInfo_GetVersionString,
544 CLRRuntimeInfo_GetRuntimeDirectory,
545 CLRRuntimeInfo_IsLoaded,
546 CLRRuntimeInfo_LoadErrorString,
547 CLRRuntimeInfo_LoadLibrary,
548 CLRRuntimeInfo_GetProcAddress,
549 CLRRuntimeInfo_GetInterface,
550 CLRRuntimeInfo_IsLoadable,
551 CLRRuntimeInfo_SetDefaultStartupFlags,
552 CLRRuntimeInfo_GetDefaultStartupFlags,
553 CLRRuntimeInfo_BindAsLegacyV2Runtime,
554 CLRRuntimeInfo_IsStarted
557 HRESULT ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo *iface, RuntimeHost **result)
559 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
561 assert(This->ICLRRuntimeInfo_iface.lpVtbl == &CLRRuntimeInfoVtbl);
563 return CLRRuntimeInfo_GetRuntimeHost(This, result);
566 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version)
568 static const WCHAR mono_dll[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
569 static const WCHAR libmono_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
570 static const WCHAR mono2_dll[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
571 static const WCHAR libmono2_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
572 DWORD attributes=INVALID_FILE_ATTRIBUTES;
574 if (abi_version == 1)
576 strcpyW(dll_path, path);
577 strcatW(dll_path, mono_dll);
578 attributes = GetFileAttributesW(dll_path);
580 if (attributes == INVALID_FILE_ATTRIBUTES)
582 strcpyW(dll_path, path);
583 strcatW(dll_path, libmono_dll);
584 attributes = GetFileAttributesW(dll_path);
587 else if (abi_version == 2)
589 strcpyW(dll_path, path);
590 strcatW(dll_path, mono2_dll);
591 attributes = GetFileAttributesW(dll_path);
593 if (attributes == INVALID_FILE_ATTRIBUTES)
595 strcpyW(dll_path, path);
596 strcatW(dll_path, libmono2_dll);
597 attributes = GetFileAttributesW(dll_path);
601 return (attributes != INVALID_FILE_ATTRIBUTES);
604 static BOOL get_mono_path_from_registry(LPWSTR path, int abi_version)
606 static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
607 static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
608 static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
609 static const WCHAR slash[] = {'\\',0};
611 WCHAR version[64], version_key[MAX_PATH];
612 DWORD len;
613 HKEY key;
614 WCHAR dll_path[MAX_PATH];
616 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
617 return FALSE;
619 len = sizeof(version);
620 if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
622 RegCloseKey(key);
623 return FALSE;
625 RegCloseKey(key);
627 lstrcpyW(version_key, mono_key);
628 lstrcatW(version_key, slash);
629 lstrcatW(version_key, version);
631 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
632 return FALSE;
634 len = sizeof(WCHAR) * MAX_PATH;
635 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
637 RegCloseKey(key);
638 return FALSE;
640 RegCloseKey(key);
642 return find_mono_dll(path, dll_path, abi_version);
645 static BOOL get_mono_path_from_folder(LPCWSTR folder, LPWSTR mono_path, int abi_version)
647 static const WCHAR mono_one_dot_zero[] = {'\\','m','o','n','o','-','1','.','0', 0};
648 static const WCHAR mono_two_dot_zero[] = {'\\','m','o','n','o','-','2','.','0', 0};
649 WCHAR mono_dll_path[MAX_PATH];
650 BOOL found = FALSE;
652 strcpyW(mono_path, folder);
654 if (abi_version == 1)
655 strcatW(mono_path, mono_one_dot_zero);
656 else if (abi_version == 2)
657 strcatW(mono_path, mono_two_dot_zero);
659 found = find_mono_dll(mono_path, mono_dll_path, abi_version);
661 return found;
664 static BOOL get_mono_path(LPWSTR path, int abi_version)
666 static const WCHAR subdir_mono[] = {'\\','m','o','n','o',0};
667 static const WCHAR sibling_mono[] = {'\\','.','.','\\','m','o','n','o',0};
668 WCHAR base_path[MAX_PATH];
669 const char *unix_data_dir;
670 WCHAR *dos_data_dir;
671 int build_tree=0;
672 static WCHAR* (CDECL *wine_get_dos_file_name)(const char*);
674 /* First try c:\windows\mono */
675 GetWindowsDirectoryW(base_path, MAX_PATH);
676 strcatW(base_path, subdir_mono);
678 if (get_mono_path_from_folder(base_path, path, abi_version))
679 return TRUE;
681 /* Next: /usr/share/wine/mono */
682 unix_data_dir = wine_get_data_dir();
684 if (!unix_data_dir)
686 unix_data_dir = wine_get_build_dir();
687 build_tree = 1;
690 if (unix_data_dir)
692 if (!wine_get_dos_file_name)
693 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
695 if (wine_get_dos_file_name)
697 dos_data_dir = wine_get_dos_file_name(unix_data_dir);
699 if (dos_data_dir)
701 strcpyW(base_path, dos_data_dir);
702 strcatW(base_path, build_tree ? sibling_mono : subdir_mono);
704 HeapFree(GetProcessHeap(), 0, dos_data_dir);
706 if (get_mono_path_from_folder(base_path, path, abi_version))
707 return TRUE;
712 /* Last: the registry */
713 return get_mono_path_from_registry(path, abi_version);
716 static void find_runtimes(void)
718 int abi_version, i;
719 static const WCHAR libmono[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
720 static const WCHAR mscorlib[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
721 WCHAR mono_path[MAX_PATH], lib_path[MAX_PATH];
722 BOOL any_runtimes_found = FALSE;
724 if (runtimes_initialized) return;
726 EnterCriticalSection(&runtime_list_cs);
728 if (runtimes_initialized) goto end;
730 for (abi_version=NUM_ABI_VERSIONS; abi_version>0; abi_version--)
732 if (!get_mono_path(mono_path, abi_version))
733 continue;
735 for (i=0; i<NUM_RUNTIMES; i++)
737 if (runtimes[i].mono_abi_version == 0)
739 strcpyW(lib_path, mono_path);
740 strcatW(lib_path, libmono);
741 strcatW(lib_path, runtimes[i].mono_libdir);
742 strcatW(lib_path, mscorlib);
744 if (GetFileAttributesW(lib_path) != INVALID_FILE_ATTRIBUTES)
746 runtimes[i].mono_abi_version = abi_version;
748 strcpyW(runtimes[i].mono_path, mono_path);
749 strcpyW(runtimes[i].mscorlib_path, lib_path);
751 any_runtimes_found = TRUE;
757 if (!any_runtimes_found)
759 /* Report all runtimes are available if Mono isn't installed.
760 * FIXME: Remove this when Mono is properly packaged. */
761 for (i=0; i<NUM_RUNTIMES; i++)
762 runtimes[i].mono_abi_version = -1;
765 runtimes_initialized = 1;
767 end:
768 LeaveCriticalSection(&runtime_list_cs);
771 struct InstalledRuntimeEnum
773 IEnumUnknown IEnumUnknown_iface;
774 LONG ref;
775 ULONG pos;
778 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
780 static inline struct InstalledRuntimeEnum *impl_from_IEnumUnknown(IEnumUnknown *iface)
782 return CONTAINING_RECORD(iface, struct InstalledRuntimeEnum, IEnumUnknown_iface);
785 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface, REFIID riid,
786 void **ppvObject)
788 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
790 if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
791 IsEqualGUID( riid, &IID_IUnknown ) )
793 *ppvObject = iface;
795 else
797 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
798 return E_NOINTERFACE;
801 IEnumUnknown_AddRef( iface );
803 return S_OK;
806 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
808 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
809 ULONG ref = InterlockedIncrement(&This->ref);
811 TRACE("(%p) refcount=%u\n", iface, ref);
813 return ref;
816 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
818 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
819 ULONG ref = InterlockedDecrement(&This->ref);
821 TRACE("(%p) refcount=%u\n", iface, ref);
823 if (ref == 0)
825 HeapFree(GetProcessHeap(), 0, This);
828 return ref;
831 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
832 IUnknown **rgelt, ULONG *pceltFetched)
834 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
835 int num_fetched = 0;
836 HRESULT hr=S_OK;
837 IUnknown *item;
839 TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
841 while (num_fetched < celt)
843 if (This->pos >= NUM_RUNTIMES)
845 hr = S_FALSE;
846 break;
848 if (runtimes[This->pos].mono_abi_version)
850 item = (IUnknown*)&runtimes[This->pos].ICLRRuntimeInfo_iface;
851 IUnknown_AddRef(item);
852 rgelt[num_fetched] = item;
853 num_fetched++;
855 This->pos++;
858 if (pceltFetched)
859 *pceltFetched = num_fetched;
861 return hr;
864 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
866 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
867 int num_fetched = 0;
868 HRESULT hr=S_OK;
870 TRACE("(%p,%u)\n", iface, celt);
872 while (num_fetched < celt)
874 if (This->pos >= NUM_RUNTIMES)
876 hr = S_FALSE;
877 break;
879 if (runtimes[This->pos].mono_abi_version)
881 num_fetched++;
883 This->pos++;
886 return hr;
889 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
891 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
893 TRACE("(%p)\n", iface);
895 This->pos = 0;
897 return S_OK;
900 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
902 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
903 struct InstalledRuntimeEnum *new_enum;
905 TRACE("(%p)\n", iface);
907 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
908 if (!new_enum)
909 return E_OUTOFMEMORY;
911 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
912 new_enum->ref = 1;
913 new_enum->pos = This->pos;
915 *ppenum = &new_enum->IEnumUnknown_iface;
917 return S_OK;
920 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
921 InstalledRuntimeEnum_QueryInterface,
922 InstalledRuntimeEnum_AddRef,
923 InstalledRuntimeEnum_Release,
924 InstalledRuntimeEnum_Next,
925 InstalledRuntimeEnum_Skip,
926 InstalledRuntimeEnum_Reset,
927 InstalledRuntimeEnum_Clone
930 struct CLRMetaHost
932 ICLRMetaHost ICLRMetaHost_iface;
935 static struct CLRMetaHost GlobalCLRMetaHost;
937 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
938 REFIID riid,
939 void **ppvObject)
941 TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
943 if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
944 IsEqualGUID( riid, &IID_IUnknown ) )
946 *ppvObject = iface;
948 else
950 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
951 return E_NOINTERFACE;
954 ICLRMetaHost_AddRef( iface );
956 return S_OK;
959 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
961 return 2;
964 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
966 return 1;
969 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
971 *major = 0;
972 *minor = 0;
973 *build = 0;
975 if (version[0] == 'v')
977 version++;
978 if (!isdigit(*version))
979 return FALSE;
981 while (isdigit(*version))
982 *major = *major * 10 + (*version++ - '0');
984 if (*version == 0)
985 return TRUE;
987 if (*version++ != '.' || !isdigit(*version))
988 return FALSE;
990 while (isdigit(*version))
991 *minor = *minor * 10 + (*version++ - '0');
993 if (*version == 0)
994 return TRUE;
996 if (*version++ != '.' || !isdigit(*version))
997 return FALSE;
999 while (isdigit(*version))
1000 *build = *build * 10 + (*version++ - '0');
1002 return *version == 0;
1004 else
1005 return FALSE;
1008 HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
1009 LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
1011 int i;
1012 DWORD major, minor, build;
1014 TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
1016 if (!pwzVersion)
1017 return E_POINTER;
1019 if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
1021 ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
1022 return CLR_E_SHIM_RUNTIME;
1025 find_runtimes();
1027 for (i=0; i<NUM_RUNTIMES; i++)
1029 if (runtimes[i].major == major && runtimes[i].minor == minor &&
1030 runtimes[i].build == build)
1032 if (runtimes[i].mono_abi_version)
1033 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface, iid,
1034 ppRuntime);
1035 else
1037 missing_runtime_message(&runtimes[i]);
1038 return CLR_E_SHIM_RUNTIME;
1043 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
1044 return CLR_E_SHIM_RUNTIME;
1047 HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
1048 LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
1050 ASSEMBLY *assembly;
1051 HRESULT hr;
1052 LPSTR version;
1053 ULONG buffer_size=*pcchBuffer;
1055 TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
1057 hr = assembly_create(&assembly, pwzFilePath);
1059 if (SUCCEEDED(hr))
1061 hr = assembly_get_runtime_version(assembly, &version);
1063 if (SUCCEEDED(hr))
1065 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
1067 if (pwzBuffer)
1069 if (buffer_size >= *pcchBuffer)
1070 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
1071 else
1072 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
1076 assembly_release(assembly);
1079 return hr;
1082 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
1083 IEnumUnknown **ppEnumerator)
1085 struct InstalledRuntimeEnum *new_enum;
1087 TRACE("%p\n", ppEnumerator);
1089 find_runtimes();
1091 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
1092 if (!new_enum)
1093 return E_OUTOFMEMORY;
1095 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
1096 new_enum->ref = 1;
1097 new_enum->pos = 0;
1099 *ppEnumerator = &new_enum->IEnumUnknown_iface;
1101 return S_OK;
1104 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
1105 HANDLE hndProcess, IEnumUnknown **ppEnumerator)
1107 FIXME("%p %p\n", hndProcess, ppEnumerator);
1109 return E_NOTIMPL;
1112 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
1113 RuntimeLoadedCallbackFnPtr pCallbackFunction)
1115 FIXME("%p\n", pCallbackFunction);
1117 return E_NOTIMPL;
1120 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
1121 REFIID riid, LPVOID *ppUnk)
1123 FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
1125 return E_NOTIMPL;
1128 static HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
1130 FIXME("%i: stub\n", iExitCode);
1132 ExitProcess(iExitCode);
1135 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
1137 CLRMetaHost_QueryInterface,
1138 CLRMetaHost_AddRef,
1139 CLRMetaHost_Release,
1140 CLRMetaHost_GetRuntime,
1141 CLRMetaHost_GetVersionFromFile,
1142 CLRMetaHost_EnumerateInstalledRuntimes,
1143 CLRMetaHost_EnumerateLoadedRuntimes,
1144 CLRMetaHost_RequestRuntimeLoadedNotification,
1145 CLRMetaHost_QueryLegacyV2RuntimeBinding,
1146 CLRMetaHost_ExitProcess
1149 static struct CLRMetaHost GlobalCLRMetaHost = {
1150 { &CLRMetaHost_vtbl }
1153 HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1155 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost.ICLRMetaHost_iface, riid, ppobj);
1158 static MonoAssembly* mono_assembly_search_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data)
1160 loaded_mono *mono = user_data;
1161 HRESULT hr=S_OK;
1162 MonoAssembly *result=NULL;
1163 char *stringname=NULL;
1164 LPWSTR stringnameW;
1165 int stringnameW_size;
1166 IAssemblyCache *asmcache;
1167 ASSEMBLY_INFO info;
1168 WCHAR path[MAX_PATH];
1169 char *pathA;
1170 MonoImageOpenStatus stat;
1171 static WCHAR fusiondll[] = {'f','u','s','i','o','n',0};
1172 HMODULE hfusion=NULL;
1173 static HRESULT WINAPI (*pCreateAssemblyCache)(IAssemblyCache**,DWORD);
1175 stringname = mono->mono_stringify_assembly_name(aname);
1177 TRACE("%s\n", debugstr_a(stringname));
1179 if (!stringname) return NULL;
1181 /* FIXME: We should search the given paths before the GAC. */
1183 if (!pCreateAssemblyCache)
1185 hr = LoadLibraryShim(fusiondll, NULL, NULL, &hfusion);
1187 if (SUCCEEDED(hr))
1189 pCreateAssemblyCache = (void*)GetProcAddress(hfusion, "CreateAssemblyCache");
1190 if (!pCreateAssemblyCache)
1191 hr = E_FAIL;
1195 if (SUCCEEDED(hr))
1196 hr = pCreateAssemblyCache(&asmcache, 0);
1198 if (SUCCEEDED(hr))
1200 stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, stringname, -1, NULL, 0);
1202 stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1203 if (stringnameW)
1204 MultiByteToWideChar(CP_UTF8, 0, stringname, -1, stringnameW, stringnameW_size);
1205 else
1206 hr = E_OUTOFMEMORY;
1208 if (SUCCEEDED(hr))
1210 info.cbAssemblyInfo = sizeof(info);
1211 info.pszCurrentAssemblyPathBuf = path;
1212 info.cchBuf = MAX_PATH;
1213 path[0] = 0;
1215 hr = IAssemblyCache_QueryAssemblyInfo(asmcache, 0, stringnameW, &info);
1218 HeapFree(GetProcessHeap(), 0, stringnameW);
1220 IAssemblyCache_Release(asmcache);
1223 if (SUCCEEDED(hr))
1225 TRACE("found: %s\n", debugstr_w(path));
1227 pathA = WtoA(path);
1229 if (pathA)
1231 result = mono->mono_assembly_open(pathA, &stat);
1233 if (!result)
1234 ERR("Failed to load %s, status=%u\n", debugstr_w(path), stat);
1236 HeapFree(GetProcessHeap(), 0, pathA);
1240 mono->mono_free(stringname);
1242 return result;
1245 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1246 DWORD startup_flags, DWORD runtimeinfo_flags, BOOL legacy, ICLRRuntimeInfo **result)
1248 static const WCHAR dotconfig[] = {'.','c','o','n','f','i','g',0};
1249 static const DWORD supported_startup_flags = 0;
1250 static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1251 int i;
1252 WCHAR local_version[MAX_PATH];
1253 ULONG local_version_size = MAX_PATH;
1254 WCHAR local_config_file[MAX_PATH];
1255 HRESULT hr;
1256 parsed_config_file parsed_config;
1258 if (startup_flags & ~supported_startup_flags)
1259 FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
1261 if (runtimeinfo_flags & ~supported_runtime_flags)
1262 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
1264 if (exefile && !config_file)
1266 strcpyW(local_config_file, exefile);
1267 strcatW(local_config_file, dotconfig);
1269 config_file = local_config_file;
1272 if (config_file)
1274 int found=0;
1275 hr = parse_config_file(config_file, &parsed_config);
1277 if (SUCCEEDED(hr))
1279 supported_runtime *entry;
1280 LIST_FOR_EACH_ENTRY(entry, &parsed_config.supported_runtimes, supported_runtime, entry)
1282 hr = CLRMetaHost_GetRuntime(0, entry->version, &IID_ICLRRuntimeInfo, (void**)result);
1283 if (SUCCEEDED(hr))
1285 found = 1;
1286 break;
1290 else
1292 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file), hr);
1295 free_parsed_config_file(&parsed_config);
1297 if (found)
1298 return S_OK;
1301 if (exefile && !version)
1303 hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1305 version = local_version;
1307 if (FAILED(hr)) return hr;
1310 if (version)
1312 return CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1315 if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1317 find_runtimes();
1319 if (legacy)
1320 i = 2;
1321 else
1322 i = NUM_RUNTIMES;
1324 while (i--)
1326 if (runtimes[i].mono_abi_version)
1327 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface,
1328 &IID_ICLRRuntimeInfo, (void **)result);
1331 if (legacy)
1332 missing_runtime_message(&runtimes[1]);
1333 else
1334 missing_runtime_message(&runtimes[NUM_RUNTIMES-1]);
1336 return CLR_E_SHIM_RUNTIME;
1339 return CLR_E_SHIM_RUNTIME;