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