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 WCHAR net_20_subdir
[] = {'2','.','0',0};
49 static const WCHAR net_40_subdir
[] = {'4','.','0',0};
51 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl
;
53 #define NUM_RUNTIMES 3
55 static struct CLRRuntimeInfo runtimes
[NUM_RUNTIMES
] = {
56 {{&CLRRuntimeInfoVtbl
}, net_20_subdir
, 1, 1, 4322, 0},
57 {{&CLRRuntimeInfoVtbl
}, net_20_subdir
, 2, 0, 50727, 0},
58 {{&CLRRuntimeInfoVtbl
}, net_40_subdir
, 4, 0, 30319, 0}
61 static int runtimes_initialized
;
63 static CRITICAL_SECTION runtime_list_cs
;
64 static CRITICAL_SECTION_DEBUG runtime_list_cs_debug
=
66 0, 0, &runtime_list_cs
,
67 { &runtime_list_cs_debug
.ProcessLocksList
,
68 &runtime_list_cs_debug
.ProcessLocksList
},
69 0, 0, { (DWORD_PTR
)(__FILE__
": runtime_list_cs") }
71 static CRITICAL_SECTION runtime_list_cs
= { &runtime_list_cs_debug
, -1, 0, 0, 0, 0 };
73 #define NUM_ABI_VERSIONS 2
75 static loaded_mono loaded_monos
[NUM_ABI_VERSIONS
];
77 static BOOL
find_mono_dll(LPCWSTR path
, LPWSTR dll_path
, int abi_version
);
79 static MonoAssembly
* mono_assembly_search_hook_fn(MonoAssemblyName
*aname
, char **assemblies_path
, void *user_data
);
81 static void mono_shutdown_callback_fn(MonoProfiler
*prof
);
83 static void set_environment(LPCWSTR bin_path
)
85 WCHAR path_env
[MAX_PATH
];
88 static const WCHAR pathW
[] = {'P','A','T','H',0};
90 /* We have to modify PATH as Mono loads other DLLs from this directory. */
91 GetEnvironmentVariableW(pathW
, path_env
, sizeof(path_env
)/sizeof(WCHAR
));
92 len
= strlenW(path_env
);
93 path_env
[len
++] = ';';
94 strcpyW(path_env
+len
, bin_path
);
95 SetEnvironmentVariableW(pathW
, path_env
);
98 static void CDECL
do_nothing(void)
102 static MonoImage
* image_open_module_handle_dummy(HMODULE module_handle
,
103 char* fname
, UINT has_entry_point
, MonoImageOpenStatus
* status
, int abi_version
)
105 return loaded_monos
[abi_version
-1].mono_image_open(fname
, status
);
108 static CDECL MonoImage
* image_open_module_handle_dummy_1(HMODULE module_handle
,
109 char* fname
, UINT has_entry_point
, MonoImageOpenStatus
* status
)
111 return image_open_module_handle_dummy(module_handle
, fname
, has_entry_point
, status
, 1);
114 static CDECL MonoImage
* image_open_module_handle_dummy_2(HMODULE module_handle
,
115 char* fname
, UINT has_entry_point
, MonoImageOpenStatus
* status
)
117 return image_open_module_handle_dummy(module_handle
, fname
, has_entry_point
, status
, 2);
120 MonoImage
* (CDECL
* const image_from_handle_fn
[NUM_ABI_VERSIONS
])(HMODULE module_handle
, char* fname
, UINT has_entry_point
, MonoImageOpenStatus
* status
) = {
121 image_open_module_handle_dummy_1
,
122 image_open_module_handle_dummy_2
125 static void missing_runtime_message(void)
127 MESSAGE("wine: Install Mono for Windows to run .NET applications.\n");
130 static HRESULT
load_mono(CLRRuntimeInfo
*This
, loaded_mono
**result
)
132 static const WCHAR bin
[] = {'\\','b','i','n',0};
133 static const WCHAR lib
[] = {'\\','l','i','b',0};
134 static const WCHAR etc
[] = {'\\','e','t','c',0};
135 static const WCHAR glibdll
[] = {'l','i','b','g','l','i','b','-','2','.','0','-','0','.','d','l','l',0};
136 WCHAR mono_dll_path
[MAX_PATH
+16], mono_bin_path
[MAX_PATH
+4];
137 WCHAR mono_lib_path
[MAX_PATH
+4], mono_etc_path
[MAX_PATH
+4];
138 char mono_lib_path_a
[MAX_PATH
], mono_etc_path_a
[MAX_PATH
];
140 char trace_setting
[256];
142 if (This
->mono_abi_version
<= 0 || This
->mono_abi_version
> NUM_ABI_VERSIONS
)
144 missing_runtime_message();
148 *result
= &loaded_monos
[This
->mono_abi_version
-1];
150 if ((*result
)->is_shutdown
)
152 ERR("Cannot load Mono after it has been shut down.\n");
157 if (!(*result
)->mono_handle
)
159 strcpyW(mono_bin_path
, This
->mono_path
);
160 strcatW(mono_bin_path
, bin
);
161 set_environment(mono_bin_path
);
163 strcpyW(mono_lib_path
, This
->mono_path
);
164 strcatW(mono_lib_path
, lib
);
165 WideCharToMultiByte(CP_UTF8
, 0, mono_lib_path
, -1, mono_lib_path_a
, MAX_PATH
, NULL
, NULL
);
167 strcpyW(mono_etc_path
, This
->mono_path
);
168 strcatW(mono_etc_path
, etc
);
169 WideCharToMultiByte(CP_UTF8
, 0, mono_etc_path
, -1, mono_etc_path_a
, MAX_PATH
, NULL
, NULL
);
171 if (!find_mono_dll(This
->mono_path
, mono_dll_path
, This
->mono_abi_version
)) goto fail
;
173 (*result
)->mono_handle
= LoadLibraryW(mono_dll_path
);
175 if (!(*result
)->mono_handle
) goto fail
;
177 #define LOAD_MONO_FUNCTION(x) do { \
178 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
179 if (!(*result)->x) { \
184 LOAD_MONO_FUNCTION(mono_assembly_get_image
);
185 LOAD_MONO_FUNCTION(mono_assembly_load_from
);
186 LOAD_MONO_FUNCTION(mono_assembly_open
);
187 LOAD_MONO_FUNCTION(mono_config_parse
);
188 LOAD_MONO_FUNCTION(mono_class_from_mono_type
);
189 LOAD_MONO_FUNCTION(mono_class_from_name
);
190 LOAD_MONO_FUNCTION(mono_class_get_method_from_name
);
191 LOAD_MONO_FUNCTION(mono_domain_assembly_open
);
192 LOAD_MONO_FUNCTION(mono_image_open
);
193 LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook
);
194 LOAD_MONO_FUNCTION(mono_jit_exec
);
195 LOAD_MONO_FUNCTION(mono_jit_init
);
196 LOAD_MONO_FUNCTION(mono_jit_set_trace_options
);
197 LOAD_MONO_FUNCTION(mono_marshal_get_vtfixup_ftnptr
);
198 LOAD_MONO_FUNCTION(mono_object_get_domain
);
199 LOAD_MONO_FUNCTION(mono_object_new
);
200 LOAD_MONO_FUNCTION(mono_object_unbox
);
201 LOAD_MONO_FUNCTION(mono_profiler_install
);
202 LOAD_MONO_FUNCTION(mono_reflection_type_from_name
);
203 LOAD_MONO_FUNCTION(mono_runtime_invoke
);
204 LOAD_MONO_FUNCTION(mono_runtime_object_init
);
205 LOAD_MONO_FUNCTION(mono_runtime_quit
);
206 LOAD_MONO_FUNCTION(mono_set_dirs
);
207 LOAD_MONO_FUNCTION(mono_stringify_assembly_name
);
208 LOAD_MONO_FUNCTION(mono_string_new
);
209 LOAD_MONO_FUNCTION(mono_thread_attach
);
211 /* GLib imports obsoleted by the 2.0 ABI */
212 if (This
->mono_abi_version
== 1)
214 (*result
)->glib_handle
= LoadLibraryW(glibdll
);
215 if (!(*result
)->glib_handle
) goto fail
;
217 (*result
)->mono_free
= (void*)GetProcAddress((*result
)->glib_handle
, "g_free");
218 if (!(*result
)->mono_free
) goto fail
;
222 LOAD_MONO_FUNCTION(mono_free
);
225 #undef LOAD_MONO_FUNCTION
227 #define LOAD_OPT_MONO_FUNCTION(x, default) do { \
228 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
229 if (!(*result)->x) { \
230 (*result)->x = default; \
234 LOAD_OPT_MONO_FUNCTION(mono_image_open_from_module_handle
, image_from_handle_fn
[This
->mono_abi_version
-1]);
235 LOAD_OPT_MONO_FUNCTION(mono_runtime_set_shutting_down
, do_nothing
);
236 LOAD_OPT_MONO_FUNCTION(mono_thread_pool_cleanup
, do_nothing
);
237 LOAD_OPT_MONO_FUNCTION(mono_thread_suspend_all_other_threads
, do_nothing
);
238 LOAD_OPT_MONO_FUNCTION(mono_threads_set_shutting_down
, do_nothing
);
240 #undef LOAD_OPT_MONO_FUNCTION
242 (*result
)->mono_profiler_install((MonoProfiler
*)*result
, mono_shutdown_callback_fn
);
244 (*result
)->mono_set_dirs(mono_lib_path_a
, mono_etc_path_a
);
246 (*result
)->mono_config_parse(NULL
);
248 (*result
)->mono_install_assembly_preload_hook(mono_assembly_search_hook_fn
, *result
);
250 trace_size
= GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting
, sizeof(trace_setting
));
254 (*result
)->mono_jit_set_trace_options(trace_setting
);
261 ERR("Could not load Mono into this process\n");
262 FreeLibrary((*result
)->mono_handle
);
263 FreeLibrary((*result
)->glib_handle
);
264 (*result
)->mono_handle
= NULL
;
265 (*result
)->glib_handle
= NULL
;
269 static void mono_shutdown_callback_fn(MonoProfiler
*prof
)
271 loaded_mono
*mono
= (loaded_mono
*)prof
;
273 mono
->is_shutdown
= TRUE
;
276 static HRESULT
CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo
*This
, RuntimeHost
**result
)
279 loaded_mono
*ploaded_mono
;
281 if (This
->loaded_runtime
)
283 *result
= This
->loaded_runtime
;
287 EnterCriticalSection(&runtime_list_cs
);
289 hr
= load_mono(This
, &ploaded_mono
);
292 hr
= RuntimeHost_Construct(This
, ploaded_mono
, &This
->loaded_runtime
);
294 LeaveCriticalSection(&runtime_list_cs
);
297 *result
= This
->loaded_runtime
;
302 void unload_all_runtimes(void)
306 for (i
=0; i
<NUM_ABI_VERSIONS
; i
++)
308 loaded_mono
*mono
= &loaded_monos
[i
];
309 if (mono
->mono_handle
&& mono
->is_started
&& !mono
->is_shutdown
)
311 /* Copied from Mono's ves_icall_System_Environment_Exit */
312 mono
->mono_threads_set_shutting_down();
313 mono
->mono_runtime_set_shutting_down();
314 mono
->mono_thread_pool_cleanup();
315 mono
->mono_thread_suspend_all_other_threads();
316 mono
->mono_runtime_quit();
320 for (i
=0; i
<NUM_RUNTIMES
; i
++)
321 if (runtimes
[i
].loaded_runtime
)
322 RuntimeHost_Destroy(runtimes
[i
].loaded_runtime
);
325 void expect_no_runtimes(void)
329 for (i
=0; i
<NUM_ABI_VERSIONS
; i
++)
331 loaded_mono
*mono
= &loaded_monos
[i
];
332 if (mono
->mono_handle
&& mono
->is_started
&& !mono
->is_shutdown
)
334 ERR("Process exited with a Mono runtime loaded.\n");
340 static inline CLRRuntimeInfo
*impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo
*iface
)
342 return CONTAINING_RECORD(iface
, CLRRuntimeInfo
, ICLRRuntimeInfo_iface
);
345 static HRESULT WINAPI
CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo
* iface
,
349 TRACE("%p %s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
351 if ( IsEqualGUID( riid
, &IID_ICLRRuntimeInfo
) ||
352 IsEqualGUID( riid
, &IID_IUnknown
) )
358 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
359 return E_NOINTERFACE
;
362 ICLRRuntimeInfo_AddRef( iface
);
367 static ULONG WINAPI
CLRRuntimeInfo_AddRef(ICLRRuntimeInfo
* iface
)
372 static ULONG WINAPI
CLRRuntimeInfo_Release(ICLRRuntimeInfo
* iface
)
377 static HRESULT WINAPI
CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo
* iface
,
378 LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
380 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
381 DWORD buffer_size
= *pcchBuffer
;
386 TRACE("%p %p %p\n", iface
, pwzBuffer
, pcchBuffer
);
388 size
= snprintf(version
, sizeof(version
), "v%u.%u.%u", This
->major
, This
->minor
, This
->build
);
390 assert(size
<= sizeof(version
));
392 *pcchBuffer
= MultiByteToWideChar(CP_UTF8
, 0, version
, -1, NULL
, 0);
396 if (buffer_size
>= *pcchBuffer
)
397 MultiByteToWideChar(CP_UTF8
, 0, version
, -1, pwzBuffer
, buffer_size
);
399 hr
= HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
405 static BOOL
get_install_root(LPWSTR install_dir
)
407 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};
408 const WCHAR install_root
[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
413 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, dotnet_key
, 0, KEY_READ
, &key
))
417 if (RegQueryValueExW(key
, install_root
, 0, NULL
, (LPBYTE
)install_dir
, &len
))
427 static HRESULT WINAPI
CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo
* iface
,
428 LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
430 static const WCHAR slash
[] = {'\\',0};
431 DWORD buffer_size
= *pcchBuffer
;
432 WCHAR system_dir
[MAX_PATH
];
433 WCHAR version
[MAX_PATH
];
434 DWORD version_size
, size
;
437 TRACE("%p %p %p\n", iface
, pwzBuffer
, pcchBuffer
);
439 if (!get_install_root(system_dir
))
441 ERR("error reading registry key for installroot\n");
446 version_size
= MAX_PATH
;
447 ICLRRuntimeInfo_GetVersionString(iface
, version
, &version_size
);
448 lstrcatW(system_dir
, version
);
449 lstrcatW(system_dir
, slash
);
450 size
= lstrlenW(system_dir
) + 1;
457 if (buffer_size
>= size
)
458 strcpyW(pwzBuffer
, system_dir
);
460 hr
= HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
466 static HRESULT WINAPI
CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo
* iface
,
467 HANDLE hndProcess
, BOOL
*pbLoaded
)
469 FIXME("%p %p %p\n", iface
, hndProcess
, pbLoaded
);
474 static HRESULT WINAPI
CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo
* iface
,
475 UINT iResourceID
, LPWSTR pwzBuffer
, DWORD
*pcchBuffer
, LONG iLocaleid
)
477 FIXME("%p %u %p %p %x\n", iface
, iResourceID
, pwzBuffer
, pcchBuffer
, iLocaleid
);
482 static HRESULT WINAPI
CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo
* iface
,
483 LPCWSTR pwzDllName
, HMODULE
*phndModule
)
485 WCHAR version
[MAX_PATH
];
489 TRACE("%p %s %p\n", iface
, debugstr_w(pwzDllName
), phndModule
);
491 cchBuffer
= MAX_PATH
;
492 hr
= ICLRRuntimeInfo_GetVersionString(iface
, version
, &cchBuffer
);
493 if (FAILED(hr
)) return hr
;
495 return LoadLibraryShim(pwzDllName
, version
, NULL
, phndModule
);
498 static HRESULT WINAPI
CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo
* iface
,
499 LPCSTR pszProcName
, LPVOID
*ppProc
)
501 FIXME("%p %s %p\n", iface
, debugstr_a(pszProcName
), ppProc
);
506 static HRESULT WINAPI
CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo
* iface
,
507 REFCLSID rclsid
, REFIID riid
, LPVOID
*ppUnk
)
509 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
513 TRACE("%p %s %s %p\n", iface
, debugstr_guid(rclsid
), debugstr_guid(riid
), ppUnk
);
515 hr
= CLRRuntimeInfo_GetRuntimeHost(This
, &host
);
518 hr
= RuntimeHost_GetInterface(host
, rclsid
, riid
, ppUnk
);
523 static HRESULT WINAPI
CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo
* iface
,
526 FIXME("%p %p\n", iface
, pbLoadable
);
531 static HRESULT WINAPI
CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo
* iface
,
532 DWORD dwStartupFlags
, LPCWSTR pwzHostConfigFile
)
534 FIXME("%p %x %s\n", iface
, dwStartupFlags
, debugstr_w(pwzHostConfigFile
));
539 static HRESULT WINAPI
CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo
* iface
,
540 DWORD
*pdwStartupFlags
, LPWSTR pwzHostConfigFile
, DWORD
*pcchHostConfigFile
)
542 FIXME("%p %p %p %p\n", iface
, pdwStartupFlags
, pwzHostConfigFile
, pcchHostConfigFile
);
547 static HRESULT WINAPI
CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo
* iface
)
549 FIXME("%p\n", iface
);
554 static HRESULT WINAPI
CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo
* iface
,
555 BOOL
*pbStarted
, DWORD
*pdwStartupFlags
)
557 FIXME("%p %p %p\n", iface
, pbStarted
, pdwStartupFlags
);
562 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl
= {
563 CLRRuntimeInfo_QueryInterface
,
564 CLRRuntimeInfo_AddRef
,
565 CLRRuntimeInfo_Release
,
566 CLRRuntimeInfo_GetVersionString
,
567 CLRRuntimeInfo_GetRuntimeDirectory
,
568 CLRRuntimeInfo_IsLoaded
,
569 CLRRuntimeInfo_LoadErrorString
,
570 CLRRuntimeInfo_LoadLibrary
,
571 CLRRuntimeInfo_GetProcAddress
,
572 CLRRuntimeInfo_GetInterface
,
573 CLRRuntimeInfo_IsLoadable
,
574 CLRRuntimeInfo_SetDefaultStartupFlags
,
575 CLRRuntimeInfo_GetDefaultStartupFlags
,
576 CLRRuntimeInfo_BindAsLegacyV2Runtime
,
577 CLRRuntimeInfo_IsStarted
580 HRESULT
ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo
*iface
, RuntimeHost
**result
)
582 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
584 assert(This
->ICLRRuntimeInfo_iface
.lpVtbl
== &CLRRuntimeInfoVtbl
);
586 return CLRRuntimeInfo_GetRuntimeHost(This
, result
);
590 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};
591 #elif defined(__x86_64__)
592 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};
594 static const WCHAR libmono2_arch_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
597 static BOOL
find_mono_dll(LPCWSTR path
, LPWSTR dll_path
, int abi_version
)
599 static const WCHAR mono_dll
[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
600 static const WCHAR libmono_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
601 static const WCHAR mono2_dll
[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
602 static const WCHAR libmono2_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
603 DWORD attributes
=INVALID_FILE_ATTRIBUTES
;
605 if (abi_version
== 1)
607 strcpyW(dll_path
, path
);
608 strcatW(dll_path
, mono_dll
);
609 attributes
= GetFileAttributesW(dll_path
);
611 if (attributes
== INVALID_FILE_ATTRIBUTES
)
613 strcpyW(dll_path
, path
);
614 strcatW(dll_path
, libmono_dll
);
615 attributes
= GetFileAttributesW(dll_path
);
618 else if (abi_version
== 2)
620 strcpyW(dll_path
, path
);
621 strcatW(dll_path
, libmono2_arch_dll
);
622 attributes
= GetFileAttributesW(dll_path
);
624 if (attributes
== INVALID_FILE_ATTRIBUTES
)
626 strcpyW(dll_path
, path
);
627 strcatW(dll_path
, mono2_dll
);
628 attributes
= GetFileAttributesW(dll_path
);
631 if (attributes
== INVALID_FILE_ATTRIBUTES
)
633 strcpyW(dll_path
, path
);
634 strcatW(dll_path
, libmono2_dll
);
635 attributes
= GetFileAttributesW(dll_path
);
639 return (attributes
!= INVALID_FILE_ATTRIBUTES
);
642 static BOOL
get_mono_path_from_registry(LPWSTR path
, int abi_version
)
644 static const WCHAR mono_key
[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
645 static const WCHAR defaul_clr
[] = {'D','e','f','a','u','l','t','C','L','R',0};
646 static const WCHAR install_root
[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
647 static const WCHAR slash
[] = {'\\',0};
649 WCHAR version
[64], version_key
[MAX_PATH
];
652 WCHAR dll_path
[MAX_PATH
];
654 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, mono_key
, 0, KEY_READ
, &key
))
657 len
= sizeof(version
);
658 if (RegQueryValueExW(key
, defaul_clr
, 0, NULL
, (LPBYTE
)version
, &len
))
665 lstrcpyW(version_key
, mono_key
);
666 lstrcatW(version_key
, slash
);
667 lstrcatW(version_key
, version
);
669 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, version_key
, 0, KEY_READ
, &key
))
672 len
= sizeof(WCHAR
) * MAX_PATH
;
673 if (RegQueryValueExW(key
, install_root
, 0, NULL
, (LPBYTE
)path
, &len
))
680 return find_mono_dll(path
, dll_path
, abi_version
);
683 static BOOL
get_mono_path_from_folder(LPCWSTR folder
, LPWSTR mono_path
, int abi_version
)
685 static const WCHAR mono_one_dot_zero
[] = {'\\','m','o','n','o','-','1','.','0', 0};
686 static const WCHAR mono_two_dot_zero
[] = {'\\','m','o','n','o','-','2','.','0', 0};
687 WCHAR mono_dll_path
[MAX_PATH
];
690 strcpyW(mono_path
, folder
);
692 if (abi_version
== 1)
693 strcatW(mono_path
, mono_one_dot_zero
);
694 else if (abi_version
== 2)
695 strcatW(mono_path
, mono_two_dot_zero
);
697 found
= find_mono_dll(mono_path
, mono_dll_path
, abi_version
);
702 static BOOL
get_mono_path(LPWSTR path
, int abi_version
)
704 static const WCHAR subdir_mono
[] = {'\\','m','o','n','o',0};
705 static const WCHAR sibling_mono
[] = {'\\','.','.','\\','m','o','n','o',0};
706 WCHAR base_path
[MAX_PATH
];
707 const char *unix_data_dir
;
710 static WCHAR
* (CDECL
*wine_get_dos_file_name
)(const char*);
712 /* First try c:\windows\mono */
713 GetWindowsDirectoryW(base_path
, MAX_PATH
);
714 strcatW(base_path
, subdir_mono
);
716 if (get_mono_path_from_folder(base_path
, path
, abi_version
))
719 /* Next: /usr/share/wine/mono */
720 unix_data_dir
= wine_get_data_dir();
724 unix_data_dir
= wine_get_build_dir();
730 if (!wine_get_dos_file_name
)
731 wine_get_dos_file_name
= (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
733 if (wine_get_dos_file_name
)
735 dos_data_dir
= wine_get_dos_file_name(unix_data_dir
);
739 strcpyW(base_path
, dos_data_dir
);
740 strcatW(base_path
, build_tree
? sibling_mono
: subdir_mono
);
742 HeapFree(GetProcessHeap(), 0, dos_data_dir
);
744 if (get_mono_path_from_folder(base_path
, path
, abi_version
))
750 /* Last: the registry */
751 return get_mono_path_from_registry(path
, abi_version
);
754 static void find_runtimes(void)
757 static const WCHAR libmono
[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
758 static const WCHAR mscorlib
[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
759 WCHAR mono_path
[MAX_PATH
], lib_path
[MAX_PATH
];
760 BOOL any_runtimes_found
= FALSE
;
762 if (runtimes_initialized
) return;
764 EnterCriticalSection(&runtime_list_cs
);
766 if (runtimes_initialized
) goto end
;
768 for (abi_version
=NUM_ABI_VERSIONS
; abi_version
>0; abi_version
--)
770 if (!get_mono_path(mono_path
, abi_version
))
773 for (i
=0; i
<NUM_RUNTIMES
; i
++)
775 if (runtimes
[i
].mono_abi_version
== 0)
777 strcpyW(lib_path
, mono_path
);
778 strcatW(lib_path
, libmono
);
779 strcatW(lib_path
, runtimes
[i
].mono_libdir
);
780 strcatW(lib_path
, mscorlib
);
782 if (GetFileAttributesW(lib_path
) != INVALID_FILE_ATTRIBUTES
)
784 runtimes
[i
].mono_abi_version
= abi_version
;
786 strcpyW(runtimes
[i
].mono_path
, mono_path
);
787 strcpyW(runtimes
[i
].mscorlib_path
, lib_path
);
789 any_runtimes_found
= TRUE
;
795 if (!any_runtimes_found
)
797 /* Report all runtimes are available if Mono isn't installed.
798 * FIXME: Remove this when Mono is properly packaged. */
799 for (i
=0; i
<NUM_RUNTIMES
; i
++)
800 runtimes
[i
].mono_abi_version
= -1;
803 runtimes_initialized
= 1;
806 LeaveCriticalSection(&runtime_list_cs
);
809 struct InstalledRuntimeEnum
811 IEnumUnknown IEnumUnknown_iface
;
816 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl
;
818 static inline struct InstalledRuntimeEnum
*impl_from_IEnumUnknown(IEnumUnknown
*iface
)
820 return CONTAINING_RECORD(iface
, struct InstalledRuntimeEnum
, IEnumUnknown_iface
);
823 static HRESULT WINAPI
InstalledRuntimeEnum_QueryInterface(IEnumUnknown
* iface
, REFIID riid
,
826 TRACE("%p %s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
828 if ( IsEqualGUID( riid
, &IID_IEnumUnknown
) ||
829 IsEqualGUID( riid
, &IID_IUnknown
) )
835 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
836 return E_NOINTERFACE
;
839 IEnumUnknown_AddRef( iface
);
844 static ULONG WINAPI
InstalledRuntimeEnum_AddRef(IEnumUnknown
* iface
)
846 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
847 ULONG ref
= InterlockedIncrement(&This
->ref
);
849 TRACE("(%p) refcount=%u\n", iface
, ref
);
854 static ULONG WINAPI
InstalledRuntimeEnum_Release(IEnumUnknown
* iface
)
856 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
857 ULONG ref
= InterlockedDecrement(&This
->ref
);
859 TRACE("(%p) refcount=%u\n", iface
, ref
);
863 HeapFree(GetProcessHeap(), 0, This
);
869 static HRESULT WINAPI
InstalledRuntimeEnum_Next(IEnumUnknown
*iface
, ULONG celt
,
870 IUnknown
**rgelt
, ULONG
*pceltFetched
)
872 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
877 TRACE("(%p,%u,%p,%p)\n", iface
, celt
, rgelt
, pceltFetched
);
879 while (num_fetched
< celt
)
881 if (This
->pos
>= NUM_RUNTIMES
)
886 if (runtimes
[This
->pos
].mono_abi_version
)
888 item
= (IUnknown
*)&runtimes
[This
->pos
].ICLRRuntimeInfo_iface
;
889 IUnknown_AddRef(item
);
890 rgelt
[num_fetched
] = item
;
897 *pceltFetched
= num_fetched
;
902 static HRESULT WINAPI
InstalledRuntimeEnum_Skip(IEnumUnknown
*iface
, ULONG celt
)
904 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
908 TRACE("(%p,%u)\n", iface
, celt
);
910 while (num_fetched
< celt
)
912 if (This
->pos
>= NUM_RUNTIMES
)
917 if (runtimes
[This
->pos
].mono_abi_version
)
927 static HRESULT WINAPI
InstalledRuntimeEnum_Reset(IEnumUnknown
*iface
)
929 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
931 TRACE("(%p)\n", iface
);
938 static HRESULT WINAPI
InstalledRuntimeEnum_Clone(IEnumUnknown
*iface
, IEnumUnknown
**ppenum
)
940 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
941 struct InstalledRuntimeEnum
*new_enum
;
943 TRACE("(%p)\n", iface
);
945 new_enum
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum
));
947 return E_OUTOFMEMORY
;
949 new_enum
->IEnumUnknown_iface
.lpVtbl
= &InstalledRuntimeEnum_Vtbl
;
951 new_enum
->pos
= This
->pos
;
953 *ppenum
= &new_enum
->IEnumUnknown_iface
;
958 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl
= {
959 InstalledRuntimeEnum_QueryInterface
,
960 InstalledRuntimeEnum_AddRef
,
961 InstalledRuntimeEnum_Release
,
962 InstalledRuntimeEnum_Next
,
963 InstalledRuntimeEnum_Skip
,
964 InstalledRuntimeEnum_Reset
,
965 InstalledRuntimeEnum_Clone
970 ICLRMetaHost ICLRMetaHost_iface
;
973 static struct CLRMetaHost GlobalCLRMetaHost
;
975 static HRESULT WINAPI
CLRMetaHost_QueryInterface(ICLRMetaHost
* iface
,
979 TRACE("%s %p\n", debugstr_guid(riid
), ppvObject
);
981 if ( IsEqualGUID( riid
, &IID_ICLRMetaHost
) ||
982 IsEqualGUID( riid
, &IID_IUnknown
) )
988 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
989 return E_NOINTERFACE
;
992 ICLRMetaHost_AddRef( iface
);
997 static ULONG WINAPI
CLRMetaHost_AddRef(ICLRMetaHost
* iface
)
1002 static ULONG WINAPI
CLRMetaHost_Release(ICLRMetaHost
* iface
)
1007 static BOOL
parse_runtime_version(LPCWSTR version
, DWORD
*major
, DWORD
*minor
, DWORD
*build
)
1013 if (version
[0] == 'v' || version
[0] == 'V')
1016 if (!isdigit(*version
))
1019 while (isdigit(*version
))
1020 *major
= *major
* 10 + (*version
++ - '0');
1025 if (*version
++ != '.' || !isdigit(*version
))
1028 while (isdigit(*version
))
1029 *minor
= *minor
* 10 + (*version
++ - '0');
1034 if (*version
++ != '.' || !isdigit(*version
))
1037 while (isdigit(*version
))
1038 *build
= *build
* 10 + (*version
++ - '0');
1040 return *version
== 0;
1046 HRESULT WINAPI
CLRMetaHost_GetRuntime(ICLRMetaHost
* iface
,
1047 LPCWSTR pwzVersion
, REFIID iid
, LPVOID
*ppRuntime
)
1050 DWORD major
, minor
, build
;
1052 TRACE("%s %s %p\n", debugstr_w(pwzVersion
), debugstr_guid(iid
), ppRuntime
);
1057 if (!parse_runtime_version(pwzVersion
, &major
, &minor
, &build
))
1059 ERR("Cannot parse %s\n", debugstr_w(pwzVersion
));
1060 return CLR_E_SHIM_RUNTIME
;
1065 for (i
=0; i
<NUM_RUNTIMES
; i
++)
1067 if (runtimes
[i
].major
== major
&& runtimes
[i
].minor
== minor
&&
1068 runtimes
[i
].build
== build
)
1070 if (runtimes
[i
].mono_abi_version
)
1071 return ICLRRuntimeInfo_QueryInterface(&runtimes
[i
].ICLRRuntimeInfo_iface
, iid
,
1075 missing_runtime_message();
1076 return CLR_E_SHIM_RUNTIME
;
1081 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion
));
1082 return CLR_E_SHIM_RUNTIME
;
1085 HRESULT WINAPI
CLRMetaHost_GetVersionFromFile(ICLRMetaHost
* iface
,
1086 LPCWSTR pwzFilePath
, LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
1091 ULONG buffer_size
=*pcchBuffer
;
1093 TRACE("%s %p %p\n", debugstr_w(pwzFilePath
), pwzBuffer
, pcchBuffer
);
1095 hr
= assembly_create(&assembly
, pwzFilePath
);
1099 hr
= assembly_get_runtime_version(assembly
, &version
);
1103 *pcchBuffer
= MultiByteToWideChar(CP_UTF8
, 0, version
, -1, NULL
, 0);
1107 if (buffer_size
>= *pcchBuffer
)
1108 MultiByteToWideChar(CP_UTF8
, 0, version
, -1, pwzBuffer
, buffer_size
);
1110 hr
= HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
1114 assembly_release(assembly
);
1120 static HRESULT WINAPI
CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost
* iface
,
1121 IEnumUnknown
**ppEnumerator
)
1123 struct InstalledRuntimeEnum
*new_enum
;
1125 TRACE("%p\n", ppEnumerator
);
1129 new_enum
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum
));
1131 return E_OUTOFMEMORY
;
1133 new_enum
->IEnumUnknown_iface
.lpVtbl
= &InstalledRuntimeEnum_Vtbl
;
1137 *ppEnumerator
= &new_enum
->IEnumUnknown_iface
;
1142 static HRESULT WINAPI
CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost
* iface
,
1143 HANDLE hndProcess
, IEnumUnknown
**ppEnumerator
)
1145 FIXME("%p %p\n", hndProcess
, ppEnumerator
);
1150 static HRESULT WINAPI
CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost
* iface
,
1151 RuntimeLoadedCallbackFnPtr pCallbackFunction
)
1153 FIXME("%p\n", pCallbackFunction
);
1158 static HRESULT WINAPI
CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost
* iface
,
1159 REFIID riid
, LPVOID
*ppUnk
)
1161 FIXME("%s %p\n", debugstr_guid(riid
), ppUnk
);
1166 static HRESULT WINAPI
CLRMetaHost_ExitProcess(ICLRMetaHost
* iface
, INT32 iExitCode
)
1168 FIXME("%i: stub\n", iExitCode
);
1170 ExitProcess(iExitCode
);
1173 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl
=
1175 CLRMetaHost_QueryInterface
,
1177 CLRMetaHost_Release
,
1178 CLRMetaHost_GetRuntime
,
1179 CLRMetaHost_GetVersionFromFile
,
1180 CLRMetaHost_EnumerateInstalledRuntimes
,
1181 CLRMetaHost_EnumerateLoadedRuntimes
,
1182 CLRMetaHost_RequestRuntimeLoadedNotification
,
1183 CLRMetaHost_QueryLegacyV2RuntimeBinding
,
1184 CLRMetaHost_ExitProcess
1187 static struct CLRMetaHost GlobalCLRMetaHost
= {
1188 { &CLRMetaHost_vtbl
}
1191 HRESULT
CLRMetaHost_CreateInstance(REFIID riid
, void **ppobj
)
1193 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost
.ICLRMetaHost_iface
, riid
, ppobj
);
1196 static MonoAssembly
* mono_assembly_search_hook_fn(MonoAssemblyName
*aname
, char **assemblies_path
, void *user_data
)
1198 loaded_mono
*mono
= user_data
;
1200 MonoAssembly
*result
=NULL
;
1201 char *stringname
=NULL
;
1203 int stringnameW_size
;
1204 IAssemblyCache
*asmcache
;
1206 WCHAR path
[MAX_PATH
];
1208 MonoImageOpenStatus stat
;
1209 static WCHAR fusiondll
[] = {'f','u','s','i','o','n',0};
1210 HMODULE hfusion
=NULL
;
1211 static HRESULT (WINAPI
*pCreateAssemblyCache
)(IAssemblyCache
**,DWORD
);
1213 stringname
= mono
->mono_stringify_assembly_name(aname
);
1215 TRACE("%s\n", debugstr_a(stringname
));
1217 if (!stringname
) return NULL
;
1219 /* FIXME: We should search the given paths before the GAC. */
1221 if (!pCreateAssemblyCache
)
1223 hr
= LoadLibraryShim(fusiondll
, NULL
, NULL
, &hfusion
);
1227 pCreateAssemblyCache
= (void*)GetProcAddress(hfusion
, "CreateAssemblyCache");
1228 if (!pCreateAssemblyCache
)
1234 hr
= pCreateAssemblyCache(&asmcache
, 0);
1238 stringnameW_size
= MultiByteToWideChar(CP_UTF8
, 0, stringname
, -1, NULL
, 0);
1240 stringnameW
= HeapAlloc(GetProcessHeap(), 0, stringnameW_size
* sizeof(WCHAR
));
1242 MultiByteToWideChar(CP_UTF8
, 0, stringname
, -1, stringnameW
, stringnameW_size
);
1248 info
.cbAssemblyInfo
= sizeof(info
);
1249 info
.pszCurrentAssemblyPathBuf
= path
;
1250 info
.cchBuf
= MAX_PATH
;
1253 hr
= IAssemblyCache_QueryAssemblyInfo(asmcache
, 0, stringnameW
, &info
);
1256 HeapFree(GetProcessHeap(), 0, stringnameW
);
1258 IAssemblyCache_Release(asmcache
);
1263 TRACE("found: %s\n", debugstr_w(path
));
1269 result
= mono
->mono_assembly_open(pathA
, &stat
);
1272 ERR("Failed to load %s, status=%u\n", debugstr_w(path
), stat
);
1274 HeapFree(GetProcessHeap(), 0, pathA
);
1278 mono
->mono_free(stringname
);
1283 HRESULT
get_runtime_info(LPCWSTR exefile
, LPCWSTR version
, LPCWSTR config_file
,
1284 DWORD startup_flags
, DWORD runtimeinfo_flags
, BOOL legacy
, ICLRRuntimeInfo
**result
)
1286 static const WCHAR dotconfig
[] = {'.','c','o','n','f','i','g',0};
1287 static const DWORD supported_startup_flags
= 0;
1288 static const DWORD supported_runtime_flags
= RUNTIME_INFO_UPGRADE_VERSION
;
1290 WCHAR local_version
[MAX_PATH
];
1291 ULONG local_version_size
= MAX_PATH
;
1292 WCHAR local_config_file
[MAX_PATH
];
1294 parsed_config_file parsed_config
;
1296 if (startup_flags
& ~supported_startup_flags
)
1297 FIXME("unsupported startup flags %x\n", startup_flags
& ~supported_startup_flags
);
1299 if (runtimeinfo_flags
& ~supported_runtime_flags
)
1300 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags
& ~supported_runtime_flags
);
1302 if (exefile
&& !config_file
)
1304 strcpyW(local_config_file
, exefile
);
1305 strcatW(local_config_file
, dotconfig
);
1307 config_file
= local_config_file
;
1313 hr
= parse_config_file(config_file
, &parsed_config
);
1317 supported_runtime
*entry
;
1318 LIST_FOR_EACH_ENTRY(entry
, &parsed_config
.supported_runtimes
, supported_runtime
, entry
)
1320 hr
= CLRMetaHost_GetRuntime(0, entry
->version
, &IID_ICLRRuntimeInfo
, (void**)result
);
1330 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file
), hr
);
1333 free_parsed_config_file(&parsed_config
);
1339 if (exefile
&& !version
)
1341 hr
= CLRMetaHost_GetVersionFromFile(0, exefile
, local_version
, &local_version_size
);
1343 version
= local_version
;
1345 if (FAILED(hr
)) return hr
;
1350 hr
= CLRMetaHost_GetRuntime(0, version
, &IID_ICLRRuntimeInfo
, (void**)result
);
1355 if (runtimeinfo_flags
& RUNTIME_INFO_UPGRADE_VERSION
)
1357 DWORD major
, minor
, build
;
1359 if (version
&& !parse_runtime_version(version
, &major
, &minor
, &build
))
1361 ERR("Cannot parse %s\n", debugstr_w(version
));
1362 return CLR_E_SHIM_RUNTIME
;
1374 if (runtimes
[i
].mono_abi_version
)
1376 /* Must be greater or equal to the version passed in. */
1377 if (!version
|| ((runtimes
[i
].major
>= major
&& runtimes
[i
].minor
>= minor
&& runtimes
[i
].build
>= build
) ||
1378 (runtimes
[i
].major
>= major
&& runtimes
[i
].minor
> minor
) ||
1379 (runtimes
[i
].major
> major
)))
1381 return ICLRRuntimeInfo_QueryInterface(&runtimes
[i
].ICLRRuntimeInfo_iface
,
1382 &IID_ICLRRuntimeInfo
, (void **)result
);
1387 missing_runtime_message();
1389 return CLR_E_SHIM_RUNTIME
;
1392 return CLR_E_SHIM_RUNTIME
;