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