ntdll: Use the standard C library names for the wide char functions.
[wine.git] / dlls / mscoree / metahost.c
blob77029942528949559a4068fdd7c3e92f0e230b06
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"
33 #include "corerror.h"
34 #include "cor.h"
35 #include "mscoree.h"
36 #include "corhdr.h"
37 #include "cordebug.h"
38 #include "metahost.h"
39 #include "fusion.h"
40 #include "wine/list.h"
41 #include "wine/heap.h"
42 #include "mscoree_private.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL( mscoree );
48 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl;
50 #define NUM_RUNTIMES 4
52 static struct CLRRuntimeInfo runtimes[NUM_RUNTIMES] = {
53 {{&CLRRuntimeInfoVtbl}, 1, 0, 3705, 0},
54 {{&CLRRuntimeInfoVtbl}, 1, 1, 4322, 0},
55 {{&CLRRuntimeInfoVtbl}, 2, 0, 50727, 0},
56 {{&CLRRuntimeInfoVtbl}, 4, 0, 30319, 0}
59 static CRITICAL_SECTION runtime_list_cs;
60 static CRITICAL_SECTION_DEBUG runtime_list_cs_debug =
62 0, 0, &runtime_list_cs,
63 { &runtime_list_cs_debug.ProcessLocksList,
64 &runtime_list_cs_debug.ProcessLocksList },
65 0, 0, { (DWORD_PTR)(__FILE__ ": runtime_list_cs") }
67 static CRITICAL_SECTION runtime_list_cs = { &runtime_list_cs_debug, -1, 0, 0, 0, 0 };
69 struct CLRMetaHost
71 ICLRMetaHost ICLRMetaHost_iface;
73 RuntimeLoadedCallbackFnPtr callback;
76 static struct CLRMetaHost GlobalCLRMetaHost;
78 static HMODULE mono_handle;
80 BOOL is_mono_started;
81 static BOOL is_mono_shutdown;
83 typedef struct _MonoProfilerDesc *MonoProfilerHandle;
85 typedef void (CDECL *MonoProfilerRuntimeShutdownBeginCallback) (MonoProfiler *prof);
87 MonoImage* (CDECL *mono_assembly_get_image)(MonoAssembly *assembly);
88 MonoAssembly* (CDECL *mono_assembly_load_from)(MonoImage *image, const char *fname, MonoImageOpenStatus *status);
89 const char* (CDECL *mono_assembly_name_get_name)(MonoAssemblyName *aname);
90 MonoAssembly* (CDECL *mono_assembly_open)(const char *filename, MonoImageOpenStatus *status);
91 void (CDECL *mono_callspec_set_assembly)(MonoAssembly *assembly);
92 MonoClass* (CDECL *mono_class_from_mono_type)(MonoType *type);
93 MonoClass* (CDECL *mono_class_from_name)(MonoImage *image, const char* name_space, const char *name);
94 MonoMethod* (CDECL *mono_class_get_method_from_name)(MonoClass *klass, const char *name, int param_count);
95 static void (CDECL *mono_config_parse)(const char *filename);
96 MonoDomain* (CDECL *mono_domain_get)(void);
97 MonoDomain* (CDECL *mono_domain_get_by_id)(int id);
98 BOOL (CDECL *mono_domain_set)(MonoDomain *domain,BOOL force);
99 void (CDECL *mono_domain_set_config)(MonoDomain *domain,const char *base_dir,const char *config_file_name);
100 static void (CDECL *mono_free)(void *);
101 MonoImage* (CDECL *mono_get_corlib)(void);
102 static MonoImage* (CDECL *mono_image_open)(const char *fname, MonoImageOpenStatus *status);
103 MonoImage* (CDECL *mono_image_open_from_module_handle)(HMODULE module_handle, char* fname, UINT has_entry_point, MonoImageOpenStatus* status);
104 static void (CDECL *mono_install_assembly_preload_hook)(MonoAssemblyPreLoadFunc func, void *user_data);
105 int (CDECL *mono_jit_exec)(MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[]);
106 MonoDomain* (CDECL *mono_jit_init_version)(const char *domain_name, const char *runtime_version);
107 static int (CDECL *mono_jit_set_trace_options)(const char* options);
108 void* (CDECL *mono_marshal_get_vtfixup_ftnptr)(MonoImage *image, DWORD token, WORD type);
109 MonoDomain* (CDECL *mono_object_get_domain)(MonoObject *obj);
110 MonoMethod* (CDECL *mono_object_get_virtual_method)(MonoObject *obj, MonoMethod *method);
111 MonoObject* (CDECL *mono_object_new)(MonoDomain *domain, MonoClass *klass);
112 void* (CDECL *mono_object_unbox)(MonoObject *obj);
113 static MonoProfilerHandle (CDECL *mono_profiler_create)(MonoProfiler *prof);
114 static void (CDECL *mono_profiler_install)(MonoProfiler *prof, MonoProfileFunc shutdown_callback);
115 static void (CDECL *mono_profiler_set_runtime_shutdown_begin_callback)(MonoProfilerHandle handle, MonoProfilerRuntimeShutdownBeginCallback cb);
116 MonoType* (CDECL *mono_reflection_type_from_name)(char *name, MonoImage *image);
117 MonoObject* (CDECL *mono_runtime_invoke)(MonoMethod *method, void *obj, void **params, MonoObject **exc);
118 void (CDECL *mono_runtime_object_init)(MonoObject *this_obj);
119 void (CDECL *mono_runtime_quit)(void);
120 static void (CDECL *mono_set_crash_chaining)(BOOL chain_signals);
121 static void (CDECL *mono_set_dirs)(const char *assembly_dir, const char *config_dir);
122 static void (CDECL *mono_set_verbose_level)(DWORD level);
123 MonoString* (CDECL *mono_string_new)(MonoDomain *domain, const char *str);
124 static char* (CDECL *mono_stringify_assembly_name)(MonoAssemblyName *aname);
125 MonoThread* (CDECL *mono_thread_attach)(MonoDomain *domain);
126 void (CDECL *mono_thread_manage)(void);
127 void (CDECL *mono_trace_set_print_handler)(MonoPrintCallback callback);
128 void (CDECL *mono_trace_set_printerr_handler)(MonoPrintCallback callback);
130 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path);
132 static MonoAssembly* CDECL mono_assembly_preload_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data);
134 static void CDECL mono_shutdown_callback_fn(MonoProfiler *prof);
136 static void CDECL mono_print_handler_fn(const char *string, INT is_stdout);
138 static MonoImage* CDECL image_open_module_handle_dummy(HMODULE module_handle,
139 char* fname, UINT has_entry_point, MonoImageOpenStatus* status)
141 return mono_image_open(fname, status);
144 static void CDECL set_crash_chaining_dummy(BOOL crash_chaining)
148 static void CDECL set_print_handler_dummy(MonoPrintCallback callback)
152 static HRESULT load_mono(LPCWSTR mono_path)
154 static const WCHAR lib[] = {'\\','l','i','b',0};
155 static const WCHAR etc[] = {'\\','e','t','c',0};
156 WCHAR mono_dll_path[MAX_PATH+16];
157 WCHAR mono_lib_path[MAX_PATH+4], mono_etc_path[MAX_PATH+4];
158 char mono_lib_path_a[MAX_PATH], mono_etc_path_a[MAX_PATH];
159 int trace_size;
160 char trace_setting[256];
161 int verbose_size;
162 char verbose_setting[256];
164 if (is_mono_shutdown)
166 ERR("Cannot load Mono after it has been shut down.\n");
167 return E_FAIL;
170 if (!mono_handle)
172 lstrcpyW(mono_lib_path, mono_path);
173 lstrcatW(mono_lib_path, lib);
174 WideCharToMultiByte(CP_UTF8, 0, mono_lib_path, -1, mono_lib_path_a, MAX_PATH, NULL, NULL);
176 lstrcpyW(mono_etc_path, mono_path);
177 lstrcatW(mono_etc_path, etc);
178 WideCharToMultiByte(CP_UTF8, 0, mono_etc_path, -1, mono_etc_path_a, MAX_PATH, NULL, NULL);
180 if (!find_mono_dll(mono_path, mono_dll_path)) goto fail;
182 mono_handle = LoadLibraryW(mono_dll_path);
184 if (!mono_handle) goto fail;
186 #define LOAD_MONO_FUNCTION(x) do { \
187 x = (void*)GetProcAddress(mono_handle, #x); \
188 if (!x) { \
189 goto fail; \
191 } while (0);
193 LOAD_MONO_FUNCTION(mono_assembly_get_image);
194 LOAD_MONO_FUNCTION(mono_assembly_load_from);
195 LOAD_MONO_FUNCTION(mono_assembly_name_get_name);
196 LOAD_MONO_FUNCTION(mono_assembly_open);
197 LOAD_MONO_FUNCTION(mono_config_parse);
198 LOAD_MONO_FUNCTION(mono_class_from_mono_type);
199 LOAD_MONO_FUNCTION(mono_class_from_name);
200 LOAD_MONO_FUNCTION(mono_class_get_method_from_name);
201 LOAD_MONO_FUNCTION(mono_domain_get);
202 LOAD_MONO_FUNCTION(mono_domain_get_by_id);
203 LOAD_MONO_FUNCTION(mono_domain_set);
204 LOAD_MONO_FUNCTION(mono_domain_set_config);
205 LOAD_MONO_FUNCTION(mono_free);
206 LOAD_MONO_FUNCTION(mono_get_corlib);
207 LOAD_MONO_FUNCTION(mono_image_open);
208 LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook);
209 LOAD_MONO_FUNCTION(mono_jit_exec);
210 LOAD_MONO_FUNCTION(mono_jit_init_version);
211 LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
212 LOAD_MONO_FUNCTION(mono_marshal_get_vtfixup_ftnptr);
213 LOAD_MONO_FUNCTION(mono_object_get_domain);
214 LOAD_MONO_FUNCTION(mono_object_get_virtual_method);
215 LOAD_MONO_FUNCTION(mono_object_new);
216 LOAD_MONO_FUNCTION(mono_object_unbox);
217 LOAD_MONO_FUNCTION(mono_reflection_type_from_name);
218 LOAD_MONO_FUNCTION(mono_runtime_invoke);
219 LOAD_MONO_FUNCTION(mono_runtime_object_init);
220 LOAD_MONO_FUNCTION(mono_runtime_quit);
221 LOAD_MONO_FUNCTION(mono_set_dirs);
222 LOAD_MONO_FUNCTION(mono_set_verbose_level);
223 LOAD_MONO_FUNCTION(mono_stringify_assembly_name);
224 LOAD_MONO_FUNCTION(mono_string_new);
225 LOAD_MONO_FUNCTION(mono_thread_attach);
226 LOAD_MONO_FUNCTION(mono_thread_manage);
228 #undef LOAD_MONO_FUNCTION
230 #define LOAD_OPT_MONO_FUNCTION(x, default) do { \
231 x = (void*)GetProcAddress(mono_handle, #x); \
232 if (!x) { \
233 x = default; \
235 } while (0);
237 LOAD_OPT_MONO_FUNCTION(mono_callspec_set_assembly, NULL);
238 LOAD_OPT_MONO_FUNCTION(mono_image_open_from_module_handle, image_open_module_handle_dummy);
239 LOAD_OPT_MONO_FUNCTION(mono_profiler_create, NULL);
240 LOAD_OPT_MONO_FUNCTION(mono_profiler_install, NULL);
241 LOAD_OPT_MONO_FUNCTION(mono_profiler_set_runtime_shutdown_begin_callback, NULL);
242 LOAD_OPT_MONO_FUNCTION(mono_set_crash_chaining, set_crash_chaining_dummy);
243 LOAD_OPT_MONO_FUNCTION(mono_trace_set_print_handler, set_print_handler_dummy);
244 LOAD_OPT_MONO_FUNCTION(mono_trace_set_printerr_handler, set_print_handler_dummy);
246 #undef LOAD_OPT_MONO_FUNCTION
248 if (mono_callspec_set_assembly == NULL)
250 mono_callspec_set_assembly = (void*)GetProcAddress(mono_handle, "mono_trace_set_assembly");
251 if (!mono_callspec_set_assembly) goto fail;
254 if (mono_profiler_create != NULL)
256 /* Profiler API v2 */
257 MonoProfilerHandle handle = mono_profiler_create(NULL);
258 mono_profiler_set_runtime_shutdown_begin_callback(handle, mono_shutdown_callback_fn);
260 else if (mono_profiler_install != NULL)
262 /* Profiler API v1 */
263 mono_profiler_install(NULL, mono_shutdown_callback_fn);
266 mono_set_crash_chaining(TRUE);
268 mono_trace_set_print_handler(mono_print_handler_fn);
269 mono_trace_set_printerr_handler(mono_print_handler_fn);
271 mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
273 mono_config_parse(NULL);
275 mono_install_assembly_preload_hook(mono_assembly_preload_hook_fn, NULL);
277 trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
279 if (trace_size)
281 mono_jit_set_trace_options(trace_setting);
284 verbose_size = GetEnvironmentVariableA("WINE_MONO_VERBOSE", verbose_setting, sizeof(verbose_setting));
286 if (verbose_size)
288 mono_set_verbose_level(verbose_setting[0] - '0');
292 return S_OK;
294 fail:
295 ERR("Could not load Mono into this process\n");
296 FreeLibrary(mono_handle);
297 mono_handle = NULL;
298 return E_FAIL;
301 MonoDomain* get_root_domain(void)
303 static MonoDomain* root_domain;
305 if (root_domain != NULL)
306 return root_domain;
308 EnterCriticalSection(&runtime_list_cs);
310 if (root_domain == NULL)
312 /* FIXME: Use exe filename to name the domain? */
313 root_domain = mono_jit_init_version("mscorlib.dll", "v4.0.30319");
315 is_mono_started = TRUE;
318 LeaveCriticalSection(&runtime_list_cs);
320 return root_domain;
323 static void CDECL mono_shutdown_callback_fn(MonoProfiler *prof)
325 is_mono_shutdown = TRUE;
328 static void CDECL mono_print_handler_fn(const char *string, INT is_stdout)
330 const char *p;
331 for (; *string; string = p)
333 if ((p = strstr(string, "\n"))) p++;
334 else p = string + strlen(string);
335 wine_dbg_printf("%.*s", (int)(p - string), string);
339 static HRESULT WINAPI thread_set_fn(void)
341 WARN("stub\n");
342 return S_OK;
345 static HRESULT WINAPI thread_unset_fn(void)
347 WARN("stub\n");
348 return S_OK;
351 static HRESULT CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo *This, RuntimeHost **result)
353 HRESULT hr = S_OK;
354 WCHAR mono_path[MAX_PATH];
356 if (This->loaded_runtime)
358 *result = This->loaded_runtime;
359 return hr;
362 if (!get_mono_path(mono_path, FALSE))
364 ERR("Wine Mono is not installed\n");
365 return CLR_E_SHIM_RUNTIME;
368 EnterCriticalSection(&runtime_list_cs);
370 if (This->loaded_runtime)
372 *result = This->loaded_runtime;
373 LeaveCriticalSection(&runtime_list_cs);
374 return hr;
377 if (GlobalCLRMetaHost.callback)
379 GlobalCLRMetaHost.callback(&This->ICLRRuntimeInfo_iface, thread_set_fn, thread_unset_fn);
382 hr = load_mono(mono_path);
384 if (SUCCEEDED(hr))
385 hr = RuntimeHost_Construct(This, &This->loaded_runtime);
387 LeaveCriticalSection(&runtime_list_cs);
389 if (SUCCEEDED(hr))
390 *result = This->loaded_runtime;
392 return hr;
395 void expect_no_runtimes(void)
397 if (mono_handle && is_mono_started && !is_mono_shutdown)
399 ERR("Process exited with a Mono runtime loaded.\n");
400 return;
404 static inline CLRRuntimeInfo *impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo *iface)
406 return CONTAINING_RECORD(iface, CLRRuntimeInfo, ICLRRuntimeInfo_iface);
409 static HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
410 REFIID riid,
411 void **ppvObject)
413 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
415 if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
416 IsEqualGUID( riid, &IID_IUnknown ) )
418 *ppvObject = iface;
420 else
422 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
423 return E_NOINTERFACE;
426 ICLRRuntimeInfo_AddRef( iface );
428 return S_OK;
431 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
433 return 2;
436 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
438 return 1;
441 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
442 LPWSTR pwzBuffer, DWORD *pcchBuffer)
444 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
445 DWORD buffer_size = *pcchBuffer;
446 HRESULT hr = S_OK;
447 char version[11];
448 DWORD size;
450 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
452 size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
454 assert(size <= sizeof(version));
456 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
458 if (pwzBuffer)
460 if (buffer_size >= *pcchBuffer)
461 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
462 else
463 hr = E_NOT_SUFFICIENT_BUFFER;
466 return hr;
469 static BOOL get_install_root(LPWSTR install_dir)
471 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};
472 static const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
474 DWORD len;
475 HKEY key;
477 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
478 return FALSE;
480 len = MAX_PATH * sizeof(WCHAR);
481 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
483 RegCloseKey(key);
484 return FALSE;
486 RegCloseKey(key);
488 return TRUE;
491 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
492 LPWSTR pwzBuffer, DWORD *pcchBuffer)
494 static const WCHAR slash[] = {'\\',0};
495 DWORD buffer_size = *pcchBuffer;
496 WCHAR system_dir[MAX_PATH];
497 WCHAR version[MAX_PATH];
498 DWORD version_size, size;
499 HRESULT hr = S_OK;
501 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
503 if (!get_install_root(system_dir))
505 ERR("error reading registry key for installroot\n");
506 return E_FAIL;
508 else
510 version_size = MAX_PATH;
511 ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
512 lstrcatW(system_dir, version);
513 lstrcatW(system_dir, slash);
514 size = lstrlenW(system_dir) + 1;
517 *pcchBuffer = size;
519 if (pwzBuffer)
521 if (buffer_size >= size)
522 lstrcpyW(pwzBuffer, system_dir);
523 else
524 hr = E_NOT_SUFFICIENT_BUFFER;
527 return hr;
530 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
531 HANDLE hndProcess, BOOL *pbLoaded)
533 FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
535 return E_NOTIMPL;
538 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
539 UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
541 FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
543 return E_NOTIMPL;
546 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
547 LPCWSTR pwzDllName, HMODULE *phndModule)
549 WCHAR version[MAX_PATH];
550 HRESULT hr;
551 DWORD cchBuffer;
553 TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
555 cchBuffer = MAX_PATH;
556 hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
557 if (FAILED(hr)) return hr;
559 return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
562 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
563 LPCSTR pszProcName, LPVOID *ppProc)
565 FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
567 return E_NOTIMPL;
570 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
571 REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
573 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
574 RuntimeHost *host;
575 HRESULT hr;
577 TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
579 hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
581 if (SUCCEEDED(hr))
582 hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
584 return hr;
587 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
588 BOOL *pbLoadable)
590 FIXME("%p %p\n", iface, pbLoadable);
592 return E_NOTIMPL;
595 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
596 DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
598 FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
600 return E_NOTIMPL;
603 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
604 DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
606 FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
608 return E_NOTIMPL;
611 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
613 FIXME("%p\n", iface);
615 return E_NOTIMPL;
618 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
619 BOOL *pbStarted, DWORD *pdwStartupFlags)
621 FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
623 return E_NOTIMPL;
626 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
627 CLRRuntimeInfo_QueryInterface,
628 CLRRuntimeInfo_AddRef,
629 CLRRuntimeInfo_Release,
630 CLRRuntimeInfo_GetVersionString,
631 CLRRuntimeInfo_GetRuntimeDirectory,
632 CLRRuntimeInfo_IsLoaded,
633 CLRRuntimeInfo_LoadErrorString,
634 CLRRuntimeInfo_LoadLibrary,
635 CLRRuntimeInfo_GetProcAddress,
636 CLRRuntimeInfo_GetInterface,
637 CLRRuntimeInfo_IsLoadable,
638 CLRRuntimeInfo_SetDefaultStartupFlags,
639 CLRRuntimeInfo_GetDefaultStartupFlags,
640 CLRRuntimeInfo_BindAsLegacyV2Runtime,
641 CLRRuntimeInfo_IsStarted
644 HRESULT ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo *iface, RuntimeHost **result)
646 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
648 assert(This->ICLRRuntimeInfo_iface.lpVtbl == &CLRRuntimeInfoVtbl);
650 return CLRRuntimeInfo_GetRuntimeHost(This, result);
653 #ifdef __i386__
654 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};
655 #elif defined(__x86_64__)
656 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};
657 #else
658 static const WCHAR libmono2_arch_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
659 #endif
661 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path)
663 static const WCHAR mono2_dll[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
664 static const WCHAR libmono2_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
665 DWORD attributes=INVALID_FILE_ATTRIBUTES;
667 lstrcpyW(dll_path, path);
668 lstrcatW(dll_path, libmono2_arch_dll);
669 attributes = GetFileAttributesW(dll_path);
671 if (attributes == INVALID_FILE_ATTRIBUTES)
673 lstrcpyW(dll_path, path);
674 lstrcatW(dll_path, mono2_dll);
675 attributes = GetFileAttributesW(dll_path);
678 if (attributes == INVALID_FILE_ATTRIBUTES)
680 lstrcpyW(dll_path, path);
681 lstrcatW(dll_path, libmono2_dll);
682 attributes = GetFileAttributesW(dll_path);
685 return (attributes != INVALID_FILE_ATTRIBUTES);
688 static BOOL get_mono_path_local(LPWSTR path)
690 static const WCHAR subdir_mono[] = {'\\','m','o','n','o','\\','m','o','n','o','-','2','.','0', 0};
691 WCHAR base_path[MAX_PATH], mono_dll_path[MAX_PATH];
693 /* c:\windows\mono\mono-2.0 */
694 GetWindowsDirectoryW(base_path, MAX_PATH);
695 lstrcatW(base_path, subdir_mono);
697 if (find_mono_dll(base_path, mono_dll_path))
699 lstrcpyW(path, base_path);
700 return TRUE;
703 return FALSE;
706 static BOOL get_mono_path_registry(LPWSTR path)
708 static const WCHAR keyname[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\','M','o','n','o',0};
709 static const WCHAR valuename[] = {'R','u','n','t','i','m','e','P','a','t','h',0};
710 WCHAR base_path[MAX_PATH], mono_dll_path[MAX_PATH];
711 HKEY hkey;
712 DWORD res, valuesize;
713 BOOL ret=FALSE;
715 /* @@ Wine registry key: HKCU\Software\Wine\Mono */
716 res = RegOpenKeyW(HKEY_CURRENT_USER, keyname, &hkey);
717 if (res != ERROR_SUCCESS)
718 return FALSE;
720 valuesize = sizeof(base_path);
721 res = RegGetValueW(hkey, NULL, valuename, RRF_RT_REG_SZ, NULL, base_path, &valuesize);
722 if (res == ERROR_SUCCESS && find_mono_dll(base_path, mono_dll_path))
724 lstrcpyW(path, base_path);
725 ret = TRUE;
728 RegCloseKey(hkey);
730 return ret;
733 static BOOL get_mono_path_dos(const WCHAR *dir, LPWSTR path)
735 static const WCHAR unix_prefix[] = {'\\','\\','?','\\','u','n','i','x','\\'};
736 static const WCHAR basedir[] = L"\\wine-mono-" WINE_MONO_VERSION;
737 LPWSTR dos_dir;
738 WCHAR mono_dll_path[MAX_PATH];
739 DWORD len;
740 BOOL ret;
742 if (memcmp(dir, unix_prefix, sizeof(unix_prefix)) == 0)
743 return FALSE; /* No drive letter for this directory */
745 len = lstrlenW( dir ) + lstrlenW( basedir ) + 1;
746 if (!(dos_dir = heap_alloc( len * sizeof(WCHAR) ))) return FALSE;
747 lstrcpyW( dos_dir, dir );
748 lstrcatW( dos_dir, basedir );
750 ret = find_mono_dll(dos_dir, mono_dll_path);
751 if (ret)
752 lstrcpyW(path, dos_dir);
754 heap_free(dos_dir);
756 return ret;
759 static BOOL get_mono_path_unix(const char *unix_dir, LPWSTR path)
761 static WCHAR * (CDECL *p_wine_get_dos_file_name)(const char*);
762 LPWSTR dos_dir;
763 BOOL ret;
765 if (!p_wine_get_dos_file_name)
767 p_wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
768 if (!p_wine_get_dos_file_name)
769 return FALSE;
772 dos_dir = p_wine_get_dos_file_name(unix_dir);
773 if (!dos_dir)
774 return FALSE;
776 ret = get_mono_path_dos( dos_dir, path);
778 heap_free(dos_dir);
779 return ret;
782 static BOOL get_mono_path_datadir(LPWSTR path)
784 static const WCHAR winedatadirW[] = {'W','I','N','E','D','A','T','A','D','I','R',0};
785 static const WCHAR winebuilddirW[] = {'W','I','N','E','B','U','I','L','D','D','I','R',0};
786 static const WCHAR unix_prefix[] = {'\\','?','?','\\','u','n','i','x','\\'};
787 static const WCHAR monoW[] = {'\\','m','o','n','o',0};
788 static const WCHAR dotdotmonoW[] = {'\\','.','.','\\','m','o','n','o',0};
789 const WCHAR *data_dir, *suffix;
790 WCHAR *package_dir;
791 BOOL ret;
793 if ((data_dir = _wgetenv( winedatadirW )))
794 suffix = monoW;
795 else if ((data_dir = _wgetenv( winebuilddirW )))
796 suffix = dotdotmonoW;
797 else
798 return FALSE;
800 if (!wcsncmp( data_dir, unix_prefix, wcslen(unix_prefix) )) return FALSE;
801 data_dir += 4; /* skip \??\ prefix */
802 package_dir = heap_alloc( (lstrlenW(data_dir) + lstrlenW(suffix) + 1) * sizeof(WCHAR));
803 lstrcpyW( package_dir, data_dir );
804 lstrcatW( package_dir, suffix );
806 ret = get_mono_path_dos(package_dir, path);
808 heap_free(package_dir);
810 return ret;
813 BOOL get_mono_path(LPWSTR path, BOOL skip_local)
815 return (!skip_local && get_mono_path_local(path)) ||
816 get_mono_path_registry(path) ||
817 get_mono_path_datadir(path) ||
818 get_mono_path_unix(INSTALL_DATADIR "/wine/mono", path) ||
819 (strcmp(INSTALL_DATADIR, "/usr/share") &&
820 get_mono_path_unix("/usr/share/wine/mono", path)) ||
821 get_mono_path_unix("/opt/wine/mono", path);
824 struct InstalledRuntimeEnum
826 IEnumUnknown IEnumUnknown_iface;
827 LONG ref;
828 ULONG pos;
831 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
833 static inline struct InstalledRuntimeEnum *impl_from_IEnumUnknown(IEnumUnknown *iface)
835 return CONTAINING_RECORD(iface, struct InstalledRuntimeEnum, IEnumUnknown_iface);
838 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface, REFIID riid,
839 void **ppvObject)
841 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
843 if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
844 IsEqualGUID( riid, &IID_IUnknown ) )
846 *ppvObject = iface;
848 else
850 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
851 return E_NOINTERFACE;
854 IEnumUnknown_AddRef( iface );
856 return S_OK;
859 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
861 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
862 ULONG ref = InterlockedIncrement(&This->ref);
864 TRACE("(%p) refcount=%u\n", iface, ref);
866 return ref;
869 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
871 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
872 ULONG ref = InterlockedDecrement(&This->ref);
874 TRACE("(%p) refcount=%u\n", iface, ref);
876 if (ref == 0)
878 HeapFree(GetProcessHeap(), 0, This);
881 return ref;
884 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
885 IUnknown **rgelt, ULONG *pceltFetched)
887 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
888 ULONG num_fetched = 0;
889 HRESULT hr=S_OK;
890 IUnknown *item;
892 TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
894 while (num_fetched < celt)
896 if (This->pos >= NUM_RUNTIMES)
898 hr = S_FALSE;
899 break;
901 item = (IUnknown*)&runtimes[This->pos].ICLRRuntimeInfo_iface;
902 IUnknown_AddRef(item);
903 rgelt[num_fetched] = item;
904 num_fetched++;
905 This->pos++;
908 if (pceltFetched)
909 *pceltFetched = num_fetched;
911 return hr;
914 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
916 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
917 ULONG num_fetched = 0;
918 HRESULT hr=S_OK;
920 TRACE("(%p,%u)\n", iface, celt);
922 while (num_fetched < celt)
924 if (This->pos >= NUM_RUNTIMES)
926 hr = S_FALSE;
927 break;
929 num_fetched++;
930 This->pos++;
933 return hr;
936 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
938 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
940 TRACE("(%p)\n", iface);
942 This->pos = 0;
944 return S_OK;
947 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
949 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
950 struct InstalledRuntimeEnum *new_enum;
952 TRACE("(%p)\n", iface);
954 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
955 if (!new_enum)
956 return E_OUTOFMEMORY;
958 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
959 new_enum->ref = 1;
960 new_enum->pos = This->pos;
962 *ppenum = &new_enum->IEnumUnknown_iface;
964 return S_OK;
967 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
968 InstalledRuntimeEnum_QueryInterface,
969 InstalledRuntimeEnum_AddRef,
970 InstalledRuntimeEnum_Release,
971 InstalledRuntimeEnum_Next,
972 InstalledRuntimeEnum_Skip,
973 InstalledRuntimeEnum_Reset,
974 InstalledRuntimeEnum_Clone
977 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
978 REFIID riid,
979 void **ppvObject)
981 TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
983 if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
984 IsEqualGUID( riid, &IID_IUnknown ) )
986 *ppvObject = iface;
988 else
990 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
991 return E_NOINTERFACE;
994 ICLRMetaHost_AddRef( iface );
996 return S_OK;
999 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
1001 return 2;
1004 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
1006 return 1;
1009 static inline BOOL isDigit(WCHAR c)
1011 return c >= '0' && c <= '9';
1014 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
1016 *major = 0;
1017 *minor = 0;
1018 *build = 0;
1020 if (version[0] == 'v' || version[0] == 'V')
1022 version++;
1023 if (!isDigit(*version))
1024 return FALSE;
1026 while (isDigit(*version))
1027 *major = *major * 10 + (*version++ - '0');
1029 if (*version == 0)
1030 return TRUE;
1032 if (*version++ != '.' || !isDigit(*version))
1033 return FALSE;
1035 while (isDigit(*version))
1036 *minor = *minor * 10 + (*version++ - '0');
1038 if (*version == 0)
1039 return TRUE;
1041 if (*version++ != '.' || !isDigit(*version))
1042 return FALSE;
1044 while (isDigit(*version))
1045 *build = *build * 10 + (*version++ - '0');
1047 return *version == 0;
1049 else
1050 return FALSE;
1053 static HRESULT get_runtime(LPCWSTR pwzVersion, BOOL allow_short,
1054 REFIID iid, LPVOID *ppRuntime)
1056 int i;
1057 DWORD major, minor, build;
1059 if (!pwzVersion)
1060 return E_POINTER;
1062 if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
1064 ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
1065 return CLR_E_SHIM_RUNTIME;
1068 for (i=0; i<NUM_RUNTIMES; i++)
1070 if (runtimes[i].major == major && runtimes[i].minor == minor &&
1071 (runtimes[i].build == build || (allow_short && major >= 4 && build == 0)))
1073 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface, iid,
1074 ppRuntime);
1078 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
1079 return CLR_E_SHIM_RUNTIME;
1082 HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
1083 LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
1085 TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
1087 return get_runtime(pwzVersion, FALSE, iid, ppRuntime);
1090 HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
1091 LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
1093 ASSEMBLY *assembly;
1094 HRESULT hr;
1095 LPSTR version;
1096 ULONG buffer_size=*pcchBuffer;
1098 TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
1100 hr = assembly_create(&assembly, pwzFilePath);
1102 if (SUCCEEDED(hr))
1104 hr = assembly_get_runtime_version(assembly, &version);
1106 if (SUCCEEDED(hr))
1108 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
1110 if (pwzBuffer)
1112 if (buffer_size >= *pcchBuffer)
1113 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
1114 else
1115 hr = E_NOT_SUFFICIENT_BUFFER;
1119 assembly_release(assembly);
1122 return hr;
1125 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
1126 IEnumUnknown **ppEnumerator)
1128 struct InstalledRuntimeEnum *new_enum;
1130 TRACE("%p\n", ppEnumerator);
1132 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
1133 if (!new_enum)
1134 return E_OUTOFMEMORY;
1136 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
1137 new_enum->ref = 1;
1138 new_enum->pos = 0;
1140 *ppEnumerator = &new_enum->IEnumUnknown_iface;
1142 return S_OK;
1145 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
1146 HANDLE hndProcess, IEnumUnknown **ppEnumerator)
1148 FIXME("%p %p\n", hndProcess, ppEnumerator);
1150 return E_NOTIMPL;
1153 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
1154 RuntimeLoadedCallbackFnPtr pCallbackFunction)
1156 TRACE("%p\n", pCallbackFunction);
1158 if(!pCallbackFunction)
1159 return E_POINTER;
1161 if (GlobalCLRMetaHost.callback)
1162 return HOST_E_INVALIDOPERATION;
1164 GlobalCLRMetaHost.callback = pCallbackFunction;
1166 return S_OK;
1169 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
1170 REFIID riid, LPVOID *ppUnk)
1172 FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
1174 return E_NOTIMPL;
1177 HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
1179 TRACE("%i\n", iExitCode);
1181 EnterCriticalSection(&runtime_list_cs);
1183 if (is_mono_started && !is_mono_shutdown)
1185 /* search for a runtime and call System.Environment.Exit() */
1186 int i;
1188 for (i=0; i<NUM_RUNTIMES; i++)
1189 if (runtimes[i].loaded_runtime)
1190 RuntimeHost_ExitProcess(runtimes[i].loaded_runtime, iExitCode);
1193 ExitProcess(iExitCode);
1196 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
1198 CLRMetaHost_QueryInterface,
1199 CLRMetaHost_AddRef,
1200 CLRMetaHost_Release,
1201 CLRMetaHost_GetRuntime,
1202 CLRMetaHost_GetVersionFromFile,
1203 CLRMetaHost_EnumerateInstalledRuntimes,
1204 CLRMetaHost_EnumerateLoadedRuntimes,
1205 CLRMetaHost_RequestRuntimeLoadedNotification,
1206 CLRMetaHost_QueryLegacyV2RuntimeBinding,
1207 CLRMetaHost_ExitProcess
1210 static struct CLRMetaHost GlobalCLRMetaHost = {
1211 { &CLRMetaHost_vtbl }
1214 HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1216 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost.ICLRMetaHost_iface, riid, ppobj);
1219 struct CLRMetaHostPolicy
1221 ICLRMetaHostPolicy ICLRMetaHostPolicy_iface;
1224 static struct CLRMetaHostPolicy GlobalCLRMetaHostPolicy;
1226 static HRESULT WINAPI metahostpolicy_QueryInterface(ICLRMetaHostPolicy *iface, REFIID riid, void **obj)
1228 TRACE("%s %p\n", debugstr_guid(riid), obj);
1230 if ( IsEqualGUID( riid, &IID_ICLRMetaHostPolicy ) ||
1231 IsEqualGUID( riid, &IID_IUnknown ) )
1233 ICLRMetaHostPolicy_AddRef( iface );
1234 *obj = iface;
1235 return S_OK;
1238 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
1240 *obj = NULL;
1241 return E_NOINTERFACE;
1244 static ULONG WINAPI metahostpolicy_AddRef(ICLRMetaHostPolicy *iface)
1246 return 2;
1249 static ULONG WINAPI metahostpolicy_Release(ICLRMetaHostPolicy *iface)
1251 return 1;
1254 static HRESULT WINAPI metahostpolicy_GetRequestedRuntime(ICLRMetaHostPolicy *iface, METAHOST_POLICY_FLAGS dwPolicyFlags,
1255 LPCWSTR pwzBinary, IStream *pCfgStream, LPWSTR pwzVersion, DWORD *pcchVersion,
1256 LPWSTR pwzImageVersion, DWORD *pcchImageVersion, DWORD *pdwConfigFlags, REFIID riid,
1257 LPVOID *ppRuntime)
1259 ICLRRuntimeInfo *result;
1260 HRESULT hr;
1261 WCHAR filename[MAX_PATH];
1262 const WCHAR *path = NULL;
1263 int flags = 0;
1265 TRACE("%d %p %p %p %p %p %p %p %s %p\n", dwPolicyFlags, pwzBinary, pCfgStream,
1266 pwzVersion, pcchVersion, pwzImageVersion, pcchImageVersion, pdwConfigFlags,
1267 debugstr_guid(riid), ppRuntime);
1269 if (pdwConfigFlags) {
1270 FIXME("ignoring config flags\n");
1271 *pdwConfigFlags = 0;
1274 if(dwPolicyFlags & METAHOST_POLICY_USE_PROCESS_IMAGE_PATH)
1276 GetModuleFileNameW(0, filename, MAX_PATH);
1277 path = filename;
1279 else if(pwzBinary)
1281 path = pwzBinary;
1284 if(dwPolicyFlags & METAHOST_POLICY_APPLY_UPGRADE_POLICY)
1285 flags |= RUNTIME_INFO_UPGRADE_VERSION;
1287 hr = get_runtime_info(path, pwzImageVersion, NULL, pCfgStream, 0, flags, FALSE, &result);
1288 if (SUCCEEDED(hr))
1290 if (pwzImageVersion)
1292 /* Ignoring errors on purpose */
1293 ICLRRuntimeInfo_GetVersionString(result, pwzImageVersion, pcchImageVersion);
1296 hr = ICLRRuntimeInfo_QueryInterface(result, riid, ppRuntime);
1298 ICLRRuntimeInfo_Release(result);
1301 TRACE("<- 0x%08x\n", hr);
1303 return hr;
1306 static const struct ICLRMetaHostPolicyVtbl CLRMetaHostPolicy_vtbl =
1308 metahostpolicy_QueryInterface,
1309 metahostpolicy_AddRef,
1310 metahostpolicy_Release,
1311 metahostpolicy_GetRequestedRuntime
1314 static struct CLRMetaHostPolicy GlobalCLRMetaHostPolicy = {
1315 { &CLRMetaHostPolicy_vtbl }
1318 HRESULT CLRMetaHostPolicy_CreateInstance(REFIID riid, void **ppobj)
1320 return ICLRMetaHostPolicy_QueryInterface(&GlobalCLRMetaHostPolicy.ICLRMetaHostPolicy_iface, riid, ppobj);
1324 * Assembly search override settings:
1326 * WINE_MONO_OVERRIDES=*,Gac=n
1327 * Never search the GAC for libraries.
1329 * WINE_MONO_OVERRIDES=Microsoft.Xna.Framework,Gac=n
1330 * Never search the GAC for Microsoft.Xna.Framework
1332 * WINE_MONO_OVERRIDES=Microsoft.Xna.Framework.*,Gac=n;Microsoft.Xna.Framework.GamerServices,Gac=y
1333 * Never search the GAC for Microsoft.Xna.Framework, or any library starting
1334 * with Microsoft.Xna.Framework, except for Microsoft.Xna.Framework.GamerServices
1337 /* assembly search override flags */
1338 #define ASSEMBLY_SEARCH_GAC 1
1339 #define ASSEMBLY_SEARCH_UNDEFINED 2
1340 #define ASSEMBLY_SEARCH_DEFAULT ASSEMBLY_SEARCH_GAC
1342 typedef struct override_entry {
1343 char *name;
1344 DWORD flags;
1345 struct list entry;
1346 } override_entry;
1348 static struct list env_overrides = LIST_INIT(env_overrides);
1350 #define IS_OPTION_TRUE(ch) \
1351 ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
1352 #define IS_OPTION_FALSE(ch) \
1353 ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
1355 static void parse_override_entry(override_entry *entry, const char *string, int string_len)
1357 const char *next_key, *equals, *value;
1358 UINT kvp_len, key_len;
1360 entry->flags = ASSEMBLY_SEARCH_DEFAULT;
1362 while (string && string_len > 0)
1364 next_key = memchr(string, ',', string_len);
1366 if (next_key)
1368 kvp_len = next_key - string;
1369 next_key++;
1371 else
1372 kvp_len = string_len;
1374 equals = memchr(string, '=', kvp_len);
1376 if (equals)
1378 key_len = equals - string;
1379 value = equals + 1;
1380 switch (key_len) {
1381 case 3:
1382 if (!_strnicmp(string, "gac", 3)) {
1383 if (IS_OPTION_TRUE(*value))
1384 entry->flags |= ASSEMBLY_SEARCH_GAC;
1385 else if (IS_OPTION_FALSE(*value))
1386 entry->flags &= ~ASSEMBLY_SEARCH_GAC;
1388 break;
1389 default:
1390 break;
1394 string = next_key;
1395 string_len -= kvp_len + 1;
1399 static BOOL WINAPI parse_env_overrides(INIT_ONCE *once, void *param, void **context)
1401 const char *override_string = getenv("WINE_MONO_OVERRIDES");
1402 struct override_entry *entry;
1404 if (override_string)
1406 const char *entry_start;
1408 entry_start = override_string;
1410 while (entry_start && *entry_start)
1412 const char *next_entry, *basename_end;
1413 UINT entry_len;
1415 next_entry = strchr(entry_start, ';');
1417 if (next_entry)
1419 entry_len = next_entry - entry_start;
1420 next_entry++;
1422 else
1423 entry_len = strlen(entry_start);
1425 basename_end = memchr(entry_start, ',', entry_len);
1427 if (!basename_end)
1429 entry_start = next_entry;
1430 continue;
1433 entry = heap_alloc_zero(sizeof(*entry));
1434 if (!entry)
1436 ERR("out of memory\n");
1437 break;
1440 entry->name = heap_alloc_zero(basename_end - entry_start + 1);
1441 if (!entry->name)
1443 ERR("out of memory\n");
1444 heap_free(entry);
1445 break;
1448 memcpy(entry->name, entry_start, basename_end - entry_start);
1450 entry_len -= basename_end - entry_start + 1;
1451 entry_start = basename_end + 1;
1453 parse_override_entry(entry, entry_start, entry_len);
1455 list_add_tail(&env_overrides, &entry->entry);
1457 entry_start = next_entry;
1461 return TRUE;
1464 static DWORD get_basename_search_flags(const char *basename, MonoAssemblyName *aname, HKEY userkey, HKEY appkey)
1466 struct override_entry *entry;
1467 char buffer[256];
1468 DWORD buffer_size;
1469 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
1471 InitOnceExecuteOnce(&init_once, parse_env_overrides, NULL, NULL);
1473 LIST_FOR_EACH_ENTRY(entry, &env_overrides, override_entry, entry)
1475 if (strcmp(basename, entry->name) == 0)
1477 return entry->flags;
1481 buffer_size = sizeof(buffer);
1482 if (appkey && !RegQueryValueExA(appkey, basename, 0, NULL, (LPBYTE)buffer, &buffer_size))
1484 override_entry reg_entry;
1486 memset(&reg_entry, 0, sizeof(reg_entry));
1488 parse_override_entry(&reg_entry, buffer, strlen(buffer));
1490 return reg_entry.flags;
1493 buffer_size = sizeof(buffer);
1494 if (userkey && !RegQueryValueExA(userkey, basename, 0, NULL, (LPBYTE)buffer, &buffer_size))
1496 override_entry reg_entry;
1498 memset(&reg_entry, 0, sizeof(reg_entry));
1500 parse_override_entry(&reg_entry, buffer, strlen(buffer));
1502 return reg_entry.flags;
1505 return ASSEMBLY_SEARCH_UNDEFINED;
1508 static HKEY get_app_overrides_key(void)
1510 static const WCHAR subkeyW[] = {'\\','M','o','n','o','\\','A','s','m','O','v','e','r','r','i','d','e','s',0};
1511 WCHAR bufferW[MAX_PATH+18];
1512 HKEY appkey = 0;
1513 DWORD len;
1515 len = (GetModuleFileNameW( 0, bufferW, MAX_PATH ));
1516 if (len && len < MAX_PATH)
1518 HKEY tmpkey;
1519 WCHAR *p, *appname = bufferW;
1520 if ((p = wcsrchr( appname, '/' ))) appname = p + 1;
1521 if ((p = wcsrchr( appname, '\\' ))) appname = p + 1;
1522 lstrcatW( appname, subkeyW );
1523 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Mono\AsmOverrides */
1524 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
1526 if (RegOpenKeyW( tmpkey, appname, &appkey )) appkey = 0;
1527 RegCloseKey( tmpkey );
1531 return appkey;
1534 static DWORD get_assembly_search_flags(MonoAssemblyName *aname)
1536 const char *name = mono_assembly_name_get_name(aname);
1537 char *name_copy, *name_end;
1538 DWORD result;
1539 HKEY appkey = 0, userkey;
1541 /* @@ Wine registry key: HKCU\Software\Wine\Mono\AsmOverrides */
1542 if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Mono\\AsmOverrides", &userkey )) userkey = 0;
1544 appkey = get_app_overrides_key();
1546 result = get_basename_search_flags(name, aname, userkey, appkey);
1547 if (result != ASSEMBLY_SEARCH_UNDEFINED)
1549 if (userkey) RegCloseKey(userkey);
1550 if (appkey) RegCloseKey(appkey);
1551 return result;
1554 name_copy = heap_alloc((strlen(name) + 3) * sizeof(WCHAR));
1555 if (!name_copy)
1557 ERR("out of memory\n");
1558 if (userkey) RegCloseKey(userkey);
1559 if (appkey) RegCloseKey(appkey);
1560 return ASSEMBLY_SEARCH_DEFAULT;
1563 strcpy(name_copy, name);
1564 name_end = name_copy + strlen(name);
1568 strcpy(name_end, ".*");
1569 result = get_basename_search_flags(name_copy, aname, userkey, appkey);
1570 if (result != ASSEMBLY_SEARCH_UNDEFINED) break;
1572 *name_end = 0;
1573 name_end = strrchr(name_copy, '.');
1574 } while (name_end != NULL);
1576 /* default flags */
1577 if (result == ASSEMBLY_SEARCH_UNDEFINED)
1579 result = get_basename_search_flags("*", aname, userkey, appkey);
1580 if (result == ASSEMBLY_SEARCH_UNDEFINED)
1581 result = ASSEMBLY_SEARCH_DEFAULT;
1584 heap_free(name_copy);
1585 if (appkey) RegCloseKey(appkey);
1586 if (userkey) RegCloseKey(userkey);
1588 return result;
1591 HRESULT get_file_from_strongname(WCHAR* stringnameW, WCHAR* assemblies_path, int path_length)
1593 HRESULT hr=S_OK;
1594 IAssemblyCache *asmcache;
1595 ASSEMBLY_INFO info;
1596 static const WCHAR fusiondll[] = {'f','u','s','i','o','n',0};
1597 HMODULE hfusion=NULL;
1598 static HRESULT (WINAPI *pCreateAssemblyCache)(IAssemblyCache**,DWORD);
1600 if (!pCreateAssemblyCache)
1602 hr = LoadLibraryShim(fusiondll, NULL, NULL, &hfusion);
1604 if (SUCCEEDED(hr))
1606 pCreateAssemblyCache = (void*)GetProcAddress(hfusion, "CreateAssemblyCache");
1607 if (!pCreateAssemblyCache)
1608 hr = E_FAIL;
1612 if (SUCCEEDED(hr))
1613 hr = pCreateAssemblyCache(&asmcache, 0);
1615 if (SUCCEEDED(hr))
1617 info.cbAssemblyInfo = sizeof(info);
1618 info.pszCurrentAssemblyPathBuf = assemblies_path;
1619 info.cchBuf = path_length;
1620 assemblies_path[0] = 0;
1622 hr = IAssemblyCache_QueryAssemblyInfo(asmcache, 0, stringnameW, &info);
1624 IAssemblyCache_Release(asmcache);
1627 return hr;
1630 static MonoAssembly* CDECL mono_assembly_preload_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data)
1632 HRESULT hr;
1633 MonoAssembly *result=NULL;
1634 char *stringname=NULL;
1635 const char *assemblyname;
1636 LPWSTR stringnameW;
1637 int stringnameW_size;
1638 WCHAR path[MAX_PATH];
1639 char *pathA;
1640 MonoImageOpenStatus stat;
1641 DWORD search_flags;
1642 int i;
1643 static const WCHAR dotdllW[] = {'.','d','l','l',0};
1644 static const WCHAR slashW[] = {'\\',0};
1646 stringname = mono_stringify_assembly_name(aname);
1647 assemblyname = mono_assembly_name_get_name(aname);
1649 TRACE("%s\n", debugstr_a(stringname));
1651 if (!stringname || !assemblyname) return NULL;
1653 search_flags = get_assembly_search_flags(aname);
1654 if (private_path)
1656 stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, assemblyname, -1, NULL, 0);
1657 stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1658 if (stringnameW)
1660 MultiByteToWideChar(CP_UTF8, 0, assemblyname, -1, stringnameW, stringnameW_size);
1661 for (i = 0; private_path[i] != NULL; i++)
1663 wcscpy(path, private_path[i]);
1664 wcscat(path, slashW);
1665 wcscat(path, stringnameW);
1666 wcscat(path, dotdllW);
1667 pathA = WtoA(path);
1668 if (pathA)
1670 result = mono_assembly_open(pathA, &stat);
1671 if (result)
1673 TRACE("found: %s\n", debugstr_w(path));
1674 HeapFree(GetProcessHeap(), 0, pathA);
1675 HeapFree(GetProcessHeap(), 0, stringnameW);
1676 mono_free(stringname);
1677 return result;
1679 HeapFree(GetProcessHeap(), 0, pathA);
1682 HeapFree(GetProcessHeap(), 0, stringnameW);
1686 /* FIXME: We should search the given paths before the GAC. */
1688 if ((search_flags & ASSEMBLY_SEARCH_GAC) != 0)
1690 stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, stringname, -1, NULL, 0);
1692 stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1693 if (stringnameW)
1695 MultiByteToWideChar(CP_UTF8, 0, stringname, -1, stringnameW, stringnameW_size);
1697 hr = get_file_from_strongname(stringnameW, path, MAX_PATH);
1699 HeapFree(GetProcessHeap(), 0, stringnameW);
1701 else
1702 hr = E_OUTOFMEMORY;
1704 if (SUCCEEDED(hr))
1706 TRACE("found: %s\n", debugstr_w(path));
1708 pathA = WtoA(path);
1710 if (pathA)
1712 result = mono_assembly_open(pathA, &stat);
1714 if (!result)
1715 ERR("Failed to load %s, status=%u\n", debugstr_w(path), stat);
1717 HeapFree(GetProcessHeap(), 0, pathA);
1721 else
1722 TRACE("skipping Windows GAC search due to override setting\n");
1724 mono_free(stringname);
1726 return result;
1729 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1730 IStream *config_stream, DWORD startup_flags, DWORD runtimeinfo_flags,
1731 BOOL legacy, ICLRRuntimeInfo **result)
1733 static const WCHAR dotconfig[] = {'.','c','o','n','f','i','g',0};
1734 static const DWORD supported_startup_flags = 0;
1735 static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1736 int i;
1737 WCHAR local_version[MAX_PATH];
1738 ULONG local_version_size = MAX_PATH;
1739 WCHAR local_config_file[MAX_PATH];
1740 HRESULT hr;
1741 parsed_config_file parsed_config;
1743 if (startup_flags & ~supported_startup_flags)
1744 FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
1746 if (runtimeinfo_flags & ~supported_runtime_flags)
1747 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
1749 if (exefile && !exefile[0])
1750 exefile = NULL;
1752 if (exefile && !config_file && !config_stream)
1754 lstrcpyW(local_config_file, exefile);
1755 lstrcatW(local_config_file, dotconfig);
1757 config_file = local_config_file;
1760 if (config_file || config_stream)
1762 BOOL found = FALSE;
1763 if (config_file)
1764 hr = parse_config_file(config_file, &parsed_config);
1765 else
1766 hr = parse_config_stream(config_stream, &parsed_config);
1768 if (SUCCEEDED(hr))
1770 supported_runtime *entry;
1771 LIST_FOR_EACH_ENTRY(entry, &parsed_config.supported_runtimes, supported_runtime, entry)
1773 hr = get_runtime(entry->version, TRUE, &IID_ICLRRuntimeInfo, (void**)result);
1774 if (SUCCEEDED(hr))
1776 found = TRUE;
1777 break;
1781 else
1783 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file), hr);
1786 free_parsed_config_file(&parsed_config);
1788 if (found)
1789 return S_OK;
1792 if (exefile && !version)
1794 DWORD major, minor, build;
1796 hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1798 version = local_version;
1800 if (FAILED(hr)) return hr;
1802 /* When running an executable, specifically when getting the version number from
1803 * the exe, native accepts a matching major.minor with build <= expected build. */
1804 if (!parse_runtime_version(version, &major, &minor, &build))
1806 ERR("Cannot parse %s\n", debugstr_w(version));
1807 return CLR_E_SHIM_RUNTIME;
1810 if (legacy)
1811 i = 3;
1812 else
1813 i = NUM_RUNTIMES;
1815 while (i--)
1817 if (runtimes[i].major == major && runtimes[i].minor == minor && runtimes[i].build >= build)
1819 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface,
1820 &IID_ICLRRuntimeInfo, (void **)result);
1825 if (version)
1827 hr = CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1828 if(SUCCEEDED(hr))
1829 return hr;
1832 if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1834 DWORD major, minor, build;
1836 if (version && !parse_runtime_version(version, &major, &minor, &build))
1838 ERR("Cannot parse %s\n", debugstr_w(version));
1839 return CLR_E_SHIM_RUNTIME;
1842 if (legacy)
1843 i = 3;
1844 else
1845 i = NUM_RUNTIMES;
1847 while (i--)
1849 /* Must be greater or equal to the version passed in. */
1850 if (!version || ((runtimes[i].major >= major && runtimes[i].minor >= minor && runtimes[i].build >= build) ||
1851 (runtimes[i].major >= major && runtimes[i].minor > minor) ||
1852 (runtimes[i].major > major)))
1854 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface,
1855 &IID_ICLRRuntimeInfo, (void **)result);
1859 return CLR_E_SHIM_RUNTIME;
1862 return CLR_E_SHIM_RUNTIME;