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