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