maintainers: Update the Direct3D section.
[wine.git] / dlls / mscoree / metahost.c
blobc72f510c8ac5802d200493cea8dc35f4dc5af8f1
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);
133 static void (CDECL *wine_mono_install_assembly_preload_hook)(WineMonoAssemblyPreLoadFunc func, void *user_data);
135 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path);
137 static MonoAssembly* CDECL mono_assembly_preload_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data);
138 static MonoAssembly* CDECL wine_mono_assembly_preload_hook_fn(MonoAssemblyName *aname, char **assemblies_path, int *search_path, void *user_data);
140 static void CDECL mono_shutdown_callback_fn(MonoProfiler *prof);
142 static MonoImage* CDECL image_open_module_handle_dummy(HMODULE module_handle,
143 char* fname, UINT has_entry_point, MonoImageOpenStatus* status)
145 return mono_image_open(fname, status);
148 static void CDECL set_crash_chaining_dummy(BOOL crash_chaining)
152 static void CDECL set_print_handler_dummy(MonoPrintCallback callback)
156 static HRESULT load_mono(LPCWSTR mono_path)
158 static const WCHAR lib[] = {'\\','l','i','b',0};
159 static const WCHAR etc[] = {'\\','e','t','c',0};
160 WCHAR mono_dll_path[MAX_PATH+16];
161 WCHAR mono_lib_path[MAX_PATH+4], mono_etc_path[MAX_PATH+4];
162 char mono_lib_path_a[MAX_PATH], mono_etc_path_a[MAX_PATH];
163 int aot_size;
164 char aot_setting[256];
165 int trace_size;
166 char trace_setting[256];
167 int verbose_size;
168 char verbose_setting[256];
170 if (is_mono_shutdown)
172 ERR("Cannot load Mono after it has been shut down.\n");
173 return E_FAIL;
176 if (!mono_handle)
178 lstrcpyW(mono_lib_path, mono_path);
179 lstrcatW(mono_lib_path, lib);
180 WideCharToMultiByte(CP_UTF8, 0, mono_lib_path, -1, mono_lib_path_a, MAX_PATH, NULL, NULL);
182 lstrcpyW(mono_etc_path, mono_path);
183 lstrcatW(mono_etc_path, etc);
184 WideCharToMultiByte(CP_UTF8, 0, mono_etc_path, -1, mono_etc_path_a, MAX_PATH, NULL, NULL);
186 if (!find_mono_dll(mono_path, mono_dll_path)) goto fail;
188 mono_handle = LoadLibraryW(mono_dll_path);
190 if (!mono_handle) goto fail;
192 #define LOAD_MONO_FUNCTION(x) do { \
193 x = (void*)GetProcAddress(mono_handle, #x); \
194 if (!x) { \
195 goto fail; \
197 } while (0);
199 LOAD_MONO_FUNCTION(mono_assembly_get_image);
200 LOAD_MONO_FUNCTION(mono_assembly_load_from);
201 LOAD_MONO_FUNCTION(mono_assembly_name_get_name);
202 LOAD_MONO_FUNCTION(mono_assembly_name_get_culture);
203 LOAD_MONO_FUNCTION(mono_assembly_name_get_version);
204 LOAD_MONO_FUNCTION(mono_assembly_open);
205 LOAD_MONO_FUNCTION(mono_config_parse);
206 LOAD_MONO_FUNCTION(mono_class_from_mono_type);
207 LOAD_MONO_FUNCTION(mono_class_from_name);
208 LOAD_MONO_FUNCTION(mono_class_get_method_from_name);
209 LOAD_MONO_FUNCTION(mono_domain_get);
210 LOAD_MONO_FUNCTION(mono_domain_get_by_id);
211 LOAD_MONO_FUNCTION(mono_domain_set);
212 LOAD_MONO_FUNCTION(mono_domain_set_config);
213 LOAD_MONO_FUNCTION(mono_free);
214 LOAD_MONO_FUNCTION(mono_get_corlib);
215 LOAD_MONO_FUNCTION(mono_image_open);
216 LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook);
217 LOAD_MONO_FUNCTION(mono_jit_exec);
218 LOAD_MONO_FUNCTION(mono_jit_init_version);
219 LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
220 LOAD_MONO_FUNCTION(mono_marshal_get_vtfixup_ftnptr);
221 LOAD_MONO_FUNCTION(mono_object_get_domain);
222 LOAD_MONO_FUNCTION(mono_object_get_virtual_method);
223 LOAD_MONO_FUNCTION(mono_object_new);
224 LOAD_MONO_FUNCTION(mono_object_unbox);
225 LOAD_MONO_FUNCTION(mono_reflection_type_from_name);
226 LOAD_MONO_FUNCTION(mono_runtime_invoke);
227 LOAD_MONO_FUNCTION(mono_runtime_object_init);
228 LOAD_MONO_FUNCTION(mono_runtime_quit);
229 LOAD_MONO_FUNCTION(mono_set_dirs);
230 LOAD_MONO_FUNCTION(mono_set_verbose_level);
231 LOAD_MONO_FUNCTION(mono_stringify_assembly_name);
232 LOAD_MONO_FUNCTION(mono_string_new);
233 LOAD_MONO_FUNCTION(mono_thread_attach);
234 LOAD_MONO_FUNCTION(mono_thread_manage);
236 #undef LOAD_MONO_FUNCTION
238 #define LOAD_OPT_MONO_FUNCTION(x, default) do { \
239 x = (void*)GetProcAddress(mono_handle, #x); \
240 if (!x) { \
241 x = default; \
243 } while (0);
245 LOAD_OPT_MONO_FUNCTION(mono_callspec_set_assembly, NULL);
246 LOAD_OPT_MONO_FUNCTION(mono_image_open_from_module_handle, image_open_module_handle_dummy);
247 LOAD_OPT_MONO_FUNCTION(mono_jit_set_aot_mode, NULL);
248 LOAD_OPT_MONO_FUNCTION(mono_profiler_create, NULL);
249 LOAD_OPT_MONO_FUNCTION(mono_profiler_install, NULL);
250 LOAD_OPT_MONO_FUNCTION(mono_profiler_set_runtime_shutdown_begin_callback, NULL);
251 LOAD_OPT_MONO_FUNCTION(mono_set_crash_chaining, set_crash_chaining_dummy);
252 LOAD_OPT_MONO_FUNCTION(mono_trace_set_print_handler, set_print_handler_dummy);
253 LOAD_OPT_MONO_FUNCTION(mono_trace_set_printerr_handler, set_print_handler_dummy);
254 LOAD_OPT_MONO_FUNCTION(wine_mono_install_assembly_preload_hook, NULL);
256 #undef LOAD_OPT_MONO_FUNCTION
258 if (mono_callspec_set_assembly == NULL)
260 mono_callspec_set_assembly = (void*)GetProcAddress(mono_handle, "mono_trace_set_assembly");
261 if (!mono_callspec_set_assembly) goto fail;
264 if (mono_profiler_create != NULL)
266 /* Profiler API v2 */
267 MonoProfilerHandle handle = mono_profiler_create(NULL);
268 mono_profiler_set_runtime_shutdown_begin_callback(handle, mono_shutdown_callback_fn);
270 else if (mono_profiler_install != NULL)
272 /* Profiler API v1 */
273 mono_profiler_install(NULL, mono_shutdown_callback_fn);
276 mono_set_crash_chaining(TRUE);
278 mono_trace_set_print_handler(mono_print_handler_fn);
279 mono_trace_set_printerr_handler(mono_print_handler_fn);
281 mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
283 mono_config_parse(NULL);
285 if (wine_mono_install_assembly_preload_hook)
286 wine_mono_install_assembly_preload_hook(wine_mono_assembly_preload_hook_fn, NULL);
287 else
288 mono_install_assembly_preload_hook(mono_assembly_preload_hook_fn, NULL);
290 aot_size = GetEnvironmentVariableA("WINE_MONO_AOT", aot_setting, sizeof(aot_setting));
292 if (aot_size)
294 MonoAotMode mode;
295 if (strcmp(aot_setting, "interp") == 0)
296 mode = MONO_AOT_MODE_INTERP_ONLY;
297 else if (strcmp(aot_setting, "none") == 0)
298 mode = MONO_AOT_MODE_NONE;
299 else
301 ERR("unknown WINE_MONO_AOT setting, valid settings are interp and none\n");
302 mode = MONO_AOT_MODE_NONE;
304 if (mono_jit_set_aot_mode != NULL)
306 mono_jit_set_aot_mode(mode);
308 else
310 ERR("mono_jit_set_aot_mode export not found\n");
314 trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
316 if (trace_size)
318 mono_jit_set_trace_options(trace_setting);
321 verbose_size = GetEnvironmentVariableA("WINE_MONO_VERBOSE", verbose_setting, sizeof(verbose_setting));
323 if (verbose_size)
325 mono_set_verbose_level(verbose_setting[0] - '0');
329 return S_OK;
331 fail:
332 ERR("Could not load Mono into this process\n");
333 FreeLibrary(mono_handle);
334 mono_handle = NULL;
335 return E_FAIL;
338 static char* get_exe_basename_utf8(void)
340 WCHAR filenameW[MAX_PATH], *basenameW;
342 GetModuleFileNameW(NULL, filenameW, MAX_PATH);
344 basenameW = wcsrchr(filenameW, '\\');
345 if (basenameW)
346 basenameW += 1;
347 else
348 basenameW = filenameW;
350 return WtoA(basenameW);
353 MonoDomain* get_root_domain(void)
355 static MonoDomain* root_domain;
357 if (root_domain != NULL)
358 return root_domain;
360 EnterCriticalSection(&runtime_list_cs);
362 if (root_domain == NULL)
364 char *exe_basename;
366 exe_basename = get_exe_basename_utf8();
368 root_domain = mono_jit_init_version(exe_basename, "v4.0.30319");
370 HeapFree(GetProcessHeap(), 0, exe_basename);
372 is_mono_started = TRUE;
375 LeaveCriticalSection(&runtime_list_cs);
377 return root_domain;
380 static void CDECL mono_shutdown_callback_fn(MonoProfiler *prof)
382 is_mono_shutdown = TRUE;
385 static HRESULT WINAPI thread_set_fn(void)
387 WARN("stub\n");
388 return S_OK;
391 static HRESULT WINAPI thread_unset_fn(void)
393 WARN("stub\n");
394 return S_OK;
397 static HRESULT CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo *This, RuntimeHost **result)
399 HRESULT hr = S_OK;
400 WCHAR mono_path[MAX_PATH];
402 if (This->loaded_runtime)
404 *result = This->loaded_runtime;
405 return hr;
408 if (!get_mono_path(mono_path, FALSE))
410 ERR("Wine Mono is not installed\n");
411 return CLR_E_SHIM_RUNTIME;
414 EnterCriticalSection(&runtime_list_cs);
416 if (This->loaded_runtime)
418 *result = This->loaded_runtime;
419 LeaveCriticalSection(&runtime_list_cs);
420 return hr;
423 if (GlobalCLRMetaHost.callback)
425 GlobalCLRMetaHost.callback(&This->ICLRRuntimeInfo_iface, thread_set_fn, thread_unset_fn);
428 hr = load_mono(mono_path);
430 if (SUCCEEDED(hr))
431 hr = RuntimeHost_Construct(This, &This->loaded_runtime);
433 LeaveCriticalSection(&runtime_list_cs);
435 if (SUCCEEDED(hr))
436 *result = This->loaded_runtime;
438 return hr;
441 void expect_no_runtimes(void)
443 if (mono_handle && is_mono_started && !is_mono_shutdown)
445 ERR("Process exited with a Mono runtime loaded.\n");
446 return;
450 static inline CLRRuntimeInfo *impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo *iface)
452 return CONTAINING_RECORD(iface, CLRRuntimeInfo, ICLRRuntimeInfo_iface);
455 static HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
456 REFIID riid,
457 void **ppvObject)
459 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
461 if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
462 IsEqualGUID( riid, &IID_IUnknown ) )
464 *ppvObject = iface;
466 else
468 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
469 return E_NOINTERFACE;
472 ICLRRuntimeInfo_AddRef( iface );
474 return S_OK;
477 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
479 return 2;
482 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
484 return 1;
487 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
488 LPWSTR pwzBuffer, DWORD *pcchBuffer)
490 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
491 DWORD buffer_size = *pcchBuffer;
492 HRESULT hr = S_OK;
493 char version[11];
494 DWORD size;
496 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
498 size = snprintf(version, sizeof(version), "v%lu.%lu.%lu", This->major, This->minor, This->build);
500 assert(size <= sizeof(version));
502 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
504 if (pwzBuffer)
506 if (buffer_size >= *pcchBuffer)
507 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
508 else
509 hr = E_NOT_SUFFICIENT_BUFFER;
512 return hr;
515 static BOOL get_install_root(LPWSTR install_dir)
517 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};
518 static const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
520 DWORD len;
521 HKEY key;
523 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
524 return FALSE;
526 len = MAX_PATH * sizeof(WCHAR);
527 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
529 RegCloseKey(key);
530 return FALSE;
532 RegCloseKey(key);
534 return TRUE;
537 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
538 LPWSTR pwzBuffer, DWORD *pcchBuffer)
540 static const WCHAR slash[] = {'\\',0};
541 DWORD buffer_size = *pcchBuffer;
542 WCHAR system_dir[MAX_PATH];
543 WCHAR version[MAX_PATH];
544 DWORD version_size, size;
545 HRESULT hr = S_OK;
547 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
549 if (!get_install_root(system_dir))
551 ERR("error reading registry key for installroot\n");
552 return E_FAIL;
554 else
556 version_size = MAX_PATH;
557 ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
558 lstrcatW(system_dir, version);
559 lstrcatW(system_dir, slash);
560 size = lstrlenW(system_dir) + 1;
563 *pcchBuffer = size;
565 if (pwzBuffer)
567 if (buffer_size >= size)
568 lstrcpyW(pwzBuffer, system_dir);
569 else
570 hr = E_NOT_SUFFICIENT_BUFFER;
573 return hr;
576 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
577 HANDLE hndProcess, BOOL *pbLoaded)
579 FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
581 return E_NOTIMPL;
584 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
585 UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
587 FIXME("%p %u %p %p %lx\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
589 return E_NOTIMPL;
592 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
593 LPCWSTR pwzDllName, HMODULE *phndModule)
595 WCHAR version[MAX_PATH];
596 HRESULT hr;
597 DWORD cchBuffer;
599 TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
601 cchBuffer = MAX_PATH;
602 hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
603 if (FAILED(hr)) return hr;
605 return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
608 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
609 LPCSTR pszProcName, LPVOID *ppProc)
611 FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
613 return E_NOTIMPL;
616 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
617 REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
619 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
620 RuntimeHost *host;
621 HRESULT hr;
623 TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
625 hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
627 if (SUCCEEDED(hr))
628 hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
630 return hr;
633 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
634 BOOL *pbLoadable)
636 FIXME("%p %p\n", iface, pbLoadable);
638 return E_NOTIMPL;
641 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
642 DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
644 FIXME("%p %lx %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
646 return E_NOTIMPL;
649 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
650 DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
652 FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
654 return E_NOTIMPL;
657 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
659 FIXME("%p\n", iface);
661 return E_NOTIMPL;
664 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
665 BOOL *pbStarted, DWORD *pdwStartupFlags)
667 FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
669 return E_NOTIMPL;
672 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
673 CLRRuntimeInfo_QueryInterface,
674 CLRRuntimeInfo_AddRef,
675 CLRRuntimeInfo_Release,
676 CLRRuntimeInfo_GetVersionString,
677 CLRRuntimeInfo_GetRuntimeDirectory,
678 CLRRuntimeInfo_IsLoaded,
679 CLRRuntimeInfo_LoadErrorString,
680 CLRRuntimeInfo_LoadLibrary,
681 CLRRuntimeInfo_GetProcAddress,
682 CLRRuntimeInfo_GetInterface,
683 CLRRuntimeInfo_IsLoadable,
684 CLRRuntimeInfo_SetDefaultStartupFlags,
685 CLRRuntimeInfo_GetDefaultStartupFlags,
686 CLRRuntimeInfo_BindAsLegacyV2Runtime,
687 CLRRuntimeInfo_IsStarted
690 HRESULT ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo *iface, RuntimeHost **result)
692 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
694 assert(This->ICLRRuntimeInfo_iface.lpVtbl == &CLRRuntimeInfoVtbl);
696 return CLRRuntimeInfo_GetRuntimeHost(This, result);
699 #ifdef __i386__
700 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};
701 #elif defined(__x86_64__)
702 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};
703 #else
704 static const WCHAR libmono2_arch_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
705 #endif
707 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path)
709 static const WCHAR mono2_dll[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
710 static const WCHAR libmono2_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
711 DWORD attributes=INVALID_FILE_ATTRIBUTES;
713 lstrcpyW(dll_path, path);
714 lstrcatW(dll_path, libmono2_arch_dll);
715 attributes = GetFileAttributesW(dll_path);
717 if (attributes == INVALID_FILE_ATTRIBUTES)
719 lstrcpyW(dll_path, path);
720 lstrcatW(dll_path, mono2_dll);
721 attributes = GetFileAttributesW(dll_path);
724 if (attributes == INVALID_FILE_ATTRIBUTES)
726 lstrcpyW(dll_path, path);
727 lstrcatW(dll_path, libmono2_dll);
728 attributes = GetFileAttributesW(dll_path);
731 return (attributes != INVALID_FILE_ATTRIBUTES);
734 static BOOL get_mono_path_local(LPWSTR path)
736 static const WCHAR subdir_mono[] = {'\\','m','o','n','o','\\','m','o','n','o','-','2','.','0', 0};
737 WCHAR base_path[MAX_PATH], mono_dll_path[MAX_PATH];
739 /* c:\windows\mono\mono-2.0 */
740 GetWindowsDirectoryW(base_path, MAX_PATH);
741 lstrcatW(base_path, subdir_mono);
743 if (find_mono_dll(base_path, mono_dll_path))
745 lstrcpyW(path, base_path);
746 return TRUE;
749 return FALSE;
752 static BOOL get_mono_path_registry(LPWSTR path)
754 static const WCHAR keyname[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\','M','o','n','o',0};
755 static const WCHAR valuename[] = {'R','u','n','t','i','m','e','P','a','t','h',0};
756 WCHAR base_path[MAX_PATH], mono_dll_path[MAX_PATH];
757 HKEY hkey;
758 DWORD res, valuesize;
759 BOOL ret=FALSE;
761 /* @@ Wine registry key: HKCU\Software\Wine\Mono */
762 res = RegOpenKeyW(HKEY_CURRENT_USER, keyname, &hkey);
763 if (res != ERROR_SUCCESS)
764 return FALSE;
766 valuesize = sizeof(base_path);
767 res = RegGetValueW(hkey, NULL, valuename, RRF_RT_REG_SZ, NULL, base_path, &valuesize);
768 if (res == ERROR_SUCCESS && find_mono_dll(base_path, mono_dll_path))
770 lstrcpyW(path, base_path);
771 ret = TRUE;
774 RegCloseKey(hkey);
776 return ret;
779 static BOOL get_mono_path_dos(const WCHAR *dir, LPWSTR path)
781 static const WCHAR unix_prefix[] = {'\\','\\','?','\\','u','n','i','x','\\'};
782 static const WCHAR basedir[] = L"\\wine-mono-" WINE_MONO_VERSION;
783 LPWSTR dos_dir;
784 WCHAR mono_dll_path[MAX_PATH];
785 DWORD len;
786 BOOL ret;
788 if (memcmp(dir, unix_prefix, sizeof(unix_prefix)) == 0)
789 return FALSE; /* No drive letter for this directory */
791 len = lstrlenW( dir ) + lstrlenW( basedir ) + 1;
792 if (!(dos_dir = heap_alloc( len * sizeof(WCHAR) ))) return FALSE;
793 lstrcpyW( dos_dir, dir );
794 lstrcatW( dos_dir, basedir );
796 ret = find_mono_dll(dos_dir, mono_dll_path);
797 if (ret)
798 lstrcpyW(path, dos_dir);
800 heap_free(dos_dir);
802 return ret;
805 static BOOL get_mono_path_unix(const char *unix_dir, LPWSTR path)
807 static WCHAR * (CDECL *p_wine_get_dos_file_name)(const char*);
808 LPWSTR dos_dir;
809 BOOL ret;
811 if (!p_wine_get_dos_file_name)
813 p_wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
814 if (!p_wine_get_dos_file_name)
815 return FALSE;
818 dos_dir = p_wine_get_dos_file_name(unix_dir);
819 if (!dos_dir)
820 return FALSE;
822 ret = get_mono_path_dos( dos_dir, path);
824 heap_free(dos_dir);
825 return ret;
828 static BOOL get_mono_path_datadir(LPWSTR path)
830 static const WCHAR winedatadirW[] = {'W','I','N','E','D','A','T','A','D','I','R',0};
831 static const WCHAR winebuilddirW[] = {'W','I','N','E','B','U','I','L','D','D','I','R',0};
832 static const WCHAR unix_prefix[] = {'\\','?','?','\\','u','n','i','x','\\'};
833 static const WCHAR monoW[] = {'\\','m','o','n','o',0};
834 static const WCHAR dotdotmonoW[] = {'\\','.','.','\\','m','o','n','o',0};
835 const WCHAR *data_dir, *suffix;
836 WCHAR *package_dir;
837 BOOL ret;
839 if ((data_dir = _wgetenv( winedatadirW )))
840 suffix = monoW;
841 else if ((data_dir = _wgetenv( winebuilddirW )))
842 suffix = dotdotmonoW;
843 else
844 return FALSE;
846 if (!wcsncmp( data_dir, unix_prefix, wcslen(unix_prefix) )) return FALSE;
847 data_dir += 4; /* skip \??\ prefix */
848 package_dir = heap_alloc( (lstrlenW(data_dir) + lstrlenW(suffix) + 1) * sizeof(WCHAR));
849 lstrcpyW( package_dir, data_dir );
850 lstrcatW( package_dir, suffix );
852 ret = get_mono_path_dos(package_dir, path);
854 heap_free(package_dir);
856 return ret;
859 BOOL get_mono_path(LPWSTR path, BOOL skip_local)
861 return (!skip_local && get_mono_path_local(path)) ||
862 get_mono_path_registry(path) ||
863 get_mono_path_datadir(path) ||
864 get_mono_path_unix(INSTALL_DATADIR "/wine/mono", path) ||
865 (strcmp(INSTALL_DATADIR, "/usr/share") &&
866 get_mono_path_unix("/usr/share/wine/mono", path)) ||
867 get_mono_path_unix("/opt/wine/mono", path);
870 struct InstalledRuntimeEnum
872 IEnumUnknown IEnumUnknown_iface;
873 LONG ref;
874 ULONG pos;
877 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
879 static inline struct InstalledRuntimeEnum *impl_from_IEnumUnknown(IEnumUnknown *iface)
881 return CONTAINING_RECORD(iface, struct InstalledRuntimeEnum, IEnumUnknown_iface);
884 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface, REFIID riid,
885 void **ppvObject)
887 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
889 if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
890 IsEqualGUID( riid, &IID_IUnknown ) )
892 *ppvObject = iface;
894 else
896 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
897 return E_NOINTERFACE;
900 IEnumUnknown_AddRef( iface );
902 return S_OK;
905 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
907 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
908 ULONG ref = InterlockedIncrement(&This->ref);
910 TRACE("(%p) refcount=%lu\n", iface, ref);
912 return ref;
915 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
917 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
918 ULONG ref = InterlockedDecrement(&This->ref);
920 TRACE("(%p) refcount=%lu\n", iface, ref);
922 if (ref == 0)
924 HeapFree(GetProcessHeap(), 0, This);
927 return ref;
930 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
931 IUnknown **rgelt, ULONG *pceltFetched)
933 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
934 ULONG num_fetched = 0;
935 HRESULT hr=S_OK;
936 IUnknown *item;
938 TRACE("(%p,%lu,%p,%p)\n", iface, celt, rgelt, pceltFetched);
940 while (num_fetched < celt)
942 if (This->pos >= NUM_RUNTIMES)
944 hr = S_FALSE;
945 break;
947 item = (IUnknown*)&runtimes[This->pos].ICLRRuntimeInfo_iface;
948 IUnknown_AddRef(item);
949 rgelt[num_fetched] = item;
950 num_fetched++;
951 This->pos++;
954 if (pceltFetched)
955 *pceltFetched = num_fetched;
957 return hr;
960 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
962 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
963 ULONG num_fetched = 0;
964 HRESULT hr=S_OK;
966 TRACE("(%p,%lu)\n", iface, celt);
968 while (num_fetched < celt)
970 if (This->pos >= NUM_RUNTIMES)
972 hr = S_FALSE;
973 break;
975 num_fetched++;
976 This->pos++;
979 return hr;
982 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
984 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
986 TRACE("(%p)\n", iface);
988 This->pos = 0;
990 return S_OK;
993 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
995 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
996 struct InstalledRuntimeEnum *new_enum;
998 TRACE("(%p)\n", iface);
1000 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
1001 if (!new_enum)
1002 return E_OUTOFMEMORY;
1004 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
1005 new_enum->ref = 1;
1006 new_enum->pos = This->pos;
1008 *ppenum = &new_enum->IEnumUnknown_iface;
1010 return S_OK;
1013 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
1014 InstalledRuntimeEnum_QueryInterface,
1015 InstalledRuntimeEnum_AddRef,
1016 InstalledRuntimeEnum_Release,
1017 InstalledRuntimeEnum_Next,
1018 InstalledRuntimeEnum_Skip,
1019 InstalledRuntimeEnum_Reset,
1020 InstalledRuntimeEnum_Clone
1023 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
1024 REFIID riid,
1025 void **ppvObject)
1027 TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
1029 if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
1030 IsEqualGUID( riid, &IID_IUnknown ) )
1032 *ppvObject = iface;
1034 else
1036 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
1037 return E_NOINTERFACE;
1040 ICLRMetaHost_AddRef( iface );
1042 return S_OK;
1045 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
1047 return 2;
1050 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
1052 return 1;
1055 static inline BOOL isDigit(WCHAR c)
1057 return c >= '0' && c <= '9';
1060 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
1062 *major = 0;
1063 *minor = 0;
1064 *build = 0;
1066 if (version[0] == 'v' || version[0] == 'V')
1068 version++;
1069 if (!isDigit(*version))
1070 return FALSE;
1072 while (isDigit(*version))
1073 *major = *major * 10 + (*version++ - '0');
1075 if (*version == 0)
1076 return TRUE;
1078 if (*version++ != '.' || !isDigit(*version))
1079 return FALSE;
1081 while (isDigit(*version))
1082 *minor = *minor * 10 + (*version++ - '0');
1084 if (*version == 0)
1085 return TRUE;
1087 if (*version++ != '.' || !isDigit(*version))
1088 return FALSE;
1090 while (isDigit(*version))
1091 *build = *build * 10 + (*version++ - '0');
1093 return *version == 0;
1095 else
1096 return FALSE;
1099 static HRESULT get_runtime(LPCWSTR pwzVersion, BOOL allow_short,
1100 REFIID iid, LPVOID *ppRuntime)
1102 int i;
1103 DWORD major, minor, build;
1105 if (!pwzVersion)
1106 return E_POINTER;
1108 if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
1110 ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
1111 return CLR_E_SHIM_RUNTIME;
1114 for (i=0; i<NUM_RUNTIMES; i++)
1116 if (runtimes[i].major == major && runtimes[i].minor == minor &&
1117 (runtimes[i].build == build || (allow_short && major >= 4 && build == 0)))
1119 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface, iid,
1120 ppRuntime);
1124 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
1125 return CLR_E_SHIM_RUNTIME;
1128 HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
1129 LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
1131 TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
1133 return get_runtime(pwzVersion, FALSE, iid, ppRuntime);
1136 HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
1137 LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
1139 ASSEMBLY *assembly;
1140 HRESULT hr;
1141 LPSTR version;
1142 ULONG buffer_size=*pcchBuffer;
1144 TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
1146 hr = assembly_create(&assembly, pwzFilePath);
1148 if (SUCCEEDED(hr))
1150 hr = assembly_get_runtime_version(assembly, &version);
1152 if (SUCCEEDED(hr))
1154 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
1156 if (pwzBuffer)
1158 if (buffer_size >= *pcchBuffer)
1159 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
1160 else
1161 hr = E_NOT_SUFFICIENT_BUFFER;
1165 assembly_release(assembly);
1168 return hr;
1171 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
1172 IEnumUnknown **ppEnumerator)
1174 struct InstalledRuntimeEnum *new_enum;
1176 TRACE("%p\n", ppEnumerator);
1178 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
1179 if (!new_enum)
1180 return E_OUTOFMEMORY;
1182 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
1183 new_enum->ref = 1;
1184 new_enum->pos = 0;
1186 *ppEnumerator = &new_enum->IEnumUnknown_iface;
1188 return S_OK;
1191 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
1192 HANDLE hndProcess, IEnumUnknown **ppEnumerator)
1194 FIXME("%p %p\n", hndProcess, ppEnumerator);
1196 return E_NOTIMPL;
1199 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
1200 RuntimeLoadedCallbackFnPtr pCallbackFunction)
1202 TRACE("%p\n", pCallbackFunction);
1204 if(!pCallbackFunction)
1205 return E_POINTER;
1207 if (GlobalCLRMetaHost.callback)
1208 return HOST_E_INVALIDOPERATION;
1210 GlobalCLRMetaHost.callback = pCallbackFunction;
1212 return S_OK;
1215 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
1216 REFIID riid, LPVOID *ppUnk)
1218 FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
1220 return E_NOTIMPL;
1223 HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
1225 TRACE("%i\n", iExitCode);
1227 EnterCriticalSection(&runtime_list_cs);
1229 if (is_mono_started && !is_mono_shutdown)
1231 /* search for a runtime and call System.Environment.Exit() */
1232 int i;
1234 for (i=0; i<NUM_RUNTIMES; i++)
1235 if (runtimes[i].loaded_runtime)
1236 RuntimeHost_ExitProcess(runtimes[i].loaded_runtime, iExitCode);
1239 ExitProcess(iExitCode);
1242 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
1244 CLRMetaHost_QueryInterface,
1245 CLRMetaHost_AddRef,
1246 CLRMetaHost_Release,
1247 CLRMetaHost_GetRuntime,
1248 CLRMetaHost_GetVersionFromFile,
1249 CLRMetaHost_EnumerateInstalledRuntimes,
1250 CLRMetaHost_EnumerateLoadedRuntimes,
1251 CLRMetaHost_RequestRuntimeLoadedNotification,
1252 CLRMetaHost_QueryLegacyV2RuntimeBinding,
1253 CLRMetaHost_ExitProcess
1256 static struct CLRMetaHost GlobalCLRMetaHost = {
1257 { &CLRMetaHost_vtbl }
1260 HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1262 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost.ICLRMetaHost_iface, riid, ppobj);
1265 struct CLRMetaHostPolicy
1267 ICLRMetaHostPolicy ICLRMetaHostPolicy_iface;
1270 static struct CLRMetaHostPolicy GlobalCLRMetaHostPolicy;
1272 static HRESULT WINAPI metahostpolicy_QueryInterface(ICLRMetaHostPolicy *iface, REFIID riid, void **obj)
1274 TRACE("%s %p\n", debugstr_guid(riid), obj);
1276 if ( IsEqualGUID( riid, &IID_ICLRMetaHostPolicy ) ||
1277 IsEqualGUID( riid, &IID_IUnknown ) )
1279 ICLRMetaHostPolicy_AddRef( iface );
1280 *obj = iface;
1281 return S_OK;
1284 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
1286 *obj = NULL;
1287 return E_NOINTERFACE;
1290 static ULONG WINAPI metahostpolicy_AddRef(ICLRMetaHostPolicy *iface)
1292 return 2;
1295 static ULONG WINAPI metahostpolicy_Release(ICLRMetaHostPolicy *iface)
1297 return 1;
1300 static HRESULT WINAPI metahostpolicy_GetRequestedRuntime(ICLRMetaHostPolicy *iface, METAHOST_POLICY_FLAGS dwPolicyFlags,
1301 LPCWSTR pwzBinary, IStream *pCfgStream, LPWSTR pwzVersion, DWORD *pcchVersion,
1302 LPWSTR pwzImageVersion, DWORD *pcchImageVersion, DWORD *pdwConfigFlags, REFIID riid,
1303 LPVOID *ppRuntime)
1305 ICLRRuntimeInfo *result;
1306 HRESULT hr;
1307 WCHAR filename[MAX_PATH];
1308 const WCHAR *path = NULL;
1309 int flags = 0;
1311 TRACE("%d %p %p %p %p %p %p %p %s %p\n", dwPolicyFlags, pwzBinary, pCfgStream,
1312 pwzVersion, pcchVersion, pwzImageVersion, pcchImageVersion, pdwConfigFlags,
1313 debugstr_guid(riid), ppRuntime);
1315 if (pdwConfigFlags) {
1316 FIXME("ignoring config flags\n");
1317 *pdwConfigFlags = 0;
1320 if(dwPolicyFlags & METAHOST_POLICY_USE_PROCESS_IMAGE_PATH)
1322 GetModuleFileNameW(0, filename, MAX_PATH);
1323 path = filename;
1325 else if(pwzBinary)
1327 path = pwzBinary;
1330 if(dwPolicyFlags & METAHOST_POLICY_APPLY_UPGRADE_POLICY)
1331 flags |= RUNTIME_INFO_UPGRADE_VERSION;
1333 hr = get_runtime_info(path, pwzImageVersion, NULL, pCfgStream, 0, flags, FALSE, &result);
1334 if (SUCCEEDED(hr))
1336 if (pwzImageVersion)
1338 /* Ignoring errors on purpose */
1339 ICLRRuntimeInfo_GetVersionString(result, pwzImageVersion, pcchImageVersion);
1342 hr = ICLRRuntimeInfo_QueryInterface(result, riid, ppRuntime);
1344 ICLRRuntimeInfo_Release(result);
1347 TRACE("<- 0x%08lx\n", hr);
1349 return hr;
1352 static const struct ICLRMetaHostPolicyVtbl CLRMetaHostPolicy_vtbl =
1354 metahostpolicy_QueryInterface,
1355 metahostpolicy_AddRef,
1356 metahostpolicy_Release,
1357 metahostpolicy_GetRequestedRuntime
1360 static struct CLRMetaHostPolicy GlobalCLRMetaHostPolicy = {
1361 { &CLRMetaHostPolicy_vtbl }
1364 HRESULT CLRMetaHostPolicy_CreateInstance(REFIID riid, void **ppobj)
1366 return ICLRMetaHostPolicy_QueryInterface(&GlobalCLRMetaHostPolicy.ICLRMetaHostPolicy_iface, riid, ppobj);
1370 * Assembly search override settings:
1372 * WINE_MONO_OVERRIDES=*,Gac=n
1373 * Never search the GAC for libraries.
1375 * WINE_MONO_OVERRIDES=*,PrivatePath=n
1376 * Never search the AppDomain search path for libraries.
1378 * WINE_MONO_OVERRIDES=Microsoft.Xna.Framework,Gac=n
1379 * Never search the GAC for Microsoft.Xna.Framework
1381 * WINE_MONO_OVERRIDES=Microsoft.Xna.Framework.*,Gac=n;Microsoft.Xna.Framework.GamerServices,Gac=y
1382 * Never search the GAC for Microsoft.Xna.Framework, or any library starting
1383 * with Microsoft.Xna.Framework, except for Microsoft.Xna.Framework.GamerServices
1386 /* assembly search override flags */
1387 #define ASSEMBLY_SEARCH_GAC 1
1388 #define ASSEMBLY_SEARCH_UNDEFINED 2
1389 #define ASSEMBLY_SEARCH_PRIVATEPATH 4
1390 #define ASSEMBLY_SEARCH_DEFAULT (ASSEMBLY_SEARCH_GAC|ASSEMBLY_SEARCH_PRIVATEPATH)
1392 typedef struct override_entry {
1393 char *name;
1394 DWORD flags;
1395 struct list entry;
1396 } override_entry;
1398 static struct list env_overrides = LIST_INIT(env_overrides);
1400 #define IS_OPTION_TRUE(ch) \
1401 ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
1402 #define IS_OPTION_FALSE(ch) \
1403 ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
1405 static void parse_override_entry(override_entry *entry, const char *string, int string_len)
1407 const char *next_key, *equals, *value;
1408 UINT kvp_len, key_len;
1410 entry->flags = ASSEMBLY_SEARCH_DEFAULT;
1412 while (string && string_len > 0)
1414 next_key = memchr(string, ',', string_len);
1416 if (next_key)
1418 kvp_len = next_key - string;
1419 next_key++;
1421 else
1422 kvp_len = string_len;
1424 equals = memchr(string, '=', kvp_len);
1426 if (equals)
1428 key_len = equals - string;
1429 value = equals + 1;
1430 switch (key_len) {
1431 case 3:
1432 if (!_strnicmp(string, "gac", 3)) {
1433 if (IS_OPTION_TRUE(*value))
1434 entry->flags |= ASSEMBLY_SEARCH_GAC;
1435 else if (IS_OPTION_FALSE(*value))
1436 entry->flags &= ~ASSEMBLY_SEARCH_GAC;
1438 break;
1439 case 11:
1440 if (!_strnicmp(string, "privatepath", 11)) {
1441 if (IS_OPTION_TRUE(*value))
1442 entry->flags |= ASSEMBLY_SEARCH_PRIVATEPATH;
1443 else if (IS_OPTION_FALSE(*value))
1444 entry->flags &= ~ASSEMBLY_SEARCH_PRIVATEPATH;
1446 break;
1447 default:
1448 break;
1452 string = next_key;
1453 string_len -= kvp_len + 1;
1457 static BOOL WINAPI parse_env_overrides(INIT_ONCE *once, void *param, void **context)
1459 const char *override_string = getenv("WINE_MONO_OVERRIDES");
1460 struct override_entry *entry;
1462 if (override_string)
1464 const char *entry_start;
1466 entry_start = override_string;
1468 while (entry_start && *entry_start)
1470 const char *next_entry, *basename_end;
1471 UINT entry_len;
1473 next_entry = strchr(entry_start, ';');
1475 if (next_entry)
1477 entry_len = next_entry - entry_start;
1478 next_entry++;
1480 else
1481 entry_len = strlen(entry_start);
1483 basename_end = memchr(entry_start, ',', entry_len);
1485 if (!basename_end)
1487 entry_start = next_entry;
1488 continue;
1491 entry = heap_alloc_zero(sizeof(*entry));
1492 if (!entry)
1494 ERR("out of memory\n");
1495 break;
1498 entry->name = heap_alloc_zero(basename_end - entry_start + 1);
1499 if (!entry->name)
1501 ERR("out of memory\n");
1502 heap_free(entry);
1503 break;
1506 memcpy(entry->name, entry_start, basename_end - entry_start);
1508 entry_len -= basename_end - entry_start + 1;
1509 entry_start = basename_end + 1;
1511 parse_override_entry(entry, entry_start, entry_len);
1513 list_add_tail(&env_overrides, &entry->entry);
1515 entry_start = next_entry;
1519 return TRUE;
1522 static DWORD get_basename_search_flags(const char *basename, MonoAssemblyName *aname, HKEY userkey, HKEY appkey)
1524 struct override_entry *entry;
1525 char buffer[256];
1526 DWORD buffer_size;
1527 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
1529 InitOnceExecuteOnce(&init_once, parse_env_overrides, NULL, NULL);
1531 LIST_FOR_EACH_ENTRY(entry, &env_overrides, override_entry, entry)
1533 if (strcmp(basename, entry->name) == 0)
1535 return entry->flags;
1539 buffer_size = sizeof(buffer);
1540 if (appkey && !RegQueryValueExA(appkey, basename, 0, NULL, (LPBYTE)buffer, &buffer_size))
1542 override_entry reg_entry;
1544 memset(&reg_entry, 0, sizeof(reg_entry));
1546 parse_override_entry(&reg_entry, buffer, strlen(buffer));
1548 return reg_entry.flags;
1551 buffer_size = sizeof(buffer);
1552 if (userkey && !RegQueryValueExA(userkey, basename, 0, NULL, (LPBYTE)buffer, &buffer_size))
1554 override_entry reg_entry;
1556 memset(&reg_entry, 0, sizeof(reg_entry));
1558 parse_override_entry(&reg_entry, buffer, strlen(buffer));
1560 return reg_entry.flags;
1563 if (strcmp(basename, "Microsoft.Xna.Framework.*") == 0 &&
1564 mono_assembly_name_get_version(aname, NULL, NULL, NULL) == 4)
1565 /* Use FNA as a replacement for XNA4. */
1566 return 0;
1568 return ASSEMBLY_SEARCH_UNDEFINED;
1571 static HKEY get_app_overrides_key(void)
1573 static const WCHAR subkeyW[] = {'\\','M','o','n','o','\\','A','s','m','O','v','e','r','r','i','d','e','s',0};
1574 WCHAR bufferW[MAX_PATH+18];
1575 HKEY appkey = 0;
1576 DWORD len;
1578 len = (GetModuleFileNameW( 0, bufferW, MAX_PATH ));
1579 if (len && len < MAX_PATH)
1581 HKEY tmpkey;
1582 WCHAR *p, *appname = bufferW;
1583 if ((p = wcsrchr( appname, '/' ))) appname = p + 1;
1584 if ((p = wcsrchr( appname, '\\' ))) appname = p + 1;
1585 lstrcatW( appname, subkeyW );
1586 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Mono\AsmOverrides */
1587 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
1589 if (RegOpenKeyW( tmpkey, appname, &appkey )) appkey = 0;
1590 RegCloseKey( tmpkey );
1594 return appkey;
1597 static DWORD get_assembly_search_flags(MonoAssemblyName *aname)
1599 const char *name = mono_assembly_name_get_name(aname);
1600 char *name_copy, *name_end;
1601 DWORD result;
1602 HKEY appkey = 0, userkey;
1604 /* @@ Wine registry key: HKCU\Software\Wine\Mono\AsmOverrides */
1605 if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Mono\\AsmOverrides", &userkey )) userkey = 0;
1607 appkey = get_app_overrides_key();
1609 result = get_basename_search_flags(name, aname, userkey, appkey);
1610 if (result != ASSEMBLY_SEARCH_UNDEFINED)
1612 if (userkey) RegCloseKey(userkey);
1613 if (appkey) RegCloseKey(appkey);
1614 return result;
1617 name_copy = heap_alloc((strlen(name) + 3) * sizeof(WCHAR));
1618 if (!name_copy)
1620 ERR("out of memory\n");
1621 if (userkey) RegCloseKey(userkey);
1622 if (appkey) RegCloseKey(appkey);
1623 return ASSEMBLY_SEARCH_DEFAULT;
1626 strcpy(name_copy, name);
1627 name_end = name_copy + strlen(name);
1631 strcpy(name_end, ".*");
1632 result = get_basename_search_flags(name_copy, aname, userkey, appkey);
1633 if (result != ASSEMBLY_SEARCH_UNDEFINED) break;
1635 *name_end = 0;
1636 name_end = strrchr(name_copy, '.');
1637 } while (name_end != NULL);
1639 /* default flags */
1640 if (result == ASSEMBLY_SEARCH_UNDEFINED)
1642 result = get_basename_search_flags("*", aname, userkey, appkey);
1643 if (result == ASSEMBLY_SEARCH_UNDEFINED)
1644 result = ASSEMBLY_SEARCH_DEFAULT;
1647 heap_free(name_copy);
1648 if (appkey) RegCloseKey(appkey);
1649 if (userkey) RegCloseKey(userkey);
1651 return result;
1654 HRESULT get_file_from_strongname(WCHAR* stringnameW, WCHAR* assemblies_path, int path_length)
1656 HRESULT hr=S_OK;
1657 IAssemblyCache *asmcache;
1658 ASSEMBLY_INFO info;
1659 static const WCHAR fusiondll[] = {'f','u','s','i','o','n',0};
1660 HMODULE hfusion=NULL;
1661 static HRESULT (WINAPI *pCreateAssemblyCache)(IAssemblyCache**,DWORD);
1663 if (!pCreateAssemblyCache)
1665 hr = LoadLibraryShim(fusiondll, NULL, NULL, &hfusion);
1667 if (SUCCEEDED(hr))
1669 pCreateAssemblyCache = (void*)GetProcAddress(hfusion, "CreateAssemblyCache");
1670 if (!pCreateAssemblyCache)
1671 hr = E_FAIL;
1675 if (SUCCEEDED(hr))
1676 hr = pCreateAssemblyCache(&asmcache, 0);
1678 if (SUCCEEDED(hr))
1680 info.cbAssemblyInfo = sizeof(info);
1681 info.pszCurrentAssemblyPathBuf = assemblies_path;
1682 info.cchBuf = path_length;
1683 assemblies_path[0] = 0;
1685 hr = IAssemblyCache_QueryAssemblyInfo(asmcache, 0, stringnameW, &info);
1687 IAssemblyCache_Release(asmcache);
1690 return hr;
1693 static MonoAssembly* mono_assembly_try_load(WCHAR *path)
1695 MonoAssembly *result = NULL;
1696 MonoImageOpenStatus stat;
1697 char *pathA;
1699 if (!(pathA = WtoA(path))) return NULL;
1701 result = mono_assembly_open(pathA, &stat);
1702 HeapFree(GetProcessHeap(), 0, pathA);
1704 if (result) TRACE("found: %s\n", debugstr_w(path));
1705 return result;
1708 static MonoAssembly* CDECL mono_assembly_preload_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data)
1710 int dummy;
1711 return wine_mono_assembly_preload_hook_fn(aname, assemblies_path, &dummy, user_data);
1714 static MonoAssembly* CDECL wine_mono_assembly_preload_hook_fn(MonoAssemblyName *aname, char **assemblies_path, int *halt_search, void *user_data)
1716 HRESULT hr;
1717 MonoAssembly *result=NULL;
1718 char *stringname=NULL;
1719 const char *assemblyname;
1720 const char *culture;
1721 LPWSTR stringnameW, cultureW;
1722 int stringnameW_size, cultureW_size;
1723 WCHAR path[MAX_PATH];
1724 char *pathA;
1725 MonoImageOpenStatus stat;
1726 DWORD search_flags;
1727 int i;
1728 static const WCHAR dotdllW[] = {'.','d','l','l',0};
1729 static const WCHAR dotexeW[] = {'.','e','x','e',0};
1731 stringname = mono_stringify_assembly_name(aname);
1732 assemblyname = mono_assembly_name_get_name(aname);
1733 culture = mono_assembly_name_get_culture(aname);
1734 if (culture)
1736 cultureW_size = MultiByteToWideChar(CP_UTF8, 0, culture, -1, NULL, 0);
1737 cultureW = HeapAlloc(GetProcessHeap(), 0, cultureW_size * sizeof(WCHAR));
1738 if (cultureW) MultiByteToWideChar(CP_UTF8, 0, culture, -1, cultureW, cultureW_size);
1740 else cultureW = NULL;
1742 TRACE("%s\n", debugstr_a(stringname));
1744 if (!stringname || !assemblyname) return NULL;
1746 search_flags = get_assembly_search_flags(aname);
1747 if (private_path && (search_flags & ASSEMBLY_SEARCH_PRIVATEPATH) != 0)
1749 stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, assemblyname, -1, NULL, 0);
1750 stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1751 if (stringnameW)
1753 MultiByteToWideChar(CP_UTF8, 0, assemblyname, -1, stringnameW, stringnameW_size);
1754 for (i = 0; private_path[i] != NULL; i++)
1756 /* This is the lookup order used in Mono */
1757 /* 1st try: [culture]/[name].dll (culture may be empty) */
1758 wcscpy(path, private_path[i]);
1759 if (cultureW) PathAppendW(path, cultureW);
1760 PathAppendW(path, stringnameW);
1761 wcscat(path, dotdllW);
1762 result = mono_assembly_try_load(path);
1763 if (result) break;
1765 /* 2nd try: [culture]/[name].exe (culture may be empty) */
1766 wcscpy(path + wcslen(path) - wcslen(dotdllW), dotexeW);
1767 result = mono_assembly_try_load(path);
1768 if (result) break;
1770 /* 3rd try: [culture]/[name]/[name].dll (culture may be empty) */
1771 path[wcslen(path) - wcslen(dotexeW)] = 0;
1772 PathAppendW(path, stringnameW);
1773 wcscat(path, dotdllW);
1774 result = mono_assembly_try_load(path);
1775 if (result) break;
1777 /* 4th try: [culture]/[name]/[name].exe (culture may be empty) */
1778 wcscpy(path + wcslen(path) - wcslen(dotdllW), dotexeW);
1779 result = mono_assembly_try_load(path);
1780 if (result) break;
1782 HeapFree(GetProcessHeap(), 0, stringnameW);
1783 if (result) goto done;
1787 /* FIXME: We should search the given paths before the GAC. */
1789 if ((search_flags & ASSEMBLY_SEARCH_GAC) != 0)
1791 stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, stringname, -1, NULL, 0);
1793 stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1794 if (stringnameW)
1796 MultiByteToWideChar(CP_UTF8, 0, stringname, -1, stringnameW, stringnameW_size);
1798 hr = get_file_from_strongname(stringnameW, path, MAX_PATH);
1800 HeapFree(GetProcessHeap(), 0, stringnameW);
1802 else
1803 hr = E_OUTOFMEMORY;
1805 if (SUCCEEDED(hr))
1807 TRACE("found: %s\n", debugstr_w(path));
1809 pathA = WtoA(path);
1811 if (pathA)
1813 result = mono_assembly_open(pathA, &stat);
1815 if (!result)
1816 ERR("Failed to load %s, status=%u\n", debugstr_w(path), stat);
1818 HeapFree(GetProcessHeap(), 0, pathA);
1822 else
1823 TRACE("skipping Windows GAC search due to override setting\n");
1825 if ((search_flags & ASSEMBLY_SEARCH_PRIVATEPATH) == 0)
1827 TRACE("skipping AppDomain search path due to override setting\n");
1828 *halt_search = 1;
1831 done:
1832 HeapFree(GetProcessHeap(), 0, cultureW);
1833 mono_free(stringname);
1835 return result;
1838 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1839 IStream *config_stream, DWORD startup_flags, DWORD runtimeinfo_flags,
1840 BOOL legacy, ICLRRuntimeInfo **result)
1842 static const WCHAR dotconfig[] = {'.','c','o','n','f','i','g',0};
1843 static const DWORD supported_startup_flags = 0;
1844 static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1845 int i;
1846 WCHAR local_version[MAX_PATH];
1847 ULONG local_version_size = MAX_PATH;
1848 WCHAR local_config_file[MAX_PATH];
1849 HRESULT hr;
1850 parsed_config_file parsed_config;
1852 if (startup_flags & ~supported_startup_flags)
1853 FIXME("unsupported startup flags %lx\n", startup_flags & ~supported_startup_flags);
1855 if (runtimeinfo_flags & ~supported_runtime_flags)
1856 FIXME("unsupported runtimeinfo flags %lx\n", runtimeinfo_flags & ~supported_runtime_flags);
1858 if (exefile && !exefile[0])
1859 exefile = NULL;
1861 if (exefile && !config_file && !config_stream)
1863 lstrcpyW(local_config_file, exefile);
1864 lstrcatW(local_config_file, dotconfig);
1866 config_file = local_config_file;
1869 if (config_file || config_stream)
1871 BOOL found = FALSE;
1872 if (config_file)
1873 hr = parse_config_file(config_file, &parsed_config);
1874 else
1875 hr = parse_config_stream(config_stream, &parsed_config);
1877 if (SUCCEEDED(hr))
1879 supported_runtime *entry;
1880 LIST_FOR_EACH_ENTRY(entry, &parsed_config.supported_runtimes, supported_runtime, entry)
1882 hr = get_runtime(entry->version, TRUE, &IID_ICLRRuntimeInfo, (void**)result);
1883 if (SUCCEEDED(hr))
1885 found = TRUE;
1886 break;
1890 else
1892 WARN("failed to parse config file %s, hr=%lx\n", debugstr_w(config_file), hr);
1895 free_parsed_config_file(&parsed_config);
1897 if (found)
1898 return S_OK;
1901 if (exefile && !version)
1903 DWORD major, minor, build;
1905 hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1907 version = local_version;
1909 if (FAILED(hr)) return hr;
1911 /* When running an executable, specifically when getting the version number from
1912 * the exe, native accepts a matching major.minor with build <= expected build. */
1913 if (!parse_runtime_version(version, &major, &minor, &build))
1915 ERR("Cannot parse %s\n", debugstr_w(version));
1916 return CLR_E_SHIM_RUNTIME;
1919 if (legacy)
1920 i = 3;
1921 else
1922 i = NUM_RUNTIMES;
1924 while (i--)
1926 if (runtimes[i].major == major && runtimes[i].minor == minor && runtimes[i].build >= build)
1928 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface,
1929 &IID_ICLRRuntimeInfo, (void **)result);
1934 if (version)
1936 hr = CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1937 if(SUCCEEDED(hr))
1938 return hr;
1941 if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1943 DWORD major, minor, build;
1945 if (version && !parse_runtime_version(version, &major, &minor, &build))
1947 ERR("Cannot parse %s\n", debugstr_w(version));
1948 return CLR_E_SHIM_RUNTIME;
1951 if (legacy)
1952 i = 3;
1953 else
1954 i = NUM_RUNTIMES;
1956 while (i--)
1958 /* Must be greater or equal to the version passed in. */
1959 if (!version || ((runtimes[i].major >= major && runtimes[i].minor >= minor && runtimes[i].build >= build) ||
1960 (runtimes[i].major >= major && runtimes[i].minor > minor) ||
1961 (runtimes[i].major > major)))
1963 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface,
1964 &IID_ICLRRuntimeInfo, (void **)result);
1968 return CLR_E_SHIM_RUNTIME;
1971 return CLR_E_SHIM_RUNTIME;