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
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 };
71 ICLRMetaHost ICLRMetaHost_iface
;
73 RuntimeLoadedCallbackFnPtr callback
;
76 static struct CLRMetaHost GlobalCLRMetaHost
;
78 static HMODULE mono_handle
;
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
];
160 char trace_setting
[256];
162 char verbose_setting
[256];
164 if (is_mono_shutdown
)
166 ERR("Cannot load Mono after it has been shut down.\n");
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); \
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); \
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
));
281 mono_jit_set_trace_options(trace_setting
);
284 verbose_size
= GetEnvironmentVariableA("WINE_MONO_VERBOSE", verbose_setting
, sizeof(verbose_setting
));
288 mono_set_verbose_level(verbose_setting
[0] - '0');
295 ERR("Could not load Mono into this process\n");
296 FreeLibrary(mono_handle
);
301 MonoDomain
* get_root_domain(void)
303 static MonoDomain
* root_domain
;
305 if (root_domain
!= NULL
)
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
);
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
)
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)
345 static HRESULT WINAPI
thread_unset_fn(void)
351 static HRESULT
CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo
*This
, RuntimeHost
**result
)
354 WCHAR mono_path
[MAX_PATH
];
356 if (This
->loaded_runtime
)
358 *result
= This
->loaded_runtime
;
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
);
377 if (GlobalCLRMetaHost
.callback
)
379 GlobalCLRMetaHost
.callback(&This
->ICLRRuntimeInfo_iface
, thread_set_fn
, thread_unset_fn
);
382 hr
= load_mono(mono_path
);
385 hr
= RuntimeHost_Construct(This
, &This
->loaded_runtime
);
387 LeaveCriticalSection(&runtime_list_cs
);
390 *result
= This
->loaded_runtime
;
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");
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
,
413 TRACE("%p %s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
415 if ( IsEqualGUID( riid
, &IID_ICLRRuntimeInfo
) ||
416 IsEqualGUID( riid
, &IID_IUnknown
) )
422 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
423 return E_NOINTERFACE
;
426 ICLRRuntimeInfo_AddRef( iface
);
431 static ULONG WINAPI
CLRRuntimeInfo_AddRef(ICLRRuntimeInfo
* iface
)
436 static ULONG WINAPI
CLRRuntimeInfo_Release(ICLRRuntimeInfo
* iface
)
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
;
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);
460 if (buffer_size
>= *pcchBuffer
)
461 MultiByteToWideChar(CP_UTF8
, 0, version
, -1, pwzBuffer
, buffer_size
);
463 hr
= E_NOT_SUFFICIENT_BUFFER
;
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};
477 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, dotnet_key
, 0, KEY_READ
, &key
))
480 len
= MAX_PATH
* sizeof(WCHAR
);
481 if (RegQueryValueExW(key
, install_root
, 0, NULL
, (LPBYTE
)install_dir
, &len
))
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
;
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");
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;
521 if (buffer_size
>= size
)
522 lstrcpyW(pwzBuffer
, system_dir
);
524 hr
= E_NOT_SUFFICIENT_BUFFER
;
530 static HRESULT WINAPI
CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo
* iface
,
531 HANDLE hndProcess
, BOOL
*pbLoaded
)
533 FIXME("%p %p %p\n", iface
, hndProcess
, pbLoaded
);
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
);
546 static HRESULT WINAPI
CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo
* iface
,
547 LPCWSTR pwzDllName
, HMODULE
*phndModule
)
549 WCHAR version
[MAX_PATH
];
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
);
570 static HRESULT WINAPI
CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo
* iface
,
571 REFCLSID rclsid
, REFIID riid
, LPVOID
*ppUnk
)
573 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
577 TRACE("%p %s %s %p\n", iface
, debugstr_guid(rclsid
), debugstr_guid(riid
), ppUnk
);
579 hr
= CLRRuntimeInfo_GetRuntimeHost(This
, &host
);
582 hr
= RuntimeHost_GetInterface(host
, rclsid
, riid
, ppUnk
);
587 static HRESULT WINAPI
CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo
* iface
,
590 FIXME("%p %p\n", iface
, pbLoadable
);
595 static HRESULT WINAPI
CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo
* iface
,
596 DWORD dwStartupFlags
, LPCWSTR pwzHostConfigFile
)
598 FIXME("%p %x %s\n", iface
, dwStartupFlags
, debugstr_w(pwzHostConfigFile
));
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
);
611 static HRESULT WINAPI
CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo
* iface
)
613 FIXME("%p\n", iface
);
618 static HRESULT WINAPI
CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo
* iface
,
619 BOOL
*pbStarted
, DWORD
*pdwStartupFlags
)
621 FIXME("%p %p %p\n", iface
, pbStarted
, pdwStartupFlags
);
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
);
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};
658 static const WCHAR libmono2_arch_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
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
);
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
];
712 DWORD res
, valuesize
;
715 /* @@ Wine registry key: HKCU\Software\Wine\Mono */
716 res
= RegOpenKeyW(HKEY_CURRENT_USER
, keyname
, &hkey
);
717 if (res
!= ERROR_SUCCESS
)
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
);
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
;
738 WCHAR mono_dll_path
[MAX_PATH
];
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
);
752 lstrcpyW(path
, dos_dir
);
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*);
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
)
772 dos_dir
= p_wine_get_dos_file_name(unix_dir
);
776 ret
= get_mono_path_dos( dos_dir
, path
);
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
;
793 if ((data_dir
= _wgetenv( winedatadirW
)))
795 else if ((data_dir
= _wgetenv( winebuilddirW
)))
796 suffix
= dotdotmonoW
;
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
);
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
;
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
,
841 TRACE("%p %s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
843 if ( IsEqualGUID( riid
, &IID_IEnumUnknown
) ||
844 IsEqualGUID( riid
, &IID_IUnknown
) )
850 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
851 return E_NOINTERFACE
;
854 IEnumUnknown_AddRef( iface
);
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
);
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
);
878 HeapFree(GetProcessHeap(), 0, This
);
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;
892 TRACE("(%p,%u,%p,%p)\n", iface
, celt
, rgelt
, pceltFetched
);
894 while (num_fetched
< celt
)
896 if (This
->pos
>= NUM_RUNTIMES
)
901 item
= (IUnknown
*)&runtimes
[This
->pos
].ICLRRuntimeInfo_iface
;
902 IUnknown_AddRef(item
);
903 rgelt
[num_fetched
] = item
;
909 *pceltFetched
= num_fetched
;
914 static HRESULT WINAPI
InstalledRuntimeEnum_Skip(IEnumUnknown
*iface
, ULONG celt
)
916 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
917 ULONG num_fetched
= 0;
920 TRACE("(%p,%u)\n", iface
, celt
);
922 while (num_fetched
< celt
)
924 if (This
->pos
>= NUM_RUNTIMES
)
936 static HRESULT WINAPI
InstalledRuntimeEnum_Reset(IEnumUnknown
*iface
)
938 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
940 TRACE("(%p)\n", iface
);
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
));
956 return E_OUTOFMEMORY
;
958 new_enum
->IEnumUnknown_iface
.lpVtbl
= &InstalledRuntimeEnum_Vtbl
;
960 new_enum
->pos
= This
->pos
;
962 *ppenum
= &new_enum
->IEnumUnknown_iface
;
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
,
981 TRACE("%s %p\n", debugstr_guid(riid
), ppvObject
);
983 if ( IsEqualGUID( riid
, &IID_ICLRMetaHost
) ||
984 IsEqualGUID( riid
, &IID_IUnknown
) )
990 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
991 return E_NOINTERFACE
;
994 ICLRMetaHost_AddRef( iface
);
999 static ULONG WINAPI
CLRMetaHost_AddRef(ICLRMetaHost
* iface
)
1004 static ULONG WINAPI
CLRMetaHost_Release(ICLRMetaHost
* iface
)
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
)
1020 if (version
[0] == 'v' || version
[0] == 'V')
1023 if (!isDigit(*version
))
1026 while (isDigit(*version
))
1027 *major
= *major
* 10 + (*version
++ - '0');
1032 if (*version
++ != '.' || !isDigit(*version
))
1035 while (isDigit(*version
))
1036 *minor
= *minor
* 10 + (*version
++ - '0');
1041 if (*version
++ != '.' || !isDigit(*version
))
1044 while (isDigit(*version
))
1045 *build
= *build
* 10 + (*version
++ - '0');
1047 return *version
== 0;
1053 static HRESULT
get_runtime(LPCWSTR pwzVersion
, BOOL allow_short
,
1054 REFIID iid
, LPVOID
*ppRuntime
)
1057 DWORD major
, minor
, build
;
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
,
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
)
1096 ULONG buffer_size
=*pcchBuffer
;
1098 TRACE("%s %p %p\n", debugstr_w(pwzFilePath
), pwzBuffer
, pcchBuffer
);
1100 hr
= assembly_create(&assembly
, pwzFilePath
);
1104 hr
= assembly_get_runtime_version(assembly
, &version
);
1108 *pcchBuffer
= MultiByteToWideChar(CP_UTF8
, 0, version
, -1, NULL
, 0);
1112 if (buffer_size
>= *pcchBuffer
)
1113 MultiByteToWideChar(CP_UTF8
, 0, version
, -1, pwzBuffer
, buffer_size
);
1115 hr
= E_NOT_SUFFICIENT_BUFFER
;
1119 assembly_release(assembly
);
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
));
1134 return E_OUTOFMEMORY
;
1136 new_enum
->IEnumUnknown_iface
.lpVtbl
= &InstalledRuntimeEnum_Vtbl
;
1140 *ppEnumerator
= &new_enum
->IEnumUnknown_iface
;
1145 static HRESULT WINAPI
CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost
* iface
,
1146 HANDLE hndProcess
, IEnumUnknown
**ppEnumerator
)
1148 FIXME("%p %p\n", hndProcess
, ppEnumerator
);
1153 static HRESULT WINAPI
CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost
* iface
,
1154 RuntimeLoadedCallbackFnPtr pCallbackFunction
)
1156 TRACE("%p\n", pCallbackFunction
);
1158 if(!pCallbackFunction
)
1161 if (GlobalCLRMetaHost
.callback
)
1162 return HOST_E_INVALIDOPERATION
;
1164 GlobalCLRMetaHost
.callback
= pCallbackFunction
;
1169 static HRESULT WINAPI
CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost
* iface
,
1170 REFIID riid
, LPVOID
*ppUnk
)
1172 FIXME("%s %p\n", debugstr_guid(riid
), ppUnk
);
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() */
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
,
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
);
1238 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
1241 return E_NOINTERFACE
;
1244 static ULONG WINAPI
metahostpolicy_AddRef(ICLRMetaHostPolicy
*iface
)
1249 static ULONG WINAPI
metahostpolicy_Release(ICLRMetaHostPolicy
*iface
)
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
,
1259 ICLRRuntimeInfo
*result
;
1261 WCHAR filename
[MAX_PATH
];
1262 const WCHAR
*path
= NULL
;
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
);
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
);
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
);
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
{
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
);
1368 kvp_len
= next_key
- string
;
1372 kvp_len
= string_len
;
1374 equals
= memchr(string
, '=', kvp_len
);
1378 key_len
= equals
- string
;
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
;
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
;
1415 next_entry
= strchr(entry_start
, ';');
1419 entry_len
= next_entry
- entry_start
;
1423 entry_len
= strlen(entry_start
);
1425 basename_end
= memchr(entry_start
, ',', entry_len
);
1429 entry_start
= next_entry
;
1433 entry
= heap_alloc_zero(sizeof(*entry
));
1436 ERR("out of memory\n");
1440 entry
->name
= heap_alloc_zero(basename_end
- entry_start
+ 1);
1443 ERR("out of memory\n");
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
;
1464 static DWORD
get_basename_search_flags(const char *basename
, MonoAssemblyName
*aname
, HKEY userkey
, HKEY appkey
)
1466 struct override_entry
*entry
;
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(®_entry
, 0, sizeof(reg_entry
));
1488 parse_override_entry(®_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(®_entry
, 0, sizeof(reg_entry
));
1500 parse_override_entry(®_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];
1515 len
= (GetModuleFileNameW( 0, bufferW
, MAX_PATH
));
1516 if (len
&& len
< MAX_PATH
)
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
);
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
;
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
);
1554 name_copy
= heap_alloc((strlen(name
) + 3) * sizeof(WCHAR
));
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;
1573 name_end
= strrchr(name_copy
, '.');
1574 } while (name_end
!= NULL
);
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
);
1591 HRESULT
get_file_from_strongname(WCHAR
* stringnameW
, WCHAR
* assemblies_path
, int path_length
)
1594 IAssemblyCache
*asmcache
;
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
);
1606 pCreateAssemblyCache
= (void*)GetProcAddress(hfusion
, "CreateAssemblyCache");
1607 if (!pCreateAssemblyCache
)
1613 hr
= pCreateAssemblyCache(&asmcache
, 0);
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
);
1630 static MonoAssembly
* CDECL
mono_assembly_preload_hook_fn(MonoAssemblyName
*aname
, char **assemblies_path
, void *user_data
)
1633 MonoAssembly
*result
=NULL
;
1634 char *stringname
=NULL
;
1635 const char *assemblyname
;
1637 int stringnameW_size
;
1638 WCHAR path
[MAX_PATH
];
1640 MonoImageOpenStatus stat
;
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
);
1656 stringnameW_size
= MultiByteToWideChar(CP_UTF8
, 0, assemblyname
, -1, NULL
, 0);
1657 stringnameW
= HeapAlloc(GetProcessHeap(), 0, stringnameW_size
* sizeof(WCHAR
));
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
);
1670 result
= mono_assembly_open(pathA
, &stat
);
1673 TRACE("found: %s\n", debugstr_w(path
));
1674 HeapFree(GetProcessHeap(), 0, pathA
);
1675 HeapFree(GetProcessHeap(), 0, stringnameW
);
1676 mono_free(stringname
);
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
));
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
);
1706 TRACE("found: %s\n", debugstr_w(path
));
1712 result
= mono_assembly_open(pathA
, &stat
);
1715 ERR("Failed to load %s, status=%u\n", debugstr_w(path
), stat
);
1717 HeapFree(GetProcessHeap(), 0, pathA
);
1722 TRACE("skipping Windows GAC search due to override setting\n");
1724 mono_free(stringname
);
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
;
1737 WCHAR local_version
[MAX_PATH
];
1738 ULONG local_version_size
= MAX_PATH
;
1739 WCHAR local_config_file
[MAX_PATH
];
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])
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
)
1764 hr
= parse_config_file(config_file
, &parsed_config
);
1766 hr
= parse_config_stream(config_stream
, &parsed_config
);
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
);
1783 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file
), hr
);
1786 free_parsed_config_file(&parsed_config
);
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
;
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
);
1827 hr
= CLRMetaHost_GetRuntime(0, version
, &IID_ICLRRuntimeInfo
, (void**)result
);
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
;
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
;