dmime: Don't set DSBCAPS_CTRLPAN together with DSBCAPS_CTRL3D.
[wine.git] / dlls / mscoree / metahost.c
blob6b53cde4b7ae7dab0aab6a06b4402b9ab2322c6e
1 /*
2 * ICLRMetaHost - discovery and management of available .NET runtimes
4 * Copyright 2010 Vincent Povirk for CodeWeavers
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.
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.
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
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <assert.h>
25 #define COBJMACROS
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"
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"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL( mscoree );
48 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl;
50 #define NUM_RUNTIMES 4
52 static struct CLRRuntimeInfo runtimes[NUM_RUNTIMES] = {
53 {{&CLRRuntimeInfoVtbl}, 1, 0, 3705, 0},
54 {{&CLRRuntimeInfoVtbl}, 1, 1, 4322, 0},
55 {{&CLRRuntimeInfoVtbl}, 2, 0, 50727, 0},
56 {{&CLRRuntimeInfoVtbl}, 4, 0, 30319, 0}
59 static CRITICAL_SECTION runtime_list_cs;
60 static CRITICAL_SECTION_DEBUG runtime_list_cs_debug =
62 0, 0, &runtime_list_cs,
63 { &runtime_list_cs_debug.ProcessLocksList,
64 &runtime_list_cs_debug.ProcessLocksList },
65 0, 0, { (DWORD_PTR)(__FILE__ ": runtime_list_cs") }
67 static CRITICAL_SECTION runtime_list_cs = { &runtime_list_cs_debug, -1, 0, 0, 0, 0 };
69 static HMODULE mono_handle;
71 BOOL is_mono_started;
72 static BOOL is_mono_shutdown;
74 MonoImage* (CDECL *mono_assembly_get_image)(MonoAssembly *assembly);
75 MonoAssembly* (CDECL *mono_assembly_load_from)(MonoImage *image, const char *fname, MonoImageOpenStatus *status);
76 MonoAssembly* (CDECL *mono_assembly_open)(const char *filename, MonoImageOpenStatus *status);
77 MonoClass* (CDECL *mono_class_from_mono_type)(MonoType *type);
78 MonoClass* (CDECL *mono_class_from_name)(MonoImage *image, const char* name_space, const char *name);
79 MonoMethod* (CDECL *mono_class_get_method_from_name)(MonoClass *klass, const char *name, int param_count);
80 static void (CDECL *mono_config_parse)(const char *filename);
81 MonoAssembly* (CDECL *mono_domain_assembly_open)(MonoDomain *domain, const char *name);
82 static void (CDECL *mono_free)(void *);
83 static MonoImage* (CDECL *mono_image_open)(const char *fname, MonoImageOpenStatus *status);
84 MonoImage* (CDECL *mono_image_open_from_module_handle)(HMODULE module_handle, char* fname, UINT has_entry_point, MonoImageOpenStatus* status);
85 static void (CDECL *mono_install_assembly_preload_hook)(MonoAssemblyPreLoadFunc func, void *user_data);
86 int (CDECL *mono_jit_exec)(MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[]);
87 MonoDomain* (CDECL *mono_jit_init_version)(const char *domain_name, const char *runtime_version);
88 static int (CDECL *mono_jit_set_trace_options)(const char* options);
89 void* (CDECL *mono_marshal_get_vtfixup_ftnptr)(MonoImage *image, DWORD token, WORD type);
90 MonoDomain* (CDECL *mono_object_get_domain)(MonoObject *obj);
91 MonoObject* (CDECL *mono_object_new)(MonoDomain *domain, MonoClass *klass);
92 void* (CDECL *mono_object_unbox)(MonoObject *obj);
93 static void (CDECL *mono_profiler_install)(MonoProfiler *prof, MonoProfileFunc shutdown_callback);
94 MonoType* (CDECL *mono_reflection_type_from_name)(char *name, MonoImage *image);
95 MonoObject* (CDECL *mono_runtime_invoke)(MonoMethod *method, void *obj, void **params, MonoObject **exc);
96 void (CDECL *mono_runtime_object_init)(MonoObject *this_obj);
97 void (CDECL *mono_runtime_quit)(void);
98 static void (CDECL *mono_set_dirs)(const char *assembly_dir, const char *config_dir);
99 static void (CDECL *mono_set_verbose_level)(DWORD level);
100 MonoString* (CDECL *mono_string_new)(MonoDomain *domain, const char *str);
101 static char* (CDECL *mono_stringify_assembly_name)(MonoAssemblyName *aname);
102 MonoThread* (CDECL *mono_thread_attach)(MonoDomain *domain);
103 void (CDECL *mono_thread_manage)(void);
104 void (CDECL *mono_trace_set_assembly)(MonoAssembly *assembly);
105 void (CDECL *mono_trace_set_print_handler)(MonoPrintCallback callback);
106 void (CDECL *mono_trace_set_printerr_handler)(MonoPrintCallback callback);
108 static BOOL get_mono_path(LPWSTR path);
110 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path);
112 static MonoAssembly* mono_assembly_preload_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data);
114 static void mono_shutdown_callback_fn(MonoProfiler *prof);
116 static void mono_print_handler_fn(const char *string, INT is_stdout);
118 static MonoImage* CDECL image_open_module_handle_dummy(HMODULE module_handle,
119 char* fname, UINT has_entry_point, MonoImageOpenStatus* status)
121 return mono_image_open(fname, status);
124 static void CDECL set_print_handler_dummy(MonoPrintCallback callback)
128 static void missing_runtime_message(void)
130 MESSAGE("wine: Install Mono for Windows to run .NET applications.\n");
133 static HRESULT load_mono(LPCWSTR mono_path)
135 static const WCHAR lib[] = {'\\','l','i','b',0};
136 static const WCHAR etc[] = {'\\','e','t','c',0};
137 WCHAR mono_dll_path[MAX_PATH+16];
138 WCHAR mono_lib_path[MAX_PATH+4], mono_etc_path[MAX_PATH+4];
139 char mono_lib_path_a[MAX_PATH], mono_etc_path_a[MAX_PATH];
140 int trace_size;
141 char trace_setting[256];
142 int verbose_size;
143 char verbose_setting[256];
145 if (is_mono_shutdown)
147 ERR("Cannot load Mono after it has been shut down.\n");
148 return E_FAIL;
151 if (!mono_handle)
153 strcpyW(mono_lib_path, mono_path);
154 strcatW(mono_lib_path, lib);
155 WideCharToMultiByte(CP_UTF8, 0, mono_lib_path, -1, mono_lib_path_a, MAX_PATH, NULL, NULL);
157 strcpyW(mono_etc_path, mono_path);
158 strcatW(mono_etc_path, etc);
159 WideCharToMultiByte(CP_UTF8, 0, mono_etc_path, -1, mono_etc_path_a, MAX_PATH, NULL, NULL);
161 if (!find_mono_dll(mono_path, mono_dll_path)) goto fail;
163 mono_handle = LoadLibraryW(mono_dll_path);
165 if (!mono_handle) goto fail;
167 #define LOAD_MONO_FUNCTION(x) do { \
168 x = (void*)GetProcAddress(mono_handle, #x); \
169 if (!x) { \
170 goto fail; \
172 } while (0);
174 LOAD_MONO_FUNCTION(mono_assembly_get_image);
175 LOAD_MONO_FUNCTION(mono_assembly_load_from);
176 LOAD_MONO_FUNCTION(mono_assembly_open);
177 LOAD_MONO_FUNCTION(mono_config_parse);
178 LOAD_MONO_FUNCTION(mono_class_from_mono_type);
179 LOAD_MONO_FUNCTION(mono_class_from_name);
180 LOAD_MONO_FUNCTION(mono_class_get_method_from_name);
181 LOAD_MONO_FUNCTION(mono_domain_assembly_open);
182 LOAD_MONO_FUNCTION(mono_free);
183 LOAD_MONO_FUNCTION(mono_image_open);
184 LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook);
185 LOAD_MONO_FUNCTION(mono_jit_exec);
186 LOAD_MONO_FUNCTION(mono_jit_init_version);
187 LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
188 LOAD_MONO_FUNCTION(mono_marshal_get_vtfixup_ftnptr);
189 LOAD_MONO_FUNCTION(mono_object_get_domain);
190 LOAD_MONO_FUNCTION(mono_object_new);
191 LOAD_MONO_FUNCTION(mono_object_unbox);
192 LOAD_MONO_FUNCTION(mono_profiler_install);
193 LOAD_MONO_FUNCTION(mono_reflection_type_from_name);
194 LOAD_MONO_FUNCTION(mono_runtime_invoke);
195 LOAD_MONO_FUNCTION(mono_runtime_object_init);
196 LOAD_MONO_FUNCTION(mono_runtime_quit);
197 LOAD_MONO_FUNCTION(mono_set_dirs);
198 LOAD_MONO_FUNCTION(mono_set_verbose_level);
199 LOAD_MONO_FUNCTION(mono_stringify_assembly_name);
200 LOAD_MONO_FUNCTION(mono_string_new);
201 LOAD_MONO_FUNCTION(mono_thread_attach);
202 LOAD_MONO_FUNCTION(mono_thread_manage);
203 LOAD_MONO_FUNCTION(mono_trace_set_assembly);
205 #undef LOAD_MONO_FUNCTION
207 #define LOAD_OPT_MONO_FUNCTION(x, default) do { \
208 x = (void*)GetProcAddress(mono_handle, #x); \
209 if (!x) { \
210 x = default; \
212 } while (0);
214 LOAD_OPT_MONO_FUNCTION(mono_image_open_from_module_handle, image_open_module_handle_dummy);
215 LOAD_OPT_MONO_FUNCTION(mono_trace_set_print_handler, set_print_handler_dummy);
216 LOAD_OPT_MONO_FUNCTION(mono_trace_set_printerr_handler, set_print_handler_dummy);
218 #undef LOAD_OPT_MONO_FUNCTION
220 mono_profiler_install(NULL, mono_shutdown_callback_fn);
222 mono_trace_set_print_handler(mono_print_handler_fn);
223 mono_trace_set_printerr_handler(mono_print_handler_fn);
225 mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
227 mono_config_parse(NULL);
229 mono_install_assembly_preload_hook(mono_assembly_preload_hook_fn, NULL);
231 trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
233 if (trace_size)
235 mono_jit_set_trace_options(trace_setting);
238 verbose_size = GetEnvironmentVariableA("WINE_MONO_VERBOSE", verbose_setting, sizeof(verbose_setting));
240 if (verbose_size)
242 mono_set_verbose_level(verbose_setting[0] - '0');
246 return S_OK;
248 fail:
249 ERR("Could not load Mono into this process\n");
250 FreeLibrary(mono_handle);
251 mono_handle = NULL;
252 return E_FAIL;
255 static void mono_shutdown_callback_fn(MonoProfiler *prof)
257 is_mono_shutdown = TRUE;
260 static void mono_print_handler_fn(const char *string, INT is_stdout)
262 wine_dbg_printf("%s", string);
265 static HRESULT CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo *This, RuntimeHost **result)
267 HRESULT hr = S_OK;
268 WCHAR mono_path[MAX_PATH];
270 if (This->loaded_runtime)
272 *result = This->loaded_runtime;
273 return hr;
276 if (!get_mono_path(mono_path))
278 missing_runtime_message();
279 return CLR_E_SHIM_RUNTIME;
282 EnterCriticalSection(&runtime_list_cs);
284 hr = load_mono(mono_path);
286 if (SUCCEEDED(hr))
287 hr = RuntimeHost_Construct(This, &This->loaded_runtime);
289 LeaveCriticalSection(&runtime_list_cs);
291 if (SUCCEEDED(hr))
292 *result = This->loaded_runtime;
294 return hr;
297 void expect_no_runtimes(void)
299 if (mono_handle && is_mono_started && !is_mono_shutdown)
301 ERR("Process exited with a Mono runtime loaded.\n");
302 return;
306 static inline CLRRuntimeInfo *impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo *iface)
308 return CONTAINING_RECORD(iface, CLRRuntimeInfo, ICLRRuntimeInfo_iface);
311 static HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
312 REFIID riid,
313 void **ppvObject)
315 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
317 if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
318 IsEqualGUID( riid, &IID_IUnknown ) )
320 *ppvObject = iface;
322 else
324 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
325 return E_NOINTERFACE;
328 ICLRRuntimeInfo_AddRef( iface );
330 return S_OK;
333 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
335 return 2;
338 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
340 return 1;
343 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
344 LPWSTR pwzBuffer, DWORD *pcchBuffer)
346 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
347 DWORD buffer_size = *pcchBuffer;
348 HRESULT hr = S_OK;
349 char version[11];
350 DWORD size;
352 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
354 size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
356 assert(size <= sizeof(version));
358 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
360 if (pwzBuffer)
362 if (buffer_size >= *pcchBuffer)
363 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
364 else
365 hr = E_NOT_SUFFICIENT_BUFFER;
368 return hr;
371 static BOOL get_install_root(LPWSTR install_dir)
373 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};
374 const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
376 DWORD len;
377 HKEY key;
379 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
380 return FALSE;
382 len = MAX_PATH;
383 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
385 RegCloseKey(key);
386 return FALSE;
388 RegCloseKey(key);
390 return TRUE;
393 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
394 LPWSTR pwzBuffer, DWORD *pcchBuffer)
396 static const WCHAR slash[] = {'\\',0};
397 DWORD buffer_size = *pcchBuffer;
398 WCHAR system_dir[MAX_PATH];
399 WCHAR version[MAX_PATH];
400 DWORD version_size, size;
401 HRESULT hr = S_OK;
403 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
405 if (!get_install_root(system_dir))
407 ERR("error reading registry key for installroot\n");
408 return E_FAIL;
410 else
412 version_size = MAX_PATH;
413 ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
414 lstrcatW(system_dir, version);
415 lstrcatW(system_dir, slash);
416 size = lstrlenW(system_dir) + 1;
419 *pcchBuffer = size;
421 if (pwzBuffer)
423 if (buffer_size >= size)
424 strcpyW(pwzBuffer, system_dir);
425 else
426 hr = E_NOT_SUFFICIENT_BUFFER;
429 return hr;
432 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
433 HANDLE hndProcess, BOOL *pbLoaded)
435 FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
437 return E_NOTIMPL;
440 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
441 UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
443 FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
445 return E_NOTIMPL;
448 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
449 LPCWSTR pwzDllName, HMODULE *phndModule)
451 WCHAR version[MAX_PATH];
452 HRESULT hr;
453 DWORD cchBuffer;
455 TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
457 cchBuffer = MAX_PATH;
458 hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
459 if (FAILED(hr)) return hr;
461 return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
464 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
465 LPCSTR pszProcName, LPVOID *ppProc)
467 FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
469 return E_NOTIMPL;
472 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
473 REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
475 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
476 RuntimeHost *host;
477 HRESULT hr;
479 TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
481 hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
483 if (SUCCEEDED(hr))
484 hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
486 return hr;
489 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
490 BOOL *pbLoadable)
492 FIXME("%p %p\n", iface, pbLoadable);
494 return E_NOTIMPL;
497 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
498 DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
500 FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
502 return E_NOTIMPL;
505 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
506 DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
508 FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
510 return E_NOTIMPL;
513 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
515 FIXME("%p\n", iface);
517 return E_NOTIMPL;
520 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
521 BOOL *pbStarted, DWORD *pdwStartupFlags)
523 FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
525 return E_NOTIMPL;
528 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
529 CLRRuntimeInfo_QueryInterface,
530 CLRRuntimeInfo_AddRef,
531 CLRRuntimeInfo_Release,
532 CLRRuntimeInfo_GetVersionString,
533 CLRRuntimeInfo_GetRuntimeDirectory,
534 CLRRuntimeInfo_IsLoaded,
535 CLRRuntimeInfo_LoadErrorString,
536 CLRRuntimeInfo_LoadLibrary,
537 CLRRuntimeInfo_GetProcAddress,
538 CLRRuntimeInfo_GetInterface,
539 CLRRuntimeInfo_IsLoadable,
540 CLRRuntimeInfo_SetDefaultStartupFlags,
541 CLRRuntimeInfo_GetDefaultStartupFlags,
542 CLRRuntimeInfo_BindAsLegacyV2Runtime,
543 CLRRuntimeInfo_IsStarted
546 HRESULT ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo *iface, RuntimeHost **result)
548 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
550 assert(This->ICLRRuntimeInfo_iface.lpVtbl == &CLRRuntimeInfoVtbl);
552 return CLRRuntimeInfo_GetRuntimeHost(This, result);
555 #ifdef __i386__
556 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};
557 #elif defined(__x86_64__)
558 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};
559 #else
560 static const WCHAR libmono2_arch_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
561 #endif
563 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path)
565 static const WCHAR mono2_dll[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
566 static const WCHAR libmono2_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
567 DWORD attributes=INVALID_FILE_ATTRIBUTES;
569 strcpyW(dll_path, path);
570 strcatW(dll_path, libmono2_arch_dll);
571 attributes = GetFileAttributesW(dll_path);
573 if (attributes == INVALID_FILE_ATTRIBUTES)
575 strcpyW(dll_path, path);
576 strcatW(dll_path, mono2_dll);
577 attributes = GetFileAttributesW(dll_path);
580 if (attributes == INVALID_FILE_ATTRIBUTES)
582 strcpyW(dll_path, path);
583 strcatW(dll_path, libmono2_dll);
584 attributes = GetFileAttributesW(dll_path);
587 return (attributes != INVALID_FILE_ATTRIBUTES);
590 static BOOL get_mono_path_from_registry(LPWSTR path)
592 static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
593 static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
594 static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
595 static const WCHAR slash[] = {'\\',0};
597 WCHAR version[64], version_key[MAX_PATH];
598 DWORD len;
599 HKEY key;
600 WCHAR dll_path[MAX_PATH];
602 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
603 return FALSE;
605 len = sizeof(version);
606 if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
608 RegCloseKey(key);
609 return FALSE;
611 RegCloseKey(key);
613 lstrcpyW(version_key, mono_key);
614 lstrcatW(version_key, slash);
615 lstrcatW(version_key, version);
617 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
618 return FALSE;
620 len = sizeof(WCHAR) * MAX_PATH;
621 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
623 RegCloseKey(key);
624 return FALSE;
626 RegCloseKey(key);
628 return find_mono_dll(path, dll_path);
631 static BOOL get_mono_path_from_folder(LPCWSTR folder, LPWSTR mono_path)
633 static const WCHAR mono_two_dot_zero[] = {'\\','m','o','n','o','-','2','.','0', 0};
634 WCHAR mono_dll_path[MAX_PATH];
635 BOOL found = FALSE;
637 strcpyW(mono_path, folder);
639 strcatW(mono_path, mono_two_dot_zero);
641 found = find_mono_dll(mono_path, mono_dll_path);
643 return found;
646 static BOOL get_mono_path(LPWSTR path)
648 static const WCHAR subdir_mono[] = {'\\','m','o','n','o',0};
649 static const WCHAR sibling_mono[] = {'\\','.','.','\\','m','o','n','o',0};
650 WCHAR base_path[MAX_PATH];
651 const char *unix_data_dir;
652 WCHAR *dos_data_dir;
653 BOOL build_tree = FALSE;
654 static WCHAR* (CDECL *wine_get_dos_file_name)(const char*);
656 /* First try c:\windows\mono */
657 GetWindowsDirectoryW(base_path, MAX_PATH);
658 strcatW(base_path, subdir_mono);
660 if (get_mono_path_from_folder(base_path, path))
661 return TRUE;
663 /* Next: /usr/share/wine/mono */
664 unix_data_dir = wine_get_data_dir();
666 if (!unix_data_dir)
668 unix_data_dir = wine_get_build_dir();
669 build_tree = TRUE;
672 if (unix_data_dir)
674 if (!wine_get_dos_file_name)
675 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
677 if (wine_get_dos_file_name)
679 dos_data_dir = wine_get_dos_file_name(unix_data_dir);
681 if (dos_data_dir)
683 strcpyW(base_path, dos_data_dir);
684 strcatW(base_path, build_tree ? sibling_mono : subdir_mono);
686 HeapFree(GetProcessHeap(), 0, dos_data_dir);
688 if (get_mono_path_from_folder(base_path, path))
689 return TRUE;
694 /* Last: the registry */
695 return get_mono_path_from_registry(path);
698 struct InstalledRuntimeEnum
700 IEnumUnknown IEnumUnknown_iface;
701 LONG ref;
702 ULONG pos;
705 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
707 static inline struct InstalledRuntimeEnum *impl_from_IEnumUnknown(IEnumUnknown *iface)
709 return CONTAINING_RECORD(iface, struct InstalledRuntimeEnum, IEnumUnknown_iface);
712 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface, REFIID riid,
713 void **ppvObject)
715 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
717 if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
718 IsEqualGUID( riid, &IID_IUnknown ) )
720 *ppvObject = iface;
722 else
724 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
725 return E_NOINTERFACE;
728 IEnumUnknown_AddRef( iface );
730 return S_OK;
733 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
735 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
736 ULONG ref = InterlockedIncrement(&This->ref);
738 TRACE("(%p) refcount=%u\n", iface, ref);
740 return ref;
743 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
745 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
746 ULONG ref = InterlockedDecrement(&This->ref);
748 TRACE("(%p) refcount=%u\n", iface, ref);
750 if (ref == 0)
752 HeapFree(GetProcessHeap(), 0, This);
755 return ref;
758 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
759 IUnknown **rgelt, ULONG *pceltFetched)
761 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
762 ULONG num_fetched = 0;
763 HRESULT hr=S_OK;
764 IUnknown *item;
766 TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
768 while (num_fetched < celt)
770 if (This->pos >= NUM_RUNTIMES)
772 hr = S_FALSE;
773 break;
775 item = (IUnknown*)&runtimes[This->pos].ICLRRuntimeInfo_iface;
776 IUnknown_AddRef(item);
777 rgelt[num_fetched] = item;
778 num_fetched++;
779 This->pos++;
782 if (pceltFetched)
783 *pceltFetched = num_fetched;
785 return hr;
788 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
790 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
791 ULONG num_fetched = 0;
792 HRESULT hr=S_OK;
794 TRACE("(%p,%u)\n", iface, celt);
796 while (num_fetched < celt)
798 if (This->pos >= NUM_RUNTIMES)
800 hr = S_FALSE;
801 break;
803 num_fetched++;
804 This->pos++;
807 return hr;
810 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
812 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
814 TRACE("(%p)\n", iface);
816 This->pos = 0;
818 return S_OK;
821 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
823 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
824 struct InstalledRuntimeEnum *new_enum;
826 TRACE("(%p)\n", iface);
828 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
829 if (!new_enum)
830 return E_OUTOFMEMORY;
832 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
833 new_enum->ref = 1;
834 new_enum->pos = This->pos;
836 *ppenum = &new_enum->IEnumUnknown_iface;
838 return S_OK;
841 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
842 InstalledRuntimeEnum_QueryInterface,
843 InstalledRuntimeEnum_AddRef,
844 InstalledRuntimeEnum_Release,
845 InstalledRuntimeEnum_Next,
846 InstalledRuntimeEnum_Skip,
847 InstalledRuntimeEnum_Reset,
848 InstalledRuntimeEnum_Clone
851 struct CLRMetaHost
853 ICLRMetaHost ICLRMetaHost_iface;
855 RuntimeLoadedCallbackFnPtr callback;
858 static struct CLRMetaHost GlobalCLRMetaHost;
860 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
861 REFIID riid,
862 void **ppvObject)
864 TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
866 if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
867 IsEqualGUID( riid, &IID_IUnknown ) )
869 *ppvObject = iface;
871 else
873 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
874 return E_NOINTERFACE;
877 ICLRMetaHost_AddRef( iface );
879 return S_OK;
882 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
884 return 2;
887 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
889 return 1;
892 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
894 *major = 0;
895 *minor = 0;
896 *build = 0;
898 if (version[0] == 'v' || version[0] == 'V')
900 version++;
901 if (!isdigit(*version))
902 return FALSE;
904 while (isdigit(*version))
905 *major = *major * 10 + (*version++ - '0');
907 if (*version == 0)
908 return TRUE;
910 if (*version++ != '.' || !isdigit(*version))
911 return FALSE;
913 while (isdigit(*version))
914 *minor = *minor * 10 + (*version++ - '0');
916 if (*version == 0)
917 return TRUE;
919 if (*version++ != '.' || !isdigit(*version))
920 return FALSE;
922 while (isdigit(*version))
923 *build = *build * 10 + (*version++ - '0');
925 return *version == 0;
927 else
928 return FALSE;
931 static HRESULT get_runtime(LPCWSTR pwzVersion, BOOL allow_short,
932 REFIID iid, LPVOID *ppRuntime)
934 int i;
935 DWORD major, minor, build;
937 if (!pwzVersion)
938 return E_POINTER;
940 if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
942 ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
943 return CLR_E_SHIM_RUNTIME;
946 for (i=0; i<NUM_RUNTIMES; i++)
948 if (runtimes[i].major == major && runtimes[i].minor == minor &&
949 (runtimes[i].build == build || (allow_short && major >= 4 && build == 0)))
951 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface, iid,
952 ppRuntime);
956 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
957 return CLR_E_SHIM_RUNTIME;
960 HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
961 LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
963 TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
965 return get_runtime(pwzVersion, FALSE, iid, ppRuntime);
968 HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
969 LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
971 ASSEMBLY *assembly;
972 HRESULT hr;
973 LPSTR version;
974 ULONG buffer_size=*pcchBuffer;
976 TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
978 hr = assembly_create(&assembly, pwzFilePath);
980 if (SUCCEEDED(hr))
982 hr = assembly_get_runtime_version(assembly, &version);
984 if (SUCCEEDED(hr))
986 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
988 if (pwzBuffer)
990 if (buffer_size >= *pcchBuffer)
991 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
992 else
993 hr = E_NOT_SUFFICIENT_BUFFER;
997 assembly_release(assembly);
1000 return hr;
1003 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
1004 IEnumUnknown **ppEnumerator)
1006 struct InstalledRuntimeEnum *new_enum;
1008 TRACE("%p\n", ppEnumerator);
1010 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
1011 if (!new_enum)
1012 return E_OUTOFMEMORY;
1014 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
1015 new_enum->ref = 1;
1016 new_enum->pos = 0;
1018 *ppEnumerator = &new_enum->IEnumUnknown_iface;
1020 return S_OK;
1023 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
1024 HANDLE hndProcess, IEnumUnknown **ppEnumerator)
1026 FIXME("%p %p\n", hndProcess, ppEnumerator);
1028 return E_NOTIMPL;
1031 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
1032 RuntimeLoadedCallbackFnPtr pCallbackFunction)
1034 TRACE("%p\n", pCallbackFunction);
1036 if(!pCallbackFunction)
1037 return E_POINTER;
1039 WARN("Callback currently will not be called.\n");
1041 GlobalCLRMetaHost.callback = pCallbackFunction;
1043 return S_OK;
1046 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
1047 REFIID riid, LPVOID *ppUnk)
1049 FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
1051 return E_NOTIMPL;
1054 HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
1056 TRACE("%i\n", iExitCode);
1058 EnterCriticalSection(&runtime_list_cs);
1060 if (is_mono_started && !is_mono_shutdown)
1062 /* search for a runtime and call System.Environment.Exit() */
1063 int i;
1065 for (i=0; i<NUM_RUNTIMES; i++)
1066 if (runtimes[i].loaded_runtime)
1067 RuntimeHost_ExitProcess(runtimes[i].loaded_runtime, iExitCode);
1070 ExitProcess(iExitCode);
1073 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
1075 CLRMetaHost_QueryInterface,
1076 CLRMetaHost_AddRef,
1077 CLRMetaHost_Release,
1078 CLRMetaHost_GetRuntime,
1079 CLRMetaHost_GetVersionFromFile,
1080 CLRMetaHost_EnumerateInstalledRuntimes,
1081 CLRMetaHost_EnumerateLoadedRuntimes,
1082 CLRMetaHost_RequestRuntimeLoadedNotification,
1083 CLRMetaHost_QueryLegacyV2RuntimeBinding,
1084 CLRMetaHost_ExitProcess
1087 static struct CLRMetaHost GlobalCLRMetaHost = {
1088 { &CLRMetaHost_vtbl }
1091 HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1093 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost.ICLRMetaHost_iface, riid, ppobj);
1096 struct CLRMetaHostPolicy
1098 ICLRMetaHostPolicy ICLRMetaHostPolicy_iface;
1101 static struct CLRMetaHostPolicy GlobalCLRMetaHostPolicy;
1103 static HRESULT WINAPI metahostpolicy_QueryInterface(ICLRMetaHostPolicy *iface, REFIID riid, void **obj)
1105 TRACE("%s %p\n", debugstr_guid(riid), obj);
1107 if ( IsEqualGUID( riid, &IID_ICLRMetaHostPolicy ) ||
1108 IsEqualGUID( riid, &IID_IUnknown ) )
1110 ICLRMetaHostPolicy_AddRef( iface );
1111 *obj = iface;
1112 return S_OK;
1115 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
1117 *obj = NULL;
1118 return E_NOINTERFACE;
1121 static ULONG WINAPI metahostpolicy_AddRef(ICLRMetaHostPolicy *iface)
1123 return 2;
1126 static ULONG WINAPI metahostpolicy_Release(ICLRMetaHostPolicy *iface)
1128 return 1;
1131 static HRESULT WINAPI metahostpolicy_GetRequestedRuntime(ICLRMetaHostPolicy *iface, METAHOST_POLICY_FLAGS dwPolicyFlags,
1132 LPCWSTR pwzBinary, IStream *pCfgStream, LPWSTR pwzVersion, DWORD *pcchVersion,
1133 LPWSTR pwzImageVersion, DWORD *pcchImageVersion, DWORD *pdwConfigFlags, REFIID riid,
1134 LPVOID *ppRuntime)
1136 ICLRRuntimeInfo *result;
1137 HRESULT hr;
1138 WCHAR filename[MAX_PATH];
1139 const WCHAR *path = NULL;
1140 int flags = 0;
1142 TRACE("%d %p %p %p %p %p %p %p %s %p\n", dwPolicyFlags, pwzBinary, pCfgStream,
1143 pwzVersion, pcchVersion, pwzImageVersion, pcchImageVersion, pdwConfigFlags,
1144 debugstr_guid(riid), ppRuntime);
1146 if (pCfgStream)
1147 FIXME("ignoring config file stream\n");
1149 if (pdwConfigFlags)
1150 FIXME("ignoring config flags\n");
1152 if(dwPolicyFlags & METAHOST_POLICY_USE_PROCESS_IMAGE_PATH)
1154 GetModuleFileNameW(0, filename, MAX_PATH);
1155 path = filename;
1157 else if(pwzBinary)
1159 path = pwzBinary;
1162 if(dwPolicyFlags & METAHOST_POLICY_APPLY_UPGRADE_POLICY)
1163 flags |= RUNTIME_INFO_UPGRADE_VERSION;
1165 hr = get_runtime_info(path, pwzImageVersion, NULL, 0, flags, FALSE, &result);
1166 if (SUCCEEDED(hr))
1168 if (pwzImageVersion)
1170 /* Ignoring errors on purpose */
1171 ICLRRuntimeInfo_GetVersionString(result, pwzImageVersion, pcchImageVersion);
1174 hr = ICLRRuntimeInfo_QueryInterface(result, riid, ppRuntime);
1176 ICLRRuntimeInfo_Release(result);
1179 TRACE("<- 0x%08x\n", hr);
1181 return hr;
1184 static const struct ICLRMetaHostPolicyVtbl CLRMetaHostPolicy_vtbl =
1186 metahostpolicy_QueryInterface,
1187 metahostpolicy_AddRef,
1188 metahostpolicy_Release,
1189 metahostpolicy_GetRequestedRuntime
1192 static struct CLRMetaHostPolicy GlobalCLRMetaHostPolicy = {
1193 { &CLRMetaHostPolicy_vtbl }
1196 HRESULT CLRMetaHostPolicy_CreateInstance(REFIID riid, void **ppobj)
1198 return ICLRMetaHostPolicy_QueryInterface(&GlobalCLRMetaHostPolicy.ICLRMetaHostPolicy_iface, riid, ppobj);
1201 HRESULT get_file_from_strongname(WCHAR* stringnameW, WCHAR* assemblies_path, int path_length)
1203 HRESULT hr=S_OK;
1204 IAssemblyCache *asmcache;
1205 ASSEMBLY_INFO info;
1206 static const WCHAR fusiondll[] = {'f','u','s','i','o','n',0};
1207 HMODULE hfusion=NULL;
1208 static HRESULT (WINAPI *pCreateAssemblyCache)(IAssemblyCache**,DWORD);
1210 if (!pCreateAssemblyCache)
1212 hr = LoadLibraryShim(fusiondll, NULL, NULL, &hfusion);
1214 if (SUCCEEDED(hr))
1216 pCreateAssemblyCache = (void*)GetProcAddress(hfusion, "CreateAssemblyCache");
1217 if (!pCreateAssemblyCache)
1218 hr = E_FAIL;
1222 if (SUCCEEDED(hr))
1223 hr = pCreateAssemblyCache(&asmcache, 0);
1225 if (SUCCEEDED(hr))
1227 info.cbAssemblyInfo = sizeof(info);
1228 info.pszCurrentAssemblyPathBuf = assemblies_path;
1229 info.cchBuf = path_length;
1230 assemblies_path[0] = 0;
1232 hr = IAssemblyCache_QueryAssemblyInfo(asmcache, 0, stringnameW, &info);
1234 IAssemblyCache_Release(asmcache);
1237 return hr;
1240 static MonoAssembly* mono_assembly_preload_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data)
1242 HRESULT hr;
1243 MonoAssembly *result=NULL;
1244 char *stringname=NULL;
1245 LPWSTR stringnameW;
1246 int stringnameW_size;
1247 WCHAR path[MAX_PATH];
1248 char *pathA;
1249 MonoImageOpenStatus stat;
1251 stringname = mono_stringify_assembly_name(aname);
1253 TRACE("%s\n", debugstr_a(stringname));
1255 if (!stringname) return NULL;
1257 /* FIXME: We should search the given paths before the GAC. */
1259 stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, stringname, -1, NULL, 0);
1261 stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1262 if (stringnameW)
1264 MultiByteToWideChar(CP_UTF8, 0, stringname, -1, stringnameW, stringnameW_size);
1266 hr = get_file_from_strongname(stringnameW, path, MAX_PATH);
1268 HeapFree(GetProcessHeap(), 0, stringnameW);
1270 else
1271 hr = E_OUTOFMEMORY;
1273 if (SUCCEEDED(hr))
1275 TRACE("found: %s\n", debugstr_w(path));
1277 pathA = WtoA(path);
1279 if (pathA)
1281 result = mono_assembly_open(pathA, &stat);
1283 if (!result)
1284 ERR("Failed to load %s, status=%u\n", debugstr_w(path), stat);
1286 HeapFree(GetProcessHeap(), 0, pathA);
1290 mono_free(stringname);
1292 return result;
1295 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1296 DWORD startup_flags, DWORD runtimeinfo_flags, BOOL legacy, ICLRRuntimeInfo **result)
1298 static const WCHAR dotconfig[] = {'.','c','o','n','f','i','g',0};
1299 static const DWORD supported_startup_flags = 0;
1300 static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1301 int i;
1302 WCHAR local_version[MAX_PATH];
1303 ULONG local_version_size = MAX_PATH;
1304 WCHAR local_config_file[MAX_PATH];
1305 HRESULT hr;
1306 parsed_config_file parsed_config;
1308 if (startup_flags & ~supported_startup_flags)
1309 FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
1311 if (runtimeinfo_flags & ~supported_runtime_flags)
1312 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
1314 if (exefile && !exefile[0])
1315 exefile = NULL;
1317 if (exefile && !config_file)
1319 strcpyW(local_config_file, exefile);
1320 strcatW(local_config_file, dotconfig);
1322 config_file = local_config_file;
1325 if (config_file)
1327 BOOL found = FALSE;
1328 hr = parse_config_file(config_file, &parsed_config);
1330 if (SUCCEEDED(hr))
1332 supported_runtime *entry;
1333 LIST_FOR_EACH_ENTRY(entry, &parsed_config.supported_runtimes, supported_runtime, entry)
1335 hr = get_runtime(entry->version, TRUE, &IID_ICLRRuntimeInfo, (void**)result);
1336 if (SUCCEEDED(hr))
1338 found = TRUE;
1339 break;
1343 else
1345 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file), hr);
1348 free_parsed_config_file(&parsed_config);
1350 if (found)
1351 return S_OK;
1354 if (exefile && !version)
1356 hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1358 version = local_version;
1360 if (FAILED(hr)) return hr;
1363 if (version)
1365 hr = CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1366 if(SUCCEEDED(hr))
1367 return hr;
1370 if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1372 DWORD major, minor, build;
1374 if (version && !parse_runtime_version(version, &major, &minor, &build))
1376 ERR("Cannot parse %s\n", debugstr_w(version));
1377 return CLR_E_SHIM_RUNTIME;
1380 if (legacy)
1381 i = 3;
1382 else
1383 i = NUM_RUNTIMES;
1385 while (i--)
1387 /* Must be greater or equal to the version passed in. */
1388 if (!version || ((runtimes[i].major >= major && runtimes[i].minor >= minor && runtimes[i].build >= build) ||
1389 (runtimes[i].major >= major && runtimes[i].minor > minor) ||
1390 (runtimes[i].major > major)))
1392 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface,
1393 &IID_ICLRRuntimeInfo, (void **)result);
1397 missing_runtime_message();
1399 return CLR_E_SHIM_RUNTIME;
1402 return CLR_E_SHIM_RUNTIME;