2 * ICLRMetaHost - discovery and management of available .NET runtimes
4 * Copyright 2010 Vincent Povirk for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "mscoree_private.h"
26 #include <wine/library.h>
30 static const WCHAR net_11_subdir
[] = {'1','.','0',0};
31 static const WCHAR net_20_subdir
[] = {'2','.','0',0};
32 static const WCHAR net_40_subdir
[] = {'4','.','0',0};
34 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl
;
36 #define NUM_RUNTIMES 3
38 static struct CLRRuntimeInfo runtimes
[NUM_RUNTIMES
] = {
39 {{&CLRRuntimeInfoVtbl
}, net_11_subdir
, 1, 1, 4322, 0},
40 {{&CLRRuntimeInfoVtbl
}, net_20_subdir
, 2, 0, 50727, 0},
41 {{&CLRRuntimeInfoVtbl
}, net_40_subdir
, 4, 0, 30319, 0}
44 static int runtimes_initialized
;
46 static CRITICAL_SECTION runtime_list_cs
;
47 static CRITICAL_SECTION_DEBUG runtime_list_cs_debug
=
49 0, 0, &runtime_list_cs
,
50 { &runtime_list_cs_debug
.ProcessLocksList
,
51 &runtime_list_cs_debug
.ProcessLocksList
},
52 0, 0, { (DWORD_PTR
)(__FILE__
": runtime_list_cs") }
54 static CRITICAL_SECTION runtime_list_cs
= { &runtime_list_cs_debug
, -1, 0, 0, 0, 0 };
56 #define NUM_ABI_VERSIONS 2
58 static loaded_mono loaded_monos
[NUM_ABI_VERSIONS
];
60 static BOOL
find_mono_dll(LPCWSTR path
, LPWSTR dll_path
, int abi_version
);
62 static MonoAssembly
* mono_assembly_search_hook_fn(MonoAssemblyName
*aname
, char **assemblies_path
, void *user_data
);
64 static void mono_shutdown_callback_fn(MonoProfiler
*prof
);
66 static void set_environment(LPCWSTR bin_path
)
68 WCHAR path_env
[MAX_PATH
];
71 static const WCHAR pathW
[] = {'P','A','T','H',0};
73 /* We have to modify PATH as Mono loads other DLLs from this directory. */
74 GetEnvironmentVariableW(pathW
, path_env
, sizeof(path_env
)/sizeof(WCHAR
));
75 len
= strlenW(path_env
);
76 path_env
[len
++] = ';';
77 strcpyW(path_env
+len
, bin_path
);
78 SetEnvironmentVariableW(pathW
, path_env
);
81 static void CDECL
do_nothing(void)
85 static void missing_runtime_message(const CLRRuntimeInfo
*This
)
88 MESSAGE("wine: Install Mono 2.6 for Windows to run .NET 1.1 applications.\n");
89 else if (This
->major
== 2)
90 MESSAGE("wine: Install Mono for Windows to run .NET 2.0 applications.\n");
91 else if (This
->major
== 4)
92 MESSAGE("wine: Install Mono 2.8 or greater for Windows to run .NET 4.0 applications.\n");
95 static HRESULT
load_mono(CLRRuntimeInfo
*This
, loaded_mono
**result
)
97 static const WCHAR bin
[] = {'\\','b','i','n',0};
98 static const WCHAR lib
[] = {'\\','l','i','b',0};
99 static const WCHAR etc
[] = {'\\','e','t','c',0};
100 static const WCHAR glibdll
[] = {'l','i','b','g','l','i','b','-','2','.','0','-','0','.','d','l','l',0};
101 WCHAR mono_dll_path
[MAX_PATH
+16], mono_bin_path
[MAX_PATH
+4];
102 WCHAR mono_lib_path
[MAX_PATH
+4], mono_etc_path
[MAX_PATH
+4];
103 char mono_lib_path_a
[MAX_PATH
], mono_etc_path_a
[MAX_PATH
];
105 char trace_setting
[256];
107 if (This
->mono_abi_version
<= 0 || This
->mono_abi_version
> NUM_ABI_VERSIONS
)
109 missing_runtime_message(This
);
113 *result
= &loaded_monos
[This
->mono_abi_version
-1];
115 if ((*result
)->is_shutdown
)
117 ERR("Cannot load Mono after it has been shut down.\n");
122 if (!(*result
)->mono_handle
)
124 strcpyW(mono_bin_path
, This
->mono_path
);
125 strcatW(mono_bin_path
, bin
);
126 set_environment(mono_bin_path
);
128 strcpyW(mono_lib_path
, This
->mono_path
);
129 strcatW(mono_lib_path
, lib
);
130 WideCharToMultiByte(CP_UTF8
, 0, mono_lib_path
, -1, mono_lib_path_a
, MAX_PATH
, NULL
, NULL
);
132 strcpyW(mono_etc_path
, This
->mono_path
);
133 strcatW(mono_etc_path
, etc
);
134 WideCharToMultiByte(CP_UTF8
, 0, mono_etc_path
, -1, mono_etc_path_a
, MAX_PATH
, NULL
, NULL
);
136 if (!find_mono_dll(This
->mono_path
, mono_dll_path
, This
->mono_abi_version
)) goto fail
;
138 (*result
)->mono_handle
= LoadLibraryW(mono_dll_path
);
140 if (!(*result
)->mono_handle
) goto fail
;
142 #define LOAD_MONO_FUNCTION(x) do { \
143 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
144 if (!(*result)->x) { \
149 LOAD_MONO_FUNCTION(mono_assembly_get_image
);
150 LOAD_MONO_FUNCTION(mono_assembly_load_from
);
151 LOAD_MONO_FUNCTION(mono_assembly_open
);
152 LOAD_MONO_FUNCTION(mono_config_parse
);
153 LOAD_MONO_FUNCTION(mono_class_from_mono_type
);
154 LOAD_MONO_FUNCTION(mono_class_from_name
);
155 LOAD_MONO_FUNCTION(mono_class_get_method_from_name
);
156 LOAD_MONO_FUNCTION(mono_domain_assembly_open
);
157 LOAD_MONO_FUNCTION(mono_image_open_from_module_handle
);
158 LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook
);
159 LOAD_MONO_FUNCTION(mono_jit_exec
);
160 LOAD_MONO_FUNCTION(mono_jit_init
);
161 LOAD_MONO_FUNCTION(mono_jit_set_trace_options
);
162 LOAD_MONO_FUNCTION(mono_marshal_get_vtfixup_ftnptr
);
163 LOAD_MONO_FUNCTION(mono_object_get_domain
);
164 LOAD_MONO_FUNCTION(mono_object_new
);
165 LOAD_MONO_FUNCTION(mono_object_unbox
);
166 LOAD_MONO_FUNCTION(mono_profiler_install
);
167 LOAD_MONO_FUNCTION(mono_reflection_type_from_name
);
168 LOAD_MONO_FUNCTION(mono_runtime_invoke
);
169 LOAD_MONO_FUNCTION(mono_runtime_object_init
);
170 LOAD_MONO_FUNCTION(mono_runtime_quit
);
171 LOAD_MONO_FUNCTION(mono_set_dirs
);
172 LOAD_MONO_FUNCTION(mono_stringify_assembly_name
);
173 LOAD_MONO_FUNCTION(mono_string_new
);
174 LOAD_MONO_FUNCTION(mono_thread_attach
);
176 /* GLib imports obsoleted by the 2.0 ABI */
177 if (This
->mono_abi_version
== 1)
179 (*result
)->glib_handle
= LoadLibraryW(glibdll
);
180 if (!(*result
)->glib_handle
) goto fail
;
182 (*result
)->mono_free
= (void*)GetProcAddress((*result
)->glib_handle
, "g_free");
183 if (!(*result
)->mono_free
) goto fail
;
187 LOAD_MONO_FUNCTION(mono_free
);
190 #undef LOAD_MONO_FUNCTION
192 #define LOAD_OPT_VOID_MONO_FUNCTION(x) do { \
193 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
194 if (!(*result)->x) { \
195 (*result)->x = do_nothing; \
199 LOAD_OPT_VOID_MONO_FUNCTION(mono_runtime_set_shutting_down
);
200 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_pool_cleanup
);
201 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_suspend_all_other_threads
);
202 LOAD_OPT_VOID_MONO_FUNCTION(mono_threads_set_shutting_down
);
204 #undef LOAD_OPT_VOID_MONO_FUNCTION
206 (*result
)->mono_profiler_install((MonoProfiler
*)*result
, mono_shutdown_callback_fn
);
208 (*result
)->mono_set_dirs(mono_lib_path_a
, mono_etc_path_a
);
210 (*result
)->mono_config_parse(NULL
);
212 (*result
)->mono_install_assembly_preload_hook(mono_assembly_search_hook_fn
, *result
);
214 trace_size
= GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting
, sizeof(trace_setting
));
218 (*result
)->mono_jit_set_trace_options(trace_setting
);
225 ERR("Could not load Mono into this process\n");
226 FreeLibrary((*result
)->mono_handle
);
227 FreeLibrary((*result
)->glib_handle
);
228 (*result
)->mono_handle
= NULL
;
229 (*result
)->glib_handle
= NULL
;
233 static void mono_shutdown_callback_fn(MonoProfiler
*prof
)
235 loaded_mono
*mono
= (loaded_mono
*)prof
;
237 mono
->is_shutdown
= TRUE
;
240 static HRESULT
CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo
*This
, RuntimeHost
**result
)
243 loaded_mono
*ploaded_mono
;
245 if (This
->loaded_runtime
)
247 *result
= This
->loaded_runtime
;
251 EnterCriticalSection(&runtime_list_cs
);
253 hr
= load_mono(This
, &ploaded_mono
);
256 hr
= RuntimeHost_Construct(This
, ploaded_mono
, &This
->loaded_runtime
);
258 LeaveCriticalSection(&runtime_list_cs
);
261 *result
= This
->loaded_runtime
;
266 void unload_all_runtimes(void)
270 for (i
=0; i
<NUM_ABI_VERSIONS
; i
++)
272 loaded_mono
*mono
= &loaded_monos
[i
];
273 if (mono
->mono_handle
&& mono
->is_started
&& !mono
->is_shutdown
)
275 /* Copied from Mono's ves_icall_System_Environment_Exit */
276 mono
->mono_threads_set_shutting_down();
277 mono
->mono_runtime_set_shutting_down();
278 mono
->mono_thread_pool_cleanup();
279 mono
->mono_thread_suspend_all_other_threads();
280 mono
->mono_runtime_quit();
284 for (i
=0; i
<NUM_RUNTIMES
; i
++)
285 if (runtimes
[i
].loaded_runtime
)
286 RuntimeHost_Destroy(runtimes
[i
].loaded_runtime
);
289 void expect_no_runtimes(void)
293 for (i
=0; i
<NUM_ABI_VERSIONS
; i
++)
295 loaded_mono
*mono
= &loaded_monos
[i
];
296 if (mono
->mono_handle
&& mono
->is_started
&& !mono
->is_shutdown
)
298 ERR("Process exited with a Mono runtime loaded.\n");
304 static inline CLRRuntimeInfo
*impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo
*iface
)
306 return CONTAINING_RECORD(iface
, CLRRuntimeInfo
, ICLRRuntimeInfo_iface
);
309 static HRESULT WINAPI
CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo
* iface
,
313 TRACE("%p %s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
315 if ( IsEqualGUID( riid
, &IID_ICLRRuntimeInfo
) ||
316 IsEqualGUID( riid
, &IID_IUnknown
) )
322 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
323 return E_NOINTERFACE
;
326 ICLRRuntimeInfo_AddRef( iface
);
331 static ULONG WINAPI
CLRRuntimeInfo_AddRef(ICLRRuntimeInfo
* iface
)
336 static ULONG WINAPI
CLRRuntimeInfo_Release(ICLRRuntimeInfo
* iface
)
341 static HRESULT WINAPI
CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo
* iface
,
342 LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
344 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
345 DWORD buffer_size
= *pcchBuffer
;
350 TRACE("%p %p %p\n", iface
, pwzBuffer
, pcchBuffer
);
352 size
= snprintf(version
, sizeof(version
), "v%u.%u.%u", This
->major
, This
->minor
, This
->build
);
354 assert(size
<= sizeof(version
));
356 *pcchBuffer
= MultiByteToWideChar(CP_UTF8
, 0, version
, -1, NULL
, 0);
360 if (buffer_size
>= *pcchBuffer
)
361 MultiByteToWideChar(CP_UTF8
, 0, version
, -1, pwzBuffer
, buffer_size
);
363 hr
= HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
369 static BOOL
get_install_root(LPWSTR install_dir
)
371 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};
372 const WCHAR install_root
[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
377 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, dotnet_key
, 0, KEY_READ
, &key
))
381 if (RegQueryValueExW(key
, install_root
, 0, NULL
, (LPBYTE
)install_dir
, &len
))
391 static HRESULT WINAPI
CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo
* iface
,
392 LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
394 static const WCHAR slash
[] = {'\\',0};
395 DWORD buffer_size
= *pcchBuffer
;
396 WCHAR system_dir
[MAX_PATH
];
397 WCHAR version
[MAX_PATH
];
398 DWORD version_size
, size
;
401 TRACE("%p %p %p\n", iface
, pwzBuffer
, pcchBuffer
);
403 if (!get_install_root(system_dir
))
405 ERR("error reading registry key for installroot\n");
410 version_size
= MAX_PATH
;
411 ICLRRuntimeInfo_GetVersionString(iface
, version
, &version_size
);
412 lstrcatW(system_dir
, version
);
413 lstrcatW(system_dir
, slash
);
414 size
= lstrlenW(system_dir
) + 1;
421 if (buffer_size
>= size
)
422 strcpyW(pwzBuffer
, system_dir
);
424 hr
= HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
430 static HRESULT WINAPI
CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo
* iface
,
431 HANDLE hndProcess
, BOOL
*pbLoaded
)
433 FIXME("%p %p %p\n", iface
, hndProcess
, pbLoaded
);
438 static HRESULT WINAPI
CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo
* iface
,
439 UINT iResourceID
, LPWSTR pwzBuffer
, DWORD
*pcchBuffer
, LONG iLocaleid
)
441 FIXME("%p %u %p %p %x\n", iface
, iResourceID
, pwzBuffer
, pcchBuffer
, iLocaleid
);
446 static HRESULT WINAPI
CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo
* iface
,
447 LPCWSTR pwzDllName
, HMODULE
*phndModule
)
449 WCHAR version
[MAX_PATH
];
453 TRACE("%p %s %p\n", iface
, debugstr_w(pwzDllName
), phndModule
);
455 cchBuffer
= MAX_PATH
;
456 hr
= ICLRRuntimeInfo_GetVersionString(iface
, version
, &cchBuffer
);
457 if (FAILED(hr
)) return hr
;
459 return LoadLibraryShim(pwzDllName
, version
, NULL
, phndModule
);
462 static HRESULT WINAPI
CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo
* iface
,
463 LPCSTR pszProcName
, LPVOID
*ppProc
)
465 FIXME("%p %s %p\n", iface
, debugstr_a(pszProcName
), ppProc
);
470 static HRESULT WINAPI
CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo
* iface
,
471 REFCLSID rclsid
, REFIID riid
, LPVOID
*ppUnk
)
473 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
477 TRACE("%p %s %s %p\n", iface
, debugstr_guid(rclsid
), debugstr_guid(riid
), ppUnk
);
479 hr
= CLRRuntimeInfo_GetRuntimeHost(This
, &host
);
482 hr
= RuntimeHost_GetInterface(host
, rclsid
, riid
, ppUnk
);
487 static HRESULT WINAPI
CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo
* iface
,
490 FIXME("%p %p\n", iface
, pbLoadable
);
495 static HRESULT WINAPI
CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo
* iface
,
496 DWORD dwStartupFlags
, LPCWSTR pwzHostConfigFile
)
498 FIXME("%p %x %s\n", iface
, dwStartupFlags
, debugstr_w(pwzHostConfigFile
));
503 static HRESULT WINAPI
CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo
* iface
,
504 DWORD
*pdwStartupFlags
, LPWSTR pwzHostConfigFile
, DWORD
*pcchHostConfigFile
)
506 FIXME("%p %p %p %p\n", iface
, pdwStartupFlags
, pwzHostConfigFile
, pcchHostConfigFile
);
511 static HRESULT WINAPI
CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo
* iface
)
513 FIXME("%p\n", iface
);
518 static HRESULT WINAPI
CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo
* iface
,
519 BOOL
*pbStarted
, DWORD
*pdwStartupFlags
)
521 FIXME("%p %p %p\n", iface
, pbStarted
, pdwStartupFlags
);
526 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl
= {
527 CLRRuntimeInfo_QueryInterface
,
528 CLRRuntimeInfo_AddRef
,
529 CLRRuntimeInfo_Release
,
530 CLRRuntimeInfo_GetVersionString
,
531 CLRRuntimeInfo_GetRuntimeDirectory
,
532 CLRRuntimeInfo_IsLoaded
,
533 CLRRuntimeInfo_LoadErrorString
,
534 CLRRuntimeInfo_LoadLibrary
,
535 CLRRuntimeInfo_GetProcAddress
,
536 CLRRuntimeInfo_GetInterface
,
537 CLRRuntimeInfo_IsLoadable
,
538 CLRRuntimeInfo_SetDefaultStartupFlags
,
539 CLRRuntimeInfo_GetDefaultStartupFlags
,
540 CLRRuntimeInfo_BindAsLegacyV2Runtime
,
541 CLRRuntimeInfo_IsStarted
544 HRESULT
ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo
*iface
, RuntimeHost
**result
)
546 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
548 assert(This
->ICLRRuntimeInfo_iface
.lpVtbl
== &CLRRuntimeInfoVtbl
);
550 return CLRRuntimeInfo_GetRuntimeHost(This
, result
);
554 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};
555 #elif defined(__x86_64__)
556 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};
558 static const WCHAR libmono2_arch_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
561 static BOOL
find_mono_dll(LPCWSTR path
, LPWSTR dll_path
, int abi_version
)
563 static const WCHAR mono_dll
[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
564 static const WCHAR libmono_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
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 if (abi_version
== 1)
571 strcpyW(dll_path
, path
);
572 strcatW(dll_path
, mono_dll
);
573 attributes
= GetFileAttributesW(dll_path
);
575 if (attributes
== INVALID_FILE_ATTRIBUTES
)
577 strcpyW(dll_path
, path
);
578 strcatW(dll_path
, libmono_dll
);
579 attributes
= GetFileAttributesW(dll_path
);
582 else if (abi_version
== 2)
584 strcpyW(dll_path
, path
);
585 strcatW(dll_path
, libmono2_arch_dll
);
586 attributes
= GetFileAttributesW(dll_path
);
588 if (attributes
== INVALID_FILE_ATTRIBUTES
)
590 strcpyW(dll_path
, path
);
591 strcatW(dll_path
, mono2_dll
);
592 attributes
= GetFileAttributesW(dll_path
);
595 if (attributes
== INVALID_FILE_ATTRIBUTES
)
597 strcpyW(dll_path
, path
);
598 strcatW(dll_path
, libmono2_dll
);
599 attributes
= GetFileAttributesW(dll_path
);
603 return (attributes
!= INVALID_FILE_ATTRIBUTES
);
606 static BOOL
get_mono_path_from_registry(LPWSTR path
, int abi_version
)
608 static const WCHAR mono_key
[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
609 static const WCHAR defaul_clr
[] = {'D','e','f','a','u','l','t','C','L','R',0};
610 static const WCHAR install_root
[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
611 static const WCHAR slash
[] = {'\\',0};
613 WCHAR version
[64], version_key
[MAX_PATH
];
616 WCHAR dll_path
[MAX_PATH
];
618 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, mono_key
, 0, KEY_READ
, &key
))
621 len
= sizeof(version
);
622 if (RegQueryValueExW(key
, defaul_clr
, 0, NULL
, (LPBYTE
)version
, &len
))
629 lstrcpyW(version_key
, mono_key
);
630 lstrcatW(version_key
, slash
);
631 lstrcatW(version_key
, version
);
633 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, version_key
, 0, KEY_READ
, &key
))
636 len
= sizeof(WCHAR
) * MAX_PATH
;
637 if (RegQueryValueExW(key
, install_root
, 0, NULL
, (LPBYTE
)path
, &len
))
644 return find_mono_dll(path
, dll_path
, abi_version
);
647 static BOOL
get_mono_path_from_folder(LPCWSTR folder
, LPWSTR mono_path
, int abi_version
)
649 static const WCHAR mono_one_dot_zero
[] = {'\\','m','o','n','o','-','1','.','0', 0};
650 static const WCHAR mono_two_dot_zero
[] = {'\\','m','o','n','o','-','2','.','0', 0};
651 WCHAR mono_dll_path
[MAX_PATH
];
654 strcpyW(mono_path
, folder
);
656 if (abi_version
== 1)
657 strcatW(mono_path
, mono_one_dot_zero
);
658 else if (abi_version
== 2)
659 strcatW(mono_path
, mono_two_dot_zero
);
661 found
= find_mono_dll(mono_path
, mono_dll_path
, abi_version
);
666 static BOOL
get_mono_path(LPWSTR path
, int abi_version
)
668 static const WCHAR subdir_mono
[] = {'\\','m','o','n','o',0};
669 static const WCHAR sibling_mono
[] = {'\\','.','.','\\','m','o','n','o',0};
670 WCHAR base_path
[MAX_PATH
];
671 const char *unix_data_dir
;
674 static WCHAR
* (CDECL
*wine_get_dos_file_name
)(const char*);
676 /* First try c:\windows\mono */
677 GetWindowsDirectoryW(base_path
, MAX_PATH
);
678 strcatW(base_path
, subdir_mono
);
680 if (get_mono_path_from_folder(base_path
, path
, abi_version
))
683 /* Next: /usr/share/wine/mono */
684 unix_data_dir
= wine_get_data_dir();
688 unix_data_dir
= wine_get_build_dir();
694 if (!wine_get_dos_file_name
)
695 wine_get_dos_file_name
= (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
697 if (wine_get_dos_file_name
)
699 dos_data_dir
= wine_get_dos_file_name(unix_data_dir
);
703 strcpyW(base_path
, dos_data_dir
);
704 strcatW(base_path
, build_tree
? sibling_mono
: subdir_mono
);
706 HeapFree(GetProcessHeap(), 0, dos_data_dir
);
708 if (get_mono_path_from_folder(base_path
, path
, abi_version
))
714 /* Last: the registry */
715 return get_mono_path_from_registry(path
, abi_version
);
718 static void find_runtimes(void)
721 static const WCHAR libmono
[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
722 static const WCHAR mscorlib
[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
723 WCHAR mono_path
[MAX_PATH
], lib_path
[MAX_PATH
];
724 BOOL any_runtimes_found
= FALSE
;
726 if (runtimes_initialized
) return;
728 EnterCriticalSection(&runtime_list_cs
);
730 if (runtimes_initialized
) goto end
;
732 for (abi_version
=NUM_ABI_VERSIONS
; abi_version
>0; abi_version
--)
734 if (!get_mono_path(mono_path
, abi_version
))
737 for (i
=0; i
<NUM_RUNTIMES
; i
++)
739 if (runtimes
[i
].mono_abi_version
== 0)
741 strcpyW(lib_path
, mono_path
);
742 strcatW(lib_path
, libmono
);
743 strcatW(lib_path
, runtimes
[i
].mono_libdir
);
744 strcatW(lib_path
, mscorlib
);
746 if (GetFileAttributesW(lib_path
) != INVALID_FILE_ATTRIBUTES
)
748 runtimes
[i
].mono_abi_version
= abi_version
;
750 strcpyW(runtimes
[i
].mono_path
, mono_path
);
751 strcpyW(runtimes
[i
].mscorlib_path
, lib_path
);
753 any_runtimes_found
= TRUE
;
759 if (!any_runtimes_found
)
761 /* Report all runtimes are available if Mono isn't installed.
762 * FIXME: Remove this when Mono is properly packaged. */
763 for (i
=0; i
<NUM_RUNTIMES
; i
++)
764 runtimes
[i
].mono_abi_version
= -1;
767 runtimes_initialized
= 1;
770 LeaveCriticalSection(&runtime_list_cs
);
773 struct InstalledRuntimeEnum
775 IEnumUnknown IEnumUnknown_iface
;
780 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl
;
782 static inline struct InstalledRuntimeEnum
*impl_from_IEnumUnknown(IEnumUnknown
*iface
)
784 return CONTAINING_RECORD(iface
, struct InstalledRuntimeEnum
, IEnumUnknown_iface
);
787 static HRESULT WINAPI
InstalledRuntimeEnum_QueryInterface(IEnumUnknown
* iface
, REFIID riid
,
790 TRACE("%p %s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
792 if ( IsEqualGUID( riid
, &IID_IEnumUnknown
) ||
793 IsEqualGUID( riid
, &IID_IUnknown
) )
799 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
800 return E_NOINTERFACE
;
803 IEnumUnknown_AddRef( iface
);
808 static ULONG WINAPI
InstalledRuntimeEnum_AddRef(IEnumUnknown
* iface
)
810 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
811 ULONG ref
= InterlockedIncrement(&This
->ref
);
813 TRACE("(%p) refcount=%u\n", iface
, ref
);
818 static ULONG WINAPI
InstalledRuntimeEnum_Release(IEnumUnknown
* iface
)
820 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
821 ULONG ref
= InterlockedDecrement(&This
->ref
);
823 TRACE("(%p) refcount=%u\n", iface
, ref
);
827 HeapFree(GetProcessHeap(), 0, This
);
833 static HRESULT WINAPI
InstalledRuntimeEnum_Next(IEnumUnknown
*iface
, ULONG celt
,
834 IUnknown
**rgelt
, ULONG
*pceltFetched
)
836 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
841 TRACE("(%p,%u,%p,%p)\n", iface
, celt
, rgelt
, pceltFetched
);
843 while (num_fetched
< celt
)
845 if (This
->pos
>= NUM_RUNTIMES
)
850 if (runtimes
[This
->pos
].mono_abi_version
)
852 item
= (IUnknown
*)&runtimes
[This
->pos
].ICLRRuntimeInfo_iface
;
853 IUnknown_AddRef(item
);
854 rgelt
[num_fetched
] = item
;
861 *pceltFetched
= num_fetched
;
866 static HRESULT WINAPI
InstalledRuntimeEnum_Skip(IEnumUnknown
*iface
, ULONG celt
)
868 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
872 TRACE("(%p,%u)\n", iface
, celt
);
874 while (num_fetched
< celt
)
876 if (This
->pos
>= NUM_RUNTIMES
)
881 if (runtimes
[This
->pos
].mono_abi_version
)
891 static HRESULT WINAPI
InstalledRuntimeEnum_Reset(IEnumUnknown
*iface
)
893 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
895 TRACE("(%p)\n", iface
);
902 static HRESULT WINAPI
InstalledRuntimeEnum_Clone(IEnumUnknown
*iface
, IEnumUnknown
**ppenum
)
904 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
905 struct InstalledRuntimeEnum
*new_enum
;
907 TRACE("(%p)\n", iface
);
909 new_enum
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum
));
911 return E_OUTOFMEMORY
;
913 new_enum
->IEnumUnknown_iface
.lpVtbl
= &InstalledRuntimeEnum_Vtbl
;
915 new_enum
->pos
= This
->pos
;
917 *ppenum
= &new_enum
->IEnumUnknown_iface
;
922 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl
= {
923 InstalledRuntimeEnum_QueryInterface
,
924 InstalledRuntimeEnum_AddRef
,
925 InstalledRuntimeEnum_Release
,
926 InstalledRuntimeEnum_Next
,
927 InstalledRuntimeEnum_Skip
,
928 InstalledRuntimeEnum_Reset
,
929 InstalledRuntimeEnum_Clone
934 ICLRMetaHost ICLRMetaHost_iface
;
937 static struct CLRMetaHost GlobalCLRMetaHost
;
939 static HRESULT WINAPI
CLRMetaHost_QueryInterface(ICLRMetaHost
* iface
,
943 TRACE("%s %p\n", debugstr_guid(riid
), ppvObject
);
945 if ( IsEqualGUID( riid
, &IID_ICLRMetaHost
) ||
946 IsEqualGUID( riid
, &IID_IUnknown
) )
952 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
953 return E_NOINTERFACE
;
956 ICLRMetaHost_AddRef( iface
);
961 static ULONG WINAPI
CLRMetaHost_AddRef(ICLRMetaHost
* iface
)
966 static ULONG WINAPI
CLRMetaHost_Release(ICLRMetaHost
* iface
)
971 static BOOL
parse_runtime_version(LPCWSTR version
, DWORD
*major
, DWORD
*minor
, DWORD
*build
)
977 if (version
[0] == 'v' || version
[0] == 'V')
980 if (!isdigit(*version
))
983 while (isdigit(*version
))
984 *major
= *major
* 10 + (*version
++ - '0');
989 if (*version
++ != '.' || !isdigit(*version
))
992 while (isdigit(*version
))
993 *minor
= *minor
* 10 + (*version
++ - '0');
998 if (*version
++ != '.' || !isdigit(*version
))
1001 while (isdigit(*version
))
1002 *build
= *build
* 10 + (*version
++ - '0');
1004 return *version
== 0;
1010 HRESULT WINAPI
CLRMetaHost_GetRuntime(ICLRMetaHost
* iface
,
1011 LPCWSTR pwzVersion
, REFIID iid
, LPVOID
*ppRuntime
)
1014 DWORD major
, minor
, build
;
1016 TRACE("%s %s %p\n", debugstr_w(pwzVersion
), debugstr_guid(iid
), ppRuntime
);
1021 if (!parse_runtime_version(pwzVersion
, &major
, &minor
, &build
))
1023 ERR("Cannot parse %s\n", debugstr_w(pwzVersion
));
1024 return CLR_E_SHIM_RUNTIME
;
1029 for (i
=0; i
<NUM_RUNTIMES
; i
++)
1031 if (runtimes
[i
].major
== major
&& runtimes
[i
].minor
== minor
&&
1032 runtimes
[i
].build
== build
)
1034 if (runtimes
[i
].mono_abi_version
)
1035 return ICLRRuntimeInfo_QueryInterface(&runtimes
[i
].ICLRRuntimeInfo_iface
, iid
,
1039 missing_runtime_message(&runtimes
[i
]);
1040 return CLR_E_SHIM_RUNTIME
;
1045 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion
));
1046 return CLR_E_SHIM_RUNTIME
;
1049 HRESULT WINAPI
CLRMetaHost_GetVersionFromFile(ICLRMetaHost
* iface
,
1050 LPCWSTR pwzFilePath
, LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
1055 ULONG buffer_size
=*pcchBuffer
;
1057 TRACE("%s %p %p\n", debugstr_w(pwzFilePath
), pwzBuffer
, pcchBuffer
);
1059 hr
= assembly_create(&assembly
, pwzFilePath
);
1063 hr
= assembly_get_runtime_version(assembly
, &version
);
1067 *pcchBuffer
= MultiByteToWideChar(CP_UTF8
, 0, version
, -1, NULL
, 0);
1071 if (buffer_size
>= *pcchBuffer
)
1072 MultiByteToWideChar(CP_UTF8
, 0, version
, -1, pwzBuffer
, buffer_size
);
1074 hr
= HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
1078 assembly_release(assembly
);
1084 static HRESULT WINAPI
CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost
* iface
,
1085 IEnumUnknown
**ppEnumerator
)
1087 struct InstalledRuntimeEnum
*new_enum
;
1089 TRACE("%p\n", ppEnumerator
);
1093 new_enum
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum
));
1095 return E_OUTOFMEMORY
;
1097 new_enum
->IEnumUnknown_iface
.lpVtbl
= &InstalledRuntimeEnum_Vtbl
;
1101 *ppEnumerator
= &new_enum
->IEnumUnknown_iface
;
1106 static HRESULT WINAPI
CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost
* iface
,
1107 HANDLE hndProcess
, IEnumUnknown
**ppEnumerator
)
1109 FIXME("%p %p\n", hndProcess
, ppEnumerator
);
1114 static HRESULT WINAPI
CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost
* iface
,
1115 RuntimeLoadedCallbackFnPtr pCallbackFunction
)
1117 FIXME("%p\n", pCallbackFunction
);
1122 static HRESULT WINAPI
CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost
* iface
,
1123 REFIID riid
, LPVOID
*ppUnk
)
1125 FIXME("%s %p\n", debugstr_guid(riid
), ppUnk
);
1130 static HRESULT WINAPI
CLRMetaHost_ExitProcess(ICLRMetaHost
* iface
, INT32 iExitCode
)
1132 FIXME("%i: stub\n", iExitCode
);
1134 ExitProcess(iExitCode
);
1137 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl
=
1139 CLRMetaHost_QueryInterface
,
1141 CLRMetaHost_Release
,
1142 CLRMetaHost_GetRuntime
,
1143 CLRMetaHost_GetVersionFromFile
,
1144 CLRMetaHost_EnumerateInstalledRuntimes
,
1145 CLRMetaHost_EnumerateLoadedRuntimes
,
1146 CLRMetaHost_RequestRuntimeLoadedNotification
,
1147 CLRMetaHost_QueryLegacyV2RuntimeBinding
,
1148 CLRMetaHost_ExitProcess
1151 static struct CLRMetaHost GlobalCLRMetaHost
= {
1152 { &CLRMetaHost_vtbl
}
1155 HRESULT
CLRMetaHost_CreateInstance(REFIID riid
, void **ppobj
)
1157 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost
.ICLRMetaHost_iface
, riid
, ppobj
);
1160 static MonoAssembly
* mono_assembly_search_hook_fn(MonoAssemblyName
*aname
, char **assemblies_path
, void *user_data
)
1162 loaded_mono
*mono
= user_data
;
1164 MonoAssembly
*result
=NULL
;
1165 char *stringname
=NULL
;
1167 int stringnameW_size
;
1168 IAssemblyCache
*asmcache
;
1170 WCHAR path
[MAX_PATH
];
1172 MonoImageOpenStatus stat
;
1173 static WCHAR fusiondll
[] = {'f','u','s','i','o','n',0};
1174 HMODULE hfusion
=NULL
;
1175 static HRESULT (WINAPI
*pCreateAssemblyCache
)(IAssemblyCache
**,DWORD
);
1177 stringname
= mono
->mono_stringify_assembly_name(aname
);
1179 TRACE("%s\n", debugstr_a(stringname
));
1181 if (!stringname
) return NULL
;
1183 /* FIXME: We should search the given paths before the GAC. */
1185 if (!pCreateAssemblyCache
)
1187 hr
= LoadLibraryShim(fusiondll
, NULL
, NULL
, &hfusion
);
1191 pCreateAssemblyCache
= (void*)GetProcAddress(hfusion
, "CreateAssemblyCache");
1192 if (!pCreateAssemblyCache
)
1198 hr
= pCreateAssemblyCache(&asmcache
, 0);
1202 stringnameW_size
= MultiByteToWideChar(CP_UTF8
, 0, stringname
, -1, NULL
, 0);
1204 stringnameW
= HeapAlloc(GetProcessHeap(), 0, stringnameW_size
* sizeof(WCHAR
));
1206 MultiByteToWideChar(CP_UTF8
, 0, stringname
, -1, stringnameW
, stringnameW_size
);
1212 info
.cbAssemblyInfo
= sizeof(info
);
1213 info
.pszCurrentAssemblyPathBuf
= path
;
1214 info
.cchBuf
= MAX_PATH
;
1217 hr
= IAssemblyCache_QueryAssemblyInfo(asmcache
, 0, stringnameW
, &info
);
1220 HeapFree(GetProcessHeap(), 0, stringnameW
);
1222 IAssemblyCache_Release(asmcache
);
1227 TRACE("found: %s\n", debugstr_w(path
));
1233 result
= mono
->mono_assembly_open(pathA
, &stat
);
1236 ERR("Failed to load %s, status=%u\n", debugstr_w(path
), stat
);
1238 HeapFree(GetProcessHeap(), 0, pathA
);
1242 mono
->mono_free(stringname
);
1247 HRESULT
get_runtime_info(LPCWSTR exefile
, LPCWSTR version
, LPCWSTR config_file
,
1248 DWORD startup_flags
, DWORD runtimeinfo_flags
, BOOL legacy
, ICLRRuntimeInfo
**result
)
1250 static const WCHAR dotconfig
[] = {'.','c','o','n','f','i','g',0};
1251 static const DWORD supported_startup_flags
= 0;
1252 static const DWORD supported_runtime_flags
= RUNTIME_INFO_UPGRADE_VERSION
;
1254 WCHAR local_version
[MAX_PATH
];
1255 ULONG local_version_size
= MAX_PATH
;
1256 WCHAR local_config_file
[MAX_PATH
];
1258 parsed_config_file parsed_config
;
1260 if (startup_flags
& ~supported_startup_flags
)
1261 FIXME("unsupported startup flags %x\n", startup_flags
& ~supported_startup_flags
);
1263 if (runtimeinfo_flags
& ~supported_runtime_flags
)
1264 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags
& ~supported_runtime_flags
);
1266 if (exefile
&& !config_file
)
1268 strcpyW(local_config_file
, exefile
);
1269 strcatW(local_config_file
, dotconfig
);
1271 config_file
= local_config_file
;
1277 hr
= parse_config_file(config_file
, &parsed_config
);
1281 supported_runtime
*entry
;
1282 LIST_FOR_EACH_ENTRY(entry
, &parsed_config
.supported_runtimes
, supported_runtime
, entry
)
1284 hr
= CLRMetaHost_GetRuntime(0, entry
->version
, &IID_ICLRRuntimeInfo
, (void**)result
);
1294 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file
), hr
);
1297 free_parsed_config_file(&parsed_config
);
1303 if (exefile
&& !version
)
1305 hr
= CLRMetaHost_GetVersionFromFile(0, exefile
, local_version
, &local_version_size
);
1307 version
= local_version
;
1309 if (FAILED(hr
)) return hr
;
1314 hr
= CLRMetaHost_GetRuntime(0, version
, &IID_ICLRRuntimeInfo
, (void**)result
);
1319 if (runtimeinfo_flags
& RUNTIME_INFO_UPGRADE_VERSION
)
1321 DWORD major
, minor
, build
;
1323 if (version
&& !parse_runtime_version(version
, &major
, &minor
, &build
))
1325 ERR("Cannot parse %s\n", debugstr_w(version
));
1326 return CLR_E_SHIM_RUNTIME
;
1338 if (runtimes
[i
].mono_abi_version
)
1340 /* Must be greater or equal to the version passed in. */
1341 if (!version
|| ((runtimes
[i
].major
>= major
&& runtimes
[i
].minor
>= minor
&& runtimes
[i
].build
>= build
) ||
1342 (runtimes
[i
].major
>= major
&& runtimes
[i
].minor
> minor
) ||
1343 (runtimes
[i
].major
> major
)))
1345 return ICLRRuntimeInfo_QueryInterface(&runtimes
[i
].ICLRRuntimeInfo_iface
,
1346 &IID_ICLRRuntimeInfo
, (void **)result
);
1352 missing_runtime_message(&runtimes
[1]);
1354 missing_runtime_message(&runtimes
[NUM_RUNTIMES
-1]);
1356 return CLR_E_SHIM_RUNTIME
;
1359 return CLR_E_SHIM_RUNTIME
;