netapi32: Convert the Unix library to the __wine_unix_call interface.
[wine.git] / dlls / mscoree / metahost.c
blobb38166002140d1d55d0509bd4f563df3ded45b6d
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 void CDECL mono_print_handler_fn(const char *string, INT is_stdout);
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);
255 #undef LOAD_OPT_MONO_FUNCTION
257 if (mono_callspec_set_assembly == NULL)
259 mono_callspec_set_assembly = (void*)GetProcAddress(mono_handle, "mono_trace_set_assembly");
260 if (!mono_callspec_set_assembly) goto fail;
263 if (mono_profiler_create != NULL)
265 /* Profiler API v2 */
266 MonoProfilerHandle handle = mono_profiler_create(NULL);
267 mono_profiler_set_runtime_shutdown_begin_callback(handle, mono_shutdown_callback_fn);
269 else if (mono_profiler_install != NULL)
271 /* Profiler API v1 */
272 mono_profiler_install(NULL, mono_shutdown_callback_fn);
275 mono_set_crash_chaining(TRUE);
277 mono_trace_set_print_handler(mono_print_handler_fn);
278 mono_trace_set_printerr_handler(mono_print_handler_fn);
280 mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
282 mono_config_parse(NULL);
284 mono_install_assembly_preload_hook(mono_assembly_preload_hook_fn, NULL);
286 aot_size = GetEnvironmentVariableA("WINE_MONO_AOT", aot_setting, sizeof(aot_setting));
288 if (aot_size)
290 MonoAotMode mode;
291 if (strcmp(aot_setting, "interp") == 0)
292 mode = MONO_AOT_MODE_INTERP_ONLY;
293 else if (strcmp(aot_setting, "none") == 0)
294 mode = MONO_AOT_MODE_NONE;
295 else
297 ERR("unknown WINE_MONO_AOT setting, valid settings are interp and none\n");
298 mode = MONO_AOT_MODE_NONE;
300 if (mono_jit_set_aot_mode != NULL)
302 mono_jit_set_aot_mode(mode);
304 else
306 ERR("mono_jit_set_aot_mode export not found\n");
310 trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
312 if (trace_size)
314 mono_jit_set_trace_options(trace_setting);
317 verbose_size = GetEnvironmentVariableA("WINE_MONO_VERBOSE", verbose_setting, sizeof(verbose_setting));
319 if (verbose_size)
321 mono_set_verbose_level(verbose_setting[0] - '0');
325 return S_OK;
327 fail:
328 ERR("Could not load Mono into this process\n");
329 FreeLibrary(mono_handle);
330 mono_handle = NULL;
331 return E_FAIL;
334 static char* get_exe_basename_utf8(void)
336 WCHAR filenameW[MAX_PATH], *basenameW;
338 GetModuleFileNameW(NULL, filenameW, MAX_PATH);
340 basenameW = wcsrchr(filenameW, '\\');
341 if (basenameW)
342 basenameW += 1;
343 else
344 basenameW = filenameW;
346 return WtoA(basenameW);
349 MonoDomain* get_root_domain(void)
351 static MonoDomain* root_domain;
353 if (root_domain != NULL)
354 return root_domain;
356 EnterCriticalSection(&runtime_list_cs);
358 if (root_domain == NULL)
360 char *exe_basename;
362 exe_basename = get_exe_basename_utf8();
364 root_domain = mono_jit_init_version(exe_basename, "v4.0.30319");
366 HeapFree(GetProcessHeap(), 0, exe_basename);
368 is_mono_started = TRUE;
371 LeaveCriticalSection(&runtime_list_cs);
373 return root_domain;
376 static void CDECL mono_shutdown_callback_fn(MonoProfiler *prof)
378 is_mono_shutdown = TRUE;
381 static void CDECL mono_print_handler_fn(const char *string, INT is_stdout)
383 const char *p;
384 for (; *string; string = p)
386 if ((p = strstr(string, "\n"))) p++;
387 else p = string + strlen(string);
388 wine_dbg_printf("%.*s", (int)(p - string), string);
392 static HRESULT WINAPI thread_set_fn(void)
394 WARN("stub\n");
395 return S_OK;
398 static HRESULT WINAPI thread_unset_fn(void)
400 WARN("stub\n");
401 return S_OK;
404 static HRESULT CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo *This, RuntimeHost **result)
406 HRESULT hr = S_OK;
407 WCHAR mono_path[MAX_PATH];
409 if (This->loaded_runtime)
411 *result = This->loaded_runtime;
412 return hr;
415 if (!get_mono_path(mono_path, FALSE))
417 ERR("Wine Mono is not installed\n");
418 return CLR_E_SHIM_RUNTIME;
421 EnterCriticalSection(&runtime_list_cs);
423 if (This->loaded_runtime)
425 *result = This->loaded_runtime;
426 LeaveCriticalSection(&runtime_list_cs);
427 return hr;
430 if (GlobalCLRMetaHost.callback)
432 GlobalCLRMetaHost.callback(&This->ICLRRuntimeInfo_iface, thread_set_fn, thread_unset_fn);
435 hr = load_mono(mono_path);
437 if (SUCCEEDED(hr))
438 hr = RuntimeHost_Construct(This, &This->loaded_runtime);
440 LeaveCriticalSection(&runtime_list_cs);
442 if (SUCCEEDED(hr))
443 *result = This->loaded_runtime;
445 return hr;
448 void expect_no_runtimes(void)
450 if (mono_handle && is_mono_started && !is_mono_shutdown)
452 ERR("Process exited with a Mono runtime loaded.\n");
453 return;
457 static inline CLRRuntimeInfo *impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo *iface)
459 return CONTAINING_RECORD(iface, CLRRuntimeInfo, ICLRRuntimeInfo_iface);
462 static HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
463 REFIID riid,
464 void **ppvObject)
466 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
468 if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
469 IsEqualGUID( riid, &IID_IUnknown ) )
471 *ppvObject = iface;
473 else
475 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
476 return E_NOINTERFACE;
479 ICLRRuntimeInfo_AddRef( iface );
481 return S_OK;
484 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
486 return 2;
489 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
491 return 1;
494 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
495 LPWSTR pwzBuffer, DWORD *pcchBuffer)
497 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
498 DWORD buffer_size = *pcchBuffer;
499 HRESULT hr = S_OK;
500 char version[11];
501 DWORD size;
503 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
505 size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
507 assert(size <= sizeof(version));
509 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
511 if (pwzBuffer)
513 if (buffer_size >= *pcchBuffer)
514 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
515 else
516 hr = E_NOT_SUFFICIENT_BUFFER;
519 return hr;
522 static BOOL get_install_root(LPWSTR install_dir)
524 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};
525 static const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
527 DWORD len;
528 HKEY key;
530 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
531 return FALSE;
533 len = MAX_PATH * sizeof(WCHAR);
534 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
536 RegCloseKey(key);
537 return FALSE;
539 RegCloseKey(key);
541 return TRUE;
544 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
545 LPWSTR pwzBuffer, DWORD *pcchBuffer)
547 static const WCHAR slash[] = {'\\',0};
548 DWORD buffer_size = *pcchBuffer;
549 WCHAR system_dir[MAX_PATH];
550 WCHAR version[MAX_PATH];
551 DWORD version_size, size;
552 HRESULT hr = S_OK;
554 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
556 if (!get_install_root(system_dir))
558 ERR("error reading registry key for installroot\n");
559 return E_FAIL;
561 else
563 version_size = MAX_PATH;
564 ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
565 lstrcatW(system_dir, version);
566 lstrcatW(system_dir, slash);
567 size = lstrlenW(system_dir) + 1;
570 *pcchBuffer = size;
572 if (pwzBuffer)
574 if (buffer_size >= size)
575 lstrcpyW(pwzBuffer, system_dir);
576 else
577 hr = E_NOT_SUFFICIENT_BUFFER;
580 return hr;
583 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
584 HANDLE hndProcess, BOOL *pbLoaded)
586 FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
588 return E_NOTIMPL;
591 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
592 UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
594 FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
596 return E_NOTIMPL;
599 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
600 LPCWSTR pwzDllName, HMODULE *phndModule)
602 WCHAR version[MAX_PATH];
603 HRESULT hr;
604 DWORD cchBuffer;
606 TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
608 cchBuffer = MAX_PATH;
609 hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
610 if (FAILED(hr)) return hr;
612 return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
615 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
616 LPCSTR pszProcName, LPVOID *ppProc)
618 FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
620 return E_NOTIMPL;
623 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
624 REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
626 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
627 RuntimeHost *host;
628 HRESULT hr;
630 TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
632 hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
634 if (SUCCEEDED(hr))
635 hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
637 return hr;
640 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
641 BOOL *pbLoadable)
643 FIXME("%p %p\n", iface, pbLoadable);
645 return E_NOTIMPL;
648 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
649 DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
651 FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
653 return E_NOTIMPL;
656 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
657 DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
659 FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
661 return E_NOTIMPL;
664 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
666 FIXME("%p\n", iface);
668 return E_NOTIMPL;
671 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
672 BOOL *pbStarted, DWORD *pdwStartupFlags)
674 FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
676 return E_NOTIMPL;
679 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
680 CLRRuntimeInfo_QueryInterface,
681 CLRRuntimeInfo_AddRef,
682 CLRRuntimeInfo_Release,
683 CLRRuntimeInfo_GetVersionString,
684 CLRRuntimeInfo_GetRuntimeDirectory,
685 CLRRuntimeInfo_IsLoaded,
686 CLRRuntimeInfo_LoadErrorString,
687 CLRRuntimeInfo_LoadLibrary,
688 CLRRuntimeInfo_GetProcAddress,
689 CLRRuntimeInfo_GetInterface,
690 CLRRuntimeInfo_IsLoadable,
691 CLRRuntimeInfo_SetDefaultStartupFlags,
692 CLRRuntimeInfo_GetDefaultStartupFlags,
693 CLRRuntimeInfo_BindAsLegacyV2Runtime,
694 CLRRuntimeInfo_IsStarted
697 HRESULT ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo *iface, RuntimeHost **result)
699 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
701 assert(This->ICLRRuntimeInfo_iface.lpVtbl == &CLRRuntimeInfoVtbl);
703 return CLRRuntimeInfo_GetRuntimeHost(This, result);
706 #ifdef __i386__
707 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};
708 #elif defined(__x86_64__)
709 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};
710 #else
711 static const WCHAR libmono2_arch_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
712 #endif
714 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path)
716 static const WCHAR mono2_dll[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
717 static const WCHAR libmono2_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
718 DWORD attributes=INVALID_FILE_ATTRIBUTES;
720 lstrcpyW(dll_path, path);
721 lstrcatW(dll_path, libmono2_arch_dll);
722 attributes = GetFileAttributesW(dll_path);
724 if (attributes == INVALID_FILE_ATTRIBUTES)
726 lstrcpyW(dll_path, path);
727 lstrcatW(dll_path, mono2_dll);
728 attributes = GetFileAttributesW(dll_path);
731 if (attributes == INVALID_FILE_ATTRIBUTES)
733 lstrcpyW(dll_path, path);
734 lstrcatW(dll_path, libmono2_dll);
735 attributes = GetFileAttributesW(dll_path);
738 return (attributes != INVALID_FILE_ATTRIBUTES);
741 static BOOL get_mono_path_local(LPWSTR path)
743 static const WCHAR subdir_mono[] = {'\\','m','o','n','o','\\','m','o','n','o','-','2','.','0', 0};
744 WCHAR base_path[MAX_PATH], mono_dll_path[MAX_PATH];
746 /* c:\windows\mono\mono-2.0 */
747 GetWindowsDirectoryW(base_path, MAX_PATH);
748 lstrcatW(base_path, subdir_mono);
750 if (find_mono_dll(base_path, mono_dll_path))
752 lstrcpyW(path, base_path);
753 return TRUE;
756 return FALSE;
759 static BOOL get_mono_path_registry(LPWSTR path)
761 static const WCHAR keyname[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\','M','o','n','o',0};
762 static const WCHAR valuename[] = {'R','u','n','t','i','m','e','P','a','t','h',0};
763 WCHAR base_path[MAX_PATH], mono_dll_path[MAX_PATH];
764 HKEY hkey;
765 DWORD res, valuesize;
766 BOOL ret=FALSE;
768 /* @@ Wine registry key: HKCU\Software\Wine\Mono */
769 res = RegOpenKeyW(HKEY_CURRENT_USER, keyname, &hkey);
770 if (res != ERROR_SUCCESS)
771 return FALSE;
773 valuesize = sizeof(base_path);
774 res = RegGetValueW(hkey, NULL, valuename, RRF_RT_REG_SZ, NULL, base_path, &valuesize);
775 if (res == ERROR_SUCCESS && find_mono_dll(base_path, mono_dll_path))
777 lstrcpyW(path, base_path);
778 ret = TRUE;
781 RegCloseKey(hkey);
783 return ret;
786 static BOOL get_mono_path_dos(const WCHAR *dir, LPWSTR path)
788 static const WCHAR unix_prefix[] = {'\\','\\','?','\\','u','n','i','x','\\'};
789 static const WCHAR basedir[] = L"\\wine-mono-" WINE_MONO_VERSION;
790 LPWSTR dos_dir;
791 WCHAR mono_dll_path[MAX_PATH];
792 DWORD len;
793 BOOL ret;
795 if (memcmp(dir, unix_prefix, sizeof(unix_prefix)) == 0)
796 return FALSE; /* No drive letter for this directory */
798 len = lstrlenW( dir ) + lstrlenW( basedir ) + 1;
799 if (!(dos_dir = heap_alloc( len * sizeof(WCHAR) ))) return FALSE;
800 lstrcpyW( dos_dir, dir );
801 lstrcatW( dos_dir, basedir );
803 ret = find_mono_dll(dos_dir, mono_dll_path);
804 if (ret)
805 lstrcpyW(path, dos_dir);
807 heap_free(dos_dir);
809 return ret;
812 static BOOL get_mono_path_unix(const char *unix_dir, LPWSTR path)
814 static WCHAR * (CDECL *p_wine_get_dos_file_name)(const char*);
815 LPWSTR dos_dir;
816 BOOL ret;
818 if (!p_wine_get_dos_file_name)
820 p_wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
821 if (!p_wine_get_dos_file_name)
822 return FALSE;
825 dos_dir = p_wine_get_dos_file_name(unix_dir);
826 if (!dos_dir)
827 return FALSE;
829 ret = get_mono_path_dos( dos_dir, path);
831 heap_free(dos_dir);
832 return ret;
835 static BOOL get_mono_path_datadir(LPWSTR path)
837 static const WCHAR winedatadirW[] = {'W','I','N','E','D','A','T','A','D','I','R',0};
838 static const WCHAR winebuilddirW[] = {'W','I','N','E','B','U','I','L','D','D','I','R',0};
839 static const WCHAR unix_prefix[] = {'\\','?','?','\\','u','n','i','x','\\'};
840 static const WCHAR monoW[] = {'\\','m','o','n','o',0};
841 static const WCHAR dotdotmonoW[] = {'\\','.','.','\\','m','o','n','o',0};
842 const WCHAR *data_dir, *suffix;
843 WCHAR *package_dir;
844 BOOL ret;
846 if ((data_dir = _wgetenv( winedatadirW )))
847 suffix = monoW;
848 else if ((data_dir = _wgetenv( winebuilddirW )))
849 suffix = dotdotmonoW;
850 else
851 return FALSE;
853 if (!wcsncmp( data_dir, unix_prefix, wcslen(unix_prefix) )) return FALSE;
854 data_dir += 4; /* skip \??\ prefix */
855 package_dir = heap_alloc( (lstrlenW(data_dir) + lstrlenW(suffix) + 1) * sizeof(WCHAR));
856 lstrcpyW( package_dir, data_dir );
857 lstrcatW( package_dir, suffix );
859 ret = get_mono_path_dos(package_dir, path);
861 heap_free(package_dir);
863 return ret;
866 BOOL get_mono_path(LPWSTR path, BOOL skip_local)
868 return (!skip_local && get_mono_path_local(path)) ||
869 get_mono_path_registry(path) ||
870 get_mono_path_datadir(path) ||
871 get_mono_path_unix(INSTALL_DATADIR "/wine/mono", path) ||
872 (strcmp(INSTALL_DATADIR, "/usr/share") &&
873 get_mono_path_unix("/usr/share/wine/mono", path)) ||
874 get_mono_path_unix("/opt/wine/mono", path);
877 struct InstalledRuntimeEnum
879 IEnumUnknown IEnumUnknown_iface;
880 LONG ref;
881 ULONG pos;
884 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
886 static inline struct InstalledRuntimeEnum *impl_from_IEnumUnknown(IEnumUnknown *iface)
888 return CONTAINING_RECORD(iface, struct InstalledRuntimeEnum, IEnumUnknown_iface);
891 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface, REFIID riid,
892 void **ppvObject)
894 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
896 if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
897 IsEqualGUID( riid, &IID_IUnknown ) )
899 *ppvObject = iface;
901 else
903 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
904 return E_NOINTERFACE;
907 IEnumUnknown_AddRef( iface );
909 return S_OK;
912 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
914 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
915 ULONG ref = InterlockedIncrement(&This->ref);
917 TRACE("(%p) refcount=%u\n", iface, ref);
919 return ref;
922 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
924 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
925 ULONG ref = InterlockedDecrement(&This->ref);
927 TRACE("(%p) refcount=%u\n", iface, ref);
929 if (ref == 0)
931 HeapFree(GetProcessHeap(), 0, This);
934 return ref;
937 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
938 IUnknown **rgelt, ULONG *pceltFetched)
940 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
941 ULONG num_fetched = 0;
942 HRESULT hr=S_OK;
943 IUnknown *item;
945 TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
947 while (num_fetched < celt)
949 if (This->pos >= NUM_RUNTIMES)
951 hr = S_FALSE;
952 break;
954 item = (IUnknown*)&runtimes[This->pos].ICLRRuntimeInfo_iface;
955 IUnknown_AddRef(item);
956 rgelt[num_fetched] = item;
957 num_fetched++;
958 This->pos++;
961 if (pceltFetched)
962 *pceltFetched = num_fetched;
964 return hr;
967 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
969 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
970 ULONG num_fetched = 0;
971 HRESULT hr=S_OK;
973 TRACE("(%p,%u)\n", iface, celt);
975 while (num_fetched < celt)
977 if (This->pos >= NUM_RUNTIMES)
979 hr = S_FALSE;
980 break;
982 num_fetched++;
983 This->pos++;
986 return hr;
989 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
991 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
993 TRACE("(%p)\n", iface);
995 This->pos = 0;
997 return S_OK;
1000 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
1002 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
1003 struct InstalledRuntimeEnum *new_enum;
1005 TRACE("(%p)\n", iface);
1007 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
1008 if (!new_enum)
1009 return E_OUTOFMEMORY;
1011 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
1012 new_enum->ref = 1;
1013 new_enum->pos = This->pos;
1015 *ppenum = &new_enum->IEnumUnknown_iface;
1017 return S_OK;
1020 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
1021 InstalledRuntimeEnum_QueryInterface,
1022 InstalledRuntimeEnum_AddRef,
1023 InstalledRuntimeEnum_Release,
1024 InstalledRuntimeEnum_Next,
1025 InstalledRuntimeEnum_Skip,
1026 InstalledRuntimeEnum_Reset,
1027 InstalledRuntimeEnum_Clone
1030 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
1031 REFIID riid,
1032 void **ppvObject)
1034 TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
1036 if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
1037 IsEqualGUID( riid, &IID_IUnknown ) )
1039 *ppvObject = iface;
1041 else
1043 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
1044 return E_NOINTERFACE;
1047 ICLRMetaHost_AddRef( iface );
1049 return S_OK;
1052 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
1054 return 2;
1057 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
1059 return 1;
1062 static inline BOOL isDigit(WCHAR c)
1064 return c >= '0' && c <= '9';
1067 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
1069 *major = 0;
1070 *minor = 0;
1071 *build = 0;
1073 if (version[0] == 'v' || version[0] == 'V')
1075 version++;
1076 if (!isDigit(*version))
1077 return FALSE;
1079 while (isDigit(*version))
1080 *major = *major * 10 + (*version++ - '0');
1082 if (*version == 0)
1083 return TRUE;
1085 if (*version++ != '.' || !isDigit(*version))
1086 return FALSE;
1088 while (isDigit(*version))
1089 *minor = *minor * 10 + (*version++ - '0');
1091 if (*version == 0)
1092 return TRUE;
1094 if (*version++ != '.' || !isDigit(*version))
1095 return FALSE;
1097 while (isDigit(*version))
1098 *build = *build * 10 + (*version++ - '0');
1100 return *version == 0;
1102 else
1103 return FALSE;
1106 static HRESULT get_runtime(LPCWSTR pwzVersion, BOOL allow_short,
1107 REFIID iid, LPVOID *ppRuntime)
1109 int i;
1110 DWORD major, minor, build;
1112 if (!pwzVersion)
1113 return E_POINTER;
1115 if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
1117 ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
1118 return CLR_E_SHIM_RUNTIME;
1121 for (i=0; i<NUM_RUNTIMES; i++)
1123 if (runtimes[i].major == major && runtimes[i].minor == minor &&
1124 (runtimes[i].build == build || (allow_short && major >= 4 && build == 0)))
1126 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface, iid,
1127 ppRuntime);
1131 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
1132 return CLR_E_SHIM_RUNTIME;
1135 HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
1136 LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
1138 TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
1140 return get_runtime(pwzVersion, FALSE, iid, ppRuntime);
1143 HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
1144 LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
1146 ASSEMBLY *assembly;
1147 HRESULT hr;
1148 LPSTR version;
1149 ULONG buffer_size=*pcchBuffer;
1151 TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
1153 hr = assembly_create(&assembly, pwzFilePath);
1155 if (SUCCEEDED(hr))
1157 hr = assembly_get_runtime_version(assembly, &version);
1159 if (SUCCEEDED(hr))
1161 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
1163 if (pwzBuffer)
1165 if (buffer_size >= *pcchBuffer)
1166 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
1167 else
1168 hr = E_NOT_SUFFICIENT_BUFFER;
1172 assembly_release(assembly);
1175 return hr;
1178 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
1179 IEnumUnknown **ppEnumerator)
1181 struct InstalledRuntimeEnum *new_enum;
1183 TRACE("%p\n", ppEnumerator);
1185 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
1186 if (!new_enum)
1187 return E_OUTOFMEMORY;
1189 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
1190 new_enum->ref = 1;
1191 new_enum->pos = 0;
1193 *ppEnumerator = &new_enum->IEnumUnknown_iface;
1195 return S_OK;
1198 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
1199 HANDLE hndProcess, IEnumUnknown **ppEnumerator)
1201 FIXME("%p %p\n", hndProcess, ppEnumerator);
1203 return E_NOTIMPL;
1206 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
1207 RuntimeLoadedCallbackFnPtr pCallbackFunction)
1209 TRACE("%p\n", pCallbackFunction);
1211 if(!pCallbackFunction)
1212 return E_POINTER;
1214 if (GlobalCLRMetaHost.callback)
1215 return HOST_E_INVALIDOPERATION;
1217 GlobalCLRMetaHost.callback = pCallbackFunction;
1219 return S_OK;
1222 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
1223 REFIID riid, LPVOID *ppUnk)
1225 FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
1227 return E_NOTIMPL;
1230 HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
1232 TRACE("%i\n", iExitCode);
1234 EnterCriticalSection(&runtime_list_cs);
1236 if (is_mono_started && !is_mono_shutdown)
1238 /* search for a runtime and call System.Environment.Exit() */
1239 int i;
1241 for (i=0; i<NUM_RUNTIMES; i++)
1242 if (runtimes[i].loaded_runtime)
1243 RuntimeHost_ExitProcess(runtimes[i].loaded_runtime, iExitCode);
1246 ExitProcess(iExitCode);
1249 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
1251 CLRMetaHost_QueryInterface,
1252 CLRMetaHost_AddRef,
1253 CLRMetaHost_Release,
1254 CLRMetaHost_GetRuntime,
1255 CLRMetaHost_GetVersionFromFile,
1256 CLRMetaHost_EnumerateInstalledRuntimes,
1257 CLRMetaHost_EnumerateLoadedRuntimes,
1258 CLRMetaHost_RequestRuntimeLoadedNotification,
1259 CLRMetaHost_QueryLegacyV2RuntimeBinding,
1260 CLRMetaHost_ExitProcess
1263 static struct CLRMetaHost GlobalCLRMetaHost = {
1264 { &CLRMetaHost_vtbl }
1267 HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1269 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost.ICLRMetaHost_iface, riid, ppobj);
1272 struct CLRMetaHostPolicy
1274 ICLRMetaHostPolicy ICLRMetaHostPolicy_iface;
1277 static struct CLRMetaHostPolicy GlobalCLRMetaHostPolicy;
1279 static HRESULT WINAPI metahostpolicy_QueryInterface(ICLRMetaHostPolicy *iface, REFIID riid, void **obj)
1281 TRACE("%s %p\n", debugstr_guid(riid), obj);
1283 if ( IsEqualGUID( riid, &IID_ICLRMetaHostPolicy ) ||
1284 IsEqualGUID( riid, &IID_IUnknown ) )
1286 ICLRMetaHostPolicy_AddRef( iface );
1287 *obj = iface;
1288 return S_OK;
1291 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
1293 *obj = NULL;
1294 return E_NOINTERFACE;
1297 static ULONG WINAPI metahostpolicy_AddRef(ICLRMetaHostPolicy *iface)
1299 return 2;
1302 static ULONG WINAPI metahostpolicy_Release(ICLRMetaHostPolicy *iface)
1304 return 1;
1307 static HRESULT WINAPI metahostpolicy_GetRequestedRuntime(ICLRMetaHostPolicy *iface, METAHOST_POLICY_FLAGS dwPolicyFlags,
1308 LPCWSTR pwzBinary, IStream *pCfgStream, LPWSTR pwzVersion, DWORD *pcchVersion,
1309 LPWSTR pwzImageVersion, DWORD *pcchImageVersion, DWORD *pdwConfigFlags, REFIID riid,
1310 LPVOID *ppRuntime)
1312 ICLRRuntimeInfo *result;
1313 HRESULT hr;
1314 WCHAR filename[MAX_PATH];
1315 const WCHAR *path = NULL;
1316 int flags = 0;
1318 TRACE("%d %p %p %p %p %p %p %p %s %p\n", dwPolicyFlags, pwzBinary, pCfgStream,
1319 pwzVersion, pcchVersion, pwzImageVersion, pcchImageVersion, pdwConfigFlags,
1320 debugstr_guid(riid), ppRuntime);
1322 if (pdwConfigFlags) {
1323 FIXME("ignoring config flags\n");
1324 *pdwConfigFlags = 0;
1327 if(dwPolicyFlags & METAHOST_POLICY_USE_PROCESS_IMAGE_PATH)
1329 GetModuleFileNameW(0, filename, MAX_PATH);
1330 path = filename;
1332 else if(pwzBinary)
1334 path = pwzBinary;
1337 if(dwPolicyFlags & METAHOST_POLICY_APPLY_UPGRADE_POLICY)
1338 flags |= RUNTIME_INFO_UPGRADE_VERSION;
1340 hr = get_runtime_info(path, pwzImageVersion, NULL, pCfgStream, 0, flags, FALSE, &result);
1341 if (SUCCEEDED(hr))
1343 if (pwzImageVersion)
1345 /* Ignoring errors on purpose */
1346 ICLRRuntimeInfo_GetVersionString(result, pwzImageVersion, pcchImageVersion);
1349 hr = ICLRRuntimeInfo_QueryInterface(result, riid, ppRuntime);
1351 ICLRRuntimeInfo_Release(result);
1354 TRACE("<- 0x%08x\n", hr);
1356 return hr;
1359 static const struct ICLRMetaHostPolicyVtbl CLRMetaHostPolicy_vtbl =
1361 metahostpolicy_QueryInterface,
1362 metahostpolicy_AddRef,
1363 metahostpolicy_Release,
1364 metahostpolicy_GetRequestedRuntime
1367 static struct CLRMetaHostPolicy GlobalCLRMetaHostPolicy = {
1368 { &CLRMetaHostPolicy_vtbl }
1371 HRESULT CLRMetaHostPolicy_CreateInstance(REFIID riid, void **ppobj)
1373 return ICLRMetaHostPolicy_QueryInterface(&GlobalCLRMetaHostPolicy.ICLRMetaHostPolicy_iface, riid, ppobj);
1377 * Assembly search override settings:
1379 * WINE_MONO_OVERRIDES=*,Gac=n
1380 * Never search the GAC for libraries.
1382 * WINE_MONO_OVERRIDES=Microsoft.Xna.Framework,Gac=n
1383 * Never search the GAC for Microsoft.Xna.Framework
1385 * WINE_MONO_OVERRIDES=Microsoft.Xna.Framework.*,Gac=n;Microsoft.Xna.Framework.GamerServices,Gac=y
1386 * Never search the GAC for Microsoft.Xna.Framework, or any library starting
1387 * with Microsoft.Xna.Framework, except for Microsoft.Xna.Framework.GamerServices
1390 /* assembly search override flags */
1391 #define ASSEMBLY_SEARCH_GAC 1
1392 #define ASSEMBLY_SEARCH_UNDEFINED 2
1393 #define ASSEMBLY_SEARCH_DEFAULT ASSEMBLY_SEARCH_GAC
1395 typedef struct override_entry {
1396 char *name;
1397 DWORD flags;
1398 struct list entry;
1399 } override_entry;
1401 static struct list env_overrides = LIST_INIT(env_overrides);
1403 #define IS_OPTION_TRUE(ch) \
1404 ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
1405 #define IS_OPTION_FALSE(ch) \
1406 ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
1408 static void parse_override_entry(override_entry *entry, const char *string, int string_len)
1410 const char *next_key, *equals, *value;
1411 UINT kvp_len, key_len;
1413 entry->flags = ASSEMBLY_SEARCH_DEFAULT;
1415 while (string && string_len > 0)
1417 next_key = memchr(string, ',', string_len);
1419 if (next_key)
1421 kvp_len = next_key - string;
1422 next_key++;
1424 else
1425 kvp_len = string_len;
1427 equals = memchr(string, '=', kvp_len);
1429 if (equals)
1431 key_len = equals - string;
1432 value = equals + 1;
1433 switch (key_len) {
1434 case 3:
1435 if (!_strnicmp(string, "gac", 3)) {
1436 if (IS_OPTION_TRUE(*value))
1437 entry->flags |= ASSEMBLY_SEARCH_GAC;
1438 else if (IS_OPTION_FALSE(*value))
1439 entry->flags &= ~ASSEMBLY_SEARCH_GAC;
1441 break;
1442 default:
1443 break;
1447 string = next_key;
1448 string_len -= kvp_len + 1;
1452 static BOOL WINAPI parse_env_overrides(INIT_ONCE *once, void *param, void **context)
1454 const char *override_string = getenv("WINE_MONO_OVERRIDES");
1455 struct override_entry *entry;
1457 if (override_string)
1459 const char *entry_start;
1461 entry_start = override_string;
1463 while (entry_start && *entry_start)
1465 const char *next_entry, *basename_end;
1466 UINT entry_len;
1468 next_entry = strchr(entry_start, ';');
1470 if (next_entry)
1472 entry_len = next_entry - entry_start;
1473 next_entry++;
1475 else
1476 entry_len = strlen(entry_start);
1478 basename_end = memchr(entry_start, ',', entry_len);
1480 if (!basename_end)
1482 entry_start = next_entry;
1483 continue;
1486 entry = heap_alloc_zero(sizeof(*entry));
1487 if (!entry)
1489 ERR("out of memory\n");
1490 break;
1493 entry->name = heap_alloc_zero(basename_end - entry_start + 1);
1494 if (!entry->name)
1496 ERR("out of memory\n");
1497 heap_free(entry);
1498 break;
1501 memcpy(entry->name, entry_start, basename_end - entry_start);
1503 entry_len -= basename_end - entry_start + 1;
1504 entry_start = basename_end + 1;
1506 parse_override_entry(entry, entry_start, entry_len);
1508 list_add_tail(&env_overrides, &entry->entry);
1510 entry_start = next_entry;
1514 return TRUE;
1517 static DWORD get_basename_search_flags(const char *basename, MonoAssemblyName *aname, HKEY userkey, HKEY appkey)
1519 struct override_entry *entry;
1520 char buffer[256];
1521 DWORD buffer_size;
1522 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
1524 InitOnceExecuteOnce(&init_once, parse_env_overrides, NULL, NULL);
1526 LIST_FOR_EACH_ENTRY(entry, &env_overrides, override_entry, entry)
1528 if (strcmp(basename, entry->name) == 0)
1530 return entry->flags;
1534 buffer_size = sizeof(buffer);
1535 if (appkey && !RegQueryValueExA(appkey, basename, 0, NULL, (LPBYTE)buffer, &buffer_size))
1537 override_entry reg_entry;
1539 memset(&reg_entry, 0, sizeof(reg_entry));
1541 parse_override_entry(&reg_entry, buffer, strlen(buffer));
1543 return reg_entry.flags;
1546 buffer_size = sizeof(buffer);
1547 if (userkey && !RegQueryValueExA(userkey, basename, 0, NULL, (LPBYTE)buffer, &buffer_size))
1549 override_entry reg_entry;
1551 memset(&reg_entry, 0, sizeof(reg_entry));
1553 parse_override_entry(&reg_entry, buffer, strlen(buffer));
1555 return reg_entry.flags;
1558 if (strcmp(basename, "Microsoft.Xna.Framework.*") == 0 &&
1559 mono_assembly_name_get_version(aname, NULL, NULL, NULL) == 4)
1560 /* Use FNA as a replacement for XNA4. */
1561 return 0;
1563 return ASSEMBLY_SEARCH_UNDEFINED;
1566 static HKEY get_app_overrides_key(void)
1568 static const WCHAR subkeyW[] = {'\\','M','o','n','o','\\','A','s','m','O','v','e','r','r','i','d','e','s',0};
1569 WCHAR bufferW[MAX_PATH+18];
1570 HKEY appkey = 0;
1571 DWORD len;
1573 len = (GetModuleFileNameW( 0, bufferW, MAX_PATH ));
1574 if (len && len < MAX_PATH)
1576 HKEY tmpkey;
1577 WCHAR *p, *appname = bufferW;
1578 if ((p = wcsrchr( appname, '/' ))) appname = p + 1;
1579 if ((p = wcsrchr( appname, '\\' ))) appname = p + 1;
1580 lstrcatW( appname, subkeyW );
1581 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Mono\AsmOverrides */
1582 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
1584 if (RegOpenKeyW( tmpkey, appname, &appkey )) appkey = 0;
1585 RegCloseKey( tmpkey );
1589 return appkey;
1592 static DWORD get_assembly_search_flags(MonoAssemblyName *aname)
1594 const char *name = mono_assembly_name_get_name(aname);
1595 char *name_copy, *name_end;
1596 DWORD result;
1597 HKEY appkey = 0, userkey;
1599 /* @@ Wine registry key: HKCU\Software\Wine\Mono\AsmOverrides */
1600 if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Mono\\AsmOverrides", &userkey )) userkey = 0;
1602 appkey = get_app_overrides_key();
1604 result = get_basename_search_flags(name, aname, userkey, appkey);
1605 if (result != ASSEMBLY_SEARCH_UNDEFINED)
1607 if (userkey) RegCloseKey(userkey);
1608 if (appkey) RegCloseKey(appkey);
1609 return result;
1612 name_copy = heap_alloc((strlen(name) + 3) * sizeof(WCHAR));
1613 if (!name_copy)
1615 ERR("out of memory\n");
1616 if (userkey) RegCloseKey(userkey);
1617 if (appkey) RegCloseKey(appkey);
1618 return ASSEMBLY_SEARCH_DEFAULT;
1621 strcpy(name_copy, name);
1622 name_end = name_copy + strlen(name);
1626 strcpy(name_end, ".*");
1627 result = get_basename_search_flags(name_copy, aname, userkey, appkey);
1628 if (result != ASSEMBLY_SEARCH_UNDEFINED) break;
1630 *name_end = 0;
1631 name_end = strrchr(name_copy, '.');
1632 } while (name_end != NULL);
1634 /* default flags */
1635 if (result == ASSEMBLY_SEARCH_UNDEFINED)
1637 result = get_basename_search_flags("*", aname, userkey, appkey);
1638 if (result == ASSEMBLY_SEARCH_UNDEFINED)
1639 result = ASSEMBLY_SEARCH_DEFAULT;
1642 heap_free(name_copy);
1643 if (appkey) RegCloseKey(appkey);
1644 if (userkey) RegCloseKey(userkey);
1646 return result;
1649 HRESULT get_file_from_strongname(WCHAR* stringnameW, WCHAR* assemblies_path, int path_length)
1651 HRESULT hr=S_OK;
1652 IAssemblyCache *asmcache;
1653 ASSEMBLY_INFO info;
1654 static const WCHAR fusiondll[] = {'f','u','s','i','o','n',0};
1655 HMODULE hfusion=NULL;
1656 static HRESULT (WINAPI *pCreateAssemblyCache)(IAssemblyCache**,DWORD);
1658 if (!pCreateAssemblyCache)
1660 hr = LoadLibraryShim(fusiondll, NULL, NULL, &hfusion);
1662 if (SUCCEEDED(hr))
1664 pCreateAssemblyCache = (void*)GetProcAddress(hfusion, "CreateAssemblyCache");
1665 if (!pCreateAssemblyCache)
1666 hr = E_FAIL;
1670 if (SUCCEEDED(hr))
1671 hr = pCreateAssemblyCache(&asmcache, 0);
1673 if (SUCCEEDED(hr))
1675 info.cbAssemblyInfo = sizeof(info);
1676 info.pszCurrentAssemblyPathBuf = assemblies_path;
1677 info.cchBuf = path_length;
1678 assemblies_path[0] = 0;
1680 hr = IAssemblyCache_QueryAssemblyInfo(asmcache, 0, stringnameW, &info);
1682 IAssemblyCache_Release(asmcache);
1685 return hr;
1688 static MonoAssembly* mono_assembly_try_load(WCHAR *path)
1690 MonoAssembly *result = NULL;
1691 MonoImageOpenStatus stat;
1692 char *pathA;
1694 if (!(pathA = WtoA(path))) return NULL;
1696 result = mono_assembly_open(pathA, &stat);
1697 HeapFree(GetProcessHeap(), 0, pathA);
1699 if (result) TRACE("found: %s\n", debugstr_w(path));
1700 return result;
1703 static MonoAssembly* CDECL mono_assembly_preload_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data)
1705 HRESULT hr;
1706 MonoAssembly *result=NULL;
1707 char *stringname=NULL;
1708 const char *assemblyname;
1709 const char *culture;
1710 LPWSTR stringnameW, cultureW;
1711 int stringnameW_size, cultureW_size;
1712 WCHAR path[MAX_PATH];
1713 char *pathA;
1714 MonoImageOpenStatus stat;
1715 DWORD search_flags;
1716 int i;
1717 static const WCHAR dotdllW[] = {'.','d','l','l',0};
1718 static const WCHAR dotexeW[] = {'.','e','x','e',0};
1720 stringname = mono_stringify_assembly_name(aname);
1721 assemblyname = mono_assembly_name_get_name(aname);
1722 culture = mono_assembly_name_get_culture(aname);
1723 if (culture)
1725 cultureW_size = MultiByteToWideChar(CP_UTF8, 0, culture, -1, NULL, 0);
1726 cultureW = HeapAlloc(GetProcessHeap(), 0, cultureW_size * sizeof(WCHAR));
1727 if (cultureW) MultiByteToWideChar(CP_UTF8, 0, culture, -1, cultureW, cultureW_size);
1729 else cultureW = NULL;
1731 TRACE("%s\n", debugstr_a(stringname));
1733 if (!stringname || !assemblyname) return NULL;
1735 search_flags = get_assembly_search_flags(aname);
1736 if (private_path)
1738 stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, assemblyname, -1, NULL, 0);
1739 stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1740 if (stringnameW)
1742 MultiByteToWideChar(CP_UTF8, 0, assemblyname, -1, stringnameW, stringnameW_size);
1743 for (i = 0; private_path[i] != NULL; i++)
1745 /* This is the lookup order used in Mono */
1746 /* 1st try: [culture]/[name].dll (culture may be empty) */
1747 wcscpy(path, private_path[i]);
1748 if (cultureW) PathAppendW(path, cultureW);
1749 PathAppendW(path, stringnameW);
1750 wcscat(path, dotdllW);
1751 result = mono_assembly_try_load(path);
1752 if (result) break;
1754 /* 2nd try: [culture]/[name].exe (culture may be empty) */
1755 wcscpy(path + wcslen(path) - wcslen(dotdllW), dotexeW);
1756 result = mono_assembly_try_load(path);
1757 if (result) break;
1759 /* 3rd try: [culture]/[name]/[name].dll (culture may be empty) */
1760 path[wcslen(path) - wcslen(dotexeW)] = 0;
1761 PathAppendW(path, stringnameW);
1762 wcscat(path, dotdllW);
1763 result = mono_assembly_try_load(path);
1764 if (result) break;
1766 /* 4th try: [culture]/[name]/[name].exe (culture may be empty) */
1767 wcscpy(path + wcslen(path) - wcslen(dotdllW), dotexeW);
1768 result = mono_assembly_try_load(path);
1769 if (result) break;
1771 HeapFree(GetProcessHeap(), 0, stringnameW);
1772 if (result) goto done;
1776 /* FIXME: We should search the given paths before the GAC. */
1778 if ((search_flags & ASSEMBLY_SEARCH_GAC) != 0)
1780 stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, stringname, -1, NULL, 0);
1782 stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1783 if (stringnameW)
1785 MultiByteToWideChar(CP_UTF8, 0, stringname, -1, stringnameW, stringnameW_size);
1787 hr = get_file_from_strongname(stringnameW, path, MAX_PATH);
1789 HeapFree(GetProcessHeap(), 0, stringnameW);
1791 else
1792 hr = E_OUTOFMEMORY;
1794 if (SUCCEEDED(hr))
1796 TRACE("found: %s\n", debugstr_w(path));
1798 pathA = WtoA(path);
1800 if (pathA)
1802 result = mono_assembly_open(pathA, &stat);
1804 if (!result)
1805 ERR("Failed to load %s, status=%u\n", debugstr_w(path), stat);
1807 HeapFree(GetProcessHeap(), 0, pathA);
1811 else
1812 TRACE("skipping Windows GAC search due to override setting\n");
1814 done:
1815 HeapFree(GetProcessHeap(), 0, cultureW);
1816 mono_free(stringname);
1818 return result;
1821 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1822 IStream *config_stream, DWORD startup_flags, DWORD runtimeinfo_flags,
1823 BOOL legacy, ICLRRuntimeInfo **result)
1825 static const WCHAR dotconfig[] = {'.','c','o','n','f','i','g',0};
1826 static const DWORD supported_startup_flags = 0;
1827 static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1828 int i;
1829 WCHAR local_version[MAX_PATH];
1830 ULONG local_version_size = MAX_PATH;
1831 WCHAR local_config_file[MAX_PATH];
1832 HRESULT hr;
1833 parsed_config_file parsed_config;
1835 if (startup_flags & ~supported_startup_flags)
1836 FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
1838 if (runtimeinfo_flags & ~supported_runtime_flags)
1839 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
1841 if (exefile && !exefile[0])
1842 exefile = NULL;
1844 if (exefile && !config_file && !config_stream)
1846 lstrcpyW(local_config_file, exefile);
1847 lstrcatW(local_config_file, dotconfig);
1849 config_file = local_config_file;
1852 if (config_file || config_stream)
1854 BOOL found = FALSE;
1855 if (config_file)
1856 hr = parse_config_file(config_file, &parsed_config);
1857 else
1858 hr = parse_config_stream(config_stream, &parsed_config);
1860 if (SUCCEEDED(hr))
1862 supported_runtime *entry;
1863 LIST_FOR_EACH_ENTRY(entry, &parsed_config.supported_runtimes, supported_runtime, entry)
1865 hr = get_runtime(entry->version, TRUE, &IID_ICLRRuntimeInfo, (void**)result);
1866 if (SUCCEEDED(hr))
1868 found = TRUE;
1869 break;
1873 else
1875 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file), hr);
1878 free_parsed_config_file(&parsed_config);
1880 if (found)
1881 return S_OK;
1884 if (exefile && !version)
1886 DWORD major, minor, build;
1888 hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1890 version = local_version;
1892 if (FAILED(hr)) return hr;
1894 /* When running an executable, specifically when getting the version number from
1895 * the exe, native accepts a matching major.minor with build <= expected build. */
1896 if (!parse_runtime_version(version, &major, &minor, &build))
1898 ERR("Cannot parse %s\n", debugstr_w(version));
1899 return CLR_E_SHIM_RUNTIME;
1902 if (legacy)
1903 i = 3;
1904 else
1905 i = NUM_RUNTIMES;
1907 while (i--)
1909 if (runtimes[i].major == major && runtimes[i].minor == minor && runtimes[i].build >= build)
1911 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface,
1912 &IID_ICLRRuntimeInfo, (void **)result);
1917 if (version)
1919 hr = CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1920 if(SUCCEEDED(hr))
1921 return hr;
1924 if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1926 DWORD major, minor, build;
1928 if (version && !parse_runtime_version(version, &major, &minor, &build))
1930 ERR("Cannot parse %s\n", debugstr_w(version));
1931 return CLR_E_SHIM_RUNTIME;
1934 if (legacy)
1935 i = 3;
1936 else
1937 i = NUM_RUNTIMES;
1939 while (i--)
1941 /* Must be greater or equal to the version passed in. */
1942 if (!version || ((runtimes[i].major >= major && runtimes[i].minor >= minor && runtimes[i].build >= build) ||
1943 (runtimes[i].major >= major && runtimes[i].minor > minor) ||
1944 (runtimes[i].major > major)))
1946 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface,
1947 &IID_ICLRRuntimeInfo, (void **)result);
1951 return CLR_E_SHIM_RUNTIME;
1954 return CLR_E_SHIM_RUNTIME;