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