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