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
27 #include "wine/unicode.h"
28 #include "wine/library.h"
41 #include "wine/list.h"
42 #include "mscoree_private.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL( mscoree
);
48 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl
;
50 #define NUM_RUNTIMES 4
52 static struct CLRRuntimeInfo runtimes
[NUM_RUNTIMES
] = {
53 {{&CLRRuntimeInfoVtbl
}, 1, 0, 3705, 0},
54 {{&CLRRuntimeInfoVtbl
}, 1, 1, 4322, 0},
55 {{&CLRRuntimeInfoVtbl
}, 2, 0, 50727, 0},
56 {{&CLRRuntimeInfoVtbl
}, 4, 0, 30319, 0}
59 static CRITICAL_SECTION runtime_list_cs
;
60 static CRITICAL_SECTION_DEBUG runtime_list_cs_debug
=
62 0, 0, &runtime_list_cs
,
63 { &runtime_list_cs_debug
.ProcessLocksList
,
64 &runtime_list_cs_debug
.ProcessLocksList
},
65 0, 0, { (DWORD_PTR
)(__FILE__
": runtime_list_cs") }
67 static CRITICAL_SECTION runtime_list_cs
= { &runtime_list_cs_debug
, -1, 0, 0, 0, 0 };
69 static HMODULE mono_handle
;
72 static BOOL is_mono_shutdown
;
74 MonoImage
* (CDECL
*mono_assembly_get_image
)(MonoAssembly
*assembly
);
75 MonoAssembly
* (CDECL
*mono_assembly_load_from
)(MonoImage
*image
, const char *fname
, MonoImageOpenStatus
*status
);
76 MonoAssembly
* (CDECL
*mono_assembly_open
)(const char *filename
, MonoImageOpenStatus
*status
);
77 MonoClass
* (CDECL
*mono_class_from_mono_type
)(MonoType
*type
);
78 MonoClass
* (CDECL
*mono_class_from_name
)(MonoImage
*image
, const char* name_space
, const char *name
);
79 MonoMethod
* (CDECL
*mono_class_get_method_from_name
)(MonoClass
*klass
, const char *name
, int param_count
);
80 static void (CDECL
*mono_config_parse
)(const char *filename
);
81 MonoAssembly
* (CDECL
*mono_domain_assembly_open
)(MonoDomain
*domain
, const char *name
);
82 static void (CDECL
*mono_free
)(void *);
83 static MonoImage
* (CDECL
*mono_image_open
)(const char *fname
, MonoImageOpenStatus
*status
);
84 MonoImage
* (CDECL
*mono_image_open_from_module_handle
)(HMODULE module_handle
, char* fname
, UINT has_entry_point
, MonoImageOpenStatus
* status
);
85 static void (CDECL
*mono_install_assembly_preload_hook
)(MonoAssemblyPreLoadFunc func
, void *user_data
);
86 int (CDECL
*mono_jit_exec
)(MonoDomain
*domain
, MonoAssembly
*assembly
, int argc
, char *argv
[]);
87 MonoDomain
* (CDECL
*mono_jit_init_version
)(const char *domain_name
, const char *runtime_version
);
88 static int (CDECL
*mono_jit_set_trace_options
)(const char* options
);
89 void* (CDECL
*mono_marshal_get_vtfixup_ftnptr
)(MonoImage
*image
, DWORD token
, WORD type
);
90 MonoDomain
* (CDECL
*mono_object_get_domain
)(MonoObject
*obj
);
91 MonoObject
* (CDECL
*mono_object_new
)(MonoDomain
*domain
, MonoClass
*klass
);
92 void* (CDECL
*mono_object_unbox
)(MonoObject
*obj
);
93 static void (CDECL
*mono_profiler_install
)(MonoProfiler
*prof
, MonoProfileFunc shutdown_callback
);
94 MonoType
* (CDECL
*mono_reflection_type_from_name
)(char *name
, MonoImage
*image
);
95 MonoObject
* (CDECL
*mono_runtime_invoke
)(MonoMethod
*method
, void *obj
, void **params
, MonoObject
**exc
);
96 void (CDECL
*mono_runtime_object_init
)(MonoObject
*this_obj
);
97 void (CDECL
*mono_runtime_quit
)(void);
98 static void (CDECL
*mono_set_dirs
)(const char *assembly_dir
, const char *config_dir
);
99 static void (CDECL
*mono_set_verbose_level
)(DWORD level
);
100 MonoString
* (CDECL
*mono_string_new
)(MonoDomain
*domain
, const char *str
);
101 static char* (CDECL
*mono_stringify_assembly_name
)(MonoAssemblyName
*aname
);
102 MonoThread
* (CDECL
*mono_thread_attach
)(MonoDomain
*domain
);
103 void (CDECL
*mono_thread_manage
)(void);
104 void (CDECL
*mono_trace_set_assembly
)(MonoAssembly
*assembly
);
105 void (CDECL
*mono_trace_set_print_handler
)(MonoPrintCallback callback
);
106 void (CDECL
*mono_trace_set_printerr_handler
)(MonoPrintCallback callback
);
108 static BOOL
get_mono_path(LPWSTR path
);
110 static BOOL
find_mono_dll(LPCWSTR path
, LPWSTR dll_path
);
112 static MonoAssembly
* mono_assembly_preload_hook_fn(MonoAssemblyName
*aname
, char **assemblies_path
, void *user_data
);
114 static void mono_shutdown_callback_fn(MonoProfiler
*prof
);
116 static void mono_print_handler_fn(const char *string
, INT is_stdout
);
118 static MonoImage
* CDECL
image_open_module_handle_dummy(HMODULE module_handle
,
119 char* fname
, UINT has_entry_point
, MonoImageOpenStatus
* status
)
121 return mono_image_open(fname
, status
);
124 static void CDECL
set_print_handler_dummy(MonoPrintCallback callback
)
128 static void missing_runtime_message(void)
130 MESSAGE("wine: Install Mono for Windows to run .NET applications.\n");
133 static HRESULT
load_mono(LPCWSTR mono_path
)
135 static const WCHAR lib
[] = {'\\','l','i','b',0};
136 static const WCHAR etc
[] = {'\\','e','t','c',0};
137 WCHAR mono_dll_path
[MAX_PATH
+16];
138 WCHAR mono_lib_path
[MAX_PATH
+4], mono_etc_path
[MAX_PATH
+4];
139 char mono_lib_path_a
[MAX_PATH
], mono_etc_path_a
[MAX_PATH
];
141 char trace_setting
[256];
143 char verbose_setting
[256];
145 if (is_mono_shutdown
)
147 ERR("Cannot load Mono after it has been shut down.\n");
153 strcpyW(mono_lib_path
, mono_path
);
154 strcatW(mono_lib_path
, lib
);
155 WideCharToMultiByte(CP_UTF8
, 0, mono_lib_path
, -1, mono_lib_path_a
, MAX_PATH
, NULL
, NULL
);
157 strcpyW(mono_etc_path
, mono_path
);
158 strcatW(mono_etc_path
, etc
);
159 WideCharToMultiByte(CP_UTF8
, 0, mono_etc_path
, -1, mono_etc_path_a
, MAX_PATH
, NULL
, NULL
);
161 if (!find_mono_dll(mono_path
, mono_dll_path
)) goto fail
;
163 mono_handle
= LoadLibraryW(mono_dll_path
);
165 if (!mono_handle
) goto fail
;
167 #define LOAD_MONO_FUNCTION(x) do { \
168 x = (void*)GetProcAddress(mono_handle, #x); \
174 LOAD_MONO_FUNCTION(mono_assembly_get_image
);
175 LOAD_MONO_FUNCTION(mono_assembly_load_from
);
176 LOAD_MONO_FUNCTION(mono_assembly_open
);
177 LOAD_MONO_FUNCTION(mono_config_parse
);
178 LOAD_MONO_FUNCTION(mono_class_from_mono_type
);
179 LOAD_MONO_FUNCTION(mono_class_from_name
);
180 LOAD_MONO_FUNCTION(mono_class_get_method_from_name
);
181 LOAD_MONO_FUNCTION(mono_domain_assembly_open
);
182 LOAD_MONO_FUNCTION(mono_free
);
183 LOAD_MONO_FUNCTION(mono_image_open
);
184 LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook
);
185 LOAD_MONO_FUNCTION(mono_jit_exec
);
186 LOAD_MONO_FUNCTION(mono_jit_init_version
);
187 LOAD_MONO_FUNCTION(mono_jit_set_trace_options
);
188 LOAD_MONO_FUNCTION(mono_marshal_get_vtfixup_ftnptr
);
189 LOAD_MONO_FUNCTION(mono_object_get_domain
);
190 LOAD_MONO_FUNCTION(mono_object_new
);
191 LOAD_MONO_FUNCTION(mono_object_unbox
);
192 LOAD_MONO_FUNCTION(mono_profiler_install
);
193 LOAD_MONO_FUNCTION(mono_reflection_type_from_name
);
194 LOAD_MONO_FUNCTION(mono_runtime_invoke
);
195 LOAD_MONO_FUNCTION(mono_runtime_object_init
);
196 LOAD_MONO_FUNCTION(mono_runtime_quit
);
197 LOAD_MONO_FUNCTION(mono_set_dirs
);
198 LOAD_MONO_FUNCTION(mono_set_verbose_level
);
199 LOAD_MONO_FUNCTION(mono_stringify_assembly_name
);
200 LOAD_MONO_FUNCTION(mono_string_new
);
201 LOAD_MONO_FUNCTION(mono_thread_attach
);
202 LOAD_MONO_FUNCTION(mono_thread_manage
);
203 LOAD_MONO_FUNCTION(mono_trace_set_assembly
);
205 #undef LOAD_MONO_FUNCTION
207 #define LOAD_OPT_MONO_FUNCTION(x, default) do { \
208 x = (void*)GetProcAddress(mono_handle, #x); \
214 LOAD_OPT_MONO_FUNCTION(mono_image_open_from_module_handle
, image_open_module_handle_dummy
);
215 LOAD_OPT_MONO_FUNCTION(mono_trace_set_print_handler
, set_print_handler_dummy
);
216 LOAD_OPT_MONO_FUNCTION(mono_trace_set_printerr_handler
, set_print_handler_dummy
);
218 #undef LOAD_OPT_MONO_FUNCTION
220 mono_profiler_install(NULL
, mono_shutdown_callback_fn
);
222 mono_trace_set_print_handler(mono_print_handler_fn
);
223 mono_trace_set_printerr_handler(mono_print_handler_fn
);
225 mono_set_dirs(mono_lib_path_a
, mono_etc_path_a
);
227 mono_config_parse(NULL
);
229 mono_install_assembly_preload_hook(mono_assembly_preload_hook_fn
, NULL
);
231 trace_size
= GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting
, sizeof(trace_setting
));
235 mono_jit_set_trace_options(trace_setting
);
238 verbose_size
= GetEnvironmentVariableA("WINE_MONO_VERBOSE", verbose_setting
, sizeof(verbose_setting
));
242 mono_set_verbose_level(verbose_setting
[0] - '0');
249 ERR("Could not load Mono into this process\n");
250 FreeLibrary(mono_handle
);
255 static void mono_shutdown_callback_fn(MonoProfiler
*prof
)
257 is_mono_shutdown
= TRUE
;
260 static void mono_print_handler_fn(const char *string
, INT is_stdout
)
262 wine_dbg_printf("%s", string
);
265 static HRESULT
CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo
*This
, RuntimeHost
**result
)
268 WCHAR mono_path
[MAX_PATH
];
270 if (This
->loaded_runtime
)
272 *result
= This
->loaded_runtime
;
276 if (!get_mono_path(mono_path
))
278 missing_runtime_message();
279 return CLR_E_SHIM_RUNTIME
;
282 EnterCriticalSection(&runtime_list_cs
);
284 hr
= load_mono(mono_path
);
287 hr
= RuntimeHost_Construct(This
, &This
->loaded_runtime
);
289 LeaveCriticalSection(&runtime_list_cs
);
292 *result
= This
->loaded_runtime
;
297 void expect_no_runtimes(void)
299 if (mono_handle
&& is_mono_started
&& !is_mono_shutdown
)
301 ERR("Process exited with a Mono runtime loaded.\n");
306 static inline CLRRuntimeInfo
*impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo
*iface
)
308 return CONTAINING_RECORD(iface
, CLRRuntimeInfo
, ICLRRuntimeInfo_iface
);
311 static HRESULT WINAPI
CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo
* iface
,
315 TRACE("%p %s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
317 if ( IsEqualGUID( riid
, &IID_ICLRRuntimeInfo
) ||
318 IsEqualGUID( riid
, &IID_IUnknown
) )
324 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
325 return E_NOINTERFACE
;
328 ICLRRuntimeInfo_AddRef( iface
);
333 static ULONG WINAPI
CLRRuntimeInfo_AddRef(ICLRRuntimeInfo
* iface
)
338 static ULONG WINAPI
CLRRuntimeInfo_Release(ICLRRuntimeInfo
* iface
)
343 static HRESULT WINAPI
CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo
* iface
,
344 LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
346 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
347 DWORD buffer_size
= *pcchBuffer
;
352 TRACE("%p %p %p\n", iface
, pwzBuffer
, pcchBuffer
);
354 size
= snprintf(version
, sizeof(version
), "v%u.%u.%u", This
->major
, This
->minor
, This
->build
);
356 assert(size
<= sizeof(version
));
358 *pcchBuffer
= MultiByteToWideChar(CP_UTF8
, 0, version
, -1, NULL
, 0);
362 if (buffer_size
>= *pcchBuffer
)
363 MultiByteToWideChar(CP_UTF8
, 0, version
, -1, pwzBuffer
, buffer_size
);
365 hr
= E_NOT_SUFFICIENT_BUFFER
;
371 static BOOL
get_install_root(LPWSTR install_dir
)
373 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};
374 const WCHAR install_root
[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
379 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, dotnet_key
, 0, KEY_READ
, &key
))
383 if (RegQueryValueExW(key
, install_root
, 0, NULL
, (LPBYTE
)install_dir
, &len
))
393 static HRESULT WINAPI
CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo
* iface
,
394 LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
396 static const WCHAR slash
[] = {'\\',0};
397 DWORD buffer_size
= *pcchBuffer
;
398 WCHAR system_dir
[MAX_PATH
];
399 WCHAR version
[MAX_PATH
];
400 DWORD version_size
, size
;
403 TRACE("%p %p %p\n", iface
, pwzBuffer
, pcchBuffer
);
405 if (!get_install_root(system_dir
))
407 ERR("error reading registry key for installroot\n");
412 version_size
= MAX_PATH
;
413 ICLRRuntimeInfo_GetVersionString(iface
, version
, &version_size
);
414 lstrcatW(system_dir
, version
);
415 lstrcatW(system_dir
, slash
);
416 size
= lstrlenW(system_dir
) + 1;
423 if (buffer_size
>= size
)
424 strcpyW(pwzBuffer
, system_dir
);
426 hr
= E_NOT_SUFFICIENT_BUFFER
;
432 static HRESULT WINAPI
CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo
* iface
,
433 HANDLE hndProcess
, BOOL
*pbLoaded
)
435 FIXME("%p %p %p\n", iface
, hndProcess
, pbLoaded
);
440 static HRESULT WINAPI
CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo
* iface
,
441 UINT iResourceID
, LPWSTR pwzBuffer
, DWORD
*pcchBuffer
, LONG iLocaleid
)
443 FIXME("%p %u %p %p %x\n", iface
, iResourceID
, pwzBuffer
, pcchBuffer
, iLocaleid
);
448 static HRESULT WINAPI
CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo
* iface
,
449 LPCWSTR pwzDllName
, HMODULE
*phndModule
)
451 WCHAR version
[MAX_PATH
];
455 TRACE("%p %s %p\n", iface
, debugstr_w(pwzDllName
), phndModule
);
457 cchBuffer
= MAX_PATH
;
458 hr
= ICLRRuntimeInfo_GetVersionString(iface
, version
, &cchBuffer
);
459 if (FAILED(hr
)) return hr
;
461 return LoadLibraryShim(pwzDllName
, version
, NULL
, phndModule
);
464 static HRESULT WINAPI
CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo
* iface
,
465 LPCSTR pszProcName
, LPVOID
*ppProc
)
467 FIXME("%p %s %p\n", iface
, debugstr_a(pszProcName
), ppProc
);
472 static HRESULT WINAPI
CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo
* iface
,
473 REFCLSID rclsid
, REFIID riid
, LPVOID
*ppUnk
)
475 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
479 TRACE("%p %s %s %p\n", iface
, debugstr_guid(rclsid
), debugstr_guid(riid
), ppUnk
);
481 hr
= CLRRuntimeInfo_GetRuntimeHost(This
, &host
);
484 hr
= RuntimeHost_GetInterface(host
, rclsid
, riid
, ppUnk
);
489 static HRESULT WINAPI
CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo
* iface
,
492 FIXME("%p %p\n", iface
, pbLoadable
);
497 static HRESULT WINAPI
CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo
* iface
,
498 DWORD dwStartupFlags
, LPCWSTR pwzHostConfigFile
)
500 FIXME("%p %x %s\n", iface
, dwStartupFlags
, debugstr_w(pwzHostConfigFile
));
505 static HRESULT WINAPI
CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo
* iface
,
506 DWORD
*pdwStartupFlags
, LPWSTR pwzHostConfigFile
, DWORD
*pcchHostConfigFile
)
508 FIXME("%p %p %p %p\n", iface
, pdwStartupFlags
, pwzHostConfigFile
, pcchHostConfigFile
);
513 static HRESULT WINAPI
CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo
* iface
)
515 FIXME("%p\n", iface
);
520 static HRESULT WINAPI
CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo
* iface
,
521 BOOL
*pbStarted
, DWORD
*pdwStartupFlags
)
523 FIXME("%p %p %p\n", iface
, pbStarted
, pdwStartupFlags
);
528 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl
= {
529 CLRRuntimeInfo_QueryInterface
,
530 CLRRuntimeInfo_AddRef
,
531 CLRRuntimeInfo_Release
,
532 CLRRuntimeInfo_GetVersionString
,
533 CLRRuntimeInfo_GetRuntimeDirectory
,
534 CLRRuntimeInfo_IsLoaded
,
535 CLRRuntimeInfo_LoadErrorString
,
536 CLRRuntimeInfo_LoadLibrary
,
537 CLRRuntimeInfo_GetProcAddress
,
538 CLRRuntimeInfo_GetInterface
,
539 CLRRuntimeInfo_IsLoadable
,
540 CLRRuntimeInfo_SetDefaultStartupFlags
,
541 CLRRuntimeInfo_GetDefaultStartupFlags
,
542 CLRRuntimeInfo_BindAsLegacyV2Runtime
,
543 CLRRuntimeInfo_IsStarted
546 HRESULT
ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo
*iface
, RuntimeHost
**result
)
548 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
550 assert(This
->ICLRRuntimeInfo_iface
.lpVtbl
== &CLRRuntimeInfoVtbl
);
552 return CLRRuntimeInfo_GetRuntimeHost(This
, result
);
556 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};
557 #elif defined(__x86_64__)
558 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};
560 static const WCHAR libmono2_arch_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
563 static BOOL
find_mono_dll(LPCWSTR path
, LPWSTR dll_path
)
565 static const WCHAR mono2_dll
[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
566 static const WCHAR libmono2_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
567 DWORD attributes
=INVALID_FILE_ATTRIBUTES
;
569 strcpyW(dll_path
, path
);
570 strcatW(dll_path
, libmono2_arch_dll
);
571 attributes
= GetFileAttributesW(dll_path
);
573 if (attributes
== INVALID_FILE_ATTRIBUTES
)
575 strcpyW(dll_path
, path
);
576 strcatW(dll_path
, mono2_dll
);
577 attributes
= GetFileAttributesW(dll_path
);
580 if (attributes
== INVALID_FILE_ATTRIBUTES
)
582 strcpyW(dll_path
, path
);
583 strcatW(dll_path
, libmono2_dll
);
584 attributes
= GetFileAttributesW(dll_path
);
587 return (attributes
!= INVALID_FILE_ATTRIBUTES
);
590 static BOOL
get_mono_path_from_registry(LPWSTR path
)
592 static const WCHAR mono_key
[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
593 static const WCHAR defaul_clr
[] = {'D','e','f','a','u','l','t','C','L','R',0};
594 static const WCHAR install_root
[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
595 static const WCHAR slash
[] = {'\\',0};
597 WCHAR version
[64], version_key
[MAX_PATH
];
600 WCHAR dll_path
[MAX_PATH
];
602 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, mono_key
, 0, KEY_READ
, &key
))
605 len
= sizeof(version
);
606 if (RegQueryValueExW(key
, defaul_clr
, 0, NULL
, (LPBYTE
)version
, &len
))
613 lstrcpyW(version_key
, mono_key
);
614 lstrcatW(version_key
, slash
);
615 lstrcatW(version_key
, version
);
617 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, version_key
, 0, KEY_READ
, &key
))
620 len
= sizeof(WCHAR
) * MAX_PATH
;
621 if (RegQueryValueExW(key
, install_root
, 0, NULL
, (LPBYTE
)path
, &len
))
628 return find_mono_dll(path
, dll_path
);
631 static BOOL
get_mono_path_from_folder(LPCWSTR folder
, LPWSTR mono_path
)
633 static const WCHAR mono_two_dot_zero
[] = {'\\','m','o','n','o','-','2','.','0', 0};
634 WCHAR mono_dll_path
[MAX_PATH
];
637 strcpyW(mono_path
, folder
);
639 strcatW(mono_path
, mono_two_dot_zero
);
641 found
= find_mono_dll(mono_path
, mono_dll_path
);
646 static BOOL
get_mono_path(LPWSTR path
)
648 static const WCHAR subdir_mono
[] = {'\\','m','o','n','o',0};
649 static const WCHAR sibling_mono
[] = {'\\','.','.','\\','m','o','n','o',0};
650 WCHAR base_path
[MAX_PATH
];
651 const char *unix_data_dir
;
653 BOOL build_tree
= FALSE
;
654 static WCHAR
* (CDECL
*wine_get_dos_file_name
)(const char*);
656 /* First try c:\windows\mono */
657 GetWindowsDirectoryW(base_path
, MAX_PATH
);
658 strcatW(base_path
, subdir_mono
);
660 if (get_mono_path_from_folder(base_path
, path
))
663 /* Next: /usr/share/wine/mono */
664 unix_data_dir
= wine_get_data_dir();
668 unix_data_dir
= wine_get_build_dir();
674 if (!wine_get_dos_file_name
)
675 wine_get_dos_file_name
= (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
677 if (wine_get_dos_file_name
)
679 dos_data_dir
= wine_get_dos_file_name(unix_data_dir
);
683 strcpyW(base_path
, dos_data_dir
);
684 strcatW(base_path
, build_tree
? sibling_mono
: subdir_mono
);
686 HeapFree(GetProcessHeap(), 0, dos_data_dir
);
688 if (get_mono_path_from_folder(base_path
, path
))
694 /* Last: the registry */
695 return get_mono_path_from_registry(path
);
698 struct InstalledRuntimeEnum
700 IEnumUnknown IEnumUnknown_iface
;
705 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl
;
707 static inline struct InstalledRuntimeEnum
*impl_from_IEnumUnknown(IEnumUnknown
*iface
)
709 return CONTAINING_RECORD(iface
, struct InstalledRuntimeEnum
, IEnumUnknown_iface
);
712 static HRESULT WINAPI
InstalledRuntimeEnum_QueryInterface(IEnumUnknown
* iface
, REFIID riid
,
715 TRACE("%p %s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
717 if ( IsEqualGUID( riid
, &IID_IEnumUnknown
) ||
718 IsEqualGUID( riid
, &IID_IUnknown
) )
724 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
725 return E_NOINTERFACE
;
728 IEnumUnknown_AddRef( iface
);
733 static ULONG WINAPI
InstalledRuntimeEnum_AddRef(IEnumUnknown
* iface
)
735 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
736 ULONG ref
= InterlockedIncrement(&This
->ref
);
738 TRACE("(%p) refcount=%u\n", iface
, ref
);
743 static ULONG WINAPI
InstalledRuntimeEnum_Release(IEnumUnknown
* iface
)
745 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
746 ULONG ref
= InterlockedDecrement(&This
->ref
);
748 TRACE("(%p) refcount=%u\n", iface
, ref
);
752 HeapFree(GetProcessHeap(), 0, This
);
758 static HRESULT WINAPI
InstalledRuntimeEnum_Next(IEnumUnknown
*iface
, ULONG celt
,
759 IUnknown
**rgelt
, ULONG
*pceltFetched
)
761 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
762 ULONG num_fetched
= 0;
766 TRACE("(%p,%u,%p,%p)\n", iface
, celt
, rgelt
, pceltFetched
);
768 while (num_fetched
< celt
)
770 if (This
->pos
>= NUM_RUNTIMES
)
775 item
= (IUnknown
*)&runtimes
[This
->pos
].ICLRRuntimeInfo_iface
;
776 IUnknown_AddRef(item
);
777 rgelt
[num_fetched
] = item
;
783 *pceltFetched
= num_fetched
;
788 static HRESULT WINAPI
InstalledRuntimeEnum_Skip(IEnumUnknown
*iface
, ULONG celt
)
790 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
791 ULONG num_fetched
= 0;
794 TRACE("(%p,%u)\n", iface
, celt
);
796 while (num_fetched
< celt
)
798 if (This
->pos
>= NUM_RUNTIMES
)
810 static HRESULT WINAPI
InstalledRuntimeEnum_Reset(IEnumUnknown
*iface
)
812 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
814 TRACE("(%p)\n", iface
);
821 static HRESULT WINAPI
InstalledRuntimeEnum_Clone(IEnumUnknown
*iface
, IEnumUnknown
**ppenum
)
823 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
824 struct InstalledRuntimeEnum
*new_enum
;
826 TRACE("(%p)\n", iface
);
828 new_enum
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum
));
830 return E_OUTOFMEMORY
;
832 new_enum
->IEnumUnknown_iface
.lpVtbl
= &InstalledRuntimeEnum_Vtbl
;
834 new_enum
->pos
= This
->pos
;
836 *ppenum
= &new_enum
->IEnumUnknown_iface
;
841 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl
= {
842 InstalledRuntimeEnum_QueryInterface
,
843 InstalledRuntimeEnum_AddRef
,
844 InstalledRuntimeEnum_Release
,
845 InstalledRuntimeEnum_Next
,
846 InstalledRuntimeEnum_Skip
,
847 InstalledRuntimeEnum_Reset
,
848 InstalledRuntimeEnum_Clone
853 ICLRMetaHost ICLRMetaHost_iface
;
855 RuntimeLoadedCallbackFnPtr callback
;
858 static struct CLRMetaHost GlobalCLRMetaHost
;
860 static HRESULT WINAPI
CLRMetaHost_QueryInterface(ICLRMetaHost
* iface
,
864 TRACE("%s %p\n", debugstr_guid(riid
), ppvObject
);
866 if ( IsEqualGUID( riid
, &IID_ICLRMetaHost
) ||
867 IsEqualGUID( riid
, &IID_IUnknown
) )
873 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
874 return E_NOINTERFACE
;
877 ICLRMetaHost_AddRef( iface
);
882 static ULONG WINAPI
CLRMetaHost_AddRef(ICLRMetaHost
* iface
)
887 static ULONG WINAPI
CLRMetaHost_Release(ICLRMetaHost
* iface
)
892 static BOOL
parse_runtime_version(LPCWSTR version
, DWORD
*major
, DWORD
*minor
, DWORD
*build
)
898 if (version
[0] == 'v' || version
[0] == 'V')
901 if (!isdigit(*version
))
904 while (isdigit(*version
))
905 *major
= *major
* 10 + (*version
++ - '0');
910 if (*version
++ != '.' || !isdigit(*version
))
913 while (isdigit(*version
))
914 *minor
= *minor
* 10 + (*version
++ - '0');
919 if (*version
++ != '.' || !isdigit(*version
))
922 while (isdigit(*version
))
923 *build
= *build
* 10 + (*version
++ - '0');
925 return *version
== 0;
931 static HRESULT
get_runtime(LPCWSTR pwzVersion
, BOOL allow_short
,
932 REFIID iid
, LPVOID
*ppRuntime
)
935 DWORD major
, minor
, build
;
940 if (!parse_runtime_version(pwzVersion
, &major
, &minor
, &build
))
942 ERR("Cannot parse %s\n", debugstr_w(pwzVersion
));
943 return CLR_E_SHIM_RUNTIME
;
946 for (i
=0; i
<NUM_RUNTIMES
; i
++)
948 if (runtimes
[i
].major
== major
&& runtimes
[i
].minor
== minor
&&
949 (runtimes
[i
].build
== build
|| (allow_short
&& major
>= 4 && build
== 0)))
951 return ICLRRuntimeInfo_QueryInterface(&runtimes
[i
].ICLRRuntimeInfo_iface
, iid
,
956 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion
));
957 return CLR_E_SHIM_RUNTIME
;
960 HRESULT WINAPI
CLRMetaHost_GetRuntime(ICLRMetaHost
* iface
,
961 LPCWSTR pwzVersion
, REFIID iid
, LPVOID
*ppRuntime
)
963 TRACE("%s %s %p\n", debugstr_w(pwzVersion
), debugstr_guid(iid
), ppRuntime
);
965 return get_runtime(pwzVersion
, FALSE
, iid
, ppRuntime
);
968 HRESULT WINAPI
CLRMetaHost_GetVersionFromFile(ICLRMetaHost
* iface
,
969 LPCWSTR pwzFilePath
, LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
974 ULONG buffer_size
=*pcchBuffer
;
976 TRACE("%s %p %p\n", debugstr_w(pwzFilePath
), pwzBuffer
, pcchBuffer
);
978 hr
= assembly_create(&assembly
, pwzFilePath
);
982 hr
= assembly_get_runtime_version(assembly
, &version
);
986 *pcchBuffer
= MultiByteToWideChar(CP_UTF8
, 0, version
, -1, NULL
, 0);
990 if (buffer_size
>= *pcchBuffer
)
991 MultiByteToWideChar(CP_UTF8
, 0, version
, -1, pwzBuffer
, buffer_size
);
993 hr
= E_NOT_SUFFICIENT_BUFFER
;
997 assembly_release(assembly
);
1003 static HRESULT WINAPI
CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost
* iface
,
1004 IEnumUnknown
**ppEnumerator
)
1006 struct InstalledRuntimeEnum
*new_enum
;
1008 TRACE("%p\n", ppEnumerator
);
1010 new_enum
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum
));
1012 return E_OUTOFMEMORY
;
1014 new_enum
->IEnumUnknown_iface
.lpVtbl
= &InstalledRuntimeEnum_Vtbl
;
1018 *ppEnumerator
= &new_enum
->IEnumUnknown_iface
;
1023 static HRESULT WINAPI
CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost
* iface
,
1024 HANDLE hndProcess
, IEnumUnknown
**ppEnumerator
)
1026 FIXME("%p %p\n", hndProcess
, ppEnumerator
);
1031 static HRESULT WINAPI
CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost
* iface
,
1032 RuntimeLoadedCallbackFnPtr pCallbackFunction
)
1034 TRACE("%p\n", pCallbackFunction
);
1036 if(!pCallbackFunction
)
1039 WARN("Callback currently will not be called.\n");
1041 GlobalCLRMetaHost
.callback
= pCallbackFunction
;
1046 static HRESULT WINAPI
CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost
* iface
,
1047 REFIID riid
, LPVOID
*ppUnk
)
1049 FIXME("%s %p\n", debugstr_guid(riid
), ppUnk
);
1054 HRESULT WINAPI
CLRMetaHost_ExitProcess(ICLRMetaHost
* iface
, INT32 iExitCode
)
1056 TRACE("%i\n", iExitCode
);
1058 EnterCriticalSection(&runtime_list_cs
);
1060 if (is_mono_started
&& !is_mono_shutdown
)
1062 /* search for a runtime and call System.Environment.Exit() */
1065 for (i
=0; i
<NUM_RUNTIMES
; i
++)
1066 if (runtimes
[i
].loaded_runtime
)
1067 RuntimeHost_ExitProcess(runtimes
[i
].loaded_runtime
, iExitCode
);
1070 ExitProcess(iExitCode
);
1073 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl
=
1075 CLRMetaHost_QueryInterface
,
1077 CLRMetaHost_Release
,
1078 CLRMetaHost_GetRuntime
,
1079 CLRMetaHost_GetVersionFromFile
,
1080 CLRMetaHost_EnumerateInstalledRuntimes
,
1081 CLRMetaHost_EnumerateLoadedRuntimes
,
1082 CLRMetaHost_RequestRuntimeLoadedNotification
,
1083 CLRMetaHost_QueryLegacyV2RuntimeBinding
,
1084 CLRMetaHost_ExitProcess
1087 static struct CLRMetaHost GlobalCLRMetaHost
= {
1088 { &CLRMetaHost_vtbl
}
1091 HRESULT
CLRMetaHost_CreateInstance(REFIID riid
, void **ppobj
)
1093 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost
.ICLRMetaHost_iface
, riid
, ppobj
);
1096 struct CLRMetaHostPolicy
1098 ICLRMetaHostPolicy ICLRMetaHostPolicy_iface
;
1101 static struct CLRMetaHostPolicy GlobalCLRMetaHostPolicy
;
1103 static HRESULT WINAPI
metahostpolicy_QueryInterface(ICLRMetaHostPolicy
*iface
, REFIID riid
, void **obj
)
1105 TRACE("%s %p\n", debugstr_guid(riid
), obj
);
1107 if ( IsEqualGUID( riid
, &IID_ICLRMetaHostPolicy
) ||
1108 IsEqualGUID( riid
, &IID_IUnknown
) )
1110 ICLRMetaHostPolicy_AddRef( iface
);
1115 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
1118 return E_NOINTERFACE
;
1121 static ULONG WINAPI
metahostpolicy_AddRef(ICLRMetaHostPolicy
*iface
)
1126 static ULONG WINAPI
metahostpolicy_Release(ICLRMetaHostPolicy
*iface
)
1131 static HRESULT WINAPI
metahostpolicy_GetRequestedRuntime(ICLRMetaHostPolicy
*iface
, METAHOST_POLICY_FLAGS dwPolicyFlags
,
1132 LPCWSTR pwzBinary
, IStream
*pCfgStream
, LPWSTR pwzVersion
, DWORD
*pcchVersion
,
1133 LPWSTR pwzImageVersion
, DWORD
*pcchImageVersion
, DWORD
*pdwConfigFlags
, REFIID riid
,
1136 ICLRRuntimeInfo
*result
;
1138 WCHAR filename
[MAX_PATH
];
1139 const WCHAR
*path
= NULL
;
1142 TRACE("%d %p %p %p %p %p %p %p %s %p\n", dwPolicyFlags
, pwzBinary
, pCfgStream
,
1143 pwzVersion
, pcchVersion
, pwzImageVersion
, pcchImageVersion
, pdwConfigFlags
,
1144 debugstr_guid(riid
), ppRuntime
);
1147 FIXME("ignoring config file stream\n");
1150 FIXME("ignoring config flags\n");
1152 if(dwPolicyFlags
& METAHOST_POLICY_USE_PROCESS_IMAGE_PATH
)
1154 GetModuleFileNameW(0, filename
, MAX_PATH
);
1162 if(dwPolicyFlags
& METAHOST_POLICY_APPLY_UPGRADE_POLICY
)
1163 flags
|= RUNTIME_INFO_UPGRADE_VERSION
;
1165 hr
= get_runtime_info(path
, pwzImageVersion
, NULL
, 0, flags
, FALSE
, &result
);
1168 if (pwzImageVersion
)
1170 /* Ignoring errors on purpose */
1171 ICLRRuntimeInfo_GetVersionString(result
, pwzImageVersion
, pcchImageVersion
);
1174 hr
= ICLRRuntimeInfo_QueryInterface(result
, riid
, ppRuntime
);
1176 ICLRRuntimeInfo_Release(result
);
1179 TRACE("<- 0x%08x\n", hr
);
1184 static const struct ICLRMetaHostPolicyVtbl CLRMetaHostPolicy_vtbl
=
1186 metahostpolicy_QueryInterface
,
1187 metahostpolicy_AddRef
,
1188 metahostpolicy_Release
,
1189 metahostpolicy_GetRequestedRuntime
1192 static struct CLRMetaHostPolicy GlobalCLRMetaHostPolicy
= {
1193 { &CLRMetaHostPolicy_vtbl
}
1196 HRESULT
CLRMetaHostPolicy_CreateInstance(REFIID riid
, void **ppobj
)
1198 return ICLRMetaHostPolicy_QueryInterface(&GlobalCLRMetaHostPolicy
.ICLRMetaHostPolicy_iface
, riid
, ppobj
);
1201 HRESULT
get_file_from_strongname(WCHAR
* stringnameW
, WCHAR
* assemblies_path
, int path_length
)
1204 IAssemblyCache
*asmcache
;
1206 static const WCHAR fusiondll
[] = {'f','u','s','i','o','n',0};
1207 HMODULE hfusion
=NULL
;
1208 static HRESULT (WINAPI
*pCreateAssemblyCache
)(IAssemblyCache
**,DWORD
);
1210 if (!pCreateAssemblyCache
)
1212 hr
= LoadLibraryShim(fusiondll
, NULL
, NULL
, &hfusion
);
1216 pCreateAssemblyCache
= (void*)GetProcAddress(hfusion
, "CreateAssemblyCache");
1217 if (!pCreateAssemblyCache
)
1223 hr
= pCreateAssemblyCache(&asmcache
, 0);
1227 info
.cbAssemblyInfo
= sizeof(info
);
1228 info
.pszCurrentAssemblyPathBuf
= assemblies_path
;
1229 info
.cchBuf
= path_length
;
1230 assemblies_path
[0] = 0;
1232 hr
= IAssemblyCache_QueryAssemblyInfo(asmcache
, 0, stringnameW
, &info
);
1234 IAssemblyCache_Release(asmcache
);
1240 static MonoAssembly
* mono_assembly_preload_hook_fn(MonoAssemblyName
*aname
, char **assemblies_path
, void *user_data
)
1243 MonoAssembly
*result
=NULL
;
1244 char *stringname
=NULL
;
1246 int stringnameW_size
;
1247 WCHAR path
[MAX_PATH
];
1249 MonoImageOpenStatus stat
;
1251 stringname
= mono_stringify_assembly_name(aname
);
1253 TRACE("%s\n", debugstr_a(stringname
));
1255 if (!stringname
) return NULL
;
1257 /* FIXME: We should search the given paths before the GAC. */
1259 stringnameW_size
= MultiByteToWideChar(CP_UTF8
, 0, stringname
, -1, NULL
, 0);
1261 stringnameW
= HeapAlloc(GetProcessHeap(), 0, stringnameW_size
* sizeof(WCHAR
));
1264 MultiByteToWideChar(CP_UTF8
, 0, stringname
, -1, stringnameW
, stringnameW_size
);
1266 hr
= get_file_from_strongname(stringnameW
, path
, MAX_PATH
);
1268 HeapFree(GetProcessHeap(), 0, stringnameW
);
1275 TRACE("found: %s\n", debugstr_w(path
));
1281 result
= mono_assembly_open(pathA
, &stat
);
1284 ERR("Failed to load %s, status=%u\n", debugstr_w(path
), stat
);
1286 HeapFree(GetProcessHeap(), 0, pathA
);
1290 mono_free(stringname
);
1295 HRESULT
get_runtime_info(LPCWSTR exefile
, LPCWSTR version
, LPCWSTR config_file
,
1296 DWORD startup_flags
, DWORD runtimeinfo_flags
, BOOL legacy
, ICLRRuntimeInfo
**result
)
1298 static const WCHAR dotconfig
[] = {'.','c','o','n','f','i','g',0};
1299 static const DWORD supported_startup_flags
= 0;
1300 static const DWORD supported_runtime_flags
= RUNTIME_INFO_UPGRADE_VERSION
;
1302 WCHAR local_version
[MAX_PATH
];
1303 ULONG local_version_size
= MAX_PATH
;
1304 WCHAR local_config_file
[MAX_PATH
];
1306 parsed_config_file parsed_config
;
1308 if (startup_flags
& ~supported_startup_flags
)
1309 FIXME("unsupported startup flags %x\n", startup_flags
& ~supported_startup_flags
);
1311 if (runtimeinfo_flags
& ~supported_runtime_flags
)
1312 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags
& ~supported_runtime_flags
);
1314 if (exefile
&& !exefile
[0])
1317 if (exefile
&& !config_file
)
1319 strcpyW(local_config_file
, exefile
);
1320 strcatW(local_config_file
, dotconfig
);
1322 config_file
= local_config_file
;
1328 hr
= parse_config_file(config_file
, &parsed_config
);
1332 supported_runtime
*entry
;
1333 LIST_FOR_EACH_ENTRY(entry
, &parsed_config
.supported_runtimes
, supported_runtime
, entry
)
1335 hr
= get_runtime(entry
->version
, TRUE
, &IID_ICLRRuntimeInfo
, (void**)result
);
1345 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file
), hr
);
1348 free_parsed_config_file(&parsed_config
);
1354 if (exefile
&& !version
)
1356 hr
= CLRMetaHost_GetVersionFromFile(0, exefile
, local_version
, &local_version_size
);
1358 version
= local_version
;
1360 if (FAILED(hr
)) return hr
;
1365 hr
= CLRMetaHost_GetRuntime(0, version
, &IID_ICLRRuntimeInfo
, (void**)result
);
1370 if (runtimeinfo_flags
& RUNTIME_INFO_UPGRADE_VERSION
)
1372 DWORD major
, minor
, build
;
1374 if (version
&& !parse_runtime_version(version
, &major
, &minor
, &build
))
1376 ERR("Cannot parse %s\n", debugstr_w(version
));
1377 return CLR_E_SHIM_RUNTIME
;
1387 /* Must be greater or equal to the version passed in. */
1388 if (!version
|| ((runtimes
[i
].major
>= major
&& runtimes
[i
].minor
>= minor
&& runtimes
[i
].build
>= build
) ||
1389 (runtimes
[i
].major
>= major
&& runtimes
[i
].minor
> minor
) ||
1390 (runtimes
[i
].major
> major
)))
1392 return ICLRRuntimeInfo_QueryInterface(&runtimes
[i
].ICLRRuntimeInfo_iface
,
1393 &IID_ICLRRuntimeInfo
, (void **)result
);
1397 missing_runtime_message();
1399 return CLR_E_SHIM_RUNTIME
;
1402 return CLR_E_SHIM_RUNTIME
;