ntdll: Use list_empty() instead of list_count() == 0.
[wine/multimedia.git] / dlls / mscoree / metahost.c
blob8fb846ccaefa01a60bf56440f5ff485f305c3da4
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 HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
315 REFIID riid,
316 void **ppvObject)
318 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
320 if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
321 IsEqualGUID( riid, &IID_IUnknown ) )
323 *ppvObject = iface;
325 else
327 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
328 return E_NOINTERFACE;
331 ICLRRuntimeInfo_AddRef( iface );
333 return S_OK;
336 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
338 return 2;
341 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
343 return 1;
346 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
347 LPWSTR pwzBuffer, DWORD *pcchBuffer)
349 struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
350 DWORD buffer_size = *pcchBuffer;
351 HRESULT hr = S_OK;
352 char version[11];
353 DWORD size;
355 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
357 size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
359 assert(size <= sizeof(version));
361 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
363 if (pwzBuffer)
365 if (buffer_size >= *pcchBuffer)
366 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
367 else
368 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
371 return hr;
374 static BOOL get_install_root(LPWSTR install_dir)
376 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};
377 const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
379 DWORD len;
380 HKEY key;
382 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
383 return FALSE;
385 len = MAX_PATH;
386 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
388 RegCloseKey(key);
389 return FALSE;
391 RegCloseKey(key);
393 return TRUE;
396 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
397 LPWSTR pwzBuffer, DWORD *pcchBuffer)
399 static const WCHAR slash[] = {'\\',0};
400 DWORD buffer_size = *pcchBuffer;
401 WCHAR system_dir[MAX_PATH];
402 WCHAR version[MAX_PATH];
403 DWORD version_size, size;
404 HRESULT hr = S_OK;
406 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
408 if (!get_install_root(system_dir))
410 ERR("error reading registry key for installroot\n");
411 return E_FAIL;
413 else
415 version_size = MAX_PATH;
416 ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
417 lstrcatW(system_dir, version);
418 lstrcatW(system_dir, slash);
419 size = lstrlenW(system_dir) + 1;
422 *pcchBuffer = size;
424 if (pwzBuffer)
426 if (buffer_size >= size)
427 strcpyW(pwzBuffer, system_dir);
428 else
429 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
432 return hr;
435 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
436 HANDLE hndProcess, BOOL *pbLoaded)
438 FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
440 return E_NOTIMPL;
443 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
444 UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
446 FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
448 return E_NOTIMPL;
451 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
452 LPCWSTR pwzDllName, HMODULE *phndModule)
454 WCHAR version[MAX_PATH];
455 HRESULT hr;
456 DWORD cchBuffer;
458 TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
460 cchBuffer = MAX_PATH;
461 hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
462 if (FAILED(hr)) return hr;
464 return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
467 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
468 LPCSTR pszProcName, LPVOID *ppProc)
470 FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
472 return E_NOTIMPL;
475 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
476 REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
478 struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
479 RuntimeHost *host;
480 HRESULT hr;
482 TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
484 hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
486 if (SUCCEEDED(hr))
487 hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
489 return hr;
492 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
493 BOOL *pbLoadable)
495 FIXME("%p %p\n", iface, pbLoadable);
497 return E_NOTIMPL;
500 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
501 DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
503 FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
505 return E_NOTIMPL;
508 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
509 DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
511 FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
513 return E_NOTIMPL;
516 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
518 FIXME("%p\n", iface);
520 return E_NOTIMPL;
523 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
524 BOOL *pbStarted, DWORD *pdwStartupFlags)
526 FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
528 return E_NOTIMPL;
531 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
532 CLRRuntimeInfo_QueryInterface,
533 CLRRuntimeInfo_AddRef,
534 CLRRuntimeInfo_Release,
535 CLRRuntimeInfo_GetVersionString,
536 CLRRuntimeInfo_GetRuntimeDirectory,
537 CLRRuntimeInfo_IsLoaded,
538 CLRRuntimeInfo_LoadErrorString,
539 CLRRuntimeInfo_LoadLibrary,
540 CLRRuntimeInfo_GetProcAddress,
541 CLRRuntimeInfo_GetInterface,
542 CLRRuntimeInfo_IsLoadable,
543 CLRRuntimeInfo_SetDefaultStartupFlags,
544 CLRRuntimeInfo_GetDefaultStartupFlags,
545 CLRRuntimeInfo_BindAsLegacyV2Runtime,
546 CLRRuntimeInfo_IsStarted
549 HRESULT ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo *iface, RuntimeHost **result)
551 struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
553 assert(This->ICLRRuntimeInfo_vtbl == &CLRRuntimeInfoVtbl);
555 return CLRRuntimeInfo_GetRuntimeHost(This, result);
558 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version)
560 static const WCHAR mono_dll[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
561 static const WCHAR libmono_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
562 static const WCHAR mono2_dll[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
563 static const WCHAR libmono2_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
564 DWORD attributes=INVALID_FILE_ATTRIBUTES;
566 if (abi_version == 1)
568 strcpyW(dll_path, path);
569 strcatW(dll_path, mono_dll);
570 attributes = GetFileAttributesW(dll_path);
572 if (attributes == INVALID_FILE_ATTRIBUTES)
574 strcpyW(dll_path, path);
575 strcatW(dll_path, libmono_dll);
576 attributes = GetFileAttributesW(dll_path);
579 else if (abi_version == 2)
581 strcpyW(dll_path, path);
582 strcatW(dll_path, mono2_dll);
583 attributes = GetFileAttributesW(dll_path);
585 if (attributes == INVALID_FILE_ATTRIBUTES)
587 strcpyW(dll_path, path);
588 strcatW(dll_path, libmono2_dll);
589 attributes = GetFileAttributesW(dll_path);
593 return (attributes != INVALID_FILE_ATTRIBUTES);
596 static BOOL get_mono_path_from_registry(LPWSTR path, int abi_version)
598 static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
599 static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
600 static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
601 static const WCHAR slash[] = {'\\',0};
603 WCHAR version[64], version_key[MAX_PATH];
604 DWORD len;
605 HKEY key;
606 WCHAR dll_path[MAX_PATH];
608 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
609 return FALSE;
611 len = sizeof(version);
612 if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
614 RegCloseKey(key);
615 return FALSE;
617 RegCloseKey(key);
619 lstrcpyW(version_key, mono_key);
620 lstrcatW(version_key, slash);
621 lstrcatW(version_key, version);
623 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
624 return FALSE;
626 len = sizeof(WCHAR) * MAX_PATH;
627 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
629 RegCloseKey(key);
630 return FALSE;
632 RegCloseKey(key);
634 return find_mono_dll(path, dll_path, abi_version);
637 static BOOL get_mono_path_from_folder(LPCWSTR folder, LPWSTR mono_path, int abi_version)
639 static const WCHAR mono_one_dot_zero[] = {'\\','m','o','n','o','-','1','.','0', 0};
640 static const WCHAR mono_two_dot_zero[] = {'\\','m','o','n','o','-','2','.','0', 0};
641 WCHAR mono_dll_path[MAX_PATH];
642 BOOL found = FALSE;
644 strcpyW(mono_path, folder);
646 if (abi_version == 1)
647 strcatW(mono_path, mono_one_dot_zero);
648 else if (abi_version == 2)
649 strcatW(mono_path, mono_two_dot_zero);
651 found = find_mono_dll(mono_path, mono_dll_path, abi_version);
653 return found;
656 static BOOL get_mono_path(LPWSTR path, int abi_version)
658 static const WCHAR subdir_mono[] = {'\\','m','o','n','o',0};
659 static const WCHAR sibling_mono[] = {'\\','.','.','\\','m','o','n','o',0};
660 WCHAR base_path[MAX_PATH];
661 const char *unix_data_dir;
662 WCHAR *dos_data_dir;
663 int build_tree=0;
664 static WCHAR* (CDECL *wine_get_dos_file_name)(const char*);
666 /* First try c:\windows\mono */
667 GetWindowsDirectoryW(base_path, MAX_PATH);
668 strcatW(base_path, subdir_mono);
670 if (get_mono_path_from_folder(base_path, path, abi_version))
671 return TRUE;
673 /* Next: /usr/share/wine/mono */
674 unix_data_dir = wine_get_data_dir();
676 if (!unix_data_dir)
678 unix_data_dir = wine_get_build_dir();
679 build_tree = 1;
682 if (unix_data_dir)
684 if (!wine_get_dos_file_name)
685 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
687 if (wine_get_dos_file_name)
689 dos_data_dir = wine_get_dos_file_name(unix_data_dir);
691 if (dos_data_dir)
693 strcpyW(base_path, dos_data_dir);
694 strcatW(base_path, build_tree ? sibling_mono : subdir_mono);
696 HeapFree(GetProcessHeap(), 0, dos_data_dir);
698 if (get_mono_path_from_folder(base_path, path, abi_version))
699 return TRUE;
704 /* Last: the registry */
705 return get_mono_path_from_registry(path, abi_version);
708 static void find_runtimes(void)
710 int abi_version, i;
711 static const WCHAR libmono[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
712 static const WCHAR mscorlib[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
713 WCHAR mono_path[MAX_PATH], lib_path[MAX_PATH];
714 BOOL any_runtimes_found = FALSE;
716 if (runtimes_initialized) return;
718 EnterCriticalSection(&runtime_list_cs);
720 if (runtimes_initialized) goto end;
722 for (abi_version=NUM_ABI_VERSIONS; abi_version>0; abi_version--)
724 if (!get_mono_path(mono_path, abi_version))
725 continue;
727 for (i=0; i<NUM_RUNTIMES; i++)
729 if (runtimes[i].mono_abi_version == 0)
731 strcpyW(lib_path, mono_path);
732 strcatW(lib_path, libmono);
733 strcatW(lib_path, runtimes[i].mono_libdir);
734 strcatW(lib_path, mscorlib);
736 if (GetFileAttributesW(lib_path) != INVALID_FILE_ATTRIBUTES)
738 runtimes[i].mono_abi_version = abi_version;
740 strcpyW(runtimes[i].mono_path, mono_path);
741 strcpyW(runtimes[i].mscorlib_path, lib_path);
743 any_runtimes_found = TRUE;
749 if (!any_runtimes_found)
751 /* Report all runtimes are available if Mono isn't installed.
752 * FIXME: Remove this when Mono is properly packaged. */
753 for (i=0; i<NUM_RUNTIMES; i++)
754 runtimes[i].mono_abi_version = -1;
757 runtimes_initialized = 1;
759 end:
760 LeaveCriticalSection(&runtime_list_cs);
763 struct InstalledRuntimeEnum
765 IEnumUnknown IEnumUnknown_iface;
766 LONG ref;
767 ULONG pos;
770 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
772 static inline struct InstalledRuntimeEnum *impl_from_IEnumUnknown(IEnumUnknown *iface)
774 return CONTAINING_RECORD(iface, struct InstalledRuntimeEnum, IEnumUnknown_iface);
777 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface, REFIID riid,
778 void **ppvObject)
780 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
782 if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
783 IsEqualGUID( riid, &IID_IUnknown ) )
785 *ppvObject = iface;
787 else
789 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
790 return E_NOINTERFACE;
793 IEnumUnknown_AddRef( iface );
795 return S_OK;
798 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
800 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
801 ULONG ref = InterlockedIncrement(&This->ref);
803 TRACE("(%p) refcount=%u\n", iface, ref);
805 return ref;
808 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
810 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
811 ULONG ref = InterlockedDecrement(&This->ref);
813 TRACE("(%p) refcount=%u\n", iface, ref);
815 if (ref == 0)
817 HeapFree(GetProcessHeap(), 0, This);
820 return ref;
823 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
824 IUnknown **rgelt, ULONG *pceltFetched)
826 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
827 int num_fetched = 0;
828 HRESULT hr=S_OK;
829 IUnknown *item;
831 TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
833 while (num_fetched < celt)
835 if (This->pos >= NUM_RUNTIMES)
837 hr = S_FALSE;
838 break;
840 if (runtimes[This->pos].mono_abi_version)
842 item = (IUnknown*)&runtimes[This->pos];
843 IUnknown_AddRef(item);
844 rgelt[num_fetched] = item;
845 num_fetched++;
847 This->pos++;
850 if (pceltFetched)
851 *pceltFetched = num_fetched;
853 return hr;
856 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
858 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
859 int num_fetched = 0;
860 HRESULT hr=S_OK;
862 TRACE("(%p,%u)\n", iface, celt);
864 while (num_fetched < celt)
866 if (This->pos >= NUM_RUNTIMES)
868 hr = S_FALSE;
869 break;
871 if (runtimes[This->pos].mono_abi_version)
873 num_fetched++;
875 This->pos++;
878 return hr;
881 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
883 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
885 TRACE("(%p)\n", iface);
887 This->pos = 0;
889 return S_OK;
892 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
894 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
895 struct InstalledRuntimeEnum *new_enum;
897 TRACE("(%p)\n", iface);
899 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
900 if (!new_enum)
901 return E_OUTOFMEMORY;
903 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
904 new_enum->ref = 1;
905 new_enum->pos = This->pos;
907 *ppenum = &new_enum->IEnumUnknown_iface;
909 return S_OK;
912 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
913 InstalledRuntimeEnum_QueryInterface,
914 InstalledRuntimeEnum_AddRef,
915 InstalledRuntimeEnum_Release,
916 InstalledRuntimeEnum_Next,
917 InstalledRuntimeEnum_Skip,
918 InstalledRuntimeEnum_Reset,
919 InstalledRuntimeEnum_Clone
922 struct CLRMetaHost
924 ICLRMetaHost ICLRMetaHost_iface;
927 static struct CLRMetaHost GlobalCLRMetaHost;
929 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
930 REFIID riid,
931 void **ppvObject)
933 TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
935 if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
936 IsEqualGUID( riid, &IID_IUnknown ) )
938 *ppvObject = iface;
940 else
942 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
943 return E_NOINTERFACE;
946 ICLRMetaHost_AddRef( iface );
948 return S_OK;
951 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
953 return 2;
956 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
958 return 1;
961 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
963 *major = 0;
964 *minor = 0;
965 *build = 0;
967 if (version[0] == 'v')
969 version++;
970 if (!isdigit(*version))
971 return FALSE;
973 while (isdigit(*version))
974 *major = *major * 10 + (*version++ - '0');
976 if (*version == 0)
977 return TRUE;
979 if (*version++ != '.' || !isdigit(*version))
980 return FALSE;
982 while (isdigit(*version))
983 *minor = *minor * 10 + (*version++ - '0');
985 if (*version == 0)
986 return TRUE;
988 if (*version++ != '.' || !isdigit(*version))
989 return FALSE;
991 while (isdigit(*version))
992 *build = *build * 10 + (*version++ - '0');
994 return *version == 0;
996 else
997 return FALSE;
1000 static HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
1001 LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
1003 int i;
1004 DWORD major, minor, build;
1006 TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
1008 if (!pwzVersion)
1009 return E_POINTER;
1011 if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
1013 ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
1014 return CLR_E_SHIM_RUNTIME;
1017 find_runtimes();
1019 for (i=0; i<NUM_RUNTIMES; i++)
1021 if (runtimes[i].major == major && runtimes[i].minor == minor &&
1022 runtimes[i].build == build)
1024 if (runtimes[i].mono_abi_version)
1025 return IUnknown_QueryInterface((IUnknown*)&runtimes[i], iid, ppRuntime);
1026 else
1028 missing_runtime_message(&runtimes[i]);
1029 return CLR_E_SHIM_RUNTIME;
1034 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
1035 return CLR_E_SHIM_RUNTIME;
1038 HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
1039 LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
1041 ASSEMBLY *assembly;
1042 HRESULT hr;
1043 LPSTR version;
1044 ULONG buffer_size=*pcchBuffer;
1046 TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
1048 hr = assembly_create(&assembly, pwzFilePath);
1050 if (SUCCEEDED(hr))
1052 hr = assembly_get_runtime_version(assembly, &version);
1054 if (SUCCEEDED(hr))
1056 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
1058 if (pwzBuffer)
1060 if (buffer_size >= *pcchBuffer)
1061 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
1062 else
1063 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
1067 assembly_release(assembly);
1070 return hr;
1073 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
1074 IEnumUnknown **ppEnumerator)
1076 struct InstalledRuntimeEnum *new_enum;
1078 TRACE("%p\n", ppEnumerator);
1080 find_runtimes();
1082 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
1083 if (!new_enum)
1084 return E_OUTOFMEMORY;
1086 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
1087 new_enum->ref = 1;
1088 new_enum->pos = 0;
1090 *ppEnumerator = &new_enum->IEnumUnknown_iface;
1092 return S_OK;
1095 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
1096 HANDLE hndProcess, IEnumUnknown **ppEnumerator)
1098 FIXME("%p %p\n", hndProcess, ppEnumerator);
1100 return E_NOTIMPL;
1103 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
1104 RuntimeLoadedCallbackFnPtr pCallbackFunction)
1106 FIXME("%p\n", pCallbackFunction);
1108 return E_NOTIMPL;
1111 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
1112 REFIID riid, LPVOID *ppUnk)
1114 FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
1116 return E_NOTIMPL;
1119 static HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
1121 FIXME("%i: stub\n", iExitCode);
1123 ExitProcess(iExitCode);
1126 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
1128 CLRMetaHost_QueryInterface,
1129 CLRMetaHost_AddRef,
1130 CLRMetaHost_Release,
1131 CLRMetaHost_GetRuntime,
1132 CLRMetaHost_GetVersionFromFile,
1133 CLRMetaHost_EnumerateInstalledRuntimes,
1134 CLRMetaHost_EnumerateLoadedRuntimes,
1135 CLRMetaHost_RequestRuntimeLoadedNotification,
1136 CLRMetaHost_QueryLegacyV2RuntimeBinding,
1137 CLRMetaHost_ExitProcess
1140 static struct CLRMetaHost GlobalCLRMetaHost = {
1141 { &CLRMetaHost_vtbl }
1144 extern HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1146 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost.ICLRMetaHost_iface, riid, ppobj);
1149 static MonoAssembly* mono_assembly_search_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data)
1151 loaded_mono *mono = user_data;
1152 HRESULT hr=S_OK;
1153 MonoAssembly *result=NULL;
1154 char *stringname=NULL;
1155 LPWSTR stringnameW;
1156 int stringnameW_size;
1157 IAssemblyCache *asmcache;
1158 ASSEMBLY_INFO info;
1159 WCHAR path[MAX_PATH];
1160 char *pathA;
1161 MonoImageOpenStatus stat;
1162 static WCHAR fusiondll[] = {'f','u','s','i','o','n',0};
1163 HMODULE hfusion=NULL;
1164 static HRESULT WINAPI (*pCreateAssemblyCache)(IAssemblyCache**,DWORD);
1166 stringname = mono->mono_stringify_assembly_name(aname);
1168 TRACE("%s\n", debugstr_a(stringname));
1170 if (!stringname) return NULL;
1172 /* FIXME: We should search the given paths before the GAC. */
1174 if (!pCreateAssemblyCache)
1176 hr = LoadLibraryShim(fusiondll, NULL, NULL, &hfusion);
1178 if (SUCCEEDED(hr))
1180 pCreateAssemblyCache = (void*)GetProcAddress(hfusion, "CreateAssemblyCache");
1181 if (!pCreateAssemblyCache)
1182 hr = E_FAIL;
1186 if (SUCCEEDED(hr))
1187 hr = pCreateAssemblyCache(&asmcache, 0);
1189 if (SUCCEEDED(hr))
1191 stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, stringname, -1, NULL, 0);
1193 stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1194 if (stringnameW)
1195 MultiByteToWideChar(CP_UTF8, 0, stringname, -1, stringnameW, stringnameW_size);
1196 else
1197 hr = E_OUTOFMEMORY;
1199 if (SUCCEEDED(hr))
1201 info.cbAssemblyInfo = sizeof(info);
1202 info.pszCurrentAssemblyPathBuf = path;
1203 info.cchBuf = MAX_PATH;
1204 path[0] = 0;
1206 hr = IAssemblyCache_QueryAssemblyInfo(asmcache, 0, stringnameW, &info);
1209 HeapFree(GetProcessHeap(), 0, stringnameW);
1211 IAssemblyCache_Release(asmcache);
1214 if (SUCCEEDED(hr))
1216 TRACE("found: %s\n", debugstr_w(path));
1218 pathA = WtoA(path);
1220 if (pathA)
1222 result = mono->mono_assembly_open(pathA, &stat);
1224 if (!result)
1225 ERR("Failed to load %s, status=%u\n", debugstr_w(path), stat);
1227 HeapFree(GetProcessHeap(), 0, pathA);
1231 mono->mono_free(stringname);
1233 return result;
1236 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1237 DWORD startup_flags, DWORD runtimeinfo_flags, BOOL legacy, ICLRRuntimeInfo **result)
1239 static const WCHAR dotconfig[] = {'.','c','o','n','f','i','g',0};
1240 static const DWORD supported_startup_flags = 0;
1241 static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1242 int i;
1243 WCHAR local_version[MAX_PATH];
1244 ULONG local_version_size = MAX_PATH;
1245 WCHAR local_config_file[MAX_PATH];
1246 HRESULT hr;
1247 parsed_config_file parsed_config;
1249 if (startup_flags & ~supported_startup_flags)
1250 FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
1252 if (runtimeinfo_flags & ~supported_runtime_flags)
1253 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
1255 if (exefile && !config_file)
1257 strcpyW(local_config_file, exefile);
1258 strcatW(local_config_file, dotconfig);
1260 config_file = local_config_file;
1263 if (config_file)
1265 int found=0;
1266 hr = parse_config_file(config_file, &parsed_config);
1268 if (SUCCEEDED(hr))
1270 supported_runtime *entry;
1271 LIST_FOR_EACH_ENTRY(entry, &parsed_config.supported_runtimes, supported_runtime, entry)
1273 hr = CLRMetaHost_GetRuntime(0, entry->version, &IID_ICLRRuntimeInfo, (void**)result);
1274 if (SUCCEEDED(hr))
1276 found = 1;
1277 break;
1281 else
1283 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file), hr);
1286 free_parsed_config_file(&parsed_config);
1288 if (found)
1289 return S_OK;
1292 if (exefile && !version)
1294 hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1296 version = local_version;
1298 if (FAILED(hr)) return hr;
1301 if (version)
1303 return CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1306 if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1308 find_runtimes();
1310 if (legacy)
1311 i = 2;
1312 else
1313 i = NUM_RUNTIMES;
1315 while (i--)
1317 if (runtimes[i].mono_abi_version)
1318 return IUnknown_QueryInterface((IUnknown*)&runtimes[i],
1319 &IID_ICLRRuntimeInfo, (void**)result);
1322 if (legacy)
1323 missing_runtime_message(&runtimes[1]);
1324 else
1325 missing_runtime_message(&runtimes[NUM_RUNTIMES-1]);
1327 return CLR_E_SHIM_RUNTIME;
1330 return CLR_E_SHIM_RUNTIME;