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
);
210 LOAD_MONO_FUNCTION(mono_trace_set_assembly
);
212 /* GLib imports obsoleted by the 2.0 ABI */
213 if (This
->mono_abi_version
== 1)
215 (*result
)->glib_handle
= LoadLibraryW(glibdll
);
216 if (!(*result
)->glib_handle
) goto fail
;
218 (*result
)->mono_free
= (void*)GetProcAddress((*result
)->glib_handle
, "g_free");
219 if (!(*result
)->mono_free
) goto fail
;
223 LOAD_MONO_FUNCTION(mono_free
);
226 #undef LOAD_MONO_FUNCTION
228 #define LOAD_OPT_MONO_FUNCTION(x, default) do { \
229 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
230 if (!(*result)->x) { \
231 (*result)->x = default; \
235 LOAD_OPT_MONO_FUNCTION(mono_image_open_from_module_handle
, image_from_handle_fn
[This
->mono_abi_version
-1]);
236 LOAD_OPT_MONO_FUNCTION(mono_runtime_set_shutting_down
, do_nothing
);
237 LOAD_OPT_MONO_FUNCTION(mono_thread_pool_cleanup
, do_nothing
);
238 LOAD_OPT_MONO_FUNCTION(mono_thread_suspend_all_other_threads
, do_nothing
);
239 LOAD_OPT_MONO_FUNCTION(mono_threads_set_shutting_down
, do_nothing
);
241 #undef LOAD_OPT_MONO_FUNCTION
243 (*result
)->mono_profiler_install((MonoProfiler
*)*result
, mono_shutdown_callback_fn
);
245 (*result
)->mono_set_dirs(mono_lib_path_a
, mono_etc_path_a
);
247 (*result
)->mono_config_parse(NULL
);
249 (*result
)->mono_install_assembly_preload_hook(mono_assembly_search_hook_fn
, *result
);
251 trace_size
= GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting
, sizeof(trace_setting
));
255 (*result
)->mono_jit_set_trace_options(trace_setting
);
262 ERR("Could not load Mono into this process\n");
263 FreeLibrary((*result
)->mono_handle
);
264 FreeLibrary((*result
)->glib_handle
);
265 (*result
)->mono_handle
= NULL
;
266 (*result
)->glib_handle
= NULL
;
270 static void mono_shutdown_callback_fn(MonoProfiler
*prof
)
272 loaded_mono
*mono
= (loaded_mono
*)prof
;
274 mono
->is_shutdown
= TRUE
;
277 static HRESULT
CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo
*This
, RuntimeHost
**result
)
280 loaded_mono
*ploaded_mono
;
282 if (This
->loaded_runtime
)
284 *result
= This
->loaded_runtime
;
288 EnterCriticalSection(&runtime_list_cs
);
290 hr
= load_mono(This
, &ploaded_mono
);
293 hr
= RuntimeHost_Construct(This
, ploaded_mono
, &This
->loaded_runtime
);
295 LeaveCriticalSection(&runtime_list_cs
);
298 *result
= This
->loaded_runtime
;
303 void unload_all_runtimes(void)
307 for (i
=0; i
<NUM_ABI_VERSIONS
; i
++)
309 loaded_mono
*mono
= &loaded_monos
[i
];
310 if (mono
->mono_handle
&& mono
->is_started
&& !mono
->is_shutdown
)
312 /* Copied from Mono's ves_icall_System_Environment_Exit */
313 mono
->mono_threads_set_shutting_down();
314 mono
->mono_runtime_set_shutting_down();
315 mono
->mono_thread_pool_cleanup();
316 mono
->mono_thread_suspend_all_other_threads();
317 mono
->mono_runtime_quit();
321 for (i
=0; i
<NUM_RUNTIMES
; i
++)
322 if (runtimes
[i
].loaded_runtime
)
323 RuntimeHost_Destroy(runtimes
[i
].loaded_runtime
);
326 void expect_no_runtimes(void)
330 for (i
=0; i
<NUM_ABI_VERSIONS
; i
++)
332 loaded_mono
*mono
= &loaded_monos
[i
];
333 if (mono
->mono_handle
&& mono
->is_started
&& !mono
->is_shutdown
)
335 ERR("Process exited with a Mono runtime loaded.\n");
341 static inline CLRRuntimeInfo
*impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo
*iface
)
343 return CONTAINING_RECORD(iface
, CLRRuntimeInfo
, ICLRRuntimeInfo_iface
);
346 static HRESULT WINAPI
CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo
* iface
,
350 TRACE("%p %s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
352 if ( IsEqualGUID( riid
, &IID_ICLRRuntimeInfo
) ||
353 IsEqualGUID( riid
, &IID_IUnknown
) )
359 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
360 return E_NOINTERFACE
;
363 ICLRRuntimeInfo_AddRef( iface
);
368 static ULONG WINAPI
CLRRuntimeInfo_AddRef(ICLRRuntimeInfo
* iface
)
373 static ULONG WINAPI
CLRRuntimeInfo_Release(ICLRRuntimeInfo
* iface
)
378 static HRESULT WINAPI
CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo
* iface
,
379 LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
381 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
382 DWORD buffer_size
= *pcchBuffer
;
387 TRACE("%p %p %p\n", iface
, pwzBuffer
, pcchBuffer
);
389 size
= snprintf(version
, sizeof(version
), "v%u.%u.%u", This
->major
, This
->minor
, This
->build
);
391 assert(size
<= sizeof(version
));
393 *pcchBuffer
= MultiByteToWideChar(CP_UTF8
, 0, version
, -1, NULL
, 0);
397 if (buffer_size
>= *pcchBuffer
)
398 MultiByteToWideChar(CP_UTF8
, 0, version
, -1, pwzBuffer
, buffer_size
);
400 hr
= HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
406 static BOOL
get_install_root(LPWSTR install_dir
)
408 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};
409 const WCHAR install_root
[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
414 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, dotnet_key
, 0, KEY_READ
, &key
))
418 if (RegQueryValueExW(key
, install_root
, 0, NULL
, (LPBYTE
)install_dir
, &len
))
428 static HRESULT WINAPI
CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo
* iface
,
429 LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
431 static const WCHAR slash
[] = {'\\',0};
432 DWORD buffer_size
= *pcchBuffer
;
433 WCHAR system_dir
[MAX_PATH
];
434 WCHAR version
[MAX_PATH
];
435 DWORD version_size
, size
;
438 TRACE("%p %p %p\n", iface
, pwzBuffer
, pcchBuffer
);
440 if (!get_install_root(system_dir
))
442 ERR("error reading registry key for installroot\n");
447 version_size
= MAX_PATH
;
448 ICLRRuntimeInfo_GetVersionString(iface
, version
, &version_size
);
449 lstrcatW(system_dir
, version
);
450 lstrcatW(system_dir
, slash
);
451 size
= lstrlenW(system_dir
) + 1;
458 if (buffer_size
>= size
)
459 strcpyW(pwzBuffer
, system_dir
);
461 hr
= HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
467 static HRESULT WINAPI
CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo
* iface
,
468 HANDLE hndProcess
, BOOL
*pbLoaded
)
470 FIXME("%p %p %p\n", iface
, hndProcess
, pbLoaded
);
475 static HRESULT WINAPI
CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo
* iface
,
476 UINT iResourceID
, LPWSTR pwzBuffer
, DWORD
*pcchBuffer
, LONG iLocaleid
)
478 FIXME("%p %u %p %p %x\n", iface
, iResourceID
, pwzBuffer
, pcchBuffer
, iLocaleid
);
483 static HRESULT WINAPI
CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo
* iface
,
484 LPCWSTR pwzDllName
, HMODULE
*phndModule
)
486 WCHAR version
[MAX_PATH
];
490 TRACE("%p %s %p\n", iface
, debugstr_w(pwzDllName
), phndModule
);
492 cchBuffer
= MAX_PATH
;
493 hr
= ICLRRuntimeInfo_GetVersionString(iface
, version
, &cchBuffer
);
494 if (FAILED(hr
)) return hr
;
496 return LoadLibraryShim(pwzDllName
, version
, NULL
, phndModule
);
499 static HRESULT WINAPI
CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo
* iface
,
500 LPCSTR pszProcName
, LPVOID
*ppProc
)
502 FIXME("%p %s %p\n", iface
, debugstr_a(pszProcName
), ppProc
);
507 static HRESULT WINAPI
CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo
* iface
,
508 REFCLSID rclsid
, REFIID riid
, LPVOID
*ppUnk
)
510 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
514 TRACE("%p %s %s %p\n", iface
, debugstr_guid(rclsid
), debugstr_guid(riid
), ppUnk
);
516 hr
= CLRRuntimeInfo_GetRuntimeHost(This
, &host
);
519 hr
= RuntimeHost_GetInterface(host
, rclsid
, riid
, ppUnk
);
524 static HRESULT WINAPI
CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo
* iface
,
527 FIXME("%p %p\n", iface
, pbLoadable
);
532 static HRESULT WINAPI
CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo
* iface
,
533 DWORD dwStartupFlags
, LPCWSTR pwzHostConfigFile
)
535 FIXME("%p %x %s\n", iface
, dwStartupFlags
, debugstr_w(pwzHostConfigFile
));
540 static HRESULT WINAPI
CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo
* iface
,
541 DWORD
*pdwStartupFlags
, LPWSTR pwzHostConfigFile
, DWORD
*pcchHostConfigFile
)
543 FIXME("%p %p %p %p\n", iface
, pdwStartupFlags
, pwzHostConfigFile
, pcchHostConfigFile
);
548 static HRESULT WINAPI
CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo
* iface
)
550 FIXME("%p\n", iface
);
555 static HRESULT WINAPI
CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo
* iface
,
556 BOOL
*pbStarted
, DWORD
*pdwStartupFlags
)
558 FIXME("%p %p %p\n", iface
, pbStarted
, pdwStartupFlags
);
563 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl
= {
564 CLRRuntimeInfo_QueryInterface
,
565 CLRRuntimeInfo_AddRef
,
566 CLRRuntimeInfo_Release
,
567 CLRRuntimeInfo_GetVersionString
,
568 CLRRuntimeInfo_GetRuntimeDirectory
,
569 CLRRuntimeInfo_IsLoaded
,
570 CLRRuntimeInfo_LoadErrorString
,
571 CLRRuntimeInfo_LoadLibrary
,
572 CLRRuntimeInfo_GetProcAddress
,
573 CLRRuntimeInfo_GetInterface
,
574 CLRRuntimeInfo_IsLoadable
,
575 CLRRuntimeInfo_SetDefaultStartupFlags
,
576 CLRRuntimeInfo_GetDefaultStartupFlags
,
577 CLRRuntimeInfo_BindAsLegacyV2Runtime
,
578 CLRRuntimeInfo_IsStarted
581 HRESULT
ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo
*iface
, RuntimeHost
**result
)
583 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
585 assert(This
->ICLRRuntimeInfo_iface
.lpVtbl
== &CLRRuntimeInfoVtbl
);
587 return CLRRuntimeInfo_GetRuntimeHost(This
, result
);
591 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};
592 #elif defined(__x86_64__)
593 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};
595 static const WCHAR libmono2_arch_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
598 static BOOL
find_mono_dll(LPCWSTR path
, LPWSTR dll_path
, int abi_version
)
600 static const WCHAR mono_dll
[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
601 static const WCHAR libmono_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
602 static const WCHAR mono2_dll
[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
603 static const WCHAR libmono2_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
604 DWORD attributes
=INVALID_FILE_ATTRIBUTES
;
606 if (abi_version
== 1)
608 strcpyW(dll_path
, path
);
609 strcatW(dll_path
, mono_dll
);
610 attributes
= GetFileAttributesW(dll_path
);
612 if (attributes
== INVALID_FILE_ATTRIBUTES
)
614 strcpyW(dll_path
, path
);
615 strcatW(dll_path
, libmono_dll
);
616 attributes
= GetFileAttributesW(dll_path
);
619 else if (abi_version
== 2)
621 strcpyW(dll_path
, path
);
622 strcatW(dll_path
, libmono2_arch_dll
);
623 attributes
= GetFileAttributesW(dll_path
);
625 if (attributes
== INVALID_FILE_ATTRIBUTES
)
627 strcpyW(dll_path
, path
);
628 strcatW(dll_path
, mono2_dll
);
629 attributes
= GetFileAttributesW(dll_path
);
632 if (attributes
== INVALID_FILE_ATTRIBUTES
)
634 strcpyW(dll_path
, path
);
635 strcatW(dll_path
, libmono2_dll
);
636 attributes
= GetFileAttributesW(dll_path
);
640 return (attributes
!= INVALID_FILE_ATTRIBUTES
);
643 static BOOL
get_mono_path_from_registry(LPWSTR path
, int abi_version
)
645 static const WCHAR mono_key
[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
646 static const WCHAR defaul_clr
[] = {'D','e','f','a','u','l','t','C','L','R',0};
647 static const WCHAR install_root
[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
648 static const WCHAR slash
[] = {'\\',0};
650 WCHAR version
[64], version_key
[MAX_PATH
];
653 WCHAR dll_path
[MAX_PATH
];
655 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, mono_key
, 0, KEY_READ
, &key
))
658 len
= sizeof(version
);
659 if (RegQueryValueExW(key
, defaul_clr
, 0, NULL
, (LPBYTE
)version
, &len
))
666 lstrcpyW(version_key
, mono_key
);
667 lstrcatW(version_key
, slash
);
668 lstrcatW(version_key
, version
);
670 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, version_key
, 0, KEY_READ
, &key
))
673 len
= sizeof(WCHAR
) * MAX_PATH
;
674 if (RegQueryValueExW(key
, install_root
, 0, NULL
, (LPBYTE
)path
, &len
))
681 return find_mono_dll(path
, dll_path
, abi_version
);
684 static BOOL
get_mono_path_from_folder(LPCWSTR folder
, LPWSTR mono_path
, int abi_version
)
686 static const WCHAR mono_one_dot_zero
[] = {'\\','m','o','n','o','-','1','.','0', 0};
687 static const WCHAR mono_two_dot_zero
[] = {'\\','m','o','n','o','-','2','.','0', 0};
688 WCHAR mono_dll_path
[MAX_PATH
];
691 strcpyW(mono_path
, folder
);
693 if (abi_version
== 1)
694 strcatW(mono_path
, mono_one_dot_zero
);
695 else if (abi_version
== 2)
696 strcatW(mono_path
, mono_two_dot_zero
);
698 found
= find_mono_dll(mono_path
, mono_dll_path
, abi_version
);
703 static BOOL
get_mono_path(LPWSTR path
, int abi_version
)
705 static const WCHAR subdir_mono
[] = {'\\','m','o','n','o',0};
706 static const WCHAR sibling_mono
[] = {'\\','.','.','\\','m','o','n','o',0};
707 WCHAR base_path
[MAX_PATH
];
708 const char *unix_data_dir
;
711 static WCHAR
* (CDECL
*wine_get_dos_file_name
)(const char*);
713 /* First try c:\windows\mono */
714 GetWindowsDirectoryW(base_path
, MAX_PATH
);
715 strcatW(base_path
, subdir_mono
);
717 if (get_mono_path_from_folder(base_path
, path
, abi_version
))
720 /* Next: /usr/share/wine/mono */
721 unix_data_dir
= wine_get_data_dir();
725 unix_data_dir
= wine_get_build_dir();
731 if (!wine_get_dos_file_name
)
732 wine_get_dos_file_name
= (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
734 if (wine_get_dos_file_name
)
736 dos_data_dir
= wine_get_dos_file_name(unix_data_dir
);
740 strcpyW(base_path
, dos_data_dir
);
741 strcatW(base_path
, build_tree
? sibling_mono
: subdir_mono
);
743 HeapFree(GetProcessHeap(), 0, dos_data_dir
);
745 if (get_mono_path_from_folder(base_path
, path
, abi_version
))
751 /* Last: the registry */
752 return get_mono_path_from_registry(path
, abi_version
);
755 static void find_runtimes(void)
758 static const WCHAR libmono
[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
759 static const WCHAR mscorlib
[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
760 WCHAR mono_path
[MAX_PATH
], lib_path
[MAX_PATH
];
761 BOOL any_runtimes_found
= FALSE
;
763 if (runtimes_initialized
) return;
765 EnterCriticalSection(&runtime_list_cs
);
767 if (runtimes_initialized
) goto end
;
769 for (abi_version
=NUM_ABI_VERSIONS
; abi_version
>0; abi_version
--)
771 if (!get_mono_path(mono_path
, abi_version
))
774 for (i
=0; i
<NUM_RUNTIMES
; i
++)
776 if (runtimes
[i
].mono_abi_version
== 0)
778 strcpyW(lib_path
, mono_path
);
779 strcatW(lib_path
, libmono
);
780 strcatW(lib_path
, runtimes
[i
].mono_libdir
);
781 strcatW(lib_path
, mscorlib
);
783 if (GetFileAttributesW(lib_path
) != INVALID_FILE_ATTRIBUTES
)
785 runtimes
[i
].mono_abi_version
= abi_version
;
787 strcpyW(runtimes
[i
].mono_path
, mono_path
);
788 strcpyW(runtimes
[i
].mscorlib_path
, lib_path
);
790 any_runtimes_found
= TRUE
;
796 if (!any_runtimes_found
)
798 /* Report all runtimes are available if Mono isn't installed.
799 * FIXME: Remove this when Mono is properly packaged. */
800 for (i
=0; i
<NUM_RUNTIMES
; i
++)
801 runtimes
[i
].mono_abi_version
= -1;
804 runtimes_initialized
= 1;
807 LeaveCriticalSection(&runtime_list_cs
);
810 struct InstalledRuntimeEnum
812 IEnumUnknown IEnumUnknown_iface
;
817 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl
;
819 static inline struct InstalledRuntimeEnum
*impl_from_IEnumUnknown(IEnumUnknown
*iface
)
821 return CONTAINING_RECORD(iface
, struct InstalledRuntimeEnum
, IEnumUnknown_iface
);
824 static HRESULT WINAPI
InstalledRuntimeEnum_QueryInterface(IEnumUnknown
* iface
, REFIID riid
,
827 TRACE("%p %s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
829 if ( IsEqualGUID( riid
, &IID_IEnumUnknown
) ||
830 IsEqualGUID( riid
, &IID_IUnknown
) )
836 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
837 return E_NOINTERFACE
;
840 IEnumUnknown_AddRef( iface
);
845 static ULONG WINAPI
InstalledRuntimeEnum_AddRef(IEnumUnknown
* iface
)
847 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
848 ULONG ref
= InterlockedIncrement(&This
->ref
);
850 TRACE("(%p) refcount=%u\n", iface
, ref
);
855 static ULONG WINAPI
InstalledRuntimeEnum_Release(IEnumUnknown
* iface
)
857 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
858 ULONG ref
= InterlockedDecrement(&This
->ref
);
860 TRACE("(%p) refcount=%u\n", iface
, ref
);
864 HeapFree(GetProcessHeap(), 0, This
);
870 static HRESULT WINAPI
InstalledRuntimeEnum_Next(IEnumUnknown
*iface
, ULONG celt
,
871 IUnknown
**rgelt
, ULONG
*pceltFetched
)
873 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
878 TRACE("(%p,%u,%p,%p)\n", iface
, celt
, rgelt
, pceltFetched
);
880 while (num_fetched
< celt
)
882 if (This
->pos
>= NUM_RUNTIMES
)
887 if (runtimes
[This
->pos
].mono_abi_version
)
889 item
= (IUnknown
*)&runtimes
[This
->pos
].ICLRRuntimeInfo_iface
;
890 IUnknown_AddRef(item
);
891 rgelt
[num_fetched
] = item
;
898 *pceltFetched
= num_fetched
;
903 static HRESULT WINAPI
InstalledRuntimeEnum_Skip(IEnumUnknown
*iface
, ULONG celt
)
905 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
909 TRACE("(%p,%u)\n", iface
, celt
);
911 while (num_fetched
< celt
)
913 if (This
->pos
>= NUM_RUNTIMES
)
918 if (runtimes
[This
->pos
].mono_abi_version
)
928 static HRESULT WINAPI
InstalledRuntimeEnum_Reset(IEnumUnknown
*iface
)
930 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
932 TRACE("(%p)\n", iface
);
939 static HRESULT WINAPI
InstalledRuntimeEnum_Clone(IEnumUnknown
*iface
, IEnumUnknown
**ppenum
)
941 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
942 struct InstalledRuntimeEnum
*new_enum
;
944 TRACE("(%p)\n", iface
);
946 new_enum
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum
));
948 return E_OUTOFMEMORY
;
950 new_enum
->IEnumUnknown_iface
.lpVtbl
= &InstalledRuntimeEnum_Vtbl
;
952 new_enum
->pos
= This
->pos
;
954 *ppenum
= &new_enum
->IEnumUnknown_iface
;
959 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl
= {
960 InstalledRuntimeEnum_QueryInterface
,
961 InstalledRuntimeEnum_AddRef
,
962 InstalledRuntimeEnum_Release
,
963 InstalledRuntimeEnum_Next
,
964 InstalledRuntimeEnum_Skip
,
965 InstalledRuntimeEnum_Reset
,
966 InstalledRuntimeEnum_Clone
971 ICLRMetaHost ICLRMetaHost_iface
;
974 static struct CLRMetaHost GlobalCLRMetaHost
;
976 static HRESULT WINAPI
CLRMetaHost_QueryInterface(ICLRMetaHost
* iface
,
980 TRACE("%s %p\n", debugstr_guid(riid
), ppvObject
);
982 if ( IsEqualGUID( riid
, &IID_ICLRMetaHost
) ||
983 IsEqualGUID( riid
, &IID_IUnknown
) )
989 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
990 return E_NOINTERFACE
;
993 ICLRMetaHost_AddRef( iface
);
998 static ULONG WINAPI
CLRMetaHost_AddRef(ICLRMetaHost
* iface
)
1003 static ULONG WINAPI
CLRMetaHost_Release(ICLRMetaHost
* iface
)
1008 static BOOL
parse_runtime_version(LPCWSTR version
, DWORD
*major
, DWORD
*minor
, DWORD
*build
)
1014 if (version
[0] == 'v' || version
[0] == 'V')
1017 if (!isdigit(*version
))
1020 while (isdigit(*version
))
1021 *major
= *major
* 10 + (*version
++ - '0');
1026 if (*version
++ != '.' || !isdigit(*version
))
1029 while (isdigit(*version
))
1030 *minor
= *minor
* 10 + (*version
++ - '0');
1035 if (*version
++ != '.' || !isdigit(*version
))
1038 while (isdigit(*version
))
1039 *build
= *build
* 10 + (*version
++ - '0');
1041 return *version
== 0;
1047 HRESULT WINAPI
CLRMetaHost_GetRuntime(ICLRMetaHost
* iface
,
1048 LPCWSTR pwzVersion
, REFIID iid
, LPVOID
*ppRuntime
)
1051 DWORD major
, minor
, build
;
1053 TRACE("%s %s %p\n", debugstr_w(pwzVersion
), debugstr_guid(iid
), ppRuntime
);
1058 if (!parse_runtime_version(pwzVersion
, &major
, &minor
, &build
))
1060 ERR("Cannot parse %s\n", debugstr_w(pwzVersion
));
1061 return CLR_E_SHIM_RUNTIME
;
1066 for (i
=0; i
<NUM_RUNTIMES
; i
++)
1068 if (runtimes
[i
].major
== major
&& runtimes
[i
].minor
== minor
&&
1069 runtimes
[i
].build
== build
)
1071 if (runtimes
[i
].mono_abi_version
)
1072 return ICLRRuntimeInfo_QueryInterface(&runtimes
[i
].ICLRRuntimeInfo_iface
, iid
,
1076 missing_runtime_message();
1077 return CLR_E_SHIM_RUNTIME
;
1082 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion
));
1083 return CLR_E_SHIM_RUNTIME
;
1086 HRESULT WINAPI
CLRMetaHost_GetVersionFromFile(ICLRMetaHost
* iface
,
1087 LPCWSTR pwzFilePath
, LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
1092 ULONG buffer_size
=*pcchBuffer
;
1094 TRACE("%s %p %p\n", debugstr_w(pwzFilePath
), pwzBuffer
, pcchBuffer
);
1096 hr
= assembly_create(&assembly
, pwzFilePath
);
1100 hr
= assembly_get_runtime_version(assembly
, &version
);
1104 *pcchBuffer
= MultiByteToWideChar(CP_UTF8
, 0, version
, -1, NULL
, 0);
1108 if (buffer_size
>= *pcchBuffer
)
1109 MultiByteToWideChar(CP_UTF8
, 0, version
, -1, pwzBuffer
, buffer_size
);
1111 hr
= HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
1115 assembly_release(assembly
);
1121 static HRESULT WINAPI
CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost
* iface
,
1122 IEnumUnknown
**ppEnumerator
)
1124 struct InstalledRuntimeEnum
*new_enum
;
1126 TRACE("%p\n", ppEnumerator
);
1130 new_enum
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum
));
1132 return E_OUTOFMEMORY
;
1134 new_enum
->IEnumUnknown_iface
.lpVtbl
= &InstalledRuntimeEnum_Vtbl
;
1138 *ppEnumerator
= &new_enum
->IEnumUnknown_iface
;
1143 static HRESULT WINAPI
CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost
* iface
,
1144 HANDLE hndProcess
, IEnumUnknown
**ppEnumerator
)
1146 FIXME("%p %p\n", hndProcess
, ppEnumerator
);
1151 static HRESULT WINAPI
CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost
* iface
,
1152 RuntimeLoadedCallbackFnPtr pCallbackFunction
)
1154 FIXME("%p\n", pCallbackFunction
);
1159 static HRESULT WINAPI
CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost
* iface
,
1160 REFIID riid
, LPVOID
*ppUnk
)
1162 FIXME("%s %p\n", debugstr_guid(riid
), ppUnk
);
1167 static HRESULT WINAPI
CLRMetaHost_ExitProcess(ICLRMetaHost
* iface
, INT32 iExitCode
)
1169 FIXME("%i: stub\n", iExitCode
);
1171 ExitProcess(iExitCode
);
1174 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl
=
1176 CLRMetaHost_QueryInterface
,
1178 CLRMetaHost_Release
,
1179 CLRMetaHost_GetRuntime
,
1180 CLRMetaHost_GetVersionFromFile
,
1181 CLRMetaHost_EnumerateInstalledRuntimes
,
1182 CLRMetaHost_EnumerateLoadedRuntimes
,
1183 CLRMetaHost_RequestRuntimeLoadedNotification
,
1184 CLRMetaHost_QueryLegacyV2RuntimeBinding
,
1185 CLRMetaHost_ExitProcess
1188 static struct CLRMetaHost GlobalCLRMetaHost
= {
1189 { &CLRMetaHost_vtbl
}
1192 HRESULT
CLRMetaHost_CreateInstance(REFIID riid
, void **ppobj
)
1194 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost
.ICLRMetaHost_iface
, riid
, ppobj
);
1197 static MonoAssembly
* mono_assembly_search_hook_fn(MonoAssemblyName
*aname
, char **assemblies_path
, void *user_data
)
1199 loaded_mono
*mono
= user_data
;
1201 MonoAssembly
*result
=NULL
;
1202 char *stringname
=NULL
;
1204 int stringnameW_size
;
1205 IAssemblyCache
*asmcache
;
1207 WCHAR path
[MAX_PATH
];
1209 MonoImageOpenStatus stat
;
1210 static WCHAR fusiondll
[] = {'f','u','s','i','o','n',0};
1211 HMODULE hfusion
=NULL
;
1212 static HRESULT (WINAPI
*pCreateAssemblyCache
)(IAssemblyCache
**,DWORD
);
1214 stringname
= mono
->mono_stringify_assembly_name(aname
);
1216 TRACE("%s\n", debugstr_a(stringname
));
1218 if (!stringname
) return NULL
;
1220 /* FIXME: We should search the given paths before the GAC. */
1222 if (!pCreateAssemblyCache
)
1224 hr
= LoadLibraryShim(fusiondll
, NULL
, NULL
, &hfusion
);
1228 pCreateAssemblyCache
= (void*)GetProcAddress(hfusion
, "CreateAssemblyCache");
1229 if (!pCreateAssemblyCache
)
1235 hr
= pCreateAssemblyCache(&asmcache
, 0);
1239 stringnameW_size
= MultiByteToWideChar(CP_UTF8
, 0, stringname
, -1, NULL
, 0);
1241 stringnameW
= HeapAlloc(GetProcessHeap(), 0, stringnameW_size
* sizeof(WCHAR
));
1243 MultiByteToWideChar(CP_UTF8
, 0, stringname
, -1, stringnameW
, stringnameW_size
);
1249 info
.cbAssemblyInfo
= sizeof(info
);
1250 info
.pszCurrentAssemblyPathBuf
= path
;
1251 info
.cchBuf
= MAX_PATH
;
1254 hr
= IAssemblyCache_QueryAssemblyInfo(asmcache
, 0, stringnameW
, &info
);
1257 HeapFree(GetProcessHeap(), 0, stringnameW
);
1259 IAssemblyCache_Release(asmcache
);
1264 TRACE("found: %s\n", debugstr_w(path
));
1270 result
= mono
->mono_assembly_open(pathA
, &stat
);
1273 ERR("Failed to load %s, status=%u\n", debugstr_w(path
), stat
);
1275 HeapFree(GetProcessHeap(), 0, pathA
);
1279 mono
->mono_free(stringname
);
1284 HRESULT
get_runtime_info(LPCWSTR exefile
, LPCWSTR version
, LPCWSTR config_file
,
1285 DWORD startup_flags
, DWORD runtimeinfo_flags
, BOOL legacy
, ICLRRuntimeInfo
**result
)
1287 static const WCHAR dotconfig
[] = {'.','c','o','n','f','i','g',0};
1288 static const DWORD supported_startup_flags
= 0;
1289 static const DWORD supported_runtime_flags
= RUNTIME_INFO_UPGRADE_VERSION
;
1291 WCHAR local_version
[MAX_PATH
];
1292 ULONG local_version_size
= MAX_PATH
;
1293 WCHAR local_config_file
[MAX_PATH
];
1295 parsed_config_file parsed_config
;
1297 if (startup_flags
& ~supported_startup_flags
)
1298 FIXME("unsupported startup flags %x\n", startup_flags
& ~supported_startup_flags
);
1300 if (runtimeinfo_flags
& ~supported_runtime_flags
)
1301 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags
& ~supported_runtime_flags
);
1303 if (exefile
&& !config_file
)
1305 strcpyW(local_config_file
, exefile
);
1306 strcatW(local_config_file
, dotconfig
);
1308 config_file
= local_config_file
;
1314 hr
= parse_config_file(config_file
, &parsed_config
);
1318 supported_runtime
*entry
;
1319 LIST_FOR_EACH_ENTRY(entry
, &parsed_config
.supported_runtimes
, supported_runtime
, entry
)
1321 hr
= CLRMetaHost_GetRuntime(0, entry
->version
, &IID_ICLRRuntimeInfo
, (void**)result
);
1331 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file
), hr
);
1334 free_parsed_config_file(&parsed_config
);
1340 if (exefile
&& !version
)
1342 hr
= CLRMetaHost_GetVersionFromFile(0, exefile
, local_version
, &local_version_size
);
1344 version
= local_version
;
1346 if (FAILED(hr
)) return hr
;
1351 hr
= CLRMetaHost_GetRuntime(0, version
, &IID_ICLRRuntimeInfo
, (void**)result
);
1356 if (runtimeinfo_flags
& RUNTIME_INFO_UPGRADE_VERSION
)
1358 DWORD major
, minor
, build
;
1360 if (version
&& !parse_runtime_version(version
, &major
, &minor
, &build
))
1362 ERR("Cannot parse %s\n", debugstr_w(version
));
1363 return CLR_E_SHIM_RUNTIME
;
1375 if (runtimes
[i
].mono_abi_version
)
1377 /* Must be greater or equal to the version passed in. */
1378 if (!version
|| ((runtimes
[i
].major
>= major
&& runtimes
[i
].minor
>= minor
&& runtimes
[i
].build
>= build
) ||
1379 (runtimes
[i
].major
>= major
&& runtimes
[i
].minor
> minor
) ||
1380 (runtimes
[i
].major
> major
)))
1382 return ICLRRuntimeInfo_QueryInterface(&runtimes
[i
].ICLRRuntimeInfo_iface
,
1383 &IID_ICLRRuntimeInfo
, (void **)result
);
1388 missing_runtime_message();
1390 return CLR_E_SHIM_RUNTIME
;
1393 return CLR_E_SHIM_RUNTIME
;