xref: /reactos/dll/win32/mscoree/metahost.c (revision a1fc312a)
1 /*
2  * ICLRMetaHost - discovery and management of available .NET runtimes
3  *
4  * Copyright 2010 Vincent Povirk for CodeWeavers
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #include "mscoree_private.h"
22 
23 #include <stdio.h>
24 #include <assert.h>
25 
26 #include <wine/library.h>
27 
28 #include <fusion.h>
29 
30 static const WCHAR net_11_subdir[] = {'1','.','0',0};
31 static const WCHAR net_20_subdir[] = {'2','.','0',0};
32 static const WCHAR net_40_subdir[] = {'4','.','0',0};
33 
34 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl;
35 
36 #define NUM_RUNTIMES 3
37 
38 static struct CLRRuntimeInfo runtimes[NUM_RUNTIMES] = {
39     {{&CLRRuntimeInfoVtbl}, net_11_subdir, 1, 1, 4322, 0},
40     {{&CLRRuntimeInfoVtbl}, net_20_subdir, 2, 0, 50727, 0},
41     {{&CLRRuntimeInfoVtbl}, net_40_subdir, 4, 0, 30319, 0}
42 };
43 
44 static int runtimes_initialized;
45 
46 static CRITICAL_SECTION runtime_list_cs;
47 static CRITICAL_SECTION_DEBUG runtime_list_cs_debug =
48 {
49     0, 0, &runtime_list_cs,
50     { &runtime_list_cs_debug.ProcessLocksList,
51       &runtime_list_cs_debug.ProcessLocksList },
52       0, 0, { (DWORD_PTR)(__FILE__ ": runtime_list_cs") }
53 };
54 static CRITICAL_SECTION runtime_list_cs = { &runtime_list_cs_debug, -1, 0, 0, 0, 0 };
55 
56 #define NUM_ABI_VERSIONS 2
57 
58 static loaded_mono loaded_monos[NUM_ABI_VERSIONS];
59 
60 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version);
61 
62 static MonoAssembly* mono_assembly_search_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data);
63 
64 static void mono_shutdown_callback_fn(MonoProfiler *prof);
65 
66 static void set_environment(LPCWSTR bin_path)
67 {
68     WCHAR path_env[MAX_PATH];
69     int len;
70 
71     static const WCHAR pathW[] = {'P','A','T','H',0};
72 
73     /* We have to modify PATH as Mono loads other DLLs from this directory. */
74     GetEnvironmentVariableW(pathW, path_env, sizeof(path_env)/sizeof(WCHAR));
75     len = strlenW(path_env);
76     path_env[len++] = ';';
77     strcpyW(path_env+len, bin_path);
78     SetEnvironmentVariableW(pathW, path_env);
79 }
80 
81 static void CDECL do_nothing(void)
82 {
83 }
84 
85 static void missing_runtime_message(const CLRRuntimeInfo *This)
86 {
87     if (This->major == 1)
88         MESSAGE("wine: Install Mono 2.6 for Windows to run .NET 1.1 applications.\n");
89     else if (This->major == 2)
90         MESSAGE("wine: Install Mono for Windows to run .NET 2.0 applications.\n");
91     else if (This->major == 4)
92         MESSAGE("wine: Install Mono 2.8 or greater for Windows to run .NET 4.0 applications.\n");
93 }
94 
95 static HRESULT load_mono(CLRRuntimeInfo *This, loaded_mono **result)
96 {
97     static const WCHAR bin[] = {'\\','b','i','n',0};
98     static const WCHAR lib[] = {'\\','l','i','b',0};
99     static const WCHAR etc[] = {'\\','e','t','c',0};
100     static const WCHAR glibdll[] = {'l','i','b','g','l','i','b','-','2','.','0','-','0','.','d','l','l',0};
101     WCHAR mono_dll_path[MAX_PATH+16], mono_bin_path[MAX_PATH+4];
102     WCHAR mono_lib_path[MAX_PATH+4], mono_etc_path[MAX_PATH+4];
103     char mono_lib_path_a[MAX_PATH], mono_etc_path_a[MAX_PATH];
104     int trace_size;
105     char trace_setting[256];
106 
107     if (This->mono_abi_version <= 0 || This->mono_abi_version > NUM_ABI_VERSIONS)
108     {
109         missing_runtime_message(This);
110         return E_FAIL;
111     }
112 
113     *result = &loaded_monos[This->mono_abi_version-1];
114 
115     if ((*result)->is_shutdown)
116     {
117         ERR("Cannot load Mono after it has been shut down.\n");
118         *result = NULL;
119         return E_FAIL;
120     }
121 
122     if (!(*result)->mono_handle)
123     {
124         strcpyW(mono_bin_path, This->mono_path);
125         strcatW(mono_bin_path, bin);
126         set_environment(mono_bin_path);
127 
128         strcpyW(mono_lib_path, This->mono_path);
129         strcatW(mono_lib_path, lib);
130         WideCharToMultiByte(CP_UTF8, 0, mono_lib_path, -1, mono_lib_path_a, MAX_PATH, NULL, NULL);
131 
132         strcpyW(mono_etc_path, This->mono_path);
133         strcatW(mono_etc_path, etc);
134         WideCharToMultiByte(CP_UTF8, 0, mono_etc_path, -1, mono_etc_path_a, MAX_PATH, NULL, NULL);
135 
136         if (!find_mono_dll(This->mono_path, mono_dll_path, This->mono_abi_version)) goto fail;
137 
138         (*result)->mono_handle = LoadLibraryW(mono_dll_path);
139 
140         if (!(*result)->mono_handle) goto fail;
141 
142 #define LOAD_MONO_FUNCTION(x) do { \
143     (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
144     if (!(*result)->x) { \
145         goto fail; \
146     } \
147 } while (0);
148 
149         LOAD_MONO_FUNCTION(mono_assembly_get_image);
150         LOAD_MONO_FUNCTION(mono_assembly_load_from);
151         LOAD_MONO_FUNCTION(mono_assembly_open);
152         LOAD_MONO_FUNCTION(mono_config_parse);
153         LOAD_MONO_FUNCTION(mono_class_from_mono_type);
154         LOAD_MONO_FUNCTION(mono_class_from_name);
155         LOAD_MONO_FUNCTION(mono_class_get_method_from_name);
156         LOAD_MONO_FUNCTION(mono_domain_assembly_open);
157         LOAD_MONO_FUNCTION(mono_image_open_from_module_handle);
158         LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook);
159         LOAD_MONO_FUNCTION(mono_jit_exec);
160         LOAD_MONO_FUNCTION(mono_jit_init);
161         LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
162         LOAD_MONO_FUNCTION(mono_marshal_get_vtfixup_ftnptr);
163         LOAD_MONO_FUNCTION(mono_object_get_domain);
164         LOAD_MONO_FUNCTION(mono_object_new);
165         LOAD_MONO_FUNCTION(mono_object_unbox);
166         LOAD_MONO_FUNCTION(mono_profiler_install);
167         LOAD_MONO_FUNCTION(mono_reflection_type_from_name);
168         LOAD_MONO_FUNCTION(mono_runtime_invoke);
169         LOAD_MONO_FUNCTION(mono_runtime_object_init);
170         LOAD_MONO_FUNCTION(mono_runtime_quit);
171         LOAD_MONO_FUNCTION(mono_set_dirs);
172         LOAD_MONO_FUNCTION(mono_stringify_assembly_name);
173         LOAD_MONO_FUNCTION(mono_string_new);
174         LOAD_MONO_FUNCTION(mono_thread_attach);
175 
176         /* GLib imports obsoleted by the 2.0 ABI */
177         if (This->mono_abi_version == 1)
178         {
179             (*result)->glib_handle = LoadLibraryW(glibdll);
180             if (!(*result)->glib_handle) goto fail;
181 
182             (*result)->mono_free = (void*)GetProcAddress((*result)->glib_handle, "g_free");
183             if (!(*result)->mono_free) goto fail;
184         }
185         else
186         {
187             LOAD_MONO_FUNCTION(mono_free);
188         }
189 
190 #undef LOAD_MONO_FUNCTION
191 
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; \
196     } \
197 } while (0);
198 
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);
203 
204 #undef LOAD_OPT_VOID_MONO_FUNCTION
205 
206         (*result)->mono_profiler_install((MonoProfiler*)*result, mono_shutdown_callback_fn);
207 
208         (*result)->mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
209 
210         (*result)->mono_config_parse(NULL);
211 
212         (*result)->mono_install_assembly_preload_hook(mono_assembly_search_hook_fn, *result);
213 
214         trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
215 
216         if (trace_size)
217         {
218             (*result)->mono_jit_set_trace_options(trace_setting);
219         }
220     }
221 
222     return S_OK;
223 
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;
231 }
232 
233 static void mono_shutdown_callback_fn(MonoProfiler *prof)
234 {
235     loaded_mono *mono = (loaded_mono*)prof;
236 
237     mono->is_shutdown = TRUE;
238 }
239 
240 static HRESULT CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo *This, RuntimeHost **result)
241 {
242     HRESULT hr = S_OK;
243     loaded_mono *ploaded_mono;
244 
245     if (This->loaded_runtime)
246     {
247         *result = This->loaded_runtime;
248         return hr;
249     }
250 
251     EnterCriticalSection(&runtime_list_cs);
252 
253     hr = load_mono(This, &ploaded_mono);
254 
255     if (SUCCEEDED(hr))
256         hr = RuntimeHost_Construct(This, ploaded_mono, &This->loaded_runtime);
257 
258     LeaveCriticalSection(&runtime_list_cs);
259 
260     if (SUCCEEDED(hr))
261         *result = This->loaded_runtime;
262 
263     return hr;
264 }
265 
266 void unload_all_runtimes(void)
267 {
268     int i;
269 
270     for (i=0; i<NUM_ABI_VERSIONS; i++)
271     {
272         loaded_mono *mono = &loaded_monos[i];
273         if (mono->mono_handle && mono->is_started && !mono->is_shutdown)
274         {
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();
281         }
282     }
283 
284     for (i=0; i<NUM_RUNTIMES; i++)
285         if (runtimes[i].loaded_runtime)
286             RuntimeHost_Destroy(runtimes[i].loaded_runtime);
287 }
288 
289 void expect_no_runtimes(void)
290 {
291     int i;
292 
293     for (i=0; i<NUM_ABI_VERSIONS; i++)
294     {
295         loaded_mono *mono = &loaded_monos[i];
296         if (mono->mono_handle && mono->is_started && !mono->is_shutdown)
297         {
298             ERR("Process exited with a Mono runtime loaded.\n");
299             return;
300         }
301     }
302 }
303 
304 static inline CLRRuntimeInfo *impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo *iface)
305 {
306     return CONTAINING_RECORD(iface, CLRRuntimeInfo, ICLRRuntimeInfo_iface);
307 }
308 
309 static HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
310         REFIID riid,
311         void **ppvObject)
312 {
313     TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
314 
315     if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
316          IsEqualGUID( riid, &IID_IUnknown ) )
317     {
318         *ppvObject = iface;
319     }
320     else
321     {
322         FIXME("Unsupported interface %s\n", debugstr_guid(riid));
323         return E_NOINTERFACE;
324     }
325 
326     ICLRRuntimeInfo_AddRef( iface );
327 
328     return S_OK;
329 }
330 
331 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
332 {
333     return 2;
334 }
335 
336 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
337 {
338     return 1;
339 }
340 
341 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
342     LPWSTR pwzBuffer, DWORD *pcchBuffer)
343 {
344     struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
345     DWORD buffer_size = *pcchBuffer;
346     HRESULT hr = S_OK;
347     char version[11];
348     DWORD size;
349 
350     TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
351 
352     size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
353 
354     assert(size <= sizeof(version));
355 
356     *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
357 
358     if (pwzBuffer)
359     {
360         if (buffer_size >= *pcchBuffer)
361             MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
362         else
363             hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
364     }
365 
366     return hr;
367 }
368 
369 static BOOL get_install_root(LPWSTR install_dir)
370 {
371     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};
372     const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
373 
374     DWORD len;
375     HKEY key;
376 
377     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
378         return FALSE;
379 
380     len = MAX_PATH * sizeof(WCHAR);
381     if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
382     {
383         RegCloseKey(key);
384         return FALSE;
385     }
386     RegCloseKey(key);
387 
388     return TRUE;
389 }
390 
391 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
392     LPWSTR pwzBuffer, DWORD *pcchBuffer)
393 {
394     static const WCHAR slash[] = {'\\',0};
395     DWORD buffer_size = *pcchBuffer;
396     WCHAR system_dir[MAX_PATH];
397     WCHAR version[MAX_PATH];
398     DWORD version_size, size;
399     HRESULT hr = S_OK;
400 
401     TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
402 
403     if (!get_install_root(system_dir))
404     {
405         ERR("error reading registry key for installroot\n");
406         return E_FAIL;
407     }
408     else
409     {
410         version_size = MAX_PATH;
411         ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
412         lstrcatW(system_dir, version);
413         lstrcatW(system_dir, slash);
414         size = lstrlenW(system_dir) + 1;
415     }
416 
417     *pcchBuffer = size;
418 
419     if (pwzBuffer)
420     {
421         if (buffer_size >= size)
422             strcpyW(pwzBuffer, system_dir);
423         else
424             hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
425     }
426 
427     return hr;
428 }
429 
430 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
431     HANDLE hndProcess, BOOL *pbLoaded)
432 {
433     FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
434 
435     return E_NOTIMPL;
436 }
437 
438 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
439     UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
440 {
441     FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
442 
443     return E_NOTIMPL;
444 }
445 
446 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
447     LPCWSTR pwzDllName, HMODULE *phndModule)
448 {
449     WCHAR version[MAX_PATH];
450     HRESULT hr;
451     DWORD cchBuffer;
452 
453     TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
454 
455     cchBuffer = MAX_PATH;
456     hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
457     if (FAILED(hr)) return hr;
458 
459     return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
460 }
461 
462 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
463     LPCSTR pszProcName, LPVOID *ppProc)
464 {
465     FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
466 
467     return E_NOTIMPL;
468 }
469 
470 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
471     REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
472 {
473     struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
474     RuntimeHost *host;
475     HRESULT hr;
476 
477     TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
478 
479     hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
480 
481     if (SUCCEEDED(hr))
482         hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
483 
484     return hr;
485 }
486 
487 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
488     BOOL *pbLoadable)
489 {
490     FIXME("%p %p\n", iface, pbLoadable);
491 
492     return E_NOTIMPL;
493 }
494 
495 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
496     DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
497 {
498     FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
499 
500     return E_NOTIMPL;
501 }
502 
503 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
504     DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
505 {
506     FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
507 
508     return E_NOTIMPL;
509 }
510 
511 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
512 {
513     FIXME("%p\n", iface);
514 
515     return E_NOTIMPL;
516 }
517 
518 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
519     BOOL *pbStarted, DWORD *pdwStartupFlags)
520 {
521     FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
522 
523     return E_NOTIMPL;
524 }
525 
526 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
527     CLRRuntimeInfo_QueryInterface,
528     CLRRuntimeInfo_AddRef,
529     CLRRuntimeInfo_Release,
530     CLRRuntimeInfo_GetVersionString,
531     CLRRuntimeInfo_GetRuntimeDirectory,
532     CLRRuntimeInfo_IsLoaded,
533     CLRRuntimeInfo_LoadErrorString,
534     CLRRuntimeInfo_LoadLibrary,
535     CLRRuntimeInfo_GetProcAddress,
536     CLRRuntimeInfo_GetInterface,
537     CLRRuntimeInfo_IsLoadable,
538     CLRRuntimeInfo_SetDefaultStartupFlags,
539     CLRRuntimeInfo_GetDefaultStartupFlags,
540     CLRRuntimeInfo_BindAsLegacyV2Runtime,
541     CLRRuntimeInfo_IsStarted
542 };
543 
544 HRESULT ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo *iface, RuntimeHost **result)
545 {
546     struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
547 
548     assert(This->ICLRRuntimeInfo_iface.lpVtbl == &CLRRuntimeInfoVtbl);
549 
550     return CLRRuntimeInfo_GetRuntimeHost(This, result);
551 }
552 
553 #ifdef __i386__
554 static const WCHAR libmono2_arch_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','-','x','8','6','.','d','l','l',0};
555 #elif defined(__x86_64__)
556 static const WCHAR libmono2_arch_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','-','x','8','6','_','6','4','.','d','l','l',0};
557 #else
558 static const WCHAR libmono2_arch_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
559 #endif
560 
561 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version)
562 {
563     static const WCHAR mono_dll[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
564     static const WCHAR libmono_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
565     static const WCHAR mono2_dll[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
566     static const WCHAR libmono2_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
567     DWORD attributes=INVALID_FILE_ATTRIBUTES;
568 
569     if (abi_version == 1)
570     {
571         strcpyW(dll_path, path);
572         strcatW(dll_path, mono_dll);
573         attributes = GetFileAttributesW(dll_path);
574 
575         if (attributes == INVALID_FILE_ATTRIBUTES)
576         {
577             strcpyW(dll_path, path);
578             strcatW(dll_path, libmono_dll);
579             attributes = GetFileAttributesW(dll_path);
580         }
581     }
582     else if (abi_version == 2)
583     {
584         strcpyW(dll_path, path);
585         strcatW(dll_path, libmono2_arch_dll);
586         attributes = GetFileAttributesW(dll_path);
587 
588         if (attributes == INVALID_FILE_ATTRIBUTES)
589         {
590             strcpyW(dll_path, path);
591             strcatW(dll_path, mono2_dll);
592             attributes = GetFileAttributesW(dll_path);
593         }
594 
595         if (attributes == INVALID_FILE_ATTRIBUTES)
596         {
597             strcpyW(dll_path, path);
598             strcatW(dll_path, libmono2_dll);
599             attributes = GetFileAttributesW(dll_path);
600         }
601     }
602 
603     return (attributes != INVALID_FILE_ATTRIBUTES);
604 }
605 
606 static BOOL get_mono_path_from_registry(LPWSTR path, int abi_version)
607 {
608     static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
609     static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
610     static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
611     static const WCHAR slash[] = {'\\',0};
612 
613     WCHAR version[64], version_key[MAX_PATH];
614     DWORD len;
615     HKEY key;
616     WCHAR dll_path[MAX_PATH];
617 
618     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
619         return FALSE;
620 
621     len = sizeof(version);
622     if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
623     {
624         RegCloseKey(key);
625         return FALSE;
626     }
627     RegCloseKey(key);
628 
629     lstrcpyW(version_key, mono_key);
630     lstrcatW(version_key, slash);
631     lstrcatW(version_key, version);
632 
633     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
634         return FALSE;
635 
636     len = sizeof(WCHAR) * MAX_PATH;
637     if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
638     {
639         RegCloseKey(key);
640         return FALSE;
641     }
642     RegCloseKey(key);
643 
644     return find_mono_dll(path, dll_path, abi_version);
645 }
646 
647 static BOOL get_mono_path_from_folder(LPCWSTR folder, LPWSTR mono_path, int abi_version)
648 {
649     static const WCHAR mono_one_dot_zero[] = {'\\','m','o','n','o','-','1','.','0', 0};
650     static const WCHAR mono_two_dot_zero[] = {'\\','m','o','n','o','-','2','.','0', 0};
651     WCHAR mono_dll_path[MAX_PATH];
652     BOOL found = FALSE;
653 
654     strcpyW(mono_path, folder);
655 
656     if (abi_version == 1)
657         strcatW(mono_path, mono_one_dot_zero);
658     else if (abi_version == 2)
659         strcatW(mono_path, mono_two_dot_zero);
660 
661     found = find_mono_dll(mono_path, mono_dll_path, abi_version);
662 
663     return found;
664 }
665 
666 static BOOL get_mono_path(LPWSTR path, int abi_version)
667 {
668     static const WCHAR subdir_mono[] = {'\\','m','o','n','o',0};
669     static const WCHAR sibling_mono[] = {'\\','.','.','\\','m','o','n','o',0};
670     WCHAR base_path[MAX_PATH];
671     const char *unix_data_dir;
672     WCHAR *dos_data_dir;
673     int build_tree=0;
674     static WCHAR* (CDECL *wine_get_dos_file_name)(const char*);
675 
676     /* First try c:\windows\mono */
677     GetWindowsDirectoryW(base_path, MAX_PATH);
678     strcatW(base_path, subdir_mono);
679 
680     if (get_mono_path_from_folder(base_path, path, abi_version))
681         return TRUE;
682 
683     /* Next: /usr/share/wine/mono */
684     unix_data_dir = wine_get_data_dir();
685 
686     if (!unix_data_dir)
687     {
688         unix_data_dir = wine_get_build_dir();
689         build_tree = 1;
690     }
691 
692     if (unix_data_dir)
693     {
694         if (!wine_get_dos_file_name)
695             wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
696 
697         if (wine_get_dos_file_name)
698         {
699             dos_data_dir = wine_get_dos_file_name(unix_data_dir);
700 
701             if (dos_data_dir)
702             {
703                 strcpyW(base_path, dos_data_dir);
704                 strcatW(base_path, build_tree ? sibling_mono : subdir_mono);
705 
706                 HeapFree(GetProcessHeap(), 0, dos_data_dir);
707 
708                 if (get_mono_path_from_folder(base_path, path, abi_version))
709                     return TRUE;
710             }
711         }
712     }
713 
714     /* Last: the registry */
715     return get_mono_path_from_registry(path, abi_version);
716 }
717 
718 static void find_runtimes(void)
719 {
720     int abi_version, i;
721     static const WCHAR libmono[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
722     static const WCHAR mscorlib[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
723     WCHAR mono_path[MAX_PATH], lib_path[MAX_PATH];
724     BOOL any_runtimes_found = FALSE;
725 
726     if (runtimes_initialized) return;
727 
728     EnterCriticalSection(&runtime_list_cs);
729 
730     if (runtimes_initialized) goto end;
731 
732     for (abi_version=NUM_ABI_VERSIONS; abi_version>0; abi_version--)
733     {
734         if (!get_mono_path(mono_path, abi_version))
735             continue;
736 
737         for (i=0; i<NUM_RUNTIMES; i++)
738         {
739             if (runtimes[i].mono_abi_version == 0)
740             {
741                 strcpyW(lib_path, mono_path);
742                 strcatW(lib_path, libmono);
743                 strcatW(lib_path, runtimes[i].mono_libdir);
744                 strcatW(lib_path, mscorlib);
745 
746                 if (GetFileAttributesW(lib_path) != INVALID_FILE_ATTRIBUTES)
747                 {
748                     runtimes[i].mono_abi_version = abi_version;
749 
750                     strcpyW(runtimes[i].mono_path, mono_path);
751                     strcpyW(runtimes[i].mscorlib_path, lib_path);
752 
753                     any_runtimes_found = TRUE;
754                 }
755             }
756         }
757     }
758 
759     if (!any_runtimes_found)
760     {
761         /* Report all runtimes are available if Mono isn't installed.
762          * FIXME: Remove this when Mono is properly packaged. */
763         for (i=0; i<NUM_RUNTIMES; i++)
764             runtimes[i].mono_abi_version = -1;
765     }
766 
767     runtimes_initialized = 1;
768 
769 end:
770     LeaveCriticalSection(&runtime_list_cs);
771 }
772 
773 struct InstalledRuntimeEnum
774 {
775     IEnumUnknown IEnumUnknown_iface;
776     LONG ref;
777     ULONG pos;
778 };
779 
780 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
781 
782 static inline struct InstalledRuntimeEnum *impl_from_IEnumUnknown(IEnumUnknown *iface)
783 {
784     return CONTAINING_RECORD(iface, struct InstalledRuntimeEnum, IEnumUnknown_iface);
785 }
786 
787 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface, REFIID riid,
788         void **ppvObject)
789 {
790     TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
791 
792     if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
793          IsEqualGUID( riid, &IID_IUnknown ) )
794     {
795         *ppvObject = iface;
796     }
797     else
798     {
799         FIXME("Unsupported interface %s\n", debugstr_guid(riid));
800         return E_NOINTERFACE;
801     }
802 
803     IEnumUnknown_AddRef( iface );
804 
805     return S_OK;
806 }
807 
808 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
809 {
810     struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
811     ULONG ref = InterlockedIncrement(&This->ref);
812 
813     TRACE("(%p) refcount=%u\n", iface, ref);
814 
815     return ref;
816 }
817 
818 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
819 {
820     struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
821     ULONG ref = InterlockedDecrement(&This->ref);
822 
823     TRACE("(%p) refcount=%u\n", iface, ref);
824 
825     if (ref == 0)
826     {
827         HeapFree(GetProcessHeap(), 0, This);
828     }
829 
830     return ref;
831 }
832 
833 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
834     IUnknown **rgelt, ULONG *pceltFetched)
835 {
836     struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
837     int num_fetched = 0;
838     HRESULT hr=S_OK;
839     IUnknown *item;
840 
841     TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
842 
843     while (num_fetched < celt)
844     {
845         if (This->pos >= NUM_RUNTIMES)
846         {
847             hr = S_FALSE;
848             break;
849         }
850         if (runtimes[This->pos].mono_abi_version)
851         {
852             item = (IUnknown*)&runtimes[This->pos].ICLRRuntimeInfo_iface;
853             IUnknown_AddRef(item);
854             rgelt[num_fetched] = item;
855             num_fetched++;
856         }
857         This->pos++;
858     }
859 
860     if (pceltFetched)
861         *pceltFetched = num_fetched;
862 
863     return hr;
864 }
865 
866 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
867 {
868     struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
869     int num_fetched = 0;
870     HRESULT hr=S_OK;
871 
872     TRACE("(%p,%u)\n", iface, celt);
873 
874     while (num_fetched < celt)
875     {
876         if (This->pos >= NUM_RUNTIMES)
877         {
878             hr = S_FALSE;
879             break;
880         }
881         if (runtimes[This->pos].mono_abi_version)
882         {
883             num_fetched++;
884         }
885         This->pos++;
886     }
887 
888     return hr;
889 }
890 
891 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
892 {
893     struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
894 
895     TRACE("(%p)\n", iface);
896 
897     This->pos = 0;
898 
899     return S_OK;
900 }
901 
902 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
903 {
904     struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
905     struct InstalledRuntimeEnum *new_enum;
906 
907     TRACE("(%p)\n", iface);
908 
909     new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
910     if (!new_enum)
911         return E_OUTOFMEMORY;
912 
913     new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
914     new_enum->ref = 1;
915     new_enum->pos = This->pos;
916 
917     *ppenum = &new_enum->IEnumUnknown_iface;
918 
919     return S_OK;
920 }
921 
922 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
923     InstalledRuntimeEnum_QueryInterface,
924     InstalledRuntimeEnum_AddRef,
925     InstalledRuntimeEnum_Release,
926     InstalledRuntimeEnum_Next,
927     InstalledRuntimeEnum_Skip,
928     InstalledRuntimeEnum_Reset,
929     InstalledRuntimeEnum_Clone
930 };
931 
932 struct CLRMetaHost
933 {
934     ICLRMetaHost ICLRMetaHost_iface;
935 };
936 
937 static struct CLRMetaHost GlobalCLRMetaHost;
938 
939 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
940         REFIID riid,
941         void **ppvObject)
942 {
943     TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
944 
945     if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
946          IsEqualGUID( riid, &IID_IUnknown ) )
947     {
948         *ppvObject = iface;
949     }
950     else
951     {
952         FIXME("Unsupported interface %s\n", debugstr_guid(riid));
953         return E_NOINTERFACE;
954     }
955 
956     ICLRMetaHost_AddRef( iface );
957 
958     return S_OK;
959 }
960 
961 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
962 {
963     return 2;
964 }
965 
966 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
967 {
968     return 1;
969 }
970 
971 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
972 {
973     *major = 0;
974     *minor = 0;
975     *build = 0;
976 
977     if (version[0] == 'v' || version[0] == 'V')
978     {
979         version++;
980         if (!isdigit(*version))
981             return FALSE;
982 
983         while (isdigit(*version))
984             *major = *major * 10 + (*version++ - '0');
985 
986         if (*version == 0)
987             return TRUE;
988 
989         if (*version++ != '.' || !isdigit(*version))
990             return FALSE;
991 
992         while (isdigit(*version))
993             *minor = *minor * 10 + (*version++ - '0');
994 
995         if (*version == 0)
996             return TRUE;
997 
998         if (*version++ != '.' || !isdigit(*version))
999             return FALSE;
1000 
1001         while (isdigit(*version))
1002             *build = *build * 10 + (*version++ - '0');
1003 
1004         return *version == 0;
1005     }
1006     else
1007         return FALSE;
1008 }
1009 
1010 HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
1011     LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
1012 {
1013     int i;
1014     DWORD major, minor, build;
1015 
1016     TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
1017 
1018     if (!pwzVersion)
1019         return E_POINTER;
1020 
1021     if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
1022     {
1023         ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
1024         return CLR_E_SHIM_RUNTIME;
1025     }
1026 
1027     find_runtimes();
1028 
1029     for (i=0; i<NUM_RUNTIMES; i++)
1030     {
1031         if (runtimes[i].major == major && runtimes[i].minor == minor &&
1032             runtimes[i].build == build)
1033         {
1034             if (runtimes[i].mono_abi_version)
1035                 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface, iid,
1036                         ppRuntime);
1037             else
1038             {
1039                 missing_runtime_message(&runtimes[i]);
1040                 return CLR_E_SHIM_RUNTIME;
1041             }
1042         }
1043     }
1044 
1045     FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
1046     return CLR_E_SHIM_RUNTIME;
1047 }
1048 
1049 HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
1050     LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
1051 {
1052     ASSEMBLY *assembly;
1053     HRESULT hr;
1054     LPSTR version;
1055     ULONG buffer_size=*pcchBuffer;
1056 
1057     TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
1058 
1059     hr = assembly_create(&assembly, pwzFilePath);
1060 
1061     if (SUCCEEDED(hr))
1062     {
1063         hr = assembly_get_runtime_version(assembly, &version);
1064 
1065         if (SUCCEEDED(hr))
1066         {
1067             *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
1068 
1069             if (pwzBuffer)
1070             {
1071                 if (buffer_size >= *pcchBuffer)
1072                     MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
1073                 else
1074                     hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
1075             }
1076         }
1077 
1078         assembly_release(assembly);
1079     }
1080 
1081     return hr;
1082 }
1083 
1084 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
1085     IEnumUnknown **ppEnumerator)
1086 {
1087     struct InstalledRuntimeEnum *new_enum;
1088 
1089     TRACE("%p\n", ppEnumerator);
1090 
1091     find_runtimes();
1092 
1093     new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
1094     if (!new_enum)
1095         return E_OUTOFMEMORY;
1096 
1097     new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
1098     new_enum->ref = 1;
1099     new_enum->pos = 0;
1100 
1101     *ppEnumerator = &new_enum->IEnumUnknown_iface;
1102 
1103     return S_OK;
1104 }
1105 
1106 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
1107     HANDLE hndProcess, IEnumUnknown **ppEnumerator)
1108 {
1109     FIXME("%p %p\n", hndProcess, ppEnumerator);
1110 
1111     return E_NOTIMPL;
1112 }
1113 
1114 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
1115     RuntimeLoadedCallbackFnPtr pCallbackFunction)
1116 {
1117     FIXME("%p\n", pCallbackFunction);
1118 
1119     return E_NOTIMPL;
1120 }
1121 
1122 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
1123     REFIID riid, LPVOID *ppUnk)
1124 {
1125     FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
1126 
1127     return E_NOTIMPL;
1128 }
1129 
1130 static HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
1131 {
1132     FIXME("%i: stub\n", iExitCode);
1133 
1134     ExitProcess(iExitCode);
1135 }
1136 
1137 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
1138 {
1139     CLRMetaHost_QueryInterface,
1140     CLRMetaHost_AddRef,
1141     CLRMetaHost_Release,
1142     CLRMetaHost_GetRuntime,
1143     CLRMetaHost_GetVersionFromFile,
1144     CLRMetaHost_EnumerateInstalledRuntimes,
1145     CLRMetaHost_EnumerateLoadedRuntimes,
1146     CLRMetaHost_RequestRuntimeLoadedNotification,
1147     CLRMetaHost_QueryLegacyV2RuntimeBinding,
1148     CLRMetaHost_ExitProcess
1149 };
1150 
1151 static struct CLRMetaHost GlobalCLRMetaHost = {
1152     { &CLRMetaHost_vtbl }
1153 };
1154 
1155 HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1156 {
1157     return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost.ICLRMetaHost_iface, riid, ppobj);
1158 }
1159 
1160 static MonoAssembly* mono_assembly_search_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data)
1161 {
1162     loaded_mono *mono = user_data;
1163     HRESULT hr=S_OK;
1164     MonoAssembly *result=NULL;
1165     char *stringname=NULL;
1166     LPWSTR stringnameW;
1167     int stringnameW_size;
1168     IAssemblyCache *asmcache;
1169     ASSEMBLY_INFO info;
1170     WCHAR path[MAX_PATH];
1171     char *pathA;
1172     MonoImageOpenStatus stat;
1173     static WCHAR fusiondll[] = {'f','u','s','i','o','n',0};
1174     HMODULE hfusion=NULL;
1175     static HRESULT (WINAPI *pCreateAssemblyCache)(IAssemblyCache**,DWORD);
1176 
1177     stringname = mono->mono_stringify_assembly_name(aname);
1178 
1179     TRACE("%s\n", debugstr_a(stringname));
1180 
1181     if (!stringname) return NULL;
1182 
1183     /* FIXME: We should search the given paths before the GAC. */
1184 
1185     if (!pCreateAssemblyCache)
1186     {
1187         hr = LoadLibraryShim(fusiondll, NULL, NULL, &hfusion);
1188 
1189         if (SUCCEEDED(hr))
1190         {
1191             pCreateAssemblyCache = (void*)GetProcAddress(hfusion, "CreateAssemblyCache");
1192             if (!pCreateAssemblyCache)
1193                 hr = E_FAIL;
1194         }
1195     }
1196 
1197     if (SUCCEEDED(hr))
1198         hr = pCreateAssemblyCache(&asmcache, 0);
1199 
1200     if (SUCCEEDED(hr))
1201     {
1202         stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, stringname, -1, NULL, 0);
1203 
1204         stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1205         if (stringnameW)
1206             MultiByteToWideChar(CP_UTF8, 0, stringname, -1, stringnameW, stringnameW_size);
1207         else
1208             hr = E_OUTOFMEMORY;
1209 
1210         if (SUCCEEDED(hr))
1211         {
1212             info.cbAssemblyInfo = sizeof(info);
1213             info.pszCurrentAssemblyPathBuf = path;
1214             info.cchBuf = MAX_PATH;
1215             path[0] = 0;
1216 
1217             hr = IAssemblyCache_QueryAssemblyInfo(asmcache, 0, stringnameW, &info);
1218         }
1219 
1220         HeapFree(GetProcessHeap(), 0, stringnameW);
1221 
1222         IAssemblyCache_Release(asmcache);
1223     }
1224 
1225     if (SUCCEEDED(hr))
1226     {
1227         TRACE("found: %s\n", debugstr_w(path));
1228 
1229         pathA = WtoA(path);
1230 
1231         if (pathA)
1232         {
1233             result = mono->mono_assembly_open(pathA, &stat);
1234 
1235             if (!result)
1236                 ERR("Failed to load %s, status=%u\n", debugstr_w(path), stat);
1237 
1238             HeapFree(GetProcessHeap(), 0, pathA);
1239         }
1240     }
1241 
1242     mono->mono_free(stringname);
1243 
1244     return result;
1245 }
1246 
1247 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1248     DWORD startup_flags, DWORD runtimeinfo_flags, BOOL legacy, ICLRRuntimeInfo **result)
1249 {
1250     static const WCHAR dotconfig[] = {'.','c','o','n','f','i','g',0};
1251     static const DWORD supported_startup_flags = 0;
1252     static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1253     int i;
1254     WCHAR local_version[MAX_PATH];
1255     ULONG local_version_size = MAX_PATH;
1256     WCHAR local_config_file[MAX_PATH];
1257     HRESULT hr;
1258     parsed_config_file parsed_config;
1259 
1260     if (startup_flags & ~supported_startup_flags)
1261         FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
1262 
1263     if (runtimeinfo_flags & ~supported_runtime_flags)
1264         FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
1265 
1266     if (exefile && !config_file)
1267     {
1268         strcpyW(local_config_file, exefile);
1269         strcatW(local_config_file, dotconfig);
1270 
1271         config_file = local_config_file;
1272     }
1273 
1274     if (config_file)
1275     {
1276         int found=0;
1277         hr = parse_config_file(config_file, &parsed_config);
1278 
1279         if (SUCCEEDED(hr))
1280         {
1281             supported_runtime *entry;
1282             LIST_FOR_EACH_ENTRY(entry, &parsed_config.supported_runtimes, supported_runtime, entry)
1283             {
1284                 hr = CLRMetaHost_GetRuntime(0, entry->version, &IID_ICLRRuntimeInfo, (void**)result);
1285                 if (SUCCEEDED(hr))
1286                 {
1287                     found = 1;
1288                     break;
1289                 }
1290             }
1291         }
1292         else
1293         {
1294             WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file), hr);
1295         }
1296 
1297         free_parsed_config_file(&parsed_config);
1298 
1299         if (found)
1300             return S_OK;
1301     }
1302 
1303     if (exefile && !version)
1304     {
1305         hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1306 
1307         version = local_version;
1308 
1309         if (FAILED(hr)) return hr;
1310     }
1311 
1312     if (version)
1313     {
1314         hr = CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1315         if(SUCCEEDED(hr))
1316             return hr;
1317     }
1318 
1319     if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1320     {
1321         DWORD major, minor, build;
1322 
1323         if (version && !parse_runtime_version(version, &major, &minor, &build))
1324         {
1325             ERR("Cannot parse %s\n", debugstr_w(version));
1326             return CLR_E_SHIM_RUNTIME;
1327         }
1328 
1329         find_runtimes();
1330 
1331         if (legacy)
1332             i = 2;
1333         else
1334             i = NUM_RUNTIMES;
1335 
1336         while (i--)
1337         {
1338             if (runtimes[i].mono_abi_version)
1339             {
1340                 /* Must be greater or equal to the version passed in. */
1341                 if (!version || ((runtimes[i].major >= major && runtimes[i].minor >= minor && runtimes[i].build >= build) ||
1342                      (runtimes[i].major >= major && runtimes[i].minor > minor) ||
1343                      (runtimes[i].major > major)))
1344                 {
1345                     return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface,
1346                             &IID_ICLRRuntimeInfo, (void **)result);
1347                 }
1348             }
1349         }
1350 
1351         if (legacy)
1352             missing_runtime_message(&runtimes[1]);
1353         else
1354             missing_runtime_message(&runtimes[NUM_RUNTIMES-1]);
1355 
1356         return CLR_E_SHIM_RUNTIME;
1357     }
1358 
1359     return CLR_E_SHIM_RUNTIME;
1360 }
1361