configure: Improve check for security framework and don't warn about gnutls in that...
[wine/testsucceed.git] / dlls / mscoree / metahost.c
blobfb03fb13da5e2aa2f113fca71886e8e02d74d265
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 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 HRESULT load_mono(CLRRuntimeInfo *This, loaded_mono **result)
102 static const WCHAR bin[] = {'\\','b','i','n',0};
103 static const WCHAR lib[] = {'\\','l','i','b',0};
104 static const WCHAR etc[] = {'\\','e','t','c',0};
105 static const WCHAR glibdll[] = {'l','i','b','g','l','i','b','-','2','.','0','-','0','.','d','l','l',0};
106 WCHAR mono_dll_path[MAX_PATH+16], mono_bin_path[MAX_PATH+4];
107 WCHAR mono_lib_path[MAX_PATH+4], mono_etc_path[MAX_PATH+4];
108 char mono_lib_path_a[MAX_PATH], mono_etc_path_a[MAX_PATH];
109 int trace_size;
110 char trace_setting[256];
112 if (This->mono_abi_version == -1)
113 MESSAGE("wine: Install the Windows version of Mono to run .NET executables\n");
115 if (This->mono_abi_version <= 0 || This->mono_abi_version > NUM_ABI_VERSIONS)
116 return E_FAIL;
118 *result = &loaded_monos[This->mono_abi_version-1];
120 if ((*result)->is_shutdown)
122 ERR("Cannot load Mono after it has been shut down.\n");
123 *result = NULL;
124 return E_FAIL;
127 if (!(*result)->mono_handle)
129 strcpyW(mono_bin_path, This->mono_path);
130 strcatW(mono_bin_path, bin);
131 set_environment(mono_bin_path);
133 strcpyW(mono_lib_path, This->mono_path);
134 strcatW(mono_lib_path, lib);
135 WideCharToMultiByte(CP_UTF8, 0, mono_lib_path, -1, mono_lib_path_a, MAX_PATH, NULL, NULL);
137 strcpyW(mono_etc_path, This->mono_path);
138 strcatW(mono_etc_path, etc);
139 WideCharToMultiByte(CP_UTF8, 0, mono_etc_path, -1, mono_etc_path_a, MAX_PATH, NULL, NULL);
141 if (!find_mono_dll(This->mono_path, mono_dll_path, This->mono_abi_version)) goto fail;
143 (*result)->mono_handle = LoadLibraryW(mono_dll_path);
145 if (!(*result)->mono_handle) goto fail;
147 #define LOAD_MONO_FUNCTION(x) do { \
148 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
149 if (!(*result)->x) { \
150 goto fail; \
152 } while (0);
154 LOAD_MONO_FUNCTION(mono_assembly_get_image);
155 LOAD_MONO_FUNCTION(mono_assembly_open);
156 LOAD_MONO_FUNCTION(mono_config_parse);
157 LOAD_MONO_FUNCTION(mono_class_from_mono_type);
158 LOAD_MONO_FUNCTION(mono_class_from_name);
159 LOAD_MONO_FUNCTION(mono_class_get_method_from_name);
160 LOAD_MONO_FUNCTION(mono_domain_assembly_open);
161 LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook);
162 LOAD_MONO_FUNCTION(mono_jit_exec);
163 LOAD_MONO_FUNCTION(mono_jit_init);
164 LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
165 LOAD_MONO_FUNCTION(mono_object_get_domain);
166 LOAD_MONO_FUNCTION(mono_object_new);
167 LOAD_MONO_FUNCTION(mono_object_unbox);
168 LOAD_MONO_FUNCTION(mono_profiler_install);
169 LOAD_MONO_FUNCTION(mono_reflection_type_from_name);
170 LOAD_MONO_FUNCTION(mono_runtime_invoke);
171 LOAD_MONO_FUNCTION(mono_runtime_object_init);
172 LOAD_MONO_FUNCTION(mono_runtime_quit);
173 LOAD_MONO_FUNCTION(mono_set_dirs);
174 LOAD_MONO_FUNCTION(mono_stringify_assembly_name);
176 /* GLib imports obsoleted by the 2.0 ABI */
177 if (This->mono_abi_version == 1)
179 (*result)->glib_handle = LoadLibraryW(glibdll);
180 if (!(*result)->glib_handle) goto fail;
182 (*result)->mono_free = (void*)GetProcAddress((*result)->glib_handle, "g_free");
183 if (!(*result)->mono_free) goto fail;
185 else
187 LOAD_MONO_FUNCTION(mono_free);
190 #undef LOAD_MONO_FUNCTION
192 #define LOAD_OPT_VOID_MONO_FUNCTION(x) do { \
193 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
194 if (!(*result)->x) { \
195 (*result)->x = do_nothing; \
197 } while (0);
199 LOAD_OPT_VOID_MONO_FUNCTION(mono_runtime_set_shutting_down);
200 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_pool_cleanup);
201 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_suspend_all_other_threads);
202 LOAD_OPT_VOID_MONO_FUNCTION(mono_threads_set_shutting_down);
204 #undef LOAD_OPT_VOID_MONO_FUNCTION
206 (*result)->mono_profiler_install((MonoProfiler*)*result, mono_shutdown_callback_fn);
208 (*result)->mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
210 (*result)->mono_config_parse(NULL);
212 (*result)->mono_install_assembly_preload_hook(mono_assembly_search_hook_fn, *result);
214 trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
216 if (trace_size)
218 (*result)->mono_jit_set_trace_options(trace_setting);
222 return S_OK;
224 fail:
225 ERR("Could not load Mono into this process\n");
226 FreeLibrary((*result)->mono_handle);
227 FreeLibrary((*result)->glib_handle);
228 (*result)->mono_handle = NULL;
229 (*result)->glib_handle = NULL;
230 return E_FAIL;
233 static void mono_shutdown_callback_fn(MonoProfiler *prof)
235 loaded_mono *mono = (loaded_mono*)prof;
237 mono->is_shutdown = TRUE;
240 static HRESULT CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo *This, RuntimeHost **result)
242 HRESULT hr = S_OK;
243 loaded_mono *ploaded_mono;
245 if (This->loaded_runtime)
247 *result = This->loaded_runtime;
248 return hr;
251 EnterCriticalSection(&runtime_list_cs);
253 hr = load_mono(This, &ploaded_mono);
255 if (SUCCEEDED(hr))
256 hr = RuntimeHost_Construct(This, ploaded_mono, &This->loaded_runtime);
258 LeaveCriticalSection(&runtime_list_cs);
260 if (SUCCEEDED(hr))
261 *result = This->loaded_runtime;
263 return hr;
266 void unload_all_runtimes(void)
268 int i;
270 for (i=0; i<NUM_ABI_VERSIONS; i++)
272 loaded_mono *mono = &loaded_monos[i];
273 if (mono->mono_handle && mono->is_started && !mono->is_shutdown)
275 /* Copied from Mono's ves_icall_System_Environment_Exit */
276 mono->mono_threads_set_shutting_down();
277 mono->mono_runtime_set_shutting_down();
278 mono->mono_thread_pool_cleanup();
279 mono->mono_thread_suspend_all_other_threads();
280 mono->mono_runtime_quit();
284 for (i=0; i<NUM_RUNTIMES; i++)
285 if (runtimes[i].loaded_runtime)
286 RuntimeHost_Destroy(runtimes[i].loaded_runtime);
289 void expect_no_runtimes(void)
291 int i;
293 for (i=0; i<NUM_ABI_VERSIONS; i++)
295 loaded_mono *mono = &loaded_monos[i];
296 if (mono->mono_handle && mono->is_started && !mono->is_shutdown)
298 ERR("Process exited with a Mono runtime loaded.\n");
299 return;
304 static HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
305 REFIID riid,
306 void **ppvObject)
308 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
310 if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
311 IsEqualGUID( riid, &IID_IUnknown ) )
313 *ppvObject = iface;
315 else
317 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
318 return E_NOINTERFACE;
321 ICLRRuntimeInfo_AddRef( iface );
323 return S_OK;
326 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
328 return 2;
331 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
333 return 1;
336 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
337 LPWSTR pwzBuffer, DWORD *pcchBuffer)
339 struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
340 DWORD buffer_size = *pcchBuffer;
341 HRESULT hr = S_OK;
342 char version[11];
343 DWORD size;
345 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
347 size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
349 assert(size <= sizeof(version));
351 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
353 if (pwzBuffer)
355 if (buffer_size >= *pcchBuffer)
356 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
357 else
358 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
361 return hr;
364 static BOOL get_install_root(LPWSTR install_dir)
366 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};
367 const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
369 DWORD len;
370 HKEY key;
372 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
373 return FALSE;
375 len = MAX_PATH;
376 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
378 RegCloseKey(key);
379 return FALSE;
381 RegCloseKey(key);
383 return TRUE;
386 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
387 LPWSTR pwzBuffer, DWORD *pcchBuffer)
389 static const WCHAR slash[] = {'\\',0};
390 DWORD buffer_size = *pcchBuffer;
391 WCHAR system_dir[MAX_PATH];
392 WCHAR version[MAX_PATH];
393 DWORD version_size, size;
394 HRESULT hr = S_OK;
396 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
398 if (!get_install_root(system_dir))
400 ERR("error reading registry key for installroot\n");
401 return E_FAIL;
403 else
405 version_size = MAX_PATH;
406 ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
407 lstrcatW(system_dir, version);
408 lstrcatW(system_dir, slash);
409 size = lstrlenW(system_dir) + 1;
412 *pcchBuffer = size;
414 if (pwzBuffer)
416 if (buffer_size >= size)
417 strcpyW(pwzBuffer, system_dir);
418 else
419 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
422 return hr;
425 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
426 HANDLE hndProcess, BOOL *pbLoaded)
428 FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
430 return E_NOTIMPL;
433 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
434 UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
436 FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
438 return E_NOTIMPL;
441 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
442 LPCWSTR pwzDllName, HMODULE *phndModule)
444 WCHAR version[MAX_PATH];
445 HRESULT hr;
446 DWORD cchBuffer;
448 TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
450 cchBuffer = MAX_PATH;
451 hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
452 if (FAILED(hr)) return hr;
454 return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
457 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
458 LPCSTR pszProcName, LPVOID *ppProc)
460 FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
462 return E_NOTIMPL;
465 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
466 REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
468 struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
469 RuntimeHost *host;
470 HRESULT hr;
472 TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
474 hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
476 if (SUCCEEDED(hr))
477 hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
479 return hr;
482 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
483 BOOL *pbLoadable)
485 FIXME("%p %p\n", iface, pbLoadable);
487 return E_NOTIMPL;
490 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
491 DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
493 FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
495 return E_NOTIMPL;
498 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
499 DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
501 FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
503 return E_NOTIMPL;
506 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
508 FIXME("%p\n", iface);
510 return E_NOTIMPL;
513 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
514 BOOL *pbStarted, DWORD *pdwStartupFlags)
516 FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
518 return E_NOTIMPL;
521 const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
522 CLRRuntimeInfo_QueryInterface,
523 CLRRuntimeInfo_AddRef,
524 CLRRuntimeInfo_Release,
525 CLRRuntimeInfo_GetVersionString,
526 CLRRuntimeInfo_GetRuntimeDirectory,
527 CLRRuntimeInfo_IsLoaded,
528 CLRRuntimeInfo_LoadErrorString,
529 CLRRuntimeInfo_LoadLibrary,
530 CLRRuntimeInfo_GetProcAddress,
531 CLRRuntimeInfo_GetInterface,
532 CLRRuntimeInfo_IsLoadable,
533 CLRRuntimeInfo_SetDefaultStartupFlags,
534 CLRRuntimeInfo_GetDefaultStartupFlags,
535 CLRRuntimeInfo_BindAsLegacyV2Runtime,
536 CLRRuntimeInfo_IsStarted
539 HRESULT ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo *iface, RuntimeHost **result)
541 struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
543 assert(This->ICLRRuntimeInfo_vtbl == &CLRRuntimeInfoVtbl);
545 return CLRRuntimeInfo_GetRuntimeHost(This, result);
548 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version)
550 static const WCHAR mono_dll[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
551 static const WCHAR libmono_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
552 static const WCHAR mono2_dll[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
553 static const WCHAR libmono2_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
554 DWORD attributes=INVALID_FILE_ATTRIBUTES;
556 if (abi_version == 1)
558 strcpyW(dll_path, path);
559 strcatW(dll_path, mono_dll);
560 attributes = GetFileAttributesW(dll_path);
562 if (attributes == INVALID_FILE_ATTRIBUTES)
564 strcpyW(dll_path, path);
565 strcatW(dll_path, libmono_dll);
566 attributes = GetFileAttributesW(dll_path);
569 else if (abi_version == 2)
571 strcpyW(dll_path, path);
572 strcatW(dll_path, mono2_dll);
573 attributes = GetFileAttributesW(dll_path);
575 if (attributes == INVALID_FILE_ATTRIBUTES)
577 strcpyW(dll_path, path);
578 strcatW(dll_path, libmono2_dll);
579 attributes = GetFileAttributesW(dll_path);
583 return (attributes != INVALID_FILE_ATTRIBUTES);
586 static BOOL get_mono_path_from_registry(LPWSTR path, int abi_version)
588 static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
589 static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
590 static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
591 static const WCHAR slash[] = {'\\',0};
593 WCHAR version[64], version_key[MAX_PATH];
594 DWORD len;
595 HKEY key;
596 WCHAR dll_path[MAX_PATH];
598 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
599 return FALSE;
601 len = sizeof(version);
602 if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
604 RegCloseKey(key);
605 return FALSE;
607 RegCloseKey(key);
609 lstrcpyW(version_key, mono_key);
610 lstrcatW(version_key, slash);
611 lstrcatW(version_key, version);
613 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
614 return FALSE;
616 len = sizeof(WCHAR) * MAX_PATH;
617 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
619 RegCloseKey(key);
620 return FALSE;
622 RegCloseKey(key);
624 return find_mono_dll(path, dll_path, abi_version);
627 static BOOL get_mono_path_from_folder(LPCWSTR folder, LPWSTR mono_path, int abi_version)
629 static const WCHAR mono_one_dot_zero[] = {'\\','m','o','n','o','-','1','.','0', 0};
630 static const WCHAR mono_two_dot_zero[] = {'\\','m','o','n','o','-','2','.','0', 0};
631 WCHAR mono_dll_path[MAX_PATH];
632 BOOL found = FALSE;
634 strcpyW(mono_path, folder);
636 if (abi_version == 1)
637 strcatW(mono_path, mono_one_dot_zero);
638 else if (abi_version == 2)
639 strcatW(mono_path, mono_two_dot_zero);
641 found = find_mono_dll(mono_path, mono_dll_path, abi_version);
643 return found;
646 static BOOL get_mono_path(LPWSTR path, int abi_version)
648 static const WCHAR subdir_mono[] = {'\\','m','o','n','o',0};
649 static const WCHAR sibling_mono[] = {'\\','.','.','\\','m','o','n','o',0};
650 WCHAR base_path[MAX_PATH];
651 const char *unix_data_dir;
652 WCHAR *dos_data_dir;
653 int build_tree=0;
654 static WCHAR* (CDECL *wine_get_dos_file_name)(const char*);
656 /* First try c:\windows\mono */
657 GetWindowsDirectoryW(base_path, MAX_PATH);
658 strcatW(base_path, subdir_mono);
660 if (get_mono_path_from_folder(base_path, path, abi_version))
661 return TRUE;
663 /* Next: /usr/share/wine/mono */
664 unix_data_dir = wine_get_data_dir();
666 if (!unix_data_dir)
668 unix_data_dir = wine_get_build_dir();
669 build_tree = 1;
672 if (unix_data_dir)
674 if (!wine_get_dos_file_name)
675 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
677 if (wine_get_dos_file_name)
679 dos_data_dir = wine_get_dos_file_name(unix_data_dir);
681 if (dos_data_dir)
683 strcpyW(base_path, dos_data_dir);
684 strcatW(base_path, build_tree ? sibling_mono : subdir_mono);
686 HeapFree(GetProcessHeap(), 0, dos_data_dir);
688 if (get_mono_path_from_folder(base_path, path, abi_version))
689 return TRUE;
694 /* Last: the registry */
695 return get_mono_path_from_registry(path, abi_version);
698 static void find_runtimes(void)
700 int abi_version, i;
701 static const WCHAR libmono[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
702 static const WCHAR mscorlib[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
703 WCHAR mono_path[MAX_PATH], lib_path[MAX_PATH];
704 BOOL any_runtimes_found = FALSE;
706 if (runtimes_initialized) return;
708 EnterCriticalSection(&runtime_list_cs);
710 if (runtimes_initialized) goto end;
712 for (abi_version=NUM_ABI_VERSIONS; abi_version>0; abi_version--)
714 if (!get_mono_path(mono_path, abi_version))
715 continue;
717 for (i=0; i<NUM_RUNTIMES; i++)
719 if (runtimes[i].mono_abi_version == 0)
721 strcpyW(lib_path, mono_path);
722 strcatW(lib_path, libmono);
723 strcatW(lib_path, runtimes[i].mono_libdir);
724 strcatW(lib_path, mscorlib);
726 if (GetFileAttributesW(lib_path) != INVALID_FILE_ATTRIBUTES)
728 runtimes[i].mono_abi_version = abi_version;
730 strcpyW(runtimes[i].mono_path, mono_path);
731 strcpyW(runtimes[i].mscorlib_path, lib_path);
733 any_runtimes_found = TRUE;
739 if (!any_runtimes_found)
741 /* Report all runtimes are available if Mono isn't installed.
742 * FIXME: Remove this when Mono is properly packaged. */
743 for (i=0; i<NUM_RUNTIMES; i++)
744 runtimes[i].mono_abi_version = -1;
747 runtimes_initialized = 1;
749 end:
750 LeaveCriticalSection(&runtime_list_cs);
753 struct InstalledRuntimeEnum
755 IEnumUnknown IEnumUnknown_iface;
756 LONG ref;
757 ULONG pos;
760 const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
762 static inline struct InstalledRuntimeEnum *impl_from_IEnumUnknown(IEnumUnknown *iface)
764 return CONTAINING_RECORD(iface, struct InstalledRuntimeEnum, IEnumUnknown_iface);
767 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface, REFIID riid,
768 void **ppvObject)
770 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
772 if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
773 IsEqualGUID( riid, &IID_IUnknown ) )
775 *ppvObject = iface;
777 else
779 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
780 return E_NOINTERFACE;
783 IEnumUnknown_AddRef( iface );
785 return S_OK;
788 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
790 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
791 ULONG ref = InterlockedIncrement(&This->ref);
793 TRACE("(%p) refcount=%u\n", iface, ref);
795 return ref;
798 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
800 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
801 ULONG ref = InterlockedDecrement(&This->ref);
803 TRACE("(%p) refcount=%u\n", iface, ref);
805 if (ref == 0)
807 HeapFree(GetProcessHeap(), 0, This);
810 return ref;
813 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
814 IUnknown **rgelt, ULONG *pceltFetched)
816 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
817 int num_fetched = 0;
818 HRESULT hr=S_OK;
819 IUnknown *item;
821 TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
823 while (num_fetched < celt)
825 if (This->pos >= NUM_RUNTIMES)
827 hr = S_FALSE;
828 break;
830 if (runtimes[This->pos].mono_abi_version)
832 item = (IUnknown*)&runtimes[This->pos];
833 IUnknown_AddRef(item);
834 rgelt[num_fetched] = item;
835 num_fetched++;
837 This->pos++;
840 if (pceltFetched)
841 *pceltFetched = num_fetched;
843 return hr;
846 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
848 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
849 int num_fetched = 0;
850 HRESULT hr=S_OK;
852 TRACE("(%p,%u)\n", iface, celt);
854 while (num_fetched < celt)
856 if (This->pos >= NUM_RUNTIMES)
858 hr = S_FALSE;
859 break;
861 if (runtimes[This->pos].mono_abi_version)
863 num_fetched++;
865 This->pos++;
868 return hr;
871 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
873 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
875 TRACE("(%p)\n", iface);
877 This->pos = 0;
879 return S_OK;
882 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
884 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
885 struct InstalledRuntimeEnum *new_enum;
887 TRACE("(%p)\n", iface);
889 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
890 if (!new_enum)
891 return E_OUTOFMEMORY;
893 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
894 new_enum->ref = 1;
895 new_enum->pos = This->pos;
897 *ppenum = &new_enum->IEnumUnknown_iface;
899 return S_OK;
902 const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
903 InstalledRuntimeEnum_QueryInterface,
904 InstalledRuntimeEnum_AddRef,
905 InstalledRuntimeEnum_Release,
906 InstalledRuntimeEnum_Next,
907 InstalledRuntimeEnum_Skip,
908 InstalledRuntimeEnum_Reset,
909 InstalledRuntimeEnum_Clone
912 struct CLRMetaHost
914 ICLRMetaHost ICLRMetaHost_iface;
917 static struct CLRMetaHost GlobalCLRMetaHost;
919 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
920 REFIID riid,
921 void **ppvObject)
923 TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
925 if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
926 IsEqualGUID( riid, &IID_IUnknown ) )
928 *ppvObject = iface;
930 else
932 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
933 return E_NOINTERFACE;
936 ICLRMetaHost_AddRef( iface );
938 return S_OK;
941 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
943 return 2;
946 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
948 return 1;
951 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
953 *major = 0;
954 *minor = 0;
955 *build = 0;
957 if (version[0] == 'v')
959 version++;
960 if (!isdigit(*version))
961 return FALSE;
963 while (isdigit(*version))
964 *major = *major * 10 + (*version++ - '0');
966 if (*version == 0)
967 return TRUE;
969 if (*version++ != '.' || !isdigit(*version))
970 return FALSE;
972 while (isdigit(*version))
973 *minor = *minor * 10 + (*version++ - '0');
975 if (*version == 0)
976 return TRUE;
978 if (*version++ != '.' || !isdigit(*version))
979 return FALSE;
981 while (isdigit(*version))
982 *build = *build * 10 + (*version++ - '0');
984 return *version == 0;
986 else
987 return FALSE;
990 static HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
991 LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
993 int i;
994 DWORD major, minor, build;
996 TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
998 if (!pwzVersion)
999 return E_POINTER;
1001 if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
1003 ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
1004 return CLR_E_SHIM_RUNTIME;
1007 find_runtimes();
1009 for (i=0; i<NUM_RUNTIMES; i++)
1011 if (runtimes[i].major == major && runtimes[i].minor == minor &&
1012 runtimes[i].build == build)
1014 if (runtimes[i].mono_abi_version)
1015 return IUnknown_QueryInterface((IUnknown*)&runtimes[i], iid, ppRuntime);
1016 else
1018 ERR("Mono is missing %s runtime\n", debugstr_w(pwzVersion));
1019 return CLR_E_SHIM_RUNTIME;
1024 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
1025 return CLR_E_SHIM_RUNTIME;
1028 HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
1029 LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
1031 ASSEMBLY *assembly;
1032 HRESULT hr;
1033 LPSTR version;
1034 ULONG buffer_size=*pcchBuffer;
1036 TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
1038 hr = assembly_create(&assembly, pwzFilePath);
1040 if (SUCCEEDED(hr))
1042 hr = assembly_get_runtime_version(assembly, &version);
1044 if (SUCCEEDED(hr))
1046 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
1048 if (pwzBuffer)
1050 if (buffer_size >= *pcchBuffer)
1051 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
1052 else
1053 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
1057 assembly_release(assembly);
1060 return hr;
1063 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
1064 IEnumUnknown **ppEnumerator)
1066 struct InstalledRuntimeEnum *new_enum;
1068 TRACE("%p\n", ppEnumerator);
1070 find_runtimes();
1072 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
1073 if (!new_enum)
1074 return E_OUTOFMEMORY;
1076 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
1077 new_enum->ref = 1;
1078 new_enum->pos = 0;
1080 *ppEnumerator = &new_enum->IEnumUnknown_iface;
1082 return S_OK;
1085 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
1086 HANDLE hndProcess, IEnumUnknown **ppEnumerator)
1088 FIXME("%p %p\n", hndProcess, ppEnumerator);
1090 return E_NOTIMPL;
1093 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
1094 RuntimeLoadedCallbackFnPtr pCallbackFunction)
1096 FIXME("%p\n", pCallbackFunction);
1098 return E_NOTIMPL;
1101 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
1102 REFIID riid, LPVOID *ppUnk)
1104 FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
1106 return E_NOTIMPL;
1109 static HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
1111 FIXME("%i: stub\n", iExitCode);
1113 ExitProcess(iExitCode);
1116 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
1118 CLRMetaHost_QueryInterface,
1119 CLRMetaHost_AddRef,
1120 CLRMetaHost_Release,
1121 CLRMetaHost_GetRuntime,
1122 CLRMetaHost_GetVersionFromFile,
1123 CLRMetaHost_EnumerateInstalledRuntimes,
1124 CLRMetaHost_EnumerateLoadedRuntimes,
1125 CLRMetaHost_RequestRuntimeLoadedNotification,
1126 CLRMetaHost_QueryLegacyV2RuntimeBinding,
1127 CLRMetaHost_ExitProcess
1130 static struct CLRMetaHost GlobalCLRMetaHost = {
1131 { &CLRMetaHost_vtbl }
1134 extern HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1136 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost.ICLRMetaHost_iface, riid, ppobj);
1139 static MonoAssembly* mono_assembly_search_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data)
1141 loaded_mono *mono = user_data;
1142 HRESULT hr=S_OK;
1143 MonoAssembly *result=NULL;
1144 char *stringname=NULL;
1145 LPWSTR stringnameW;
1146 int stringnameW_size;
1147 IAssemblyCache *asmcache;
1148 ASSEMBLY_INFO info;
1149 WCHAR path[MAX_PATH];
1150 char *pathA;
1151 MonoImageOpenStatus stat;
1152 static WCHAR fusiondll[] = {'f','u','s','i','o','n',0};
1153 HMODULE hfusion=NULL;
1154 static HRESULT WINAPI (*pCreateAssemblyCache)(IAssemblyCache**,DWORD);
1156 stringname = mono->mono_stringify_assembly_name(aname);
1158 TRACE("%s\n", debugstr_a(stringname));
1160 if (!stringname) return NULL;
1162 /* FIXME: We should search the given paths before the GAC. */
1164 if (!pCreateAssemblyCache)
1166 hr = LoadLibraryShim(fusiondll, NULL, NULL, &hfusion);
1168 if (SUCCEEDED(hr))
1170 pCreateAssemblyCache = (void*)GetProcAddress(hfusion, "CreateAssemblyCache");
1171 if (!pCreateAssemblyCache)
1172 hr = E_FAIL;
1176 if (SUCCEEDED(hr))
1177 hr = pCreateAssemblyCache(&asmcache, 0);
1179 if (SUCCEEDED(hr))
1181 stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, stringname, -1, NULL, 0);
1183 stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1184 if (stringnameW)
1185 MultiByteToWideChar(CP_UTF8, 0, stringname, -1, stringnameW, stringnameW_size);
1186 else
1187 hr = E_OUTOFMEMORY;
1189 if (SUCCEEDED(hr))
1191 info.cbAssemblyInfo = sizeof(info);
1192 info.pszCurrentAssemblyPathBuf = path;
1193 info.cchBuf = MAX_PATH;
1194 path[0] = 0;
1196 hr = IAssemblyCache_QueryAssemblyInfo(asmcache, 0, stringnameW, &info);
1199 HeapFree(GetProcessHeap(), 0, stringnameW);
1201 IAssemblyCache_Release(asmcache);
1204 if (SUCCEEDED(hr))
1206 TRACE("found: %s\n", debugstr_w(path));
1208 pathA = WtoA(path);
1210 if (pathA)
1212 result = mono->mono_assembly_open(pathA, &stat);
1214 if (!result)
1215 ERR("Failed to load %s, status=%u\n", debugstr_w(path), stat);
1217 HeapFree(GetProcessHeap(), 0, pathA);
1221 mono->mono_free(stringname);
1223 return result;
1226 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1227 DWORD startup_flags, DWORD runtimeinfo_flags, BOOL legacy, ICLRRuntimeInfo **result)
1229 static const WCHAR dotconfig[] = {'.','c','o','n','f','i','g',0};
1230 static const DWORD supported_startup_flags = 0;
1231 static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1232 int i;
1233 WCHAR local_version[MAX_PATH];
1234 ULONG local_version_size = MAX_PATH;
1235 WCHAR local_config_file[MAX_PATH];
1236 HRESULT hr;
1237 parsed_config_file parsed_config;
1239 if (startup_flags & ~supported_startup_flags)
1240 FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
1242 if (runtimeinfo_flags & ~supported_runtime_flags)
1243 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
1245 if (exefile && !config_file)
1247 strcpyW(local_config_file, exefile);
1248 strcatW(local_config_file, dotconfig);
1250 config_file = local_config_file;
1253 if (config_file)
1255 int found=0;
1256 hr = parse_config_file(config_file, &parsed_config);
1258 if (SUCCEEDED(hr))
1260 supported_runtime *entry;
1261 LIST_FOR_EACH_ENTRY(entry, &parsed_config.supported_runtimes, supported_runtime, entry)
1263 hr = CLRMetaHost_GetRuntime(0, entry->version, &IID_ICLRRuntimeInfo, (void**)result);
1264 if (SUCCEEDED(hr))
1266 found = 1;
1267 break;
1271 else
1273 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file), hr);
1276 free_parsed_config_file(&parsed_config);
1278 if (found)
1279 return S_OK;
1282 if (exefile && !version)
1284 hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1286 version = local_version;
1288 if (FAILED(hr)) return hr;
1291 if (version)
1293 return CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1296 if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1298 find_runtimes();
1300 if (legacy)
1301 i = 2;
1302 else
1303 i = NUM_RUNTIMES;
1305 while (i--)
1307 if (runtimes[i].mono_abi_version)
1308 return IUnknown_QueryInterface((IUnknown*)&runtimes[i],
1309 &IID_ICLRRuntimeInfo, (void**)result);
1312 ERR("No %s.NET runtime installed\n", legacy ? "legacy " : "");
1314 return CLR_E_SHIM_RUNTIME;
1317 return CLR_E_SHIM_RUNTIME;
1320 HRESULT force_get_runtime_info(ICLRRuntimeInfo **result)
1322 return IUnknown_QueryInterface((IUnknown*)&runtimes[0], &IID_ICLRRuntimeInfo, (void**)result);