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