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