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