mscoree: Mark internal symbols with hidden visibility and some vtables as static.
[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 HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
315         REFIID riid,
316         void **ppvObject)
317 {
318     TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
319
320     if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
321          IsEqualGUID( riid, &IID_IUnknown ) )
322     {
323         *ppvObject = iface;
324     }
325     else
326     {
327         FIXME("Unsupported interface %s\n", debugstr_guid(riid));
328         return E_NOINTERFACE;
329     }
330
331     ICLRRuntimeInfo_AddRef( iface );
332
333     return S_OK;
334 }
335
336 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
337 {
338     return 2;
339 }
340
341 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
342 {
343     return 1;
344 }
345
346 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
347     LPWSTR pwzBuffer, DWORD *pcchBuffer)
348 {
349     struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
350     DWORD buffer_size = *pcchBuffer;
351     HRESULT hr = S_OK;
352     char version[11];
353     DWORD size;
354
355     TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
356
357     size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
358
359     assert(size <= sizeof(version));
360
361     *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
362
363     if (pwzBuffer)
364     {
365         if (buffer_size >= *pcchBuffer)
366             MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
367         else
368             hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
369     }
370
371     return hr;
372 }
373
374 static BOOL get_install_root(LPWSTR install_dir)
375 {
376     const WCHAR dotnet_key[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t','\\','.','N','E','T','F','r','a','m','e','w','o','r','k','\\',0};
377     const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
378
379     DWORD len;
380     HKEY key;
381
382     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
383         return FALSE;
384
385     len = MAX_PATH;
386     if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
387     {
388         RegCloseKey(key);
389         return FALSE;
390     }
391     RegCloseKey(key);
392
393     return TRUE;
394 }
395
396 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
397     LPWSTR pwzBuffer, DWORD *pcchBuffer)
398 {
399     static const WCHAR slash[] = {'\\',0};
400     DWORD buffer_size = *pcchBuffer;
401     WCHAR system_dir[MAX_PATH];
402     WCHAR version[MAX_PATH];
403     DWORD version_size, size;
404     HRESULT hr = S_OK;
405
406     TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
407
408     if (!get_install_root(system_dir))
409     {
410         ERR("error reading registry key for installroot\n");
411         return E_FAIL;
412     }
413     else
414     {
415         version_size = MAX_PATH;
416         ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
417         lstrcatW(system_dir, version);
418         lstrcatW(system_dir, slash);
419         size = lstrlenW(system_dir) + 1;
420     }
421
422     *pcchBuffer = size;
423
424     if (pwzBuffer)
425     {
426         if (buffer_size >= size)
427             strcpyW(pwzBuffer, system_dir);
428         else
429             hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
430     }
431
432     return hr;
433 }
434
435 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
436     HANDLE hndProcess, BOOL *pbLoaded)
437 {
438     FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
439
440     return E_NOTIMPL;
441 }
442
443 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
444     UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
445 {
446     FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
447
448     return E_NOTIMPL;
449 }
450
451 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
452     LPCWSTR pwzDllName, HMODULE *phndModule)
453 {
454     WCHAR version[MAX_PATH];
455     HRESULT hr;
456     DWORD cchBuffer;
457
458     TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
459
460     cchBuffer = MAX_PATH;
461     hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
462     if (FAILED(hr)) return hr;
463
464     return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
465 }
466
467 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
468     LPCSTR pszProcName, LPVOID *ppProc)
469 {
470     FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
471
472     return E_NOTIMPL;
473 }
474
475 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
476     REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
477 {
478     struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
479     RuntimeHost *host;
480     HRESULT hr;
481
482     TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
483
484     hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
485
486     if (SUCCEEDED(hr))
487         hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
488
489     return hr;
490 }
491
492 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
493     BOOL *pbLoadable)
494 {
495     FIXME("%p %p\n", iface, pbLoadable);
496
497     return E_NOTIMPL;
498 }
499
500 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
501     DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
502 {
503     FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
504
505     return E_NOTIMPL;
506 }
507
508 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
509     DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
510 {
511     FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
512
513     return E_NOTIMPL;
514 }
515
516 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
517 {
518     FIXME("%p\n", iface);
519
520     return E_NOTIMPL;
521 }
522
523 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
524     BOOL *pbStarted, DWORD *pdwStartupFlags)
525 {
526     FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
527
528     return E_NOTIMPL;
529 }
530
531 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
532     CLRRuntimeInfo_QueryInterface,
533     CLRRuntimeInfo_AddRef,
534     CLRRuntimeInfo_Release,
535     CLRRuntimeInfo_GetVersionString,
536     CLRRuntimeInfo_GetRuntimeDirectory,
537     CLRRuntimeInfo_IsLoaded,
538     CLRRuntimeInfo_LoadErrorString,
539     CLRRuntimeInfo_LoadLibrary,
540     CLRRuntimeInfo_GetProcAddress,
541     CLRRuntimeInfo_GetInterface,
542     CLRRuntimeInfo_IsLoadable,
543     CLRRuntimeInfo_SetDefaultStartupFlags,
544     CLRRuntimeInfo_GetDefaultStartupFlags,
545     CLRRuntimeInfo_BindAsLegacyV2Runtime,
546     CLRRuntimeInfo_IsStarted
547 };
548
549 HRESULT ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo *iface, RuntimeHost **result)
550 {
551     struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
552
553     assert(This->ICLRRuntimeInfo_vtbl == &CLRRuntimeInfoVtbl);
554
555     return CLRRuntimeInfo_GetRuntimeHost(This, result);
556 }
557
558 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version)
559 {
560     static const WCHAR mono_dll[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
561     static const WCHAR libmono_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
562     static const WCHAR mono2_dll[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
563     static const WCHAR libmono2_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
564     DWORD attributes=INVALID_FILE_ATTRIBUTES;
565
566     if (abi_version == 1)
567     {
568         strcpyW(dll_path, path);
569         strcatW(dll_path, mono_dll);
570         attributes = GetFileAttributesW(dll_path);
571
572         if (attributes == INVALID_FILE_ATTRIBUTES)
573         {
574             strcpyW(dll_path, path);
575             strcatW(dll_path, libmono_dll);
576             attributes = GetFileAttributesW(dll_path);
577         }
578     }
579     else if (abi_version == 2)
580     {
581         strcpyW(dll_path, path);
582         strcatW(dll_path, mono2_dll);
583         attributes = GetFileAttributesW(dll_path);
584
585         if (attributes == INVALID_FILE_ATTRIBUTES)
586         {
587             strcpyW(dll_path, path);
588             strcatW(dll_path, libmono2_dll);
589             attributes = GetFileAttributesW(dll_path);
590         }
591     }
592
593     return (attributes != INVALID_FILE_ATTRIBUTES);
594 }
595
596 static BOOL get_mono_path_from_registry(LPWSTR path, int abi_version)
597 {
598     static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
599     static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
600     static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
601     static const WCHAR slash[] = {'\\',0};
602
603     WCHAR version[64], version_key[MAX_PATH];
604     DWORD len;
605     HKEY key;
606     WCHAR dll_path[MAX_PATH];
607
608     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
609         return FALSE;
610
611     len = sizeof(version);
612     if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
613     {
614         RegCloseKey(key);
615         return FALSE;
616     }
617     RegCloseKey(key);
618
619     lstrcpyW(version_key, mono_key);
620     lstrcatW(version_key, slash);
621     lstrcatW(version_key, version);
622
623     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
624         return FALSE;
625
626     len = sizeof(WCHAR) * MAX_PATH;
627     if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
628     {
629         RegCloseKey(key);
630         return FALSE;
631     }
632     RegCloseKey(key);
633
634     return find_mono_dll(path, dll_path, abi_version);
635 }
636
637 static BOOL get_mono_path_from_folder(LPCWSTR folder, LPWSTR mono_path, int abi_version)
638 {
639     static const WCHAR mono_one_dot_zero[] = {'\\','m','o','n','o','-','1','.','0', 0};
640     static const WCHAR mono_two_dot_zero[] = {'\\','m','o','n','o','-','2','.','0', 0};
641     WCHAR mono_dll_path[MAX_PATH];
642     BOOL found = FALSE;
643
644     strcpyW(mono_path, folder);
645
646     if (abi_version == 1)
647         strcatW(mono_path, mono_one_dot_zero);
648     else if (abi_version == 2)
649         strcatW(mono_path, mono_two_dot_zero);
650
651     found = find_mono_dll(mono_path, mono_dll_path, abi_version);
652
653     return found;
654 }
655
656 static BOOL get_mono_path(LPWSTR path, int abi_version)
657 {
658     static const WCHAR subdir_mono[] = {'\\','m','o','n','o',0};
659     static const WCHAR sibling_mono[] = {'\\','.','.','\\','m','o','n','o',0};
660     WCHAR base_path[MAX_PATH];
661     const char *unix_data_dir;
662     WCHAR *dos_data_dir;
663     int build_tree=0;
664     static WCHAR* (CDECL *wine_get_dos_file_name)(const char*);
665
666     /* First try c:\windows\mono */
667     GetWindowsDirectoryW(base_path, MAX_PATH);
668     strcatW(base_path, subdir_mono);
669
670     if (get_mono_path_from_folder(base_path, path, abi_version))
671         return TRUE;
672
673     /* Next: /usr/share/wine/mono */
674     unix_data_dir = wine_get_data_dir();
675
676     if (!unix_data_dir)
677     {
678         unix_data_dir = wine_get_build_dir();
679         build_tree = 1;
680     }
681
682     if (unix_data_dir)
683     {
684         if (!wine_get_dos_file_name)
685             wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
686
687         if (wine_get_dos_file_name)
688         {
689             dos_data_dir = wine_get_dos_file_name(unix_data_dir);
690
691             if (dos_data_dir)
692             {
693                 strcpyW(base_path, dos_data_dir);
694                 strcatW(base_path, build_tree ? sibling_mono : subdir_mono);
695
696                 HeapFree(GetProcessHeap(), 0, dos_data_dir);
697
698                 if (get_mono_path_from_folder(base_path, path, abi_version))
699                     return TRUE;
700             }
701         }
702     }
703
704     /* Last: the registry */
705     return get_mono_path_from_registry(path, abi_version);
706 }
707
708 static void find_runtimes(void)
709 {
710     int abi_version, i;
711     static const WCHAR libmono[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
712     static const WCHAR mscorlib[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
713     WCHAR mono_path[MAX_PATH], lib_path[MAX_PATH];
714     BOOL any_runtimes_found = FALSE;
715
716     if (runtimes_initialized) return;
717
718     EnterCriticalSection(&runtime_list_cs);
719
720     if (runtimes_initialized) goto end;
721
722     for (abi_version=NUM_ABI_VERSIONS; abi_version>0; abi_version--)
723     {
724         if (!get_mono_path(mono_path, abi_version))
725             continue;
726
727         for (i=0; i<NUM_RUNTIMES; i++)
728         {
729             if (runtimes[i].mono_abi_version == 0)
730             {
731                 strcpyW(lib_path, mono_path);
732                 strcatW(lib_path, libmono);
733                 strcatW(lib_path, runtimes[i].mono_libdir);
734                 strcatW(lib_path, mscorlib);
735
736                 if (GetFileAttributesW(lib_path) != INVALID_FILE_ATTRIBUTES)
737                 {
738                     runtimes[i].mono_abi_version = abi_version;
739
740                     strcpyW(runtimes[i].mono_path, mono_path);
741                     strcpyW(runtimes[i].mscorlib_path, lib_path);
742
743                     any_runtimes_found = TRUE;
744                 }
745             }
746         }
747     }
748
749     if (!any_runtimes_found)
750     {
751         /* Report all runtimes are available if Mono isn't installed.
752          * FIXME: Remove this when Mono is properly packaged. */
753         for (i=0; i<NUM_RUNTIMES; i++)
754             runtimes[i].mono_abi_version = -1;
755     }
756
757     runtimes_initialized = 1;
758
759 end:
760     LeaveCriticalSection(&runtime_list_cs);
761 }
762
763 struct InstalledRuntimeEnum
764 {
765     IEnumUnknown IEnumUnknown_iface;
766     LONG ref;
767     ULONG pos;
768 };
769
770 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
771
772 static inline struct InstalledRuntimeEnum *impl_from_IEnumUnknown(IEnumUnknown *iface)
773 {
774     return CONTAINING_RECORD(iface, struct InstalledRuntimeEnum, IEnumUnknown_iface);
775 }
776
777 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface, REFIID riid,
778         void **ppvObject)
779 {
780     TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
781
782     if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
783          IsEqualGUID( riid, &IID_IUnknown ) )
784     {
785         *ppvObject = iface;
786     }
787     else
788     {
789         FIXME("Unsupported interface %s\n", debugstr_guid(riid));
790         return E_NOINTERFACE;
791     }
792
793     IEnumUnknown_AddRef( iface );
794
795     return S_OK;
796 }
797
798 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
799 {
800     struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
801     ULONG ref = InterlockedIncrement(&This->ref);
802
803     TRACE("(%p) refcount=%u\n", iface, ref);
804
805     return ref;
806 }
807
808 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
809 {
810     struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
811     ULONG ref = InterlockedDecrement(&This->ref);
812
813     TRACE("(%p) refcount=%u\n", iface, ref);
814
815     if (ref == 0)
816     {
817         HeapFree(GetProcessHeap(), 0, This);
818     }
819
820     return ref;
821 }
822
823 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
824     IUnknown **rgelt, ULONG *pceltFetched)
825 {
826     struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
827     int num_fetched = 0;
828     HRESULT hr=S_OK;
829     IUnknown *item;
830
831     TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
832
833     while (num_fetched < celt)
834     {
835         if (This->pos >= NUM_RUNTIMES)
836         {
837             hr = S_FALSE;
838             break;
839         }
840         if (runtimes[This->pos].mono_abi_version)
841         {
842             item = (IUnknown*)&runtimes[This->pos];
843             IUnknown_AddRef(item);
844             rgelt[num_fetched] = item;
845             num_fetched++;
846         }
847         This->pos++;
848     }
849
850     if (pceltFetched)
851         *pceltFetched = num_fetched;
852
853     return hr;
854 }
855
856 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
857 {
858     struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
859     int num_fetched = 0;
860     HRESULT hr=S_OK;
861
862     TRACE("(%p,%u)\n", iface, celt);
863
864     while (num_fetched < celt)
865     {
866         if (This->pos >= NUM_RUNTIMES)
867         {
868             hr = S_FALSE;
869             break;
870         }
871         if (runtimes[This->pos].mono_abi_version)
872         {
873             num_fetched++;
874         }
875         This->pos++;
876     }
877
878     return hr;
879 }
880
881 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
882 {
883     struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
884
885     TRACE("(%p)\n", iface);
886
887     This->pos = 0;
888
889     return S_OK;
890 }
891
892 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
893 {
894     struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
895     struct InstalledRuntimeEnum *new_enum;
896
897     TRACE("(%p)\n", iface);
898
899     new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
900     if (!new_enum)
901         return E_OUTOFMEMORY;
902
903     new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
904     new_enum->ref = 1;
905     new_enum->pos = This->pos;
906
907     *ppenum = &new_enum->IEnumUnknown_iface;
908
909     return S_OK;
910 }
911
912 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
913     InstalledRuntimeEnum_QueryInterface,
914     InstalledRuntimeEnum_AddRef,
915     InstalledRuntimeEnum_Release,
916     InstalledRuntimeEnum_Next,
917     InstalledRuntimeEnum_Skip,
918     InstalledRuntimeEnum_Reset,
919     InstalledRuntimeEnum_Clone
920 };
921
922 struct CLRMetaHost
923 {
924     ICLRMetaHost ICLRMetaHost_iface;
925 };
926
927 static struct CLRMetaHost GlobalCLRMetaHost;
928
929 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
930         REFIID riid,
931         void **ppvObject)
932 {
933     TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
934
935     if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
936          IsEqualGUID( riid, &IID_IUnknown ) )
937     {
938         *ppvObject = iface;
939     }
940     else
941     {
942         FIXME("Unsupported interface %s\n", debugstr_guid(riid));
943         return E_NOINTERFACE;
944     }
945
946     ICLRMetaHost_AddRef( iface );
947
948     return S_OK;
949 }
950
951 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
952 {
953     return 2;
954 }
955
956 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
957 {
958     return 1;
959 }
960
961 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
962 {
963     *major = 0;
964     *minor = 0;
965     *build = 0;
966
967     if (version[0] == 'v')
968     {
969         version++;
970         if (!isdigit(*version))
971             return FALSE;
972
973         while (isdigit(*version))
974             *major = *major * 10 + (*version++ - '0');
975
976         if (*version == 0)
977             return TRUE;
978
979         if (*version++ != '.' || !isdigit(*version))
980             return FALSE;
981
982         while (isdigit(*version))
983             *minor = *minor * 10 + (*version++ - '0');
984
985         if (*version == 0)
986             return TRUE;
987
988         if (*version++ != '.' || !isdigit(*version))
989             return FALSE;
990
991         while (isdigit(*version))
992             *build = *build * 10 + (*version++ - '0');
993
994         return *version == 0;
995     }
996     else
997         return FALSE;
998 }
999
1000 static HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
1001     LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
1002 {
1003     int i;
1004     DWORD major, minor, build;
1005
1006     TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
1007
1008     if (!pwzVersion)
1009         return E_POINTER;
1010
1011     if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
1012     {
1013         ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
1014         return CLR_E_SHIM_RUNTIME;
1015     }
1016
1017     find_runtimes();
1018
1019     for (i=0; i<NUM_RUNTIMES; i++)
1020     {
1021         if (runtimes[i].major == major && runtimes[i].minor == minor &&
1022             runtimes[i].build == build)
1023         {
1024             if (runtimes[i].mono_abi_version)
1025                 return IUnknown_QueryInterface((IUnknown*)&runtimes[i], iid, ppRuntime);
1026             else
1027             {
1028                 missing_runtime_message(&runtimes[i]);
1029                 return CLR_E_SHIM_RUNTIME;
1030             }
1031         }
1032     }
1033
1034     FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
1035     return CLR_E_SHIM_RUNTIME;
1036 }
1037
1038 HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
1039     LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
1040 {
1041     ASSEMBLY *assembly;
1042     HRESULT hr;
1043     LPSTR version;
1044     ULONG buffer_size=*pcchBuffer;
1045
1046     TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
1047
1048     hr = assembly_create(&assembly, pwzFilePath);
1049
1050     if (SUCCEEDED(hr))
1051     {
1052         hr = assembly_get_runtime_version(assembly, &version);
1053
1054         if (SUCCEEDED(hr))
1055         {
1056             *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
1057
1058             if (pwzBuffer)
1059             {
1060                 if (buffer_size >= *pcchBuffer)
1061                     MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
1062                 else
1063                     hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
1064             }
1065         }
1066
1067         assembly_release(assembly);
1068     }
1069
1070     return hr;
1071 }
1072
1073 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
1074     IEnumUnknown **ppEnumerator)
1075 {
1076     struct InstalledRuntimeEnum *new_enum;
1077
1078     TRACE("%p\n", ppEnumerator);
1079
1080     find_runtimes();
1081
1082     new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
1083     if (!new_enum)
1084         return E_OUTOFMEMORY;
1085
1086     new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
1087     new_enum->ref = 1;
1088     new_enum->pos = 0;
1089
1090     *ppEnumerator = &new_enum->IEnumUnknown_iface;
1091
1092     return S_OK;
1093 }
1094
1095 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
1096     HANDLE hndProcess, IEnumUnknown **ppEnumerator)
1097 {
1098     FIXME("%p %p\n", hndProcess, ppEnumerator);
1099
1100     return E_NOTIMPL;
1101 }
1102
1103 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
1104     RuntimeLoadedCallbackFnPtr pCallbackFunction)
1105 {
1106     FIXME("%p\n", pCallbackFunction);
1107
1108     return E_NOTIMPL;
1109 }
1110
1111 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
1112     REFIID riid, LPVOID *ppUnk)
1113 {
1114     FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
1115
1116     return E_NOTIMPL;
1117 }
1118
1119 static HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
1120 {
1121     FIXME("%i: stub\n", iExitCode);
1122
1123     ExitProcess(iExitCode);
1124 }
1125
1126 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
1127 {
1128     CLRMetaHost_QueryInterface,
1129     CLRMetaHost_AddRef,
1130     CLRMetaHost_Release,
1131     CLRMetaHost_GetRuntime,
1132     CLRMetaHost_GetVersionFromFile,
1133     CLRMetaHost_EnumerateInstalledRuntimes,
1134     CLRMetaHost_EnumerateLoadedRuntimes,
1135     CLRMetaHost_RequestRuntimeLoadedNotification,
1136     CLRMetaHost_QueryLegacyV2RuntimeBinding,
1137     CLRMetaHost_ExitProcess
1138 };
1139
1140 static struct CLRMetaHost GlobalCLRMetaHost = {
1141     { &CLRMetaHost_vtbl }
1142 };
1143
1144 extern HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1145 {
1146     return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost.ICLRMetaHost_iface, riid, ppobj);
1147 }
1148
1149 static MonoAssembly* mono_assembly_search_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data)
1150 {
1151     loaded_mono *mono = user_data;
1152     HRESULT hr=S_OK;
1153     MonoAssembly *result=NULL;
1154     char *stringname=NULL;
1155     LPWSTR stringnameW;
1156     int stringnameW_size;
1157     IAssemblyCache *asmcache;
1158     ASSEMBLY_INFO info;
1159     WCHAR path[MAX_PATH];
1160     char *pathA;
1161     MonoImageOpenStatus stat;
1162     static WCHAR fusiondll[] = {'f','u','s','i','o','n',0};
1163     HMODULE hfusion=NULL;
1164     static HRESULT WINAPI (*pCreateAssemblyCache)(IAssemblyCache**,DWORD);
1165
1166     stringname = mono->mono_stringify_assembly_name(aname);
1167
1168     TRACE("%s\n", debugstr_a(stringname));
1169
1170     if (!stringname) return NULL;
1171
1172     /* FIXME: We should search the given paths before the GAC. */
1173
1174     if (!pCreateAssemblyCache)
1175     {
1176         hr = LoadLibraryShim(fusiondll, NULL, NULL, &hfusion);
1177
1178         if (SUCCEEDED(hr))
1179         {
1180             pCreateAssemblyCache = (void*)GetProcAddress(hfusion, "CreateAssemblyCache");
1181             if (!pCreateAssemblyCache)
1182                 hr = E_FAIL;
1183         }
1184     }
1185
1186     if (SUCCEEDED(hr))
1187         hr = pCreateAssemblyCache(&asmcache, 0);
1188
1189     if (SUCCEEDED(hr))
1190     {
1191         stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, stringname, -1, NULL, 0);
1192
1193         stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1194         if (stringnameW)
1195             MultiByteToWideChar(CP_UTF8, 0, stringname, -1, stringnameW, stringnameW_size);
1196         else
1197             hr = E_OUTOFMEMORY;
1198
1199         if (SUCCEEDED(hr))
1200         {
1201             info.cbAssemblyInfo = sizeof(info);
1202             info.pszCurrentAssemblyPathBuf = path;
1203             info.cchBuf = MAX_PATH;
1204             path[0] = 0;
1205
1206             hr = IAssemblyCache_QueryAssemblyInfo(asmcache, 0, stringnameW, &info);
1207         }
1208
1209         HeapFree(GetProcessHeap(), 0, stringnameW);
1210
1211         IAssemblyCache_Release(asmcache);
1212     }
1213
1214     if (SUCCEEDED(hr))
1215     {
1216         TRACE("found: %s\n", debugstr_w(path));
1217
1218         pathA = WtoA(path);
1219
1220         if (pathA)
1221         {
1222             result = mono->mono_assembly_open(pathA, &stat);
1223
1224             if (!result)
1225                 ERR("Failed to load %s, status=%u\n", debugstr_w(path), stat);
1226
1227             HeapFree(GetProcessHeap(), 0, pathA);
1228         }
1229     }
1230
1231     mono->mono_free(stringname);
1232
1233     return result;
1234 }
1235
1236 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1237     DWORD startup_flags, DWORD runtimeinfo_flags, BOOL legacy, ICLRRuntimeInfo **result)
1238 {
1239     static const WCHAR dotconfig[] = {'.','c','o','n','f','i','g',0};
1240     static const DWORD supported_startup_flags = 0;
1241     static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1242     int i;
1243     WCHAR local_version[MAX_PATH];
1244     ULONG local_version_size = MAX_PATH;
1245     WCHAR local_config_file[MAX_PATH];
1246     HRESULT hr;
1247     parsed_config_file parsed_config;
1248
1249     if (startup_flags & ~supported_startup_flags)
1250         FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
1251
1252     if (runtimeinfo_flags & ~supported_runtime_flags)
1253         FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
1254
1255     if (exefile && !config_file)
1256     {
1257         strcpyW(local_config_file, exefile);
1258         strcatW(local_config_file, dotconfig);
1259
1260         config_file = local_config_file;
1261     }
1262
1263     if (config_file)
1264     {
1265         int found=0;
1266         hr = parse_config_file(config_file, &parsed_config);
1267
1268         if (SUCCEEDED(hr))
1269         {
1270             supported_runtime *entry;
1271             LIST_FOR_EACH_ENTRY(entry, &parsed_config.supported_runtimes, supported_runtime, entry)
1272             {
1273                 hr = CLRMetaHost_GetRuntime(0, entry->version, &IID_ICLRRuntimeInfo, (void**)result);
1274                 if (SUCCEEDED(hr))
1275                 {
1276                     found = 1;
1277                     break;
1278                 }
1279             }
1280         }
1281         else
1282         {
1283             WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file), hr);
1284         }
1285
1286         free_parsed_config_file(&parsed_config);
1287
1288         if (found)
1289             return S_OK;
1290     }
1291
1292     if (exefile && !version)
1293     {
1294         hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1295
1296         version = local_version;
1297
1298         if (FAILED(hr)) return hr;
1299     }
1300
1301     if (version)
1302     {
1303         return CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1304     }
1305
1306     if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1307     {
1308         find_runtimes();
1309
1310         if (legacy)
1311             i = 2;
1312         else
1313             i = NUM_RUNTIMES;
1314
1315         while (i--)
1316         {
1317             if (runtimes[i].mono_abi_version)
1318                 return IUnknown_QueryInterface((IUnknown*)&runtimes[i],
1319                     &IID_ICLRRuntimeInfo, (void**)result);
1320         }
1321
1322         if (legacy)
1323             missing_runtime_message(&runtimes[1]);
1324         else
1325             missing_runtime_message(&runtimes[NUM_RUNTIMES-1]);
1326
1327         return CLR_E_SHIM_RUNTIME;
1328     }
1329
1330     return CLR_E_SHIM_RUNTIME;
1331 }