wined3d: Get rid of the "render_to_fbo" field from the wined3d_swapchain structure.
[wine.git] / dlls / mscoree / metahost.c
blob33ed2462125c4a496f9731b086adb22d867a6486
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 "windef.h"
28 #include "winbase.h"
29 #include "winreg.h"
30 #include "winternl.h"
31 #include "ole2.h"
32 #include "shlwapi.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 "wine/heap.h"
43 #include "mscoree_private.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL( mscoree );
49 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl;
51 #define NUM_RUNTIMES 4
53 static struct CLRRuntimeInfo runtimes[NUM_RUNTIMES] = {
54 {{&CLRRuntimeInfoVtbl}, 1, 0, 3705, 0},
55 {{&CLRRuntimeInfoVtbl}, 1, 1, 4322, 0},
56 {{&CLRRuntimeInfoVtbl}, 2, 0, 50727, 0},
57 {{&CLRRuntimeInfoVtbl}, 4, 0, 30319, 0}
60 static CRITICAL_SECTION runtime_list_cs;
61 static CRITICAL_SECTION_DEBUG runtime_list_cs_debug =
63 0, 0, &runtime_list_cs,
64 { &runtime_list_cs_debug.ProcessLocksList,
65 &runtime_list_cs_debug.ProcessLocksList },
66 0, 0, { (DWORD_PTR)(__FILE__ ": runtime_list_cs") }
68 static CRITICAL_SECTION runtime_list_cs = { &runtime_list_cs_debug, -1, 0, 0, 0, 0 };
70 struct CLRMetaHost
72 ICLRMetaHost ICLRMetaHost_iface;
74 RuntimeLoadedCallbackFnPtr callback;
77 static struct CLRMetaHost GlobalCLRMetaHost;
79 static HMODULE mono_handle;
81 BOOL is_mono_started;
82 static BOOL is_mono_shutdown;
84 typedef struct _MonoProfilerDesc *MonoProfilerHandle;
86 typedef void (CDECL *MonoProfilerRuntimeShutdownBeginCallback) (MonoProfiler *prof);
88 MonoImage* (CDECL *mono_assembly_get_image)(MonoAssembly *assembly);
89 MonoAssembly* (CDECL *mono_assembly_load_from)(MonoImage *image, const char *fname, MonoImageOpenStatus *status);
90 const char* (CDECL *mono_assembly_name_get_name)(MonoAssemblyName *aname);
91 const char* (CDECL *mono_assembly_name_get_culture)(MonoAssemblyName *aname);
92 WORD (CDECL *mono_assembly_name_get_version)(MonoAssemblyName *aname, WORD *minor, WORD *build, WORD *revision);
93 MonoAssembly* (CDECL *mono_assembly_open)(const char *filename, MonoImageOpenStatus *status);
94 void (CDECL *mono_callspec_set_assembly)(MonoAssembly *assembly);
95 MonoClass* (CDECL *mono_class_from_mono_type)(MonoType *type);
96 MonoClass* (CDECL *mono_class_from_name)(MonoImage *image, const char* name_space, const char *name);
97 MonoMethod* (CDECL *mono_class_get_method_from_name)(MonoClass *klass, const char *name, int param_count);
98 static void (CDECL *mono_config_parse)(const char *filename);
99 MonoDomain* (CDECL *mono_domain_get)(void);
100 MonoDomain* (CDECL *mono_domain_get_by_id)(int id);
101 BOOL (CDECL *mono_domain_set)(MonoDomain *domain,BOOL force);
102 void (CDECL *mono_domain_set_config)(MonoDomain *domain,const char *base_dir,const char *config_file_name);
103 static void (CDECL *mono_free)(void *);
104 MonoImage* (CDECL *mono_get_corlib)(void);
105 static MonoImage* (CDECL *mono_image_open)(const char *fname, MonoImageOpenStatus *status);
106 MonoImage* (CDECL *mono_image_open_from_module_handle)(HMODULE module_handle, char* fname, UINT has_entry_point, MonoImageOpenStatus* status);
107 static void (CDECL *mono_install_assembly_preload_hook)(MonoAssemblyPreLoadFunc func, void *user_data);
108 int (CDECL *mono_jit_exec)(MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[]);
109 MonoDomain* (CDECL *mono_jit_init_version)(const char *domain_name, const char *runtime_version);
110 static void (CDECL *mono_jit_set_aot_mode)(MonoAotMode mode);
111 static int (CDECL *mono_jit_set_trace_options)(const char* options);
112 void* (CDECL *mono_marshal_get_vtfixup_ftnptr)(MonoImage *image, DWORD token, WORD type);
113 MonoDomain* (CDECL *mono_object_get_domain)(MonoObject *obj);
114 MonoMethod* (CDECL *mono_object_get_virtual_method)(MonoObject *obj, MonoMethod *method);
115 MonoObject* (CDECL *mono_object_new)(MonoDomain *domain, MonoClass *klass);
116 void* (CDECL *mono_object_unbox)(MonoObject *obj);
117 static MonoProfilerHandle (CDECL *mono_profiler_create)(MonoProfiler *prof);
118 static void (CDECL *mono_profiler_install)(MonoProfiler *prof, MonoProfileFunc shutdown_callback);
119 static void (CDECL *mono_profiler_set_runtime_shutdown_begin_callback)(MonoProfilerHandle handle, MonoProfilerRuntimeShutdownBeginCallback cb);
120 MonoType* (CDECL *mono_reflection_type_from_name)(char *name, MonoImage *image);
121 MonoObject* (CDECL *mono_runtime_invoke)(MonoMethod *method, void *obj, void **params, MonoObject **exc);
122 void (CDECL *mono_runtime_object_init)(MonoObject *this_obj);
123 void (CDECL *mono_runtime_quit)(void);
124 static void (CDECL *mono_set_crash_chaining)(BOOL chain_signals);
125 static void (CDECL *mono_set_dirs)(const char *assembly_dir, const char *config_dir);
126 static void (CDECL *mono_set_verbose_level)(DWORD level);
127 MonoString* (CDECL *mono_string_new)(MonoDomain *domain, const char *str);
128 static char* (CDECL *mono_stringify_assembly_name)(MonoAssemblyName *aname);
129 MonoThread* (CDECL *mono_thread_attach)(MonoDomain *domain);
130 void (CDECL *mono_thread_manage)(void);
131 void (CDECL *mono_trace_set_print_handler)(MonoPrintCallback callback);
132 void (CDECL *mono_trace_set_printerr_handler)(MonoPrintCallback callback);
134 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path);
136 static MonoAssembly* CDECL mono_assembly_preload_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data);
138 static void CDECL mono_shutdown_callback_fn(MonoProfiler *prof);
140 static MonoImage* CDECL image_open_module_handle_dummy(HMODULE module_handle,
141 char* fname, UINT has_entry_point, MonoImageOpenStatus* status)
143 return mono_image_open(fname, status);
146 static void CDECL set_crash_chaining_dummy(BOOL crash_chaining)
150 static void CDECL set_print_handler_dummy(MonoPrintCallback callback)
154 static HRESULT load_mono(LPCWSTR mono_path)
156 static const WCHAR lib[] = {'\\','l','i','b',0};
157 static const WCHAR etc[] = {'\\','e','t','c',0};
158 WCHAR mono_dll_path[MAX_PATH+16];
159 WCHAR mono_lib_path[MAX_PATH+4], mono_etc_path[MAX_PATH+4];
160 char mono_lib_path_a[MAX_PATH], mono_etc_path_a[MAX_PATH];
161 int aot_size;
162 char aot_setting[256];
163 int trace_size;
164 char trace_setting[256];
165 int verbose_size;
166 char verbose_setting[256];
168 if (is_mono_shutdown)
170 ERR("Cannot load Mono after it has been shut down.\n");
171 return E_FAIL;
174 if (!mono_handle)
176 lstrcpyW(mono_lib_path, mono_path);
177 lstrcatW(mono_lib_path, lib);
178 WideCharToMultiByte(CP_UTF8, 0, mono_lib_path, -1, mono_lib_path_a, MAX_PATH, NULL, NULL);
180 lstrcpyW(mono_etc_path, mono_path);
181 lstrcatW(mono_etc_path, etc);
182 WideCharToMultiByte(CP_UTF8, 0, mono_etc_path, -1, mono_etc_path_a, MAX_PATH, NULL, NULL);
184 if (!find_mono_dll(mono_path, mono_dll_path)) goto fail;
186 mono_handle = LoadLibraryW(mono_dll_path);
188 if (!mono_handle) goto fail;
190 #define LOAD_MONO_FUNCTION(x) do { \
191 x = (void*)GetProcAddress(mono_handle, #x); \
192 if (!x) { \
193 goto fail; \
195 } while (0);
197 LOAD_MONO_FUNCTION(mono_assembly_get_image);
198 LOAD_MONO_FUNCTION(mono_assembly_load_from);
199 LOAD_MONO_FUNCTION(mono_assembly_name_get_name);
200 LOAD_MONO_FUNCTION(mono_assembly_name_get_culture);
201 LOAD_MONO_FUNCTION(mono_assembly_name_get_version);
202 LOAD_MONO_FUNCTION(mono_assembly_open);
203 LOAD_MONO_FUNCTION(mono_config_parse);
204 LOAD_MONO_FUNCTION(mono_class_from_mono_type);
205 LOAD_MONO_FUNCTION(mono_class_from_name);
206 LOAD_MONO_FUNCTION(mono_class_get_method_from_name);
207 LOAD_MONO_FUNCTION(mono_domain_get);
208 LOAD_MONO_FUNCTION(mono_domain_get_by_id);
209 LOAD_MONO_FUNCTION(mono_domain_set);
210 LOAD_MONO_FUNCTION(mono_domain_set_config);
211 LOAD_MONO_FUNCTION(mono_free);
212 LOAD_MONO_FUNCTION(mono_get_corlib);
213 LOAD_MONO_FUNCTION(mono_image_open);
214 LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook);
215 LOAD_MONO_FUNCTION(mono_jit_exec);
216 LOAD_MONO_FUNCTION(mono_jit_init_version);
217 LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
218 LOAD_MONO_FUNCTION(mono_marshal_get_vtfixup_ftnptr);
219 LOAD_MONO_FUNCTION(mono_object_get_domain);
220 LOAD_MONO_FUNCTION(mono_object_get_virtual_method);
221 LOAD_MONO_FUNCTION(mono_object_new);
222 LOAD_MONO_FUNCTION(mono_object_unbox);
223 LOAD_MONO_FUNCTION(mono_reflection_type_from_name);
224 LOAD_MONO_FUNCTION(mono_runtime_invoke);
225 LOAD_MONO_FUNCTION(mono_runtime_object_init);
226 LOAD_MONO_FUNCTION(mono_runtime_quit);
227 LOAD_MONO_FUNCTION(mono_set_dirs);
228 LOAD_MONO_FUNCTION(mono_set_verbose_level);
229 LOAD_MONO_FUNCTION(mono_stringify_assembly_name);
230 LOAD_MONO_FUNCTION(mono_string_new);
231 LOAD_MONO_FUNCTION(mono_thread_attach);
232 LOAD_MONO_FUNCTION(mono_thread_manage);
234 #undef LOAD_MONO_FUNCTION
236 #define LOAD_OPT_MONO_FUNCTION(x, default) do { \
237 x = (void*)GetProcAddress(mono_handle, #x); \
238 if (!x) { \
239 x = default; \
241 } while (0);
243 LOAD_OPT_MONO_FUNCTION(mono_callspec_set_assembly, NULL);
244 LOAD_OPT_MONO_FUNCTION(mono_image_open_from_module_handle, image_open_module_handle_dummy);
245 LOAD_OPT_MONO_FUNCTION(mono_jit_set_aot_mode, NULL);
246 LOAD_OPT_MONO_FUNCTION(mono_profiler_create, NULL);
247 LOAD_OPT_MONO_FUNCTION(mono_profiler_install, NULL);
248 LOAD_OPT_MONO_FUNCTION(mono_profiler_set_runtime_shutdown_begin_callback, NULL);
249 LOAD_OPT_MONO_FUNCTION(mono_set_crash_chaining, set_crash_chaining_dummy);
250 LOAD_OPT_MONO_FUNCTION(mono_trace_set_print_handler, set_print_handler_dummy);
251 LOAD_OPT_MONO_FUNCTION(mono_trace_set_printerr_handler, set_print_handler_dummy);
253 #undef LOAD_OPT_MONO_FUNCTION
255 if (mono_callspec_set_assembly == NULL)
257 mono_callspec_set_assembly = (void*)GetProcAddress(mono_handle, "mono_trace_set_assembly");
258 if (!mono_callspec_set_assembly) goto fail;
261 if (mono_profiler_create != NULL)
263 /* Profiler API v2 */
264 MonoProfilerHandle handle = mono_profiler_create(NULL);
265 mono_profiler_set_runtime_shutdown_begin_callback(handle, mono_shutdown_callback_fn);
267 else if (mono_profiler_install != NULL)
269 /* Profiler API v1 */
270 mono_profiler_install(NULL, mono_shutdown_callback_fn);
273 mono_set_crash_chaining(TRUE);
275 mono_trace_set_print_handler(mono_print_handler_fn);
276 mono_trace_set_printerr_handler(mono_print_handler_fn);
278 mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
280 mono_config_parse(NULL);
282 mono_install_assembly_preload_hook(mono_assembly_preload_hook_fn, NULL);
284 aot_size = GetEnvironmentVariableA("WINE_MONO_AOT", aot_setting, sizeof(aot_setting));
286 if (aot_size)
288 MonoAotMode mode;
289 if (strcmp(aot_setting, "interp") == 0)
290 mode = MONO_AOT_MODE_INTERP_ONLY;
291 else if (strcmp(aot_setting, "none") == 0)
292 mode = MONO_AOT_MODE_NONE;
293 else
295 ERR("unknown WINE_MONO_AOT setting, valid settings are interp and none\n");
296 mode = MONO_AOT_MODE_NONE;
298 if (mono_jit_set_aot_mode != NULL)
300 mono_jit_set_aot_mode(mode);
302 else
304 ERR("mono_jit_set_aot_mode export not found\n");
308 trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
310 if (trace_size)
312 mono_jit_set_trace_options(trace_setting);
315 verbose_size = GetEnvironmentVariableA("WINE_MONO_VERBOSE", verbose_setting, sizeof(verbose_setting));
317 if (verbose_size)
319 mono_set_verbose_level(verbose_setting[0] - '0');
323 return S_OK;
325 fail:
326 ERR("Could not load Mono into this process\n");
327 FreeLibrary(mono_handle);
328 mono_handle = NULL;
329 return E_FAIL;
332 static char* get_exe_basename_utf8(void)
334 WCHAR filenameW[MAX_PATH], *basenameW;
336 GetModuleFileNameW(NULL, filenameW, MAX_PATH);
338 basenameW = wcsrchr(filenameW, '\\');
339 if (basenameW)
340 basenameW += 1;
341 else
342 basenameW = filenameW;
344 return WtoA(basenameW);
347 MonoDomain* get_root_domain(void)
349 static MonoDomain* root_domain;
351 if (root_domain != NULL)
352 return root_domain;
354 EnterCriticalSection(&runtime_list_cs);
356 if (root_domain == NULL)
358 char *exe_basename;
360 exe_basename = get_exe_basename_utf8();
362 root_domain = mono_jit_init_version(exe_basename, "v4.0.30319");
364 HeapFree(GetProcessHeap(), 0, exe_basename);
366 is_mono_started = TRUE;
369 LeaveCriticalSection(&runtime_list_cs);
371 return root_domain;
374 static void CDECL mono_shutdown_callback_fn(MonoProfiler *prof)
376 is_mono_shutdown = TRUE;
379 static HRESULT WINAPI thread_set_fn(void)
381 WARN("stub\n");
382 return S_OK;
385 static HRESULT WINAPI thread_unset_fn(void)
387 WARN("stub\n");
388 return S_OK;
391 static HRESULT CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo *This, RuntimeHost **result)
393 HRESULT hr = S_OK;
394 WCHAR mono_path[MAX_PATH];
396 if (This->loaded_runtime)
398 *result = This->loaded_runtime;
399 return hr;
402 if (!get_mono_path(mono_path, FALSE))
404 ERR("Wine Mono is not installed\n");
405 return CLR_E_SHIM_RUNTIME;
408 EnterCriticalSection(&runtime_list_cs);
410 if (This->loaded_runtime)
412 *result = This->loaded_runtime;
413 LeaveCriticalSection(&runtime_list_cs);
414 return hr;
417 if (GlobalCLRMetaHost.callback)
419 GlobalCLRMetaHost.callback(&This->ICLRRuntimeInfo_iface, thread_set_fn, thread_unset_fn);
422 hr = load_mono(mono_path);
424 if (SUCCEEDED(hr))
425 hr = RuntimeHost_Construct(This, &This->loaded_runtime);
427 LeaveCriticalSection(&runtime_list_cs);
429 if (SUCCEEDED(hr))
430 *result = This->loaded_runtime;
432 return hr;
435 void expect_no_runtimes(void)
437 if (mono_handle && is_mono_started && !is_mono_shutdown)
439 ERR("Process exited with a Mono runtime loaded.\n");
440 return;
444 static inline CLRRuntimeInfo *impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo *iface)
446 return CONTAINING_RECORD(iface, CLRRuntimeInfo, ICLRRuntimeInfo_iface);
449 static HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
450 REFIID riid,
451 void **ppvObject)
453 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
455 if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
456 IsEqualGUID( riid, &IID_IUnknown ) )
458 *ppvObject = iface;
460 else
462 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
463 return E_NOINTERFACE;
466 ICLRRuntimeInfo_AddRef( iface );
468 return S_OK;
471 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
473 return 2;
476 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
478 return 1;
481 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
482 LPWSTR pwzBuffer, DWORD *pcchBuffer)
484 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
485 DWORD buffer_size = *pcchBuffer;
486 HRESULT hr = S_OK;
487 char version[11];
488 DWORD size;
490 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
492 size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
494 assert(size <= sizeof(version));
496 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
498 if (pwzBuffer)
500 if (buffer_size >= *pcchBuffer)
501 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
502 else
503 hr = E_NOT_SUFFICIENT_BUFFER;
506 return hr;
509 static BOOL get_install_root(LPWSTR install_dir)
511 static 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};
512 static const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
514 DWORD len;
515 HKEY key;
517 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
518 return FALSE;
520 len = MAX_PATH * sizeof(WCHAR);
521 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
523 RegCloseKey(key);
524 return FALSE;
526 RegCloseKey(key);
528 return TRUE;
531 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
532 LPWSTR pwzBuffer, DWORD *pcchBuffer)
534 static const WCHAR slash[] = {'\\',0};
535 DWORD buffer_size = *pcchBuffer;
536 WCHAR system_dir[MAX_PATH];
537 WCHAR version[MAX_PATH];
538 DWORD version_size, size;
539 HRESULT hr = S_OK;
541 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
543 if (!get_install_root(system_dir))
545 ERR("error reading registry key for installroot\n");
546 return E_FAIL;
548 else
550 version_size = MAX_PATH;
551 ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
552 lstrcatW(system_dir, version);
553 lstrcatW(system_dir, slash);
554 size = lstrlenW(system_dir) + 1;
557 *pcchBuffer = size;
559 if (pwzBuffer)
561 if (buffer_size >= size)
562 lstrcpyW(pwzBuffer, system_dir);
563 else
564 hr = E_NOT_SUFFICIENT_BUFFER;
567 return hr;
570 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
571 HANDLE hndProcess, BOOL *pbLoaded)
573 FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
575 return E_NOTIMPL;
578 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
579 UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
581 FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
583 return E_NOTIMPL;
586 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
587 LPCWSTR pwzDllName, HMODULE *phndModule)
589 WCHAR version[MAX_PATH];
590 HRESULT hr;
591 DWORD cchBuffer;
593 TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
595 cchBuffer = MAX_PATH;
596 hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
597 if (FAILED(hr)) return hr;
599 return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
602 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
603 LPCSTR pszProcName, LPVOID *ppProc)
605 FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
607 return E_NOTIMPL;
610 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
611 REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
613 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
614 RuntimeHost *host;
615 HRESULT hr;
617 TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
619 hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
621 if (SUCCEEDED(hr))
622 hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
624 return hr;
627 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
628 BOOL *pbLoadable)
630 FIXME("%p %p\n", iface, pbLoadable);
632 return E_NOTIMPL;
635 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
636 DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
638 FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
640 return E_NOTIMPL;
643 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
644 DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
646 FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
648 return E_NOTIMPL;
651 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
653 FIXME("%p\n", iface);
655 return E_NOTIMPL;
658 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
659 BOOL *pbStarted, DWORD *pdwStartupFlags)
661 FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
663 return E_NOTIMPL;
666 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
667 CLRRuntimeInfo_QueryInterface,
668 CLRRuntimeInfo_AddRef,
669 CLRRuntimeInfo_Release,
670 CLRRuntimeInfo_GetVersionString,
671 CLRRuntimeInfo_GetRuntimeDirectory,
672 CLRRuntimeInfo_IsLoaded,
673 CLRRuntimeInfo_LoadErrorString,
674 CLRRuntimeInfo_LoadLibrary,
675 CLRRuntimeInfo_GetProcAddress,
676 CLRRuntimeInfo_GetInterface,
677 CLRRuntimeInfo_IsLoadable,
678 CLRRuntimeInfo_SetDefaultStartupFlags,
679 CLRRuntimeInfo_GetDefaultStartupFlags,
680 CLRRuntimeInfo_BindAsLegacyV2Runtime,
681 CLRRuntimeInfo_IsStarted
684 HRESULT ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo *iface, RuntimeHost **result)
686 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
688 assert(This->ICLRRuntimeInfo_iface.lpVtbl == &CLRRuntimeInfoVtbl);
690 return CLRRuntimeInfo_GetRuntimeHost(This, result);
693 #ifdef __i386__
694 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};
695 #elif defined(__x86_64__)
696 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};
697 #else
698 static const WCHAR libmono2_arch_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
699 #endif
701 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path)
703 static const WCHAR mono2_dll[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
704 static const WCHAR libmono2_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
705 DWORD attributes=INVALID_FILE_ATTRIBUTES;
707 lstrcpyW(dll_path, path);
708 lstrcatW(dll_path, libmono2_arch_dll);
709 attributes = GetFileAttributesW(dll_path);
711 if (attributes == INVALID_FILE_ATTRIBUTES)
713 lstrcpyW(dll_path, path);
714 lstrcatW(dll_path, mono2_dll);
715 attributes = GetFileAttributesW(dll_path);
718 if (attributes == INVALID_FILE_ATTRIBUTES)
720 lstrcpyW(dll_path, path);
721 lstrcatW(dll_path, libmono2_dll);
722 attributes = GetFileAttributesW(dll_path);
725 return (attributes != INVALID_FILE_ATTRIBUTES);
728 static BOOL get_mono_path_local(LPWSTR path)
730 static const WCHAR subdir_mono[] = {'\\','m','o','n','o','\\','m','o','n','o','-','2','.','0', 0};
731 WCHAR base_path[MAX_PATH], mono_dll_path[MAX_PATH];
733 /* c:\windows\mono\mono-2.0 */
734 GetWindowsDirectoryW(base_path, MAX_PATH);
735 lstrcatW(base_path, subdir_mono);
737 if (find_mono_dll(base_path, mono_dll_path))
739 lstrcpyW(path, base_path);
740 return TRUE;
743 return FALSE;
746 static BOOL get_mono_path_registry(LPWSTR path)
748 static const WCHAR keyname[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\','M','o','n','o',0};
749 static const WCHAR valuename[] = {'R','u','n','t','i','m','e','P','a','t','h',0};
750 WCHAR base_path[MAX_PATH], mono_dll_path[MAX_PATH];
751 HKEY hkey;
752 DWORD res, valuesize;
753 BOOL ret=FALSE;
755 /* @@ Wine registry key: HKCU\Software\Wine\Mono */
756 res = RegOpenKeyW(HKEY_CURRENT_USER, keyname, &hkey);
757 if (res != ERROR_SUCCESS)
758 return FALSE;
760 valuesize = sizeof(base_path);
761 res = RegGetValueW(hkey, NULL, valuename, RRF_RT_REG_SZ, NULL, base_path, &valuesize);
762 if (res == ERROR_SUCCESS && find_mono_dll(base_path, mono_dll_path))
764 lstrcpyW(path, base_path);
765 ret = TRUE;
768 RegCloseKey(hkey);
770 return ret;
773 static BOOL get_mono_path_dos(const WCHAR *dir, LPWSTR path)
775 static const WCHAR unix_prefix[] = {'\\','\\','?','\\','u','n','i','x','\\'};
776 static const WCHAR basedir[] = L"\\wine-mono-" WINE_MONO_VERSION;
777 LPWSTR dos_dir;
778 WCHAR mono_dll_path[MAX_PATH];
779 DWORD len;
780 BOOL ret;
782 if (memcmp(dir, unix_prefix, sizeof(unix_prefix)) == 0)
783 return FALSE; /* No drive letter for this directory */
785 len = lstrlenW( dir ) + lstrlenW( basedir ) + 1;
786 if (!(dos_dir = heap_alloc( len * sizeof(WCHAR) ))) return FALSE;
787 lstrcpyW( dos_dir, dir );
788 lstrcatW( dos_dir, basedir );
790 ret = find_mono_dll(dos_dir, mono_dll_path);
791 if (ret)
792 lstrcpyW(path, dos_dir);
794 heap_free(dos_dir);
796 return ret;
799 static BOOL get_mono_path_unix(const char *unix_dir, LPWSTR path)
801 static WCHAR * (CDECL *p_wine_get_dos_file_name)(const char*);
802 LPWSTR dos_dir;
803 BOOL ret;
805 if (!p_wine_get_dos_file_name)
807 p_wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
808 if (!p_wine_get_dos_file_name)
809 return FALSE;
812 dos_dir = p_wine_get_dos_file_name(unix_dir);
813 if (!dos_dir)
814 return FALSE;
816 ret = get_mono_path_dos( dos_dir, path);
818 heap_free(dos_dir);
819 return ret;
822 static BOOL get_mono_path_datadir(LPWSTR path)
824 static const WCHAR winedatadirW[] = {'W','I','N','E','D','A','T','A','D','I','R',0};
825 static const WCHAR winebuilddirW[] = {'W','I','N','E','B','U','I','L','D','D','I','R',0};
826 static const WCHAR unix_prefix[] = {'\\','?','?','\\','u','n','i','x','\\'};
827 static const WCHAR monoW[] = {'\\','m','o','n','o',0};
828 static const WCHAR dotdotmonoW[] = {'\\','.','.','\\','m','o','n','o',0};
829 const WCHAR *data_dir, *suffix;
830 WCHAR *package_dir;
831 BOOL ret;
833 if ((data_dir = _wgetenv( winedatadirW )))
834 suffix = monoW;
835 else if ((data_dir = _wgetenv( winebuilddirW )))
836 suffix = dotdotmonoW;
837 else
838 return FALSE;
840 if (!wcsncmp( data_dir, unix_prefix, wcslen(unix_prefix) )) return FALSE;
841 data_dir += 4; /* skip \??\ prefix */
842 package_dir = heap_alloc( (lstrlenW(data_dir) + lstrlenW(suffix) + 1) * sizeof(WCHAR));
843 lstrcpyW( package_dir, data_dir );
844 lstrcatW( package_dir, suffix );
846 ret = get_mono_path_dos(package_dir, path);
848 heap_free(package_dir);
850 return ret;
853 BOOL get_mono_path(LPWSTR path, BOOL skip_local)
855 return (!skip_local && get_mono_path_local(path)) ||
856 get_mono_path_registry(path) ||
857 get_mono_path_datadir(path) ||
858 get_mono_path_unix(INSTALL_DATADIR "/wine/mono", path) ||
859 (strcmp(INSTALL_DATADIR, "/usr/share") &&
860 get_mono_path_unix("/usr/share/wine/mono", path)) ||
861 get_mono_path_unix("/opt/wine/mono", path);
864 struct InstalledRuntimeEnum
866 IEnumUnknown IEnumUnknown_iface;
867 LONG ref;
868 ULONG pos;
871 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
873 static inline struct InstalledRuntimeEnum *impl_from_IEnumUnknown(IEnumUnknown *iface)
875 return CONTAINING_RECORD(iface, struct InstalledRuntimeEnum, IEnumUnknown_iface);
878 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface, REFIID riid,
879 void **ppvObject)
881 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
883 if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
884 IsEqualGUID( riid, &IID_IUnknown ) )
886 *ppvObject = iface;
888 else
890 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
891 return E_NOINTERFACE;
894 IEnumUnknown_AddRef( iface );
896 return S_OK;
899 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
901 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
902 ULONG ref = InterlockedIncrement(&This->ref);
904 TRACE("(%p) refcount=%u\n", iface, ref);
906 return ref;
909 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
911 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
912 ULONG ref = InterlockedDecrement(&This->ref);
914 TRACE("(%p) refcount=%u\n", iface, ref);
916 if (ref == 0)
918 HeapFree(GetProcessHeap(), 0, This);
921 return ref;
924 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
925 IUnknown **rgelt, ULONG *pceltFetched)
927 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
928 ULONG num_fetched = 0;
929 HRESULT hr=S_OK;
930 IUnknown *item;
932 TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
934 while (num_fetched < celt)
936 if (This->pos >= NUM_RUNTIMES)
938 hr = S_FALSE;
939 break;
941 item = (IUnknown*)&runtimes[This->pos].ICLRRuntimeInfo_iface;
942 IUnknown_AddRef(item);
943 rgelt[num_fetched] = item;
944 num_fetched++;
945 This->pos++;
948 if (pceltFetched)
949 *pceltFetched = num_fetched;
951 return hr;
954 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
956 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
957 ULONG num_fetched = 0;
958 HRESULT hr=S_OK;
960 TRACE("(%p,%u)\n", iface, celt);
962 while (num_fetched < celt)
964 if (This->pos >= NUM_RUNTIMES)
966 hr = S_FALSE;
967 break;
969 num_fetched++;
970 This->pos++;
973 return hr;
976 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
978 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
980 TRACE("(%p)\n", iface);
982 This->pos = 0;
984 return S_OK;
987 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
989 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
990 struct InstalledRuntimeEnum *new_enum;
992 TRACE("(%p)\n", iface);
994 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
995 if (!new_enum)
996 return E_OUTOFMEMORY;
998 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
999 new_enum->ref = 1;
1000 new_enum->pos = This->pos;
1002 *ppenum = &new_enum->IEnumUnknown_iface;
1004 return S_OK;
1007 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
1008 InstalledRuntimeEnum_QueryInterface,
1009 InstalledRuntimeEnum_AddRef,
1010 InstalledRuntimeEnum_Release,
1011 InstalledRuntimeEnum_Next,
1012 InstalledRuntimeEnum_Skip,
1013 InstalledRuntimeEnum_Reset,
1014 InstalledRuntimeEnum_Clone
1017 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
1018 REFIID riid,
1019 void **ppvObject)
1021 TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
1023 if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
1024 IsEqualGUID( riid, &IID_IUnknown ) )
1026 *ppvObject = iface;
1028 else
1030 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
1031 return E_NOINTERFACE;
1034 ICLRMetaHost_AddRef( iface );
1036 return S_OK;
1039 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
1041 return 2;
1044 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
1046 return 1;
1049 static inline BOOL isDigit(WCHAR c)
1051 return c >= '0' && c <= '9';
1054 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
1056 *major = 0;
1057 *minor = 0;
1058 *build = 0;
1060 if (version[0] == 'v' || version[0] == 'V')
1062 version++;
1063 if (!isDigit(*version))
1064 return FALSE;
1066 while (isDigit(*version))
1067 *major = *major * 10 + (*version++ - '0');
1069 if (*version == 0)
1070 return TRUE;
1072 if (*version++ != '.' || !isDigit(*version))
1073 return FALSE;
1075 while (isDigit(*version))
1076 *minor = *minor * 10 + (*version++ - '0');
1078 if (*version == 0)
1079 return TRUE;
1081 if (*version++ != '.' || !isDigit(*version))
1082 return FALSE;
1084 while (isDigit(*version))
1085 *build = *build * 10 + (*version++ - '0');
1087 return *version == 0;
1089 else
1090 return FALSE;
1093 static HRESULT get_runtime(LPCWSTR pwzVersion, BOOL allow_short,
1094 REFIID iid, LPVOID *ppRuntime)
1096 int i;
1097 DWORD major, minor, build;
1099 if (!pwzVersion)
1100 return E_POINTER;
1102 if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
1104 ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
1105 return CLR_E_SHIM_RUNTIME;
1108 for (i=0; i<NUM_RUNTIMES; i++)
1110 if (runtimes[i].major == major && runtimes[i].minor == minor &&
1111 (runtimes[i].build == build || (allow_short && major >= 4 && build == 0)))
1113 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface, iid,
1114 ppRuntime);
1118 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
1119 return CLR_E_SHIM_RUNTIME;
1122 HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
1123 LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
1125 TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
1127 return get_runtime(pwzVersion, FALSE, iid, ppRuntime);
1130 HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
1131 LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
1133 ASSEMBLY *assembly;
1134 HRESULT hr;
1135 LPSTR version;
1136 ULONG buffer_size=*pcchBuffer;
1138 TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
1140 hr = assembly_create(&assembly, pwzFilePath);
1142 if (SUCCEEDED(hr))
1144 hr = assembly_get_runtime_version(assembly, &version);
1146 if (SUCCEEDED(hr))
1148 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
1150 if (pwzBuffer)
1152 if (buffer_size >= *pcchBuffer)
1153 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
1154 else
1155 hr = E_NOT_SUFFICIENT_BUFFER;
1159 assembly_release(assembly);
1162 return hr;
1165 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
1166 IEnumUnknown **ppEnumerator)
1168 struct InstalledRuntimeEnum *new_enum;
1170 TRACE("%p\n", ppEnumerator);
1172 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
1173 if (!new_enum)
1174 return E_OUTOFMEMORY;
1176 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
1177 new_enum->ref = 1;
1178 new_enum->pos = 0;
1180 *ppEnumerator = &new_enum->IEnumUnknown_iface;
1182 return S_OK;
1185 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
1186 HANDLE hndProcess, IEnumUnknown **ppEnumerator)
1188 FIXME("%p %p\n", hndProcess, ppEnumerator);
1190 return E_NOTIMPL;
1193 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
1194 RuntimeLoadedCallbackFnPtr pCallbackFunction)
1196 TRACE("%p\n", pCallbackFunction);
1198 if(!pCallbackFunction)
1199 return E_POINTER;
1201 if (GlobalCLRMetaHost.callback)
1202 return HOST_E_INVALIDOPERATION;
1204 GlobalCLRMetaHost.callback = pCallbackFunction;
1206 return S_OK;
1209 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
1210 REFIID riid, LPVOID *ppUnk)
1212 FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
1214 return E_NOTIMPL;
1217 HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
1219 TRACE("%i\n", iExitCode);
1221 EnterCriticalSection(&runtime_list_cs);
1223 if (is_mono_started && !is_mono_shutdown)
1225 /* search for a runtime and call System.Environment.Exit() */
1226 int i;
1228 for (i=0; i<NUM_RUNTIMES; i++)
1229 if (runtimes[i].loaded_runtime)
1230 RuntimeHost_ExitProcess(runtimes[i].loaded_runtime, iExitCode);
1233 ExitProcess(iExitCode);
1236 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
1238 CLRMetaHost_QueryInterface,
1239 CLRMetaHost_AddRef,
1240 CLRMetaHost_Release,
1241 CLRMetaHost_GetRuntime,
1242 CLRMetaHost_GetVersionFromFile,
1243 CLRMetaHost_EnumerateInstalledRuntimes,
1244 CLRMetaHost_EnumerateLoadedRuntimes,
1245 CLRMetaHost_RequestRuntimeLoadedNotification,
1246 CLRMetaHost_QueryLegacyV2RuntimeBinding,
1247 CLRMetaHost_ExitProcess
1250 static struct CLRMetaHost GlobalCLRMetaHost = {
1251 { &CLRMetaHost_vtbl }
1254 HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1256 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost.ICLRMetaHost_iface, riid, ppobj);
1259 struct CLRMetaHostPolicy
1261 ICLRMetaHostPolicy ICLRMetaHostPolicy_iface;
1264 static struct CLRMetaHostPolicy GlobalCLRMetaHostPolicy;
1266 static HRESULT WINAPI metahostpolicy_QueryInterface(ICLRMetaHostPolicy *iface, REFIID riid, void **obj)
1268 TRACE("%s %p\n", debugstr_guid(riid), obj);
1270 if ( IsEqualGUID( riid, &IID_ICLRMetaHostPolicy ) ||
1271 IsEqualGUID( riid, &IID_IUnknown ) )
1273 ICLRMetaHostPolicy_AddRef( iface );
1274 *obj = iface;
1275 return S_OK;
1278 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
1280 *obj = NULL;
1281 return E_NOINTERFACE;
1284 static ULONG WINAPI metahostpolicy_AddRef(ICLRMetaHostPolicy *iface)
1286 return 2;
1289 static ULONG WINAPI metahostpolicy_Release(ICLRMetaHostPolicy *iface)
1291 return 1;
1294 static HRESULT WINAPI metahostpolicy_GetRequestedRuntime(ICLRMetaHostPolicy *iface, METAHOST_POLICY_FLAGS dwPolicyFlags,
1295 LPCWSTR pwzBinary, IStream *pCfgStream, LPWSTR pwzVersion, DWORD *pcchVersion,
1296 LPWSTR pwzImageVersion, DWORD *pcchImageVersion, DWORD *pdwConfigFlags, REFIID riid,
1297 LPVOID *ppRuntime)
1299 ICLRRuntimeInfo *result;
1300 HRESULT hr;
1301 WCHAR filename[MAX_PATH];
1302 const WCHAR *path = NULL;
1303 int flags = 0;
1305 TRACE("%d %p %p %p %p %p %p %p %s %p\n", dwPolicyFlags, pwzBinary, pCfgStream,
1306 pwzVersion, pcchVersion, pwzImageVersion, pcchImageVersion, pdwConfigFlags,
1307 debugstr_guid(riid), ppRuntime);
1309 if (pdwConfigFlags) {
1310 FIXME("ignoring config flags\n");
1311 *pdwConfigFlags = 0;
1314 if(dwPolicyFlags & METAHOST_POLICY_USE_PROCESS_IMAGE_PATH)
1316 GetModuleFileNameW(0, filename, MAX_PATH);
1317 path = filename;
1319 else if(pwzBinary)
1321 path = pwzBinary;
1324 if(dwPolicyFlags & METAHOST_POLICY_APPLY_UPGRADE_POLICY)
1325 flags |= RUNTIME_INFO_UPGRADE_VERSION;
1327 hr = get_runtime_info(path, pwzImageVersion, NULL, pCfgStream, 0, flags, FALSE, &result);
1328 if (SUCCEEDED(hr))
1330 if (pwzImageVersion)
1332 /* Ignoring errors on purpose */
1333 ICLRRuntimeInfo_GetVersionString(result, pwzImageVersion, pcchImageVersion);
1336 hr = ICLRRuntimeInfo_QueryInterface(result, riid, ppRuntime);
1338 ICLRRuntimeInfo_Release(result);
1341 TRACE("<- 0x%08x\n", hr);
1343 return hr;
1346 static const struct ICLRMetaHostPolicyVtbl CLRMetaHostPolicy_vtbl =
1348 metahostpolicy_QueryInterface,
1349 metahostpolicy_AddRef,
1350 metahostpolicy_Release,
1351 metahostpolicy_GetRequestedRuntime
1354 static struct CLRMetaHostPolicy GlobalCLRMetaHostPolicy = {
1355 { &CLRMetaHostPolicy_vtbl }
1358 HRESULT CLRMetaHostPolicy_CreateInstance(REFIID riid, void **ppobj)
1360 return ICLRMetaHostPolicy_QueryInterface(&GlobalCLRMetaHostPolicy.ICLRMetaHostPolicy_iface, riid, ppobj);
1364 * Assembly search override settings:
1366 * WINE_MONO_OVERRIDES=*,Gac=n
1367 * Never search the GAC for libraries.
1369 * WINE_MONO_OVERRIDES=Microsoft.Xna.Framework,Gac=n
1370 * Never search the GAC for Microsoft.Xna.Framework
1372 * WINE_MONO_OVERRIDES=Microsoft.Xna.Framework.*,Gac=n;Microsoft.Xna.Framework.GamerServices,Gac=y
1373 * Never search the GAC for Microsoft.Xna.Framework, or any library starting
1374 * with Microsoft.Xna.Framework, except for Microsoft.Xna.Framework.GamerServices
1377 /* assembly search override flags */
1378 #define ASSEMBLY_SEARCH_GAC 1
1379 #define ASSEMBLY_SEARCH_UNDEFINED 2
1380 #define ASSEMBLY_SEARCH_DEFAULT ASSEMBLY_SEARCH_GAC
1382 typedef struct override_entry {
1383 char *name;
1384 DWORD flags;
1385 struct list entry;
1386 } override_entry;
1388 static struct list env_overrides = LIST_INIT(env_overrides);
1390 #define IS_OPTION_TRUE(ch) \
1391 ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
1392 #define IS_OPTION_FALSE(ch) \
1393 ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
1395 static void parse_override_entry(override_entry *entry, const char *string, int string_len)
1397 const char *next_key, *equals, *value;
1398 UINT kvp_len, key_len;
1400 entry->flags = ASSEMBLY_SEARCH_DEFAULT;
1402 while (string && string_len > 0)
1404 next_key = memchr(string, ',', string_len);
1406 if (next_key)
1408 kvp_len = next_key - string;
1409 next_key++;
1411 else
1412 kvp_len = string_len;
1414 equals = memchr(string, '=', kvp_len);
1416 if (equals)
1418 key_len = equals - string;
1419 value = equals + 1;
1420 switch (key_len) {
1421 case 3:
1422 if (!_strnicmp(string, "gac", 3)) {
1423 if (IS_OPTION_TRUE(*value))
1424 entry->flags |= ASSEMBLY_SEARCH_GAC;
1425 else if (IS_OPTION_FALSE(*value))
1426 entry->flags &= ~ASSEMBLY_SEARCH_GAC;
1428 break;
1429 default:
1430 break;
1434 string = next_key;
1435 string_len -= kvp_len + 1;
1439 static BOOL WINAPI parse_env_overrides(INIT_ONCE *once, void *param, void **context)
1441 const char *override_string = getenv("WINE_MONO_OVERRIDES");
1442 struct override_entry *entry;
1444 if (override_string)
1446 const char *entry_start;
1448 entry_start = override_string;
1450 while (entry_start && *entry_start)
1452 const char *next_entry, *basename_end;
1453 UINT entry_len;
1455 next_entry = strchr(entry_start, ';');
1457 if (next_entry)
1459 entry_len = next_entry - entry_start;
1460 next_entry++;
1462 else
1463 entry_len = strlen(entry_start);
1465 basename_end = memchr(entry_start, ',', entry_len);
1467 if (!basename_end)
1469 entry_start = next_entry;
1470 continue;
1473 entry = heap_alloc_zero(sizeof(*entry));
1474 if (!entry)
1476 ERR("out of memory\n");
1477 break;
1480 entry->name = heap_alloc_zero(basename_end - entry_start + 1);
1481 if (!entry->name)
1483 ERR("out of memory\n");
1484 heap_free(entry);
1485 break;
1488 memcpy(entry->name, entry_start, basename_end - entry_start);
1490 entry_len -= basename_end - entry_start + 1;
1491 entry_start = basename_end + 1;
1493 parse_override_entry(entry, entry_start, entry_len);
1495 list_add_tail(&env_overrides, &entry->entry);
1497 entry_start = next_entry;
1501 return TRUE;
1504 static DWORD get_basename_search_flags(const char *basename, MonoAssemblyName *aname, HKEY userkey, HKEY appkey)
1506 struct override_entry *entry;
1507 char buffer[256];
1508 DWORD buffer_size;
1509 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
1511 InitOnceExecuteOnce(&init_once, parse_env_overrides, NULL, NULL);
1513 LIST_FOR_EACH_ENTRY(entry, &env_overrides, override_entry, entry)
1515 if (strcmp(basename, entry->name) == 0)
1517 return entry->flags;
1521 buffer_size = sizeof(buffer);
1522 if (appkey && !RegQueryValueExA(appkey, basename, 0, NULL, (LPBYTE)buffer, &buffer_size))
1524 override_entry reg_entry;
1526 memset(&reg_entry, 0, sizeof(reg_entry));
1528 parse_override_entry(&reg_entry, buffer, strlen(buffer));
1530 return reg_entry.flags;
1533 buffer_size = sizeof(buffer);
1534 if (userkey && !RegQueryValueExA(userkey, basename, 0, NULL, (LPBYTE)buffer, &buffer_size))
1536 override_entry reg_entry;
1538 memset(&reg_entry, 0, sizeof(reg_entry));
1540 parse_override_entry(&reg_entry, buffer, strlen(buffer));
1542 return reg_entry.flags;
1545 if (strcmp(basename, "Microsoft.Xna.Framework.*") == 0 &&
1546 mono_assembly_name_get_version(aname, NULL, NULL, NULL) == 4)
1547 /* Use FNA as a replacement for XNA4. */
1548 return 0;
1550 return ASSEMBLY_SEARCH_UNDEFINED;
1553 static HKEY get_app_overrides_key(void)
1555 static const WCHAR subkeyW[] = {'\\','M','o','n','o','\\','A','s','m','O','v','e','r','r','i','d','e','s',0};
1556 WCHAR bufferW[MAX_PATH+18];
1557 HKEY appkey = 0;
1558 DWORD len;
1560 len = (GetModuleFileNameW( 0, bufferW, MAX_PATH ));
1561 if (len && len < MAX_PATH)
1563 HKEY tmpkey;
1564 WCHAR *p, *appname = bufferW;
1565 if ((p = wcsrchr( appname, '/' ))) appname = p + 1;
1566 if ((p = wcsrchr( appname, '\\' ))) appname = p + 1;
1567 lstrcatW( appname, subkeyW );
1568 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Mono\AsmOverrides */
1569 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
1571 if (RegOpenKeyW( tmpkey, appname, &appkey )) appkey = 0;
1572 RegCloseKey( tmpkey );
1576 return appkey;
1579 static DWORD get_assembly_search_flags(MonoAssemblyName *aname)
1581 const char *name = mono_assembly_name_get_name(aname);
1582 char *name_copy, *name_end;
1583 DWORD result;
1584 HKEY appkey = 0, userkey;
1586 /* @@ Wine registry key: HKCU\Software\Wine\Mono\AsmOverrides */
1587 if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Mono\\AsmOverrides", &userkey )) userkey = 0;
1589 appkey = get_app_overrides_key();
1591 result = get_basename_search_flags(name, aname, userkey, appkey);
1592 if (result != ASSEMBLY_SEARCH_UNDEFINED)
1594 if (userkey) RegCloseKey(userkey);
1595 if (appkey) RegCloseKey(appkey);
1596 return result;
1599 name_copy = heap_alloc((strlen(name) + 3) * sizeof(WCHAR));
1600 if (!name_copy)
1602 ERR("out of memory\n");
1603 if (userkey) RegCloseKey(userkey);
1604 if (appkey) RegCloseKey(appkey);
1605 return ASSEMBLY_SEARCH_DEFAULT;
1608 strcpy(name_copy, name);
1609 name_end = name_copy + strlen(name);
1613 strcpy(name_end, ".*");
1614 result = get_basename_search_flags(name_copy, aname, userkey, appkey);
1615 if (result != ASSEMBLY_SEARCH_UNDEFINED) break;
1617 *name_end = 0;
1618 name_end = strrchr(name_copy, '.');
1619 } while (name_end != NULL);
1621 /* default flags */
1622 if (result == ASSEMBLY_SEARCH_UNDEFINED)
1624 result = get_basename_search_flags("*", aname, userkey, appkey);
1625 if (result == ASSEMBLY_SEARCH_UNDEFINED)
1626 result = ASSEMBLY_SEARCH_DEFAULT;
1629 heap_free(name_copy);
1630 if (appkey) RegCloseKey(appkey);
1631 if (userkey) RegCloseKey(userkey);
1633 return result;
1636 HRESULT get_file_from_strongname(WCHAR* stringnameW, WCHAR* assemblies_path, int path_length)
1638 HRESULT hr=S_OK;
1639 IAssemblyCache *asmcache;
1640 ASSEMBLY_INFO info;
1641 static const WCHAR fusiondll[] = {'f','u','s','i','o','n',0};
1642 HMODULE hfusion=NULL;
1643 static HRESULT (WINAPI *pCreateAssemblyCache)(IAssemblyCache**,DWORD);
1645 if (!pCreateAssemblyCache)
1647 hr = LoadLibraryShim(fusiondll, NULL, NULL, &hfusion);
1649 if (SUCCEEDED(hr))
1651 pCreateAssemblyCache = (void*)GetProcAddress(hfusion, "CreateAssemblyCache");
1652 if (!pCreateAssemblyCache)
1653 hr = E_FAIL;
1657 if (SUCCEEDED(hr))
1658 hr = pCreateAssemblyCache(&asmcache, 0);
1660 if (SUCCEEDED(hr))
1662 info.cbAssemblyInfo = sizeof(info);
1663 info.pszCurrentAssemblyPathBuf = assemblies_path;
1664 info.cchBuf = path_length;
1665 assemblies_path[0] = 0;
1667 hr = IAssemblyCache_QueryAssemblyInfo(asmcache, 0, stringnameW, &info);
1669 IAssemblyCache_Release(asmcache);
1672 return hr;
1675 static MonoAssembly* mono_assembly_try_load(WCHAR *path)
1677 MonoAssembly *result = NULL;
1678 MonoImageOpenStatus stat;
1679 char *pathA;
1681 if (!(pathA = WtoA(path))) return NULL;
1683 result = mono_assembly_open(pathA, &stat);
1684 HeapFree(GetProcessHeap(), 0, pathA);
1686 if (result) TRACE("found: %s\n", debugstr_w(path));
1687 return result;
1690 static MonoAssembly* CDECL mono_assembly_preload_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data)
1692 HRESULT hr;
1693 MonoAssembly *result=NULL;
1694 char *stringname=NULL;
1695 const char *assemblyname;
1696 const char *culture;
1697 LPWSTR stringnameW, cultureW;
1698 int stringnameW_size, cultureW_size;
1699 WCHAR path[MAX_PATH];
1700 char *pathA;
1701 MonoImageOpenStatus stat;
1702 DWORD search_flags;
1703 int i;
1704 static const WCHAR dotdllW[] = {'.','d','l','l',0};
1705 static const WCHAR dotexeW[] = {'.','e','x','e',0};
1707 stringname = mono_stringify_assembly_name(aname);
1708 assemblyname = mono_assembly_name_get_name(aname);
1709 culture = mono_assembly_name_get_culture(aname);
1710 if (culture)
1712 cultureW_size = MultiByteToWideChar(CP_UTF8, 0, culture, -1, NULL, 0);
1713 cultureW = HeapAlloc(GetProcessHeap(), 0, cultureW_size * sizeof(WCHAR));
1714 if (cultureW) MultiByteToWideChar(CP_UTF8, 0, culture, -1, cultureW, cultureW_size);
1716 else cultureW = NULL;
1718 TRACE("%s\n", debugstr_a(stringname));
1720 if (!stringname || !assemblyname) return NULL;
1722 search_flags = get_assembly_search_flags(aname);
1723 if (private_path)
1725 stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, assemblyname, -1, NULL, 0);
1726 stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1727 if (stringnameW)
1729 MultiByteToWideChar(CP_UTF8, 0, assemblyname, -1, stringnameW, stringnameW_size);
1730 for (i = 0; private_path[i] != NULL; i++)
1732 /* This is the lookup order used in Mono */
1733 /* 1st try: [culture]/[name].dll (culture may be empty) */
1734 wcscpy(path, private_path[i]);
1735 if (cultureW) PathAppendW(path, cultureW);
1736 PathAppendW(path, stringnameW);
1737 wcscat(path, dotdllW);
1738 result = mono_assembly_try_load(path);
1739 if (result) break;
1741 /* 2nd try: [culture]/[name].exe (culture may be empty) */
1742 wcscpy(path + wcslen(path) - wcslen(dotdllW), dotexeW);
1743 result = mono_assembly_try_load(path);
1744 if (result) break;
1746 /* 3rd try: [culture]/[name]/[name].dll (culture may be empty) */
1747 path[wcslen(path) - wcslen(dotexeW)] = 0;
1748 PathAppendW(path, stringnameW);
1749 wcscat(path, dotdllW);
1750 result = mono_assembly_try_load(path);
1751 if (result) break;
1753 /* 4th try: [culture]/[name]/[name].exe (culture may be empty) */
1754 wcscpy(path + wcslen(path) - wcslen(dotdllW), dotexeW);
1755 result = mono_assembly_try_load(path);
1756 if (result) break;
1758 HeapFree(GetProcessHeap(), 0, stringnameW);
1759 if (result) goto done;
1763 /* FIXME: We should search the given paths before the GAC. */
1765 if ((search_flags & ASSEMBLY_SEARCH_GAC) != 0)
1767 stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, stringname, -1, NULL, 0);
1769 stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1770 if (stringnameW)
1772 MultiByteToWideChar(CP_UTF8, 0, stringname, -1, stringnameW, stringnameW_size);
1774 hr = get_file_from_strongname(stringnameW, path, MAX_PATH);
1776 HeapFree(GetProcessHeap(), 0, stringnameW);
1778 else
1779 hr = E_OUTOFMEMORY;
1781 if (SUCCEEDED(hr))
1783 TRACE("found: %s\n", debugstr_w(path));
1785 pathA = WtoA(path);
1787 if (pathA)
1789 result = mono_assembly_open(pathA, &stat);
1791 if (!result)
1792 ERR("Failed to load %s, status=%u\n", debugstr_w(path), stat);
1794 HeapFree(GetProcessHeap(), 0, pathA);
1798 else
1799 TRACE("skipping Windows GAC search due to override setting\n");
1801 done:
1802 HeapFree(GetProcessHeap(), 0, cultureW);
1803 mono_free(stringname);
1805 return result;
1808 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1809 IStream *config_stream, DWORD startup_flags, DWORD runtimeinfo_flags,
1810 BOOL legacy, ICLRRuntimeInfo **result)
1812 static const WCHAR dotconfig[] = {'.','c','o','n','f','i','g',0};
1813 static const DWORD supported_startup_flags = 0;
1814 static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1815 int i;
1816 WCHAR local_version[MAX_PATH];
1817 ULONG local_version_size = MAX_PATH;
1818 WCHAR local_config_file[MAX_PATH];
1819 HRESULT hr;
1820 parsed_config_file parsed_config;
1822 if (startup_flags & ~supported_startup_flags)
1823 FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
1825 if (runtimeinfo_flags & ~supported_runtime_flags)
1826 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
1828 if (exefile && !exefile[0])
1829 exefile = NULL;
1831 if (exefile && !config_file && !config_stream)
1833 lstrcpyW(local_config_file, exefile);
1834 lstrcatW(local_config_file, dotconfig);
1836 config_file = local_config_file;
1839 if (config_file || config_stream)
1841 BOOL found = FALSE;
1842 if (config_file)
1843 hr = parse_config_file(config_file, &parsed_config);
1844 else
1845 hr = parse_config_stream(config_stream, &parsed_config);
1847 if (SUCCEEDED(hr))
1849 supported_runtime *entry;
1850 LIST_FOR_EACH_ENTRY(entry, &parsed_config.supported_runtimes, supported_runtime, entry)
1852 hr = get_runtime(entry->version, TRUE, &IID_ICLRRuntimeInfo, (void**)result);
1853 if (SUCCEEDED(hr))
1855 found = TRUE;
1856 break;
1860 else
1862 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file), hr);
1865 free_parsed_config_file(&parsed_config);
1867 if (found)
1868 return S_OK;
1871 if (exefile && !version)
1873 DWORD major, minor, build;
1875 hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1877 version = local_version;
1879 if (FAILED(hr)) return hr;
1881 /* When running an executable, specifically when getting the version number from
1882 * the exe, native accepts a matching major.minor with build <= expected build. */
1883 if (!parse_runtime_version(version, &major, &minor, &build))
1885 ERR("Cannot parse %s\n", debugstr_w(version));
1886 return CLR_E_SHIM_RUNTIME;
1889 if (legacy)
1890 i = 3;
1891 else
1892 i = NUM_RUNTIMES;
1894 while (i--)
1896 if (runtimes[i].major == major && runtimes[i].minor == minor && runtimes[i].build >= build)
1898 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface,
1899 &IID_ICLRRuntimeInfo, (void **)result);
1904 if (version)
1906 hr = CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1907 if(SUCCEEDED(hr))
1908 return hr;
1911 if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1913 DWORD major, minor, build;
1915 if (version && !parse_runtime_version(version, &major, &minor, &build))
1917 ERR("Cannot parse %s\n", debugstr_w(version));
1918 return CLR_E_SHIM_RUNTIME;
1921 if (legacy)
1922 i = 3;
1923 else
1924 i = NUM_RUNTIMES;
1926 while (i--)
1928 /* Must be greater or equal to the version passed in. */
1929 if (!version || ((runtimes[i].major >= major && runtimes[i].minor >= minor && runtimes[i].build >= build) ||
1930 (runtimes[i].major >= major && runtimes[i].minor > minor) ||
1931 (runtimes[i].major > major)))
1933 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface,
1934 &IID_ICLRRuntimeInfo, (void **)result);
1938 return CLR_E_SHIM_RUNTIME;
1941 return CLR_E_SHIM_RUNTIME;