wined3d: Remove IWineD3DResource::UnLoad() from the public interface.
[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.");
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     const struct IEnumUnknownVtbl *Vtbl;
756     LONG ref;
757     ULONG pos;
758 };
759
760 const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
761
762 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface,
763         REFIID riid,
764         void **ppvObject)
765 {
766     TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
767
768     if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
769          IsEqualGUID( riid, &IID_IUnknown ) )
770     {
771         *ppvObject = iface;
772     }
773     else
774     {
775         FIXME("Unsupported interface %s\n", debugstr_guid(riid));
776         return E_NOINTERFACE;
777     }
778
779     IEnumUnknown_AddRef( iface );
780
781     return S_OK;
782 }
783
784 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
785 {
786     struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
787     ULONG ref = InterlockedIncrement(&This->ref);
788
789     TRACE("(%p) refcount=%u\n", iface, ref);
790
791     return ref;
792 }
793
794 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
795 {
796     struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
797     ULONG ref = InterlockedDecrement(&This->ref);
798
799     TRACE("(%p) refcount=%u\n", iface, ref);
800
801     if (ref == 0)
802     {
803         HeapFree(GetProcessHeap(), 0, This);
804     }
805
806     return ref;
807 }
808
809 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
810     IUnknown **rgelt, ULONG *pceltFetched)
811 {
812     struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
813     int num_fetched = 0;
814     HRESULT hr=S_OK;
815     IUnknown *item;
816
817     TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
818
819     while (num_fetched < celt)
820     {
821         if (This->pos >= NUM_RUNTIMES)
822         {
823             hr = S_FALSE;
824             break;
825         }
826         if (runtimes[This->pos].mono_abi_version)
827         {
828             item = (IUnknown*)&runtimes[This->pos];
829             IUnknown_AddRef(item);
830             rgelt[num_fetched] = item;
831             num_fetched++;
832         }
833         This->pos++;
834     }
835
836     if (pceltFetched)
837         *pceltFetched = num_fetched;
838
839     return hr;
840 }
841
842 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
843 {
844     struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
845     int num_fetched = 0;
846     HRESULT hr=S_OK;
847
848     TRACE("(%p,%u)\n", iface, celt);
849
850     while (num_fetched < celt)
851     {
852         if (This->pos >= NUM_RUNTIMES)
853         {
854             hr = S_FALSE;
855             break;
856         }
857         if (runtimes[This->pos].mono_abi_version)
858         {
859             num_fetched++;
860         }
861         This->pos++;
862     }
863
864     return hr;
865 }
866
867 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
868 {
869     struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
870
871     TRACE("(%p)\n", iface);
872
873     This->pos = 0;
874
875     return S_OK;
876 }
877
878 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
879 {
880     struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
881     struct InstalledRuntimeEnum *new_enum;
882
883     TRACE("(%p)\n", iface);
884
885     new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
886     if (!new_enum)
887         return E_OUTOFMEMORY;
888
889     new_enum->Vtbl = &InstalledRuntimeEnum_Vtbl;
890     new_enum->ref = 1;
891     new_enum->pos = This->pos;
892
893     *ppenum = (IEnumUnknown*)new_enum;
894
895     return S_OK;
896 }
897
898 const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
899     InstalledRuntimeEnum_QueryInterface,
900     InstalledRuntimeEnum_AddRef,
901     InstalledRuntimeEnum_Release,
902     InstalledRuntimeEnum_Next,
903     InstalledRuntimeEnum_Skip,
904     InstalledRuntimeEnum_Reset,
905     InstalledRuntimeEnum_Clone
906 };
907
908 struct CLRMetaHost
909 {
910     const struct ICLRMetaHostVtbl *CLRMetaHost_vtbl;
911 };
912
913 static const struct CLRMetaHost GlobalCLRMetaHost;
914
915 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
916         REFIID riid,
917         void **ppvObject)
918 {
919     TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
920
921     if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
922          IsEqualGUID( riid, &IID_IUnknown ) )
923     {
924         *ppvObject = iface;
925     }
926     else
927     {
928         FIXME("Unsupported interface %s\n", debugstr_guid(riid));
929         return E_NOINTERFACE;
930     }
931
932     ICLRMetaHost_AddRef( iface );
933
934     return S_OK;
935 }
936
937 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
938 {
939     return 2;
940 }
941
942 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
943 {
944     return 1;
945 }
946
947 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
948 {
949     *major = 0;
950     *minor = 0;
951     *build = 0;
952
953     if (version[0] == 'v')
954     {
955         version++;
956         if (!isdigit(*version))
957             return FALSE;
958
959         while (isdigit(*version))
960             *major = *major * 10 + (*version++ - '0');
961
962         if (*version == 0)
963             return TRUE;
964
965         if (*version++ != '.' || !isdigit(*version))
966             return FALSE;
967
968         while (isdigit(*version))
969             *minor = *minor * 10 + (*version++ - '0');
970
971         if (*version == 0)
972             return TRUE;
973
974         if (*version++ != '.' || !isdigit(*version))
975             return FALSE;
976
977         while (isdigit(*version))
978             *build = *build * 10 + (*version++ - '0');
979
980         return *version == 0;
981     }
982     else
983         return FALSE;
984 }
985
986 static HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
987     LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
988 {
989     int i;
990     DWORD major, minor, build;
991
992     TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
993
994     if (!pwzVersion)
995         return E_POINTER;
996
997     if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
998     {
999         ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
1000         return CLR_E_SHIM_RUNTIME;
1001     }
1002
1003     find_runtimes();
1004
1005     for (i=0; i<NUM_RUNTIMES; i++)
1006     {
1007         if (runtimes[i].major == major && runtimes[i].minor == minor &&
1008             runtimes[i].build == build)
1009         {
1010             if (runtimes[i].mono_abi_version)
1011                 return IUnknown_QueryInterface((IUnknown*)&runtimes[i], iid, ppRuntime);
1012             else
1013             {
1014                 ERR("Mono is missing %s runtime\n", debugstr_w(pwzVersion));
1015                 return CLR_E_SHIM_RUNTIME;
1016             }
1017         }
1018     }
1019
1020     FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
1021     return CLR_E_SHIM_RUNTIME;
1022 }
1023
1024 HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
1025     LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
1026 {
1027     ASSEMBLY *assembly;
1028     HRESULT hr;
1029     LPSTR version;
1030     ULONG buffer_size=*pcchBuffer;
1031
1032     TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
1033
1034     hr = assembly_create(&assembly, pwzFilePath);
1035
1036     if (SUCCEEDED(hr))
1037     {
1038         hr = assembly_get_runtime_version(assembly, &version);
1039
1040         if (SUCCEEDED(hr))
1041         {
1042             *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
1043
1044             if (pwzBuffer)
1045             {
1046                 if (buffer_size >= *pcchBuffer)
1047                     MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
1048                 else
1049                     hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
1050             }
1051         }
1052
1053         assembly_release(assembly);
1054     }
1055
1056     return hr;
1057 }
1058
1059 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
1060     IEnumUnknown **ppEnumerator)
1061 {
1062     struct InstalledRuntimeEnum *new_enum;
1063
1064     TRACE("%p\n", ppEnumerator);
1065
1066     find_runtimes();
1067
1068     new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
1069     if (!new_enum)
1070         return E_OUTOFMEMORY;
1071
1072     new_enum->Vtbl = &InstalledRuntimeEnum_Vtbl;
1073     new_enum->ref = 1;
1074     new_enum->pos = 0;
1075
1076     *ppEnumerator = (IEnumUnknown*)new_enum;
1077
1078     return S_OK;
1079 }
1080
1081 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
1082     HANDLE hndProcess, IEnumUnknown **ppEnumerator)
1083 {
1084     FIXME("%p %p\n", hndProcess, ppEnumerator);
1085
1086     return E_NOTIMPL;
1087 }
1088
1089 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
1090     RuntimeLoadedCallbackFnPtr pCallbackFunction)
1091 {
1092     FIXME("%p\n", pCallbackFunction);
1093
1094     return E_NOTIMPL;
1095 }
1096
1097 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
1098     REFIID riid, LPVOID *ppUnk)
1099 {
1100     FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
1101
1102     return E_NOTIMPL;
1103 }
1104
1105 static HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
1106 {
1107     FIXME("%i: stub\n", iExitCode);
1108
1109     ExitProcess(iExitCode);
1110 }
1111
1112 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
1113 {
1114     CLRMetaHost_QueryInterface,
1115     CLRMetaHost_AddRef,
1116     CLRMetaHost_Release,
1117     CLRMetaHost_GetRuntime,
1118     CLRMetaHost_GetVersionFromFile,
1119     CLRMetaHost_EnumerateInstalledRuntimes,
1120     CLRMetaHost_EnumerateLoadedRuntimes,
1121     CLRMetaHost_RequestRuntimeLoadedNotification,
1122     CLRMetaHost_QueryLegacyV2RuntimeBinding,
1123     CLRMetaHost_ExitProcess
1124 };
1125
1126 static const struct CLRMetaHost GlobalCLRMetaHost = {
1127     &CLRMetaHost_vtbl
1128 };
1129
1130 extern HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1131 {
1132     return ICLRMetaHost_QueryInterface((ICLRMetaHost*)&GlobalCLRMetaHost, riid, ppobj);
1133 }
1134
1135 static MonoAssembly* mono_assembly_search_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data)
1136 {
1137     loaded_mono *mono = user_data;
1138     HRESULT hr=S_OK;
1139     MonoAssembly *result=NULL;
1140     char *stringname=NULL;
1141     LPWSTR stringnameW;
1142     int stringnameW_size;
1143     IAssemblyCache *asmcache;
1144     ASSEMBLY_INFO info;
1145     WCHAR path[MAX_PATH];
1146     char *pathA;
1147     MonoImageOpenStatus stat;
1148     static WCHAR fusiondll[] = {'f','u','s','i','o','n',0};
1149     HMODULE hfusion=NULL;
1150     static HRESULT WINAPI (*pCreateAssemblyCache)(IAssemblyCache**,DWORD);
1151
1152     stringname = mono->mono_stringify_assembly_name(aname);
1153
1154     TRACE("%s\n", debugstr_a(stringname));
1155
1156     if (!stringname) return NULL;
1157
1158     /* FIXME: We should search the given paths before the GAC. */
1159
1160     if (!pCreateAssemblyCache)
1161     {
1162         hr = LoadLibraryShim(fusiondll, NULL, NULL, &hfusion);
1163
1164         if (SUCCEEDED(hr))
1165         {
1166             pCreateAssemblyCache = (void*)GetProcAddress(hfusion, "CreateAssemblyCache");
1167             if (!pCreateAssemblyCache)
1168                 hr = E_FAIL;
1169         }
1170     }
1171
1172     if (SUCCEEDED(hr))
1173         hr = pCreateAssemblyCache(&asmcache, 0);
1174
1175     if (SUCCEEDED(hr))
1176     {
1177         stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, stringname, -1, NULL, 0);
1178
1179         stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1180         if (stringnameW)
1181             MultiByteToWideChar(CP_UTF8, 0, stringname, -1, stringnameW, stringnameW_size);
1182         else
1183             hr = E_OUTOFMEMORY;
1184
1185         if (SUCCEEDED(hr))
1186         {
1187             info.cbAssemblyInfo = sizeof(info);
1188             info.pszCurrentAssemblyPathBuf = path;
1189             info.cchBuf = MAX_PATH;
1190             path[0] = 0;
1191
1192             hr = IAssemblyCache_QueryAssemblyInfo(asmcache, 0, stringnameW, &info);
1193         }
1194
1195         HeapFree(GetProcessHeap(), 0, stringnameW);
1196
1197         IAssemblyCache_Release(asmcache);
1198     }
1199
1200     if (SUCCEEDED(hr))
1201     {
1202         TRACE("found: %s\n", debugstr_w(path));
1203
1204         pathA = WtoA(path);
1205
1206         if (pathA)
1207         {
1208             result = mono->mono_assembly_open(pathA, &stat);
1209
1210             if (!result)
1211                 ERR("Failed to load %s, status=%u\n", debugstr_w(path), stat);
1212
1213             HeapFree(GetProcessHeap(), 0, pathA);
1214         }
1215     }
1216
1217     mono->mono_free(stringname);
1218
1219     return result;
1220 }
1221
1222 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1223     DWORD startup_flags, DWORD runtimeinfo_flags, BOOL legacy, ICLRRuntimeInfo **result)
1224 {
1225     static const WCHAR dotconfig[] = {'.','c','o','n','f','i','g',0};
1226     static const DWORD supported_startup_flags = 0;
1227     static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1228     int i;
1229     WCHAR local_version[MAX_PATH];
1230     ULONG local_version_size = MAX_PATH;
1231     WCHAR local_config_file[MAX_PATH];
1232     HRESULT hr;
1233     parsed_config_file parsed_config;
1234
1235     if (startup_flags & ~supported_startup_flags)
1236         FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
1237
1238     if (runtimeinfo_flags & ~supported_runtime_flags)
1239         FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
1240
1241     if (exefile && !config_file)
1242     {
1243         strcpyW(local_config_file, exefile);
1244         strcatW(local_config_file, dotconfig);
1245
1246         config_file = local_config_file;
1247     }
1248
1249     if (config_file)
1250     {
1251         int found=0;
1252         hr = parse_config_file(config_file, &parsed_config);
1253
1254         if (SUCCEEDED(hr))
1255         {
1256             supported_runtime *entry;
1257             LIST_FOR_EACH_ENTRY(entry, &parsed_config.supported_runtimes, supported_runtime, entry)
1258             {
1259                 hr = CLRMetaHost_GetRuntime(0, entry->version, &IID_ICLRRuntimeInfo, (void**)result);
1260                 if (SUCCEEDED(hr))
1261                 {
1262                     found = 1;
1263                     break;
1264                 }
1265             }
1266         }
1267         else
1268         {
1269             WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file), hr);
1270         }
1271
1272         free_parsed_config_file(&parsed_config);
1273
1274         if (found)
1275             return S_OK;
1276     }
1277
1278     if (exefile && !version)
1279     {
1280         hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1281
1282         version = local_version;
1283
1284         if (FAILED(hr)) return hr;
1285     }
1286
1287     if (version)
1288     {
1289         return CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1290     }
1291
1292     if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1293     {
1294         find_runtimes();
1295
1296         if (legacy)
1297             i = 2;
1298         else
1299             i = NUM_RUNTIMES;
1300
1301         while (i--)
1302         {
1303             if (runtimes[i].mono_abi_version)
1304                 return IUnknown_QueryInterface((IUnknown*)&runtimes[i],
1305                     &IID_ICLRRuntimeInfo, (void**)result);
1306         }
1307
1308         ERR("No %s.NET runtime installed\n", legacy ? "legacy " : "");
1309
1310         return CLR_E_SHIM_RUNTIME;
1311     }
1312
1313     return CLR_E_SHIM_RUNTIME;
1314 }
1315
1316 HRESULT force_get_runtime_info(ICLRRuntimeInfo **result)
1317 {
1318     return IUnknown_QueryInterface((IUnknown*)&runtimes[0], &IID_ICLRRuntimeInfo, (void**)result);
1319 }