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