winejoystick: Fix a crash on accessing a CFArray past its end due to an off-by-one...
[wine/multimedia.git] / dlls / mscoree / metahost.c
blob9cbad223a8d2517a31f77a286f702fc72302978e
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 WCHAR net_20_subdir[] = {'2','.','0',0};
49 static const WCHAR net_40_subdir[] = {'4','.','0',0};
51 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl;
53 #define NUM_RUNTIMES 4
55 static struct CLRRuntimeInfo runtimes[NUM_RUNTIMES] = {
56 {{&CLRRuntimeInfoVtbl}, net_20_subdir, 1, 0, 3705, 0},
57 {{&CLRRuntimeInfoVtbl}, net_20_subdir, 1, 1, 4322, 0},
58 {{&CLRRuntimeInfoVtbl}, net_20_subdir, 2, 0, 50727, 0},
59 {{&CLRRuntimeInfoVtbl}, net_40_subdir, 4, 0, 30319, 0}
62 static BOOL runtimes_initialized = FALSE;
64 static CRITICAL_SECTION runtime_list_cs;
65 static CRITICAL_SECTION_DEBUG runtime_list_cs_debug =
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") }
72 static CRITICAL_SECTION runtime_list_cs = { &runtime_list_cs_debug, -1, 0, 0, 0, 0 };
74 static HMODULE mono_handle;
76 BOOL is_mono_started;
77 static BOOL is_mono_shutdown;
79 MonoImage* (CDECL *mono_assembly_get_image)(MonoAssembly *assembly);
80 MonoAssembly* (CDECL *mono_assembly_load_from)(MonoImage *image, const char *fname, MonoImageOpenStatus *status);
81 MonoAssembly* (CDECL *mono_assembly_open)(const char *filename, MonoImageOpenStatus *status);
82 MonoClass* (CDECL *mono_class_from_mono_type)(MonoType *type);
83 MonoClass* (CDECL *mono_class_from_name)(MonoImage *image, const char* name_space, const char *name);
84 MonoMethod* (CDECL *mono_class_get_method_from_name)(MonoClass *klass, const char *name, int param_count);
85 static void (CDECL *mono_config_parse)(const char *filename);
86 MonoAssembly* (CDECL *mono_domain_assembly_open)(MonoDomain *domain, const char *name);
87 static void (CDECL *mono_free)(void *);
88 static MonoImage* (CDECL *mono_image_open)(const char *fname, MonoImageOpenStatus *status);
89 MonoImage* (CDECL *mono_image_open_from_module_handle)(HMODULE module_handle, char* fname, UINT has_entry_point, MonoImageOpenStatus* status);
90 static void (CDECL *mono_install_assembly_preload_hook)(MonoAssemblyPreLoadFunc func, void *user_data);
91 int (CDECL *mono_jit_exec)(MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[]);
92 MonoDomain* (CDECL *mono_jit_init)(const char *file);
93 static int (CDECL *mono_jit_set_trace_options)(const char* options);
94 void* (CDECL *mono_marshal_get_vtfixup_ftnptr)(MonoImage *image, DWORD token, WORD type);
95 MonoDomain* (CDECL *mono_object_get_domain)(MonoObject *obj);
96 MonoObject* (CDECL *mono_object_new)(MonoDomain *domain, MonoClass *klass);
97 void* (CDECL *mono_object_unbox)(MonoObject *obj);
98 static void (CDECL *mono_profiler_install)(MonoProfiler *prof, MonoProfileFunc shutdown_callback);
99 MonoType* (CDECL *mono_reflection_type_from_name)(char *name, MonoImage *image);
100 MonoObject* (CDECL *mono_runtime_invoke)(MonoMethod *method, void *obj, void **params, MonoObject **exc);
101 void (CDECL *mono_runtime_object_init)(MonoObject *this_obj);
102 void (CDECL *mono_runtime_quit)(void);
103 static void (CDECL *mono_set_dirs)(const char *assembly_dir, const char *config_dir);
104 static void (CDECL *mono_set_verbose_level)(DWORD level);
105 MonoString* (CDECL *mono_string_new)(MonoDomain *domain, const char *str);
106 static char* (CDECL *mono_stringify_assembly_name)(MonoAssemblyName *aname);
107 MonoThread* (CDECL *mono_thread_attach)(MonoDomain *domain);
108 void (CDECL *mono_thread_manage)(void);
109 void (CDECL *mono_trace_set_assembly)(MonoAssembly *assembly);
111 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path);
113 static MonoAssembly* mono_assembly_preload_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data);
115 static void mono_shutdown_callback_fn(MonoProfiler *prof);
117 static void set_environment(LPCWSTR bin_path)
119 WCHAR path_env[MAX_PATH];
120 int len;
122 static const WCHAR pathW[] = {'P','A','T','H',0};
124 /* We have to modify PATH as Mono loads other DLLs from this directory. */
125 GetEnvironmentVariableW(pathW, path_env, sizeof(path_env)/sizeof(WCHAR));
126 len = strlenW(path_env);
127 path_env[len++] = ';';
128 strcpyW(path_env+len, bin_path);
129 SetEnvironmentVariableW(pathW, path_env);
132 static MonoImage* CDECL image_open_module_handle_dummy(HMODULE module_handle,
133 char* fname, UINT has_entry_point, MonoImageOpenStatus* status)
135 return mono_image_open(fname, status);
138 static void missing_runtime_message(void)
140 MESSAGE("wine: Install Mono for Windows to run .NET applications.\n");
143 static HRESULT load_mono(CLRRuntimeInfo *This)
145 static const WCHAR bin[] = {'\\','b','i','n',0};
146 static const WCHAR lib[] = {'\\','l','i','b',0};
147 static const WCHAR etc[] = {'\\','e','t','c',0};
148 WCHAR mono_dll_path[MAX_PATH+16], mono_bin_path[MAX_PATH+4];
149 WCHAR mono_lib_path[MAX_PATH+4], mono_etc_path[MAX_PATH+4];
150 char mono_lib_path_a[MAX_PATH], mono_etc_path_a[MAX_PATH];
151 int trace_size;
152 char trace_setting[256];
153 int verbose_size;
154 char verbose_setting[256];
156 if (is_mono_shutdown)
158 ERR("Cannot load Mono after it has been shut down.\n");
159 return E_FAIL;
162 if (!mono_handle)
164 strcpyW(mono_bin_path, This->mono_path);
165 strcatW(mono_bin_path, bin);
166 set_environment(mono_bin_path);
168 strcpyW(mono_lib_path, This->mono_path);
169 strcatW(mono_lib_path, lib);
170 WideCharToMultiByte(CP_UTF8, 0, mono_lib_path, -1, mono_lib_path_a, MAX_PATH, NULL, NULL);
172 strcpyW(mono_etc_path, This->mono_path);
173 strcatW(mono_etc_path, etc);
174 WideCharToMultiByte(CP_UTF8, 0, mono_etc_path, -1, mono_etc_path_a, MAX_PATH, NULL, NULL);
176 if (!find_mono_dll(This->mono_path, mono_dll_path)) goto fail;
178 mono_handle = LoadLibraryW(mono_dll_path);
180 if (!mono_handle) goto fail;
182 #define LOAD_MONO_FUNCTION(x) do { \
183 x = (void*)GetProcAddress(mono_handle, #x); \
184 if (!x) { \
185 goto fail; \
187 } while (0);
189 LOAD_MONO_FUNCTION(mono_assembly_get_image);
190 LOAD_MONO_FUNCTION(mono_assembly_load_from);
191 LOAD_MONO_FUNCTION(mono_assembly_open);
192 LOAD_MONO_FUNCTION(mono_config_parse);
193 LOAD_MONO_FUNCTION(mono_class_from_mono_type);
194 LOAD_MONO_FUNCTION(mono_class_from_name);
195 LOAD_MONO_FUNCTION(mono_class_get_method_from_name);
196 LOAD_MONO_FUNCTION(mono_domain_assembly_open);
197 LOAD_MONO_FUNCTION(mono_free);
198 LOAD_MONO_FUNCTION(mono_image_open);
199 LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook);
200 LOAD_MONO_FUNCTION(mono_jit_exec);
201 LOAD_MONO_FUNCTION(mono_jit_init);
202 LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
203 LOAD_MONO_FUNCTION(mono_marshal_get_vtfixup_ftnptr);
204 LOAD_MONO_FUNCTION(mono_object_get_domain);
205 LOAD_MONO_FUNCTION(mono_object_new);
206 LOAD_MONO_FUNCTION(mono_object_unbox);
207 LOAD_MONO_FUNCTION(mono_profiler_install);
208 LOAD_MONO_FUNCTION(mono_reflection_type_from_name);
209 LOAD_MONO_FUNCTION(mono_runtime_invoke);
210 LOAD_MONO_FUNCTION(mono_runtime_object_init);
211 LOAD_MONO_FUNCTION(mono_runtime_quit);
212 LOAD_MONO_FUNCTION(mono_set_dirs);
213 LOAD_MONO_FUNCTION(mono_set_verbose_level);
214 LOAD_MONO_FUNCTION(mono_stringify_assembly_name);
215 LOAD_MONO_FUNCTION(mono_string_new);
216 LOAD_MONO_FUNCTION(mono_thread_attach);
217 LOAD_MONO_FUNCTION(mono_thread_manage);
218 LOAD_MONO_FUNCTION(mono_trace_set_assembly);
220 #undef LOAD_MONO_FUNCTION
222 #define LOAD_OPT_MONO_FUNCTION(x, default) do { \
223 x = (void*)GetProcAddress(mono_handle, #x); \
224 if (!x) { \
225 x = default; \
227 } while (0);
229 LOAD_OPT_MONO_FUNCTION(mono_image_open_from_module_handle, image_open_module_handle_dummy);
231 #undef LOAD_OPT_MONO_FUNCTION
233 mono_profiler_install(NULL, mono_shutdown_callback_fn);
235 mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
237 mono_config_parse(NULL);
239 mono_install_assembly_preload_hook(mono_assembly_preload_hook_fn, NULL);
241 trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
243 if (trace_size)
245 mono_jit_set_trace_options(trace_setting);
248 verbose_size = GetEnvironmentVariableA("WINE_MONO_VERBOSE", verbose_setting, sizeof(verbose_setting));
250 if (verbose_size)
252 mono_set_verbose_level(verbose_setting[0] - '0');
256 return S_OK;
258 fail:
259 ERR("Could not load Mono into this process\n");
260 FreeLibrary(mono_handle);
261 mono_handle = NULL;
262 return E_FAIL;
265 static void mono_shutdown_callback_fn(MonoProfiler *prof)
267 is_mono_shutdown = TRUE;
270 static HRESULT CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo *This, RuntimeHost **result)
272 HRESULT hr = S_OK;
274 if (This->loaded_runtime)
276 *result = This->loaded_runtime;
277 return hr;
280 EnterCriticalSection(&runtime_list_cs);
282 hr = load_mono(This);
284 if (SUCCEEDED(hr))
285 hr = RuntimeHost_Construct(This, &This->loaded_runtime);
287 LeaveCriticalSection(&runtime_list_cs);
289 if (SUCCEEDED(hr))
290 *result = This->loaded_runtime;
292 return hr;
295 void expect_no_runtimes(void)
297 if (mono_handle && is_mono_started && !is_mono_shutdown)
299 ERR("Process exited with a Mono runtime loaded.\n");
300 return;
304 static inline CLRRuntimeInfo *impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo *iface)
306 return CONTAINING_RECORD(iface, CLRRuntimeInfo, ICLRRuntimeInfo_iface);
309 static HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
310 REFIID riid,
311 void **ppvObject)
313 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
315 if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
316 IsEqualGUID( riid, &IID_IUnknown ) )
318 *ppvObject = iface;
320 else
322 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
323 return E_NOINTERFACE;
326 ICLRRuntimeInfo_AddRef( iface );
328 return S_OK;
331 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
333 return 2;
336 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
338 return 1;
341 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
342 LPWSTR pwzBuffer, DWORD *pcchBuffer)
344 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
345 DWORD buffer_size = *pcchBuffer;
346 HRESULT hr = S_OK;
347 char version[11];
348 DWORD size;
350 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
352 size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
354 assert(size <= sizeof(version));
356 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
358 if (pwzBuffer)
360 if (buffer_size >= *pcchBuffer)
361 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
362 else
363 hr = E_NOT_SUFFICIENT_BUFFER;
366 return hr;
369 static BOOL get_install_root(LPWSTR install_dir)
371 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};
372 const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
374 DWORD len;
375 HKEY key;
377 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
378 return FALSE;
380 len = MAX_PATH;
381 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
383 RegCloseKey(key);
384 return FALSE;
386 RegCloseKey(key);
388 return TRUE;
391 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
392 LPWSTR pwzBuffer, DWORD *pcchBuffer)
394 static const WCHAR slash[] = {'\\',0};
395 DWORD buffer_size = *pcchBuffer;
396 WCHAR system_dir[MAX_PATH];
397 WCHAR version[MAX_PATH];
398 DWORD version_size, size;
399 HRESULT hr = S_OK;
401 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
403 if (!get_install_root(system_dir))
405 ERR("error reading registry key for installroot\n");
406 return E_FAIL;
408 else
410 version_size = MAX_PATH;
411 ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
412 lstrcatW(system_dir, version);
413 lstrcatW(system_dir, slash);
414 size = lstrlenW(system_dir) + 1;
417 *pcchBuffer = size;
419 if (pwzBuffer)
421 if (buffer_size >= size)
422 strcpyW(pwzBuffer, system_dir);
423 else
424 hr = E_NOT_SUFFICIENT_BUFFER;
427 return hr;
430 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
431 HANDLE hndProcess, BOOL *pbLoaded)
433 FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
435 return E_NOTIMPL;
438 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
439 UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
441 FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
443 return E_NOTIMPL;
446 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
447 LPCWSTR pwzDllName, HMODULE *phndModule)
449 WCHAR version[MAX_PATH];
450 HRESULT hr;
451 DWORD cchBuffer;
453 TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
455 cchBuffer = MAX_PATH;
456 hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
457 if (FAILED(hr)) return hr;
459 return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
462 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
463 LPCSTR pszProcName, LPVOID *ppProc)
465 FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
467 return E_NOTIMPL;
470 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
471 REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
473 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
474 RuntimeHost *host;
475 HRESULT hr;
477 TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
479 hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
481 if (SUCCEEDED(hr))
482 hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
484 return hr;
487 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
488 BOOL *pbLoadable)
490 FIXME("%p %p\n", iface, pbLoadable);
492 return E_NOTIMPL;
495 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
496 DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
498 FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
500 return E_NOTIMPL;
503 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
504 DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
506 FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
508 return E_NOTIMPL;
511 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
513 FIXME("%p\n", iface);
515 return E_NOTIMPL;
518 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
519 BOOL *pbStarted, DWORD *pdwStartupFlags)
521 FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
523 return E_NOTIMPL;
526 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
527 CLRRuntimeInfo_QueryInterface,
528 CLRRuntimeInfo_AddRef,
529 CLRRuntimeInfo_Release,
530 CLRRuntimeInfo_GetVersionString,
531 CLRRuntimeInfo_GetRuntimeDirectory,
532 CLRRuntimeInfo_IsLoaded,
533 CLRRuntimeInfo_LoadErrorString,
534 CLRRuntimeInfo_LoadLibrary,
535 CLRRuntimeInfo_GetProcAddress,
536 CLRRuntimeInfo_GetInterface,
537 CLRRuntimeInfo_IsLoadable,
538 CLRRuntimeInfo_SetDefaultStartupFlags,
539 CLRRuntimeInfo_GetDefaultStartupFlags,
540 CLRRuntimeInfo_BindAsLegacyV2Runtime,
541 CLRRuntimeInfo_IsStarted
544 HRESULT ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo *iface, RuntimeHost **result)
546 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
548 assert(This->ICLRRuntimeInfo_iface.lpVtbl == &CLRRuntimeInfoVtbl);
550 return CLRRuntimeInfo_GetRuntimeHost(This, result);
553 #ifdef __i386__
554 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};
555 #elif defined(__x86_64__)
556 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};
557 #else
558 static const WCHAR libmono2_arch_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
559 #endif
561 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path)
563 static const WCHAR mono2_dll[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
564 static const WCHAR libmono2_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
565 DWORD attributes=INVALID_FILE_ATTRIBUTES;
567 strcpyW(dll_path, path);
568 strcatW(dll_path, libmono2_arch_dll);
569 attributes = GetFileAttributesW(dll_path);
571 if (attributes == INVALID_FILE_ATTRIBUTES)
573 strcpyW(dll_path, path);
574 strcatW(dll_path, mono2_dll);
575 attributes = GetFileAttributesW(dll_path);
578 if (attributes == INVALID_FILE_ATTRIBUTES)
580 strcpyW(dll_path, path);
581 strcatW(dll_path, libmono2_dll);
582 attributes = GetFileAttributesW(dll_path);
585 return (attributes != INVALID_FILE_ATTRIBUTES);
588 static BOOL get_mono_path_from_registry(LPWSTR path)
590 static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
591 static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
592 static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
593 static const WCHAR slash[] = {'\\',0};
595 WCHAR version[64], version_key[MAX_PATH];
596 DWORD len;
597 HKEY key;
598 WCHAR dll_path[MAX_PATH];
600 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
601 return FALSE;
603 len = sizeof(version);
604 if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
606 RegCloseKey(key);
607 return FALSE;
609 RegCloseKey(key);
611 lstrcpyW(version_key, mono_key);
612 lstrcatW(version_key, slash);
613 lstrcatW(version_key, version);
615 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
616 return FALSE;
618 len = sizeof(WCHAR) * MAX_PATH;
619 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
621 RegCloseKey(key);
622 return FALSE;
624 RegCloseKey(key);
626 return find_mono_dll(path, dll_path);
629 static BOOL get_mono_path_from_folder(LPCWSTR folder, LPWSTR mono_path)
631 static const WCHAR mono_two_dot_zero[] = {'\\','m','o','n','o','-','2','.','0', 0};
632 WCHAR mono_dll_path[MAX_PATH];
633 BOOL found = FALSE;
635 strcpyW(mono_path, folder);
637 strcatW(mono_path, mono_two_dot_zero);
639 found = find_mono_dll(mono_path, mono_dll_path);
641 return found;
644 static BOOL get_mono_path(LPWSTR path)
646 static const WCHAR subdir_mono[] = {'\\','m','o','n','o',0};
647 static const WCHAR sibling_mono[] = {'\\','.','.','\\','m','o','n','o',0};
648 WCHAR base_path[MAX_PATH];
649 const char *unix_data_dir;
650 WCHAR *dos_data_dir;
651 BOOL build_tree = FALSE;
652 static WCHAR* (CDECL *wine_get_dos_file_name)(const char*);
654 /* First try c:\windows\mono */
655 GetWindowsDirectoryW(base_path, MAX_PATH);
656 strcatW(base_path, subdir_mono);
658 if (get_mono_path_from_folder(base_path, path))
659 return TRUE;
661 /* Next: /usr/share/wine/mono */
662 unix_data_dir = wine_get_data_dir();
664 if (!unix_data_dir)
666 unix_data_dir = wine_get_build_dir();
667 build_tree = TRUE;
670 if (unix_data_dir)
672 if (!wine_get_dos_file_name)
673 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
675 if (wine_get_dos_file_name)
677 dos_data_dir = wine_get_dos_file_name(unix_data_dir);
679 if (dos_data_dir)
681 strcpyW(base_path, dos_data_dir);
682 strcatW(base_path, build_tree ? sibling_mono : subdir_mono);
684 HeapFree(GetProcessHeap(), 0, dos_data_dir);
686 if (get_mono_path_from_folder(base_path, path))
687 return TRUE;
692 /* Last: the registry */
693 return get_mono_path_from_registry(path);
696 static void find_runtimes(void)
698 int i;
699 static const WCHAR libmono[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
700 static const WCHAR mscorlib[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
701 WCHAR mono_path[MAX_PATH], lib_path[MAX_PATH];
702 BOOL any_runtimes_found = FALSE;
704 if (runtimes_initialized) return;
706 EnterCriticalSection(&runtime_list_cs);
708 if (runtimes_initialized) goto end;
710 if (get_mono_path(mono_path))
712 for (i=0; i<NUM_RUNTIMES; i++)
714 strcpyW(lib_path, mono_path);
715 strcatW(lib_path, libmono);
716 strcatW(lib_path, runtimes[i].mono_libdir);
717 strcatW(lib_path, mscorlib);
719 if (GetFileAttributesW(lib_path) != INVALID_FILE_ATTRIBUTES)
721 runtimes[i].found = TRUE;
723 strcpyW(runtimes[i].mono_path, mono_path);
724 strcpyW(runtimes[i].mscorlib_path, lib_path);
726 any_runtimes_found = TRUE;
731 if (!any_runtimes_found)
733 /* Report all runtimes are available if Mono isn't installed.
734 * FIXME: Remove this when Mono is properly packaged. */
735 for (i=0; i<NUM_RUNTIMES; i++)
736 runtimes[i].found = TRUE;
739 runtimes_initialized = TRUE;
741 end:
742 LeaveCriticalSection(&runtime_list_cs);
745 struct InstalledRuntimeEnum
747 IEnumUnknown IEnumUnknown_iface;
748 LONG ref;
749 ULONG pos;
752 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
754 static inline struct InstalledRuntimeEnum *impl_from_IEnumUnknown(IEnumUnknown *iface)
756 return CONTAINING_RECORD(iface, struct InstalledRuntimeEnum, IEnumUnknown_iface);
759 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface, REFIID riid,
760 void **ppvObject)
762 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
764 if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
765 IsEqualGUID( riid, &IID_IUnknown ) )
767 *ppvObject = iface;
769 else
771 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
772 return E_NOINTERFACE;
775 IEnumUnknown_AddRef( iface );
777 return S_OK;
780 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
782 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
783 ULONG ref = InterlockedIncrement(&This->ref);
785 TRACE("(%p) refcount=%u\n", iface, ref);
787 return ref;
790 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
792 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
793 ULONG ref = InterlockedDecrement(&This->ref);
795 TRACE("(%p) refcount=%u\n", iface, ref);
797 if (ref == 0)
799 HeapFree(GetProcessHeap(), 0, This);
802 return ref;
805 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
806 IUnknown **rgelt, ULONG *pceltFetched)
808 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
809 ULONG num_fetched = 0;
810 HRESULT hr=S_OK;
811 IUnknown *item;
813 TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
815 while (num_fetched < celt)
817 if (This->pos >= NUM_RUNTIMES)
819 hr = S_FALSE;
820 break;
822 if (runtimes[This->pos].found)
824 item = (IUnknown*)&runtimes[This->pos].ICLRRuntimeInfo_iface;
825 IUnknown_AddRef(item);
826 rgelt[num_fetched] = item;
827 num_fetched++;
829 This->pos++;
832 if (pceltFetched)
833 *pceltFetched = num_fetched;
835 return hr;
838 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
840 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
841 ULONG num_fetched = 0;
842 HRESULT hr=S_OK;
844 TRACE("(%p,%u)\n", iface, celt);
846 while (num_fetched < celt)
848 if (This->pos >= NUM_RUNTIMES)
850 hr = S_FALSE;
851 break;
853 if (runtimes[This->pos].found)
855 num_fetched++;
857 This->pos++;
860 return hr;
863 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
865 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
867 TRACE("(%p)\n", iface);
869 This->pos = 0;
871 return S_OK;
874 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
876 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
877 struct InstalledRuntimeEnum *new_enum;
879 TRACE("(%p)\n", iface);
881 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
882 if (!new_enum)
883 return E_OUTOFMEMORY;
885 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
886 new_enum->ref = 1;
887 new_enum->pos = This->pos;
889 *ppenum = &new_enum->IEnumUnknown_iface;
891 return S_OK;
894 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
895 InstalledRuntimeEnum_QueryInterface,
896 InstalledRuntimeEnum_AddRef,
897 InstalledRuntimeEnum_Release,
898 InstalledRuntimeEnum_Next,
899 InstalledRuntimeEnum_Skip,
900 InstalledRuntimeEnum_Reset,
901 InstalledRuntimeEnum_Clone
904 struct CLRMetaHost
906 ICLRMetaHost ICLRMetaHost_iface;
908 RuntimeLoadedCallbackFnPtr callback;
911 static struct CLRMetaHost GlobalCLRMetaHost;
913 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
914 REFIID riid,
915 void **ppvObject)
917 TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
919 if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
920 IsEqualGUID( riid, &IID_IUnknown ) )
922 *ppvObject = iface;
924 else
926 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
927 return E_NOINTERFACE;
930 ICLRMetaHost_AddRef( iface );
932 return S_OK;
935 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
937 return 2;
940 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
942 return 1;
945 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
947 *major = 0;
948 *minor = 0;
949 *build = 0;
951 if (version[0] == 'v' || version[0] == 'V')
953 version++;
954 if (!isdigit(*version))
955 return FALSE;
957 while (isdigit(*version))
958 *major = *major * 10 + (*version++ - '0');
960 if (*version == 0)
961 return TRUE;
963 if (*version++ != '.' || !isdigit(*version))
964 return FALSE;
966 while (isdigit(*version))
967 *minor = *minor * 10 + (*version++ - '0');
969 if (*version == 0)
970 return TRUE;
972 if (*version++ != '.' || !isdigit(*version))
973 return FALSE;
975 while (isdigit(*version))
976 *build = *build * 10 + (*version++ - '0');
978 return *version == 0;
980 else
981 return FALSE;
984 static HRESULT get_runtime(LPCWSTR pwzVersion, BOOL allow_short,
985 REFIID iid, LPVOID *ppRuntime)
987 int i;
988 DWORD major, minor, build;
990 if (!pwzVersion)
991 return E_POINTER;
993 if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
995 ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
996 return CLR_E_SHIM_RUNTIME;
999 find_runtimes();
1001 for (i=0; i<NUM_RUNTIMES; i++)
1003 if (runtimes[i].major == major && runtimes[i].minor == minor &&
1004 (runtimes[i].build == build || (allow_short && major >= 4 && build == 0)))
1006 if (runtimes[i].found)
1007 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface, iid,
1008 ppRuntime);
1009 else
1011 missing_runtime_message();
1012 return CLR_E_SHIM_RUNTIME;
1017 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
1018 return CLR_E_SHIM_RUNTIME;
1021 HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
1022 LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
1024 TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
1026 return get_runtime(pwzVersion, FALSE, iid, ppRuntime);
1029 HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
1030 LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
1032 ASSEMBLY *assembly;
1033 HRESULT hr;
1034 LPSTR version;
1035 ULONG buffer_size=*pcchBuffer;
1037 TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
1039 hr = assembly_create(&assembly, pwzFilePath);
1041 if (SUCCEEDED(hr))
1043 hr = assembly_get_runtime_version(assembly, &version);
1045 if (SUCCEEDED(hr))
1047 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
1049 if (pwzBuffer)
1051 if (buffer_size >= *pcchBuffer)
1052 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
1053 else
1054 hr = E_NOT_SUFFICIENT_BUFFER;
1058 assembly_release(assembly);
1061 return hr;
1064 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
1065 IEnumUnknown **ppEnumerator)
1067 struct InstalledRuntimeEnum *new_enum;
1069 TRACE("%p\n", ppEnumerator);
1071 find_runtimes();
1073 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
1074 if (!new_enum)
1075 return E_OUTOFMEMORY;
1077 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
1078 new_enum->ref = 1;
1079 new_enum->pos = 0;
1081 *ppEnumerator = &new_enum->IEnumUnknown_iface;
1083 return S_OK;
1086 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
1087 HANDLE hndProcess, IEnumUnknown **ppEnumerator)
1089 FIXME("%p %p\n", hndProcess, ppEnumerator);
1091 return E_NOTIMPL;
1094 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
1095 RuntimeLoadedCallbackFnPtr pCallbackFunction)
1097 TRACE("%p\n", pCallbackFunction);
1099 if(!pCallbackFunction)
1100 return E_POINTER;
1102 WARN("Callback currently will not be called.\n");
1104 GlobalCLRMetaHost.callback = pCallbackFunction;
1106 return S_OK;
1109 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
1110 REFIID riid, LPVOID *ppUnk)
1112 FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
1114 return E_NOTIMPL;
1117 HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
1119 TRACE("%i\n", iExitCode);
1121 EnterCriticalSection(&runtime_list_cs);
1123 if (is_mono_started && !is_mono_shutdown)
1125 /* search for a runtime and call System.Environment.Exit() */
1126 int i;
1128 for (i=0; i<NUM_RUNTIMES; i++)
1129 if (runtimes[i].loaded_runtime)
1130 RuntimeHost_ExitProcess(runtimes[i].loaded_runtime, iExitCode);
1133 ExitProcess(iExitCode);
1136 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
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
1150 static struct CLRMetaHost GlobalCLRMetaHost = {
1151 { &CLRMetaHost_vtbl }
1154 HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1156 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost.ICLRMetaHost_iface, riid, ppobj);
1159 struct CLRMetaHostPolicy
1161 ICLRMetaHostPolicy ICLRMetaHostPolicy_iface;
1164 static struct CLRMetaHostPolicy GlobalCLRMetaHostPolicy;
1166 static HRESULT WINAPI metahostpolicy_QueryInterface(ICLRMetaHostPolicy *iface, REFIID riid, void **obj)
1168 TRACE("%s %p\n", debugstr_guid(riid), obj);
1170 if ( IsEqualGUID( riid, &IID_ICLRMetaHostPolicy ) ||
1171 IsEqualGUID( riid, &IID_IUnknown ) )
1173 ICLRMetaHostPolicy_AddRef( iface );
1174 *obj = iface;
1175 return S_OK;
1178 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
1180 *obj = NULL;
1181 return E_NOINTERFACE;
1184 static ULONG WINAPI metahostpolicy_AddRef(ICLRMetaHostPolicy *iface)
1186 return 2;
1189 static ULONG WINAPI metahostpolicy_Release(ICLRMetaHostPolicy *iface)
1191 return 1;
1194 static HRESULT WINAPI metahostpolicy_GetRequestedRuntime(ICLRMetaHostPolicy *iface, METAHOST_POLICY_FLAGS dwPolicyFlags,
1195 LPCWSTR pwzBinary, IStream *pCfgStream, LPWSTR pwzVersion, DWORD *pcchVersion,
1196 LPWSTR pwzImageVersion, DWORD *pcchImageVersion, DWORD *pdwConfigFlags, REFIID riid,
1197 LPVOID *ppRuntime)
1199 ICLRRuntimeInfo *result;
1200 HRESULT hr;
1201 WCHAR filename[MAX_PATH];
1202 const WCHAR *path = NULL;
1203 int flags = 0;
1205 TRACE("%d %p %p %p %p %p %p %p %s %p\n", dwPolicyFlags, pwzBinary, pCfgStream,
1206 pwzVersion, pcchVersion, pwzImageVersion, pcchImageVersion, pdwConfigFlags,
1207 debugstr_guid(riid), ppRuntime);
1209 if (pCfgStream)
1210 FIXME("ignoring config file stream\n");
1212 if (pdwConfigFlags)
1213 FIXME("ignoring config flags\n");
1215 if(dwPolicyFlags & METAHOST_POLICY_USE_PROCESS_IMAGE_PATH)
1217 GetModuleFileNameW(0, filename, MAX_PATH);
1218 path = filename;
1220 else if(pwzBinary)
1222 path = pwzBinary;
1225 if(dwPolicyFlags & METAHOST_POLICY_APPLY_UPGRADE_POLICY)
1226 flags |= RUNTIME_INFO_UPGRADE_VERSION;
1228 hr = get_runtime_info(path, pwzImageVersion, NULL, 0, flags, FALSE, &result);
1229 if (SUCCEEDED(hr))
1231 if (pwzImageVersion)
1233 /* Ignoring errors on purpose */
1234 ICLRRuntimeInfo_GetVersionString(result, pwzImageVersion, pcchImageVersion);
1237 hr = ICLRRuntimeInfo_QueryInterface(result, riid, ppRuntime);
1239 ICLRRuntimeInfo_Release(result);
1242 TRACE("<- 0x%08x\n", hr);
1244 return hr;
1247 static const struct ICLRMetaHostPolicyVtbl CLRMetaHostPolicy_vtbl =
1249 metahostpolicy_QueryInterface,
1250 metahostpolicy_AddRef,
1251 metahostpolicy_Release,
1252 metahostpolicy_GetRequestedRuntime
1255 static struct CLRMetaHostPolicy GlobalCLRMetaHostPolicy = {
1256 { &CLRMetaHostPolicy_vtbl }
1259 HRESULT CLRMetaHostPolicy_CreateInstance(REFIID riid, void **ppobj)
1261 return ICLRMetaHostPolicy_QueryInterface(&GlobalCLRMetaHostPolicy.ICLRMetaHostPolicy_iface, riid, ppobj);
1264 HRESULT get_file_from_strongname(WCHAR* stringnameW, WCHAR* assemblies_path, int path_length)
1266 HRESULT hr=S_OK;
1267 IAssemblyCache *asmcache;
1268 ASSEMBLY_INFO info;
1269 static const WCHAR fusiondll[] = {'f','u','s','i','o','n',0};
1270 HMODULE hfusion=NULL;
1271 static HRESULT (WINAPI *pCreateAssemblyCache)(IAssemblyCache**,DWORD);
1273 if (!pCreateAssemblyCache)
1275 hr = LoadLibraryShim(fusiondll, NULL, NULL, &hfusion);
1277 if (SUCCEEDED(hr))
1279 pCreateAssemblyCache = (void*)GetProcAddress(hfusion, "CreateAssemblyCache");
1280 if (!pCreateAssemblyCache)
1281 hr = E_FAIL;
1285 if (SUCCEEDED(hr))
1286 hr = pCreateAssemblyCache(&asmcache, 0);
1288 if (SUCCEEDED(hr))
1290 info.cbAssemblyInfo = sizeof(info);
1291 info.pszCurrentAssemblyPathBuf = assemblies_path;
1292 info.cchBuf = path_length;
1293 assemblies_path[0] = 0;
1295 hr = IAssemblyCache_QueryAssemblyInfo(asmcache, 0, stringnameW, &info);
1297 IAssemblyCache_Release(asmcache);
1300 return hr;
1303 static MonoAssembly* mono_assembly_preload_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data)
1305 HRESULT hr;
1306 MonoAssembly *result=NULL;
1307 char *stringname=NULL;
1308 LPWSTR stringnameW;
1309 int stringnameW_size;
1310 WCHAR path[MAX_PATH];
1311 char *pathA;
1312 MonoImageOpenStatus stat;
1314 stringname = mono_stringify_assembly_name(aname);
1316 TRACE("%s\n", debugstr_a(stringname));
1318 if (!stringname) return NULL;
1320 /* FIXME: We should search the given paths before the GAC. */
1322 stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, stringname, -1, NULL, 0);
1324 stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1325 if (stringnameW)
1327 MultiByteToWideChar(CP_UTF8, 0, stringname, -1, stringnameW, stringnameW_size);
1329 hr = get_file_from_strongname(stringnameW, path, MAX_PATH);
1331 HeapFree(GetProcessHeap(), 0, stringnameW);
1333 else
1334 hr = E_OUTOFMEMORY;
1336 if (SUCCEEDED(hr))
1338 TRACE("found: %s\n", debugstr_w(path));
1340 pathA = WtoA(path);
1342 if (pathA)
1344 result = mono_assembly_open(pathA, &stat);
1346 if (!result)
1347 ERR("Failed to load %s, status=%u\n", debugstr_w(path), stat);
1349 HeapFree(GetProcessHeap(), 0, pathA);
1353 mono_free(stringname);
1355 return result;
1358 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1359 DWORD startup_flags, DWORD runtimeinfo_flags, BOOL legacy, ICLRRuntimeInfo **result)
1361 static const WCHAR dotconfig[] = {'.','c','o','n','f','i','g',0};
1362 static const DWORD supported_startup_flags = 0;
1363 static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1364 int i;
1365 WCHAR local_version[MAX_PATH];
1366 ULONG local_version_size = MAX_PATH;
1367 WCHAR local_config_file[MAX_PATH];
1368 HRESULT hr;
1369 parsed_config_file parsed_config;
1371 if (startup_flags & ~supported_startup_flags)
1372 FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
1374 if (runtimeinfo_flags & ~supported_runtime_flags)
1375 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
1377 if (exefile && !exefile[0])
1378 exefile = NULL;
1380 if (exefile && !config_file)
1382 strcpyW(local_config_file, exefile);
1383 strcatW(local_config_file, dotconfig);
1385 config_file = local_config_file;
1388 if (config_file)
1390 BOOL found = FALSE;
1391 hr = parse_config_file(config_file, &parsed_config);
1393 if (SUCCEEDED(hr))
1395 supported_runtime *entry;
1396 LIST_FOR_EACH_ENTRY(entry, &parsed_config.supported_runtimes, supported_runtime, entry)
1398 hr = get_runtime(entry->version, TRUE, &IID_ICLRRuntimeInfo, (void**)result);
1399 if (SUCCEEDED(hr))
1401 found = TRUE;
1402 break;
1406 else
1408 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file), hr);
1411 free_parsed_config_file(&parsed_config);
1413 if (found)
1414 return S_OK;
1417 if (exefile && !version)
1419 hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1421 version = local_version;
1423 if (FAILED(hr)) return hr;
1426 if (version)
1428 hr = CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1429 if(SUCCEEDED(hr))
1430 return hr;
1433 if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1435 DWORD major, minor, build;
1437 if (version && !parse_runtime_version(version, &major, &minor, &build))
1439 ERR("Cannot parse %s\n", debugstr_w(version));
1440 return CLR_E_SHIM_RUNTIME;
1443 find_runtimes();
1445 if (legacy)
1446 i = 3;
1447 else
1448 i = NUM_RUNTIMES;
1450 while (i--)
1452 if (runtimes[i].found)
1454 /* Must be greater or equal to the version passed in. */
1455 if (!version || ((runtimes[i].major >= major && runtimes[i].minor >= minor && runtimes[i].build >= build) ||
1456 (runtimes[i].major >= major && runtimes[i].minor > minor) ||
1457 (runtimes[i].major > major)))
1459 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface,
1460 &IID_ICLRRuntimeInfo, (void **)result);
1465 missing_runtime_message();
1467 return CLR_E_SHIM_RUNTIME;
1470 return CLR_E_SHIM_RUNTIME;