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 char verbose_setting
[256];
144 if (This
->mono_abi_version
<= 0 || This
->mono_abi_version
> NUM_ABI_VERSIONS
)
146 missing_runtime_message();
150 *result
= &loaded_monos
[This
->mono_abi_version
-1];
152 if ((*result
)->is_shutdown
)
154 ERR("Cannot load Mono after it has been shut down.\n");
159 if (!(*result
)->mono_handle
)
161 strcpyW(mono_bin_path
, This
->mono_path
);
162 strcatW(mono_bin_path
, bin
);
163 set_environment(mono_bin_path
);
165 strcpyW(mono_lib_path
, This
->mono_path
);
166 strcatW(mono_lib_path
, lib
);
167 WideCharToMultiByte(CP_UTF8
, 0, mono_lib_path
, -1, mono_lib_path_a
, MAX_PATH
, NULL
, NULL
);
169 strcpyW(mono_etc_path
, This
->mono_path
);
170 strcatW(mono_etc_path
, etc
);
171 WideCharToMultiByte(CP_UTF8
, 0, mono_etc_path
, -1, mono_etc_path_a
, MAX_PATH
, NULL
, NULL
);
173 if (!find_mono_dll(This
->mono_path
, mono_dll_path
, This
->mono_abi_version
)) goto fail
;
175 (*result
)->mono_handle
= LoadLibraryW(mono_dll_path
);
177 if (!(*result
)->mono_handle
) goto fail
;
179 #define LOAD_MONO_FUNCTION(x) do { \
180 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
181 if (!(*result)->x) { \
186 LOAD_MONO_FUNCTION(mono_assembly_get_image
);
187 LOAD_MONO_FUNCTION(mono_assembly_load_from
);
188 LOAD_MONO_FUNCTION(mono_assembly_open
);
189 LOAD_MONO_FUNCTION(mono_config_parse
);
190 LOAD_MONO_FUNCTION(mono_class_from_mono_type
);
191 LOAD_MONO_FUNCTION(mono_class_from_name
);
192 LOAD_MONO_FUNCTION(mono_class_get_method_from_name
);
193 LOAD_MONO_FUNCTION(mono_domain_assembly_open
);
194 LOAD_MONO_FUNCTION(mono_image_open
);
195 LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook
);
196 LOAD_MONO_FUNCTION(mono_jit_exec
);
197 LOAD_MONO_FUNCTION(mono_jit_init
);
198 LOAD_MONO_FUNCTION(mono_jit_set_trace_options
);
199 LOAD_MONO_FUNCTION(mono_marshal_get_vtfixup_ftnptr
);
200 LOAD_MONO_FUNCTION(mono_object_get_domain
);
201 LOAD_MONO_FUNCTION(mono_object_new
);
202 LOAD_MONO_FUNCTION(mono_object_unbox
);
203 LOAD_MONO_FUNCTION(mono_profiler_install
);
204 LOAD_MONO_FUNCTION(mono_reflection_type_from_name
);
205 LOAD_MONO_FUNCTION(mono_runtime_invoke
);
206 LOAD_MONO_FUNCTION(mono_runtime_object_init
);
207 LOAD_MONO_FUNCTION(mono_runtime_quit
);
208 LOAD_MONO_FUNCTION(mono_set_dirs
);
209 LOAD_MONO_FUNCTION(mono_set_verbose_level
);
210 LOAD_MONO_FUNCTION(mono_stringify_assembly_name
);
211 LOAD_MONO_FUNCTION(mono_string_new
);
212 LOAD_MONO_FUNCTION(mono_thread_attach
);
213 LOAD_MONO_FUNCTION(mono_trace_set_assembly
);
215 /* GLib imports obsoleted by the 2.0 ABI */
216 if (This
->mono_abi_version
== 1)
218 (*result
)->glib_handle
= LoadLibraryW(glibdll
);
219 if (!(*result
)->glib_handle
) goto fail
;
221 (*result
)->mono_free
= (void*)GetProcAddress((*result
)->glib_handle
, "g_free");
222 if (!(*result
)->mono_free
) goto fail
;
226 LOAD_MONO_FUNCTION(mono_free
);
229 #undef LOAD_MONO_FUNCTION
231 #define LOAD_OPT_MONO_FUNCTION(x, default) do { \
232 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
233 if (!(*result)->x) { \
234 (*result)->x = default; \
238 LOAD_OPT_MONO_FUNCTION(mono_image_open_from_module_handle
, image_from_handle_fn
[This
->mono_abi_version
-1]);
239 LOAD_OPT_MONO_FUNCTION(mono_runtime_set_shutting_down
, do_nothing
);
240 LOAD_OPT_MONO_FUNCTION(mono_thread_pool_cleanup
, do_nothing
);
241 LOAD_OPT_MONO_FUNCTION(mono_thread_suspend_all_other_threads
, do_nothing
);
242 LOAD_OPT_MONO_FUNCTION(mono_threads_set_shutting_down
, do_nothing
);
244 #undef LOAD_OPT_MONO_FUNCTION
246 (*result
)->mono_profiler_install((MonoProfiler
*)*result
, mono_shutdown_callback_fn
);
248 (*result
)->mono_set_dirs(mono_lib_path_a
, mono_etc_path_a
);
250 (*result
)->mono_config_parse(NULL
);
252 (*result
)->mono_install_assembly_preload_hook(mono_assembly_search_hook_fn
, *result
);
254 trace_size
= GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting
, sizeof(trace_setting
));
258 (*result
)->mono_jit_set_trace_options(trace_setting
);
261 verbose_size
= GetEnvironmentVariableA("WINE_MONO_VERBOSE", verbose_setting
, sizeof(verbose_setting
));
265 (*result
)->mono_set_verbose_level(verbose_setting
[0] - '0');
272 ERR("Could not load Mono into this process\n");
273 FreeLibrary((*result
)->mono_handle
);
274 FreeLibrary((*result
)->glib_handle
);
275 (*result
)->mono_handle
= NULL
;
276 (*result
)->glib_handle
= NULL
;
280 static void mono_shutdown_callback_fn(MonoProfiler
*prof
)
282 loaded_mono
*mono
= (loaded_mono
*)prof
;
284 mono
->is_shutdown
= TRUE
;
287 static HRESULT
CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo
*This
, RuntimeHost
**result
)
290 loaded_mono
*ploaded_mono
;
292 if (This
->loaded_runtime
)
294 *result
= This
->loaded_runtime
;
298 EnterCriticalSection(&runtime_list_cs
);
300 hr
= load_mono(This
, &ploaded_mono
);
303 hr
= RuntimeHost_Construct(This
, ploaded_mono
, &This
->loaded_runtime
);
305 LeaveCriticalSection(&runtime_list_cs
);
308 *result
= This
->loaded_runtime
;
313 void unload_all_runtimes(void)
318 /* If the only references to mscoree are through dll's that were loaded by
319 * Mono, shutting down the Mono runtime will free mscoree, so take a
320 * reference to prevent that from happening. */
321 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
, (const WCHAR
*)unload_all_runtimes
, &handle
);
323 for (i
=0; i
<NUM_ABI_VERSIONS
; i
++)
325 loaded_mono
*mono
= &loaded_monos
[i
];
326 if (mono
->mono_handle
&& mono
->is_started
&& !mono
->is_shutdown
)
328 /* Copied from Mono's ves_icall_System_Environment_Exit */
329 mono
->mono_threads_set_shutting_down();
330 mono
->mono_runtime_set_shutting_down();
331 mono
->mono_thread_pool_cleanup();
332 mono
->mono_thread_suspend_all_other_threads();
333 mono
->mono_runtime_quit();
337 for (i
=0; i
<NUM_RUNTIMES
; i
++)
338 if (runtimes
[i
].loaded_runtime
)
339 RuntimeHost_Destroy(runtimes
[i
].loaded_runtime
);
342 void expect_no_runtimes(void)
346 for (i
=0; i
<NUM_ABI_VERSIONS
; i
++)
348 loaded_mono
*mono
= &loaded_monos
[i
];
349 if (mono
->mono_handle
&& mono
->is_started
&& !mono
->is_shutdown
)
351 ERR("Process exited with a Mono runtime loaded.\n");
357 static inline CLRRuntimeInfo
*impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo
*iface
)
359 return CONTAINING_RECORD(iface
, CLRRuntimeInfo
, ICLRRuntimeInfo_iface
);
362 static HRESULT WINAPI
CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo
* iface
,
366 TRACE("%p %s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
368 if ( IsEqualGUID( riid
, &IID_ICLRRuntimeInfo
) ||
369 IsEqualGUID( riid
, &IID_IUnknown
) )
375 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
376 return E_NOINTERFACE
;
379 ICLRRuntimeInfo_AddRef( iface
);
384 static ULONG WINAPI
CLRRuntimeInfo_AddRef(ICLRRuntimeInfo
* iface
)
389 static ULONG WINAPI
CLRRuntimeInfo_Release(ICLRRuntimeInfo
* iface
)
394 static HRESULT WINAPI
CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo
* iface
,
395 LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
397 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
398 DWORD buffer_size
= *pcchBuffer
;
403 TRACE("%p %p %p\n", iface
, pwzBuffer
, pcchBuffer
);
405 size
= snprintf(version
, sizeof(version
), "v%u.%u.%u", This
->major
, This
->minor
, This
->build
);
407 assert(size
<= sizeof(version
));
409 *pcchBuffer
= MultiByteToWideChar(CP_UTF8
, 0, version
, -1, NULL
, 0);
413 if (buffer_size
>= *pcchBuffer
)
414 MultiByteToWideChar(CP_UTF8
, 0, version
, -1, pwzBuffer
, buffer_size
);
416 hr
= HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
422 static BOOL
get_install_root(LPWSTR install_dir
)
424 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};
425 const WCHAR install_root
[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
430 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, dotnet_key
, 0, KEY_READ
, &key
))
434 if (RegQueryValueExW(key
, install_root
, 0, NULL
, (LPBYTE
)install_dir
, &len
))
444 static HRESULT WINAPI
CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo
* iface
,
445 LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
447 static const WCHAR slash
[] = {'\\',0};
448 DWORD buffer_size
= *pcchBuffer
;
449 WCHAR system_dir
[MAX_PATH
];
450 WCHAR version
[MAX_PATH
];
451 DWORD version_size
, size
;
454 TRACE("%p %p %p\n", iface
, pwzBuffer
, pcchBuffer
);
456 if (!get_install_root(system_dir
))
458 ERR("error reading registry key for installroot\n");
463 version_size
= MAX_PATH
;
464 ICLRRuntimeInfo_GetVersionString(iface
, version
, &version_size
);
465 lstrcatW(system_dir
, version
);
466 lstrcatW(system_dir
, slash
);
467 size
= lstrlenW(system_dir
) + 1;
474 if (buffer_size
>= size
)
475 strcpyW(pwzBuffer
, system_dir
);
477 hr
= HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
483 static HRESULT WINAPI
CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo
* iface
,
484 HANDLE hndProcess
, BOOL
*pbLoaded
)
486 FIXME("%p %p %p\n", iface
, hndProcess
, pbLoaded
);
491 static HRESULT WINAPI
CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo
* iface
,
492 UINT iResourceID
, LPWSTR pwzBuffer
, DWORD
*pcchBuffer
, LONG iLocaleid
)
494 FIXME("%p %u %p %p %x\n", iface
, iResourceID
, pwzBuffer
, pcchBuffer
, iLocaleid
);
499 static HRESULT WINAPI
CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo
* iface
,
500 LPCWSTR pwzDllName
, HMODULE
*phndModule
)
502 WCHAR version
[MAX_PATH
];
506 TRACE("%p %s %p\n", iface
, debugstr_w(pwzDllName
), phndModule
);
508 cchBuffer
= MAX_PATH
;
509 hr
= ICLRRuntimeInfo_GetVersionString(iface
, version
, &cchBuffer
);
510 if (FAILED(hr
)) return hr
;
512 return LoadLibraryShim(pwzDllName
, version
, NULL
, phndModule
);
515 static HRESULT WINAPI
CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo
* iface
,
516 LPCSTR pszProcName
, LPVOID
*ppProc
)
518 FIXME("%p %s %p\n", iface
, debugstr_a(pszProcName
), ppProc
);
523 static HRESULT WINAPI
CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo
* iface
,
524 REFCLSID rclsid
, REFIID riid
, LPVOID
*ppUnk
)
526 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
530 TRACE("%p %s %s %p\n", iface
, debugstr_guid(rclsid
), debugstr_guid(riid
), ppUnk
);
532 hr
= CLRRuntimeInfo_GetRuntimeHost(This
, &host
);
535 hr
= RuntimeHost_GetInterface(host
, rclsid
, riid
, ppUnk
);
540 static HRESULT WINAPI
CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo
* iface
,
543 FIXME("%p %p\n", iface
, pbLoadable
);
548 static HRESULT WINAPI
CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo
* iface
,
549 DWORD dwStartupFlags
, LPCWSTR pwzHostConfigFile
)
551 FIXME("%p %x %s\n", iface
, dwStartupFlags
, debugstr_w(pwzHostConfigFile
));
556 static HRESULT WINAPI
CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo
* iface
,
557 DWORD
*pdwStartupFlags
, LPWSTR pwzHostConfigFile
, DWORD
*pcchHostConfigFile
)
559 FIXME("%p %p %p %p\n", iface
, pdwStartupFlags
, pwzHostConfigFile
, pcchHostConfigFile
);
564 static HRESULT WINAPI
CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo
* iface
)
566 FIXME("%p\n", iface
);
571 static HRESULT WINAPI
CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo
* iface
,
572 BOOL
*pbStarted
, DWORD
*pdwStartupFlags
)
574 FIXME("%p %p %p\n", iface
, pbStarted
, pdwStartupFlags
);
579 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl
= {
580 CLRRuntimeInfo_QueryInterface
,
581 CLRRuntimeInfo_AddRef
,
582 CLRRuntimeInfo_Release
,
583 CLRRuntimeInfo_GetVersionString
,
584 CLRRuntimeInfo_GetRuntimeDirectory
,
585 CLRRuntimeInfo_IsLoaded
,
586 CLRRuntimeInfo_LoadErrorString
,
587 CLRRuntimeInfo_LoadLibrary
,
588 CLRRuntimeInfo_GetProcAddress
,
589 CLRRuntimeInfo_GetInterface
,
590 CLRRuntimeInfo_IsLoadable
,
591 CLRRuntimeInfo_SetDefaultStartupFlags
,
592 CLRRuntimeInfo_GetDefaultStartupFlags
,
593 CLRRuntimeInfo_BindAsLegacyV2Runtime
,
594 CLRRuntimeInfo_IsStarted
597 HRESULT
ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo
*iface
, RuntimeHost
**result
)
599 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
601 assert(This
->ICLRRuntimeInfo_iface
.lpVtbl
== &CLRRuntimeInfoVtbl
);
603 return CLRRuntimeInfo_GetRuntimeHost(This
, result
);
607 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};
608 #elif defined(__x86_64__)
609 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};
611 static const WCHAR libmono2_arch_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
614 static BOOL
find_mono_dll(LPCWSTR path
, LPWSTR dll_path
, int abi_version
)
616 static const WCHAR mono_dll
[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
617 static const WCHAR libmono_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
618 static const WCHAR mono2_dll
[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
619 static const WCHAR libmono2_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
620 DWORD attributes
=INVALID_FILE_ATTRIBUTES
;
622 if (abi_version
== 1)
624 strcpyW(dll_path
, path
);
625 strcatW(dll_path
, mono_dll
);
626 attributes
= GetFileAttributesW(dll_path
);
628 if (attributes
== INVALID_FILE_ATTRIBUTES
)
630 strcpyW(dll_path
, path
);
631 strcatW(dll_path
, libmono_dll
);
632 attributes
= GetFileAttributesW(dll_path
);
635 else if (abi_version
== 2)
637 strcpyW(dll_path
, path
);
638 strcatW(dll_path
, libmono2_arch_dll
);
639 attributes
= GetFileAttributesW(dll_path
);
641 if (attributes
== INVALID_FILE_ATTRIBUTES
)
643 strcpyW(dll_path
, path
);
644 strcatW(dll_path
, mono2_dll
);
645 attributes
= GetFileAttributesW(dll_path
);
648 if (attributes
== INVALID_FILE_ATTRIBUTES
)
650 strcpyW(dll_path
, path
);
651 strcatW(dll_path
, libmono2_dll
);
652 attributes
= GetFileAttributesW(dll_path
);
656 return (attributes
!= INVALID_FILE_ATTRIBUTES
);
659 static BOOL
get_mono_path_from_registry(LPWSTR path
, int abi_version
)
661 static const WCHAR mono_key
[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
662 static const WCHAR defaul_clr
[] = {'D','e','f','a','u','l','t','C','L','R',0};
663 static const WCHAR install_root
[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
664 static const WCHAR slash
[] = {'\\',0};
666 WCHAR version
[64], version_key
[MAX_PATH
];
669 WCHAR dll_path
[MAX_PATH
];
671 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, mono_key
, 0, KEY_READ
, &key
))
674 len
= sizeof(version
);
675 if (RegQueryValueExW(key
, defaul_clr
, 0, NULL
, (LPBYTE
)version
, &len
))
682 lstrcpyW(version_key
, mono_key
);
683 lstrcatW(version_key
, slash
);
684 lstrcatW(version_key
, version
);
686 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, version_key
, 0, KEY_READ
, &key
))
689 len
= sizeof(WCHAR
) * MAX_PATH
;
690 if (RegQueryValueExW(key
, install_root
, 0, NULL
, (LPBYTE
)path
, &len
))
697 return find_mono_dll(path
, dll_path
, abi_version
);
700 static BOOL
get_mono_path_from_folder(LPCWSTR folder
, LPWSTR mono_path
, int abi_version
)
702 static const WCHAR mono_one_dot_zero
[] = {'\\','m','o','n','o','-','1','.','0', 0};
703 static const WCHAR mono_two_dot_zero
[] = {'\\','m','o','n','o','-','2','.','0', 0};
704 WCHAR mono_dll_path
[MAX_PATH
];
707 strcpyW(mono_path
, folder
);
709 if (abi_version
== 1)
710 strcatW(mono_path
, mono_one_dot_zero
);
711 else if (abi_version
== 2)
712 strcatW(mono_path
, mono_two_dot_zero
);
714 found
= find_mono_dll(mono_path
, mono_dll_path
, abi_version
);
719 static BOOL
get_mono_path(LPWSTR path
, int abi_version
)
721 static const WCHAR subdir_mono
[] = {'\\','m','o','n','o',0};
722 static const WCHAR sibling_mono
[] = {'\\','.','.','\\','m','o','n','o',0};
723 WCHAR base_path
[MAX_PATH
];
724 const char *unix_data_dir
;
727 static WCHAR
* (CDECL
*wine_get_dos_file_name
)(const char*);
729 /* First try c:\windows\mono */
730 GetWindowsDirectoryW(base_path
, MAX_PATH
);
731 strcatW(base_path
, subdir_mono
);
733 if (get_mono_path_from_folder(base_path
, path
, abi_version
))
736 /* Next: /usr/share/wine/mono */
737 unix_data_dir
= wine_get_data_dir();
741 unix_data_dir
= wine_get_build_dir();
747 if (!wine_get_dos_file_name
)
748 wine_get_dos_file_name
= (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
750 if (wine_get_dos_file_name
)
752 dos_data_dir
= wine_get_dos_file_name(unix_data_dir
);
756 strcpyW(base_path
, dos_data_dir
);
757 strcatW(base_path
, build_tree
? sibling_mono
: subdir_mono
);
759 HeapFree(GetProcessHeap(), 0, dos_data_dir
);
761 if (get_mono_path_from_folder(base_path
, path
, abi_version
))
767 /* Last: the registry */
768 return get_mono_path_from_registry(path
, abi_version
);
771 static void find_runtimes(void)
774 static const WCHAR libmono
[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
775 static const WCHAR mscorlib
[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
776 WCHAR mono_path
[MAX_PATH
], lib_path
[MAX_PATH
];
777 BOOL any_runtimes_found
= FALSE
;
779 if (runtimes_initialized
) return;
781 EnterCriticalSection(&runtime_list_cs
);
783 if (runtimes_initialized
) goto end
;
785 for (abi_version
=NUM_ABI_VERSIONS
; abi_version
>0; abi_version
--)
787 if (!get_mono_path(mono_path
, abi_version
))
790 for (i
=0; i
<NUM_RUNTIMES
; i
++)
792 if (runtimes
[i
].mono_abi_version
== 0)
794 strcpyW(lib_path
, mono_path
);
795 strcatW(lib_path
, libmono
);
796 strcatW(lib_path
, runtimes
[i
].mono_libdir
);
797 strcatW(lib_path
, mscorlib
);
799 if (GetFileAttributesW(lib_path
) != INVALID_FILE_ATTRIBUTES
)
801 runtimes
[i
].mono_abi_version
= abi_version
;
803 strcpyW(runtimes
[i
].mono_path
, mono_path
);
804 strcpyW(runtimes
[i
].mscorlib_path
, lib_path
);
806 any_runtimes_found
= TRUE
;
812 if (!any_runtimes_found
)
814 /* Report all runtimes are available if Mono isn't installed.
815 * FIXME: Remove this when Mono is properly packaged. */
816 for (i
=0; i
<NUM_RUNTIMES
; i
++)
817 runtimes
[i
].mono_abi_version
= -1;
820 runtimes_initialized
= 1;
823 LeaveCriticalSection(&runtime_list_cs
);
826 struct InstalledRuntimeEnum
828 IEnumUnknown IEnumUnknown_iface
;
833 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl
;
835 static inline struct InstalledRuntimeEnum
*impl_from_IEnumUnknown(IEnumUnknown
*iface
)
837 return CONTAINING_RECORD(iface
, struct InstalledRuntimeEnum
, IEnumUnknown_iface
);
840 static HRESULT WINAPI
InstalledRuntimeEnum_QueryInterface(IEnumUnknown
* iface
, REFIID riid
,
843 TRACE("%p %s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
845 if ( IsEqualGUID( riid
, &IID_IEnumUnknown
) ||
846 IsEqualGUID( riid
, &IID_IUnknown
) )
852 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
853 return E_NOINTERFACE
;
856 IEnumUnknown_AddRef( iface
);
861 static ULONG WINAPI
InstalledRuntimeEnum_AddRef(IEnumUnknown
* iface
)
863 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
864 ULONG ref
= InterlockedIncrement(&This
->ref
);
866 TRACE("(%p) refcount=%u\n", iface
, ref
);
871 static ULONG WINAPI
InstalledRuntimeEnum_Release(IEnumUnknown
* iface
)
873 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
874 ULONG ref
= InterlockedDecrement(&This
->ref
);
876 TRACE("(%p) refcount=%u\n", iface
, ref
);
880 HeapFree(GetProcessHeap(), 0, This
);
886 static HRESULT WINAPI
InstalledRuntimeEnum_Next(IEnumUnknown
*iface
, ULONG celt
,
887 IUnknown
**rgelt
, ULONG
*pceltFetched
)
889 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
890 ULONG num_fetched
= 0;
894 TRACE("(%p,%u,%p,%p)\n", iface
, celt
, rgelt
, pceltFetched
);
896 while (num_fetched
< celt
)
898 if (This
->pos
>= NUM_RUNTIMES
)
903 if (runtimes
[This
->pos
].mono_abi_version
)
905 item
= (IUnknown
*)&runtimes
[This
->pos
].ICLRRuntimeInfo_iface
;
906 IUnknown_AddRef(item
);
907 rgelt
[num_fetched
] = item
;
914 *pceltFetched
= num_fetched
;
919 static HRESULT WINAPI
InstalledRuntimeEnum_Skip(IEnumUnknown
*iface
, ULONG celt
)
921 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
922 ULONG num_fetched
= 0;
925 TRACE("(%p,%u)\n", iface
, celt
);
927 while (num_fetched
< celt
)
929 if (This
->pos
>= NUM_RUNTIMES
)
934 if (runtimes
[This
->pos
].mono_abi_version
)
944 static HRESULT WINAPI
InstalledRuntimeEnum_Reset(IEnumUnknown
*iface
)
946 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
948 TRACE("(%p)\n", iface
);
955 static HRESULT WINAPI
InstalledRuntimeEnum_Clone(IEnumUnknown
*iface
, IEnumUnknown
**ppenum
)
957 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
958 struct InstalledRuntimeEnum
*new_enum
;
960 TRACE("(%p)\n", iface
);
962 new_enum
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum
));
964 return E_OUTOFMEMORY
;
966 new_enum
->IEnumUnknown_iface
.lpVtbl
= &InstalledRuntimeEnum_Vtbl
;
968 new_enum
->pos
= This
->pos
;
970 *ppenum
= &new_enum
->IEnumUnknown_iface
;
975 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl
= {
976 InstalledRuntimeEnum_QueryInterface
,
977 InstalledRuntimeEnum_AddRef
,
978 InstalledRuntimeEnum_Release
,
979 InstalledRuntimeEnum_Next
,
980 InstalledRuntimeEnum_Skip
,
981 InstalledRuntimeEnum_Reset
,
982 InstalledRuntimeEnum_Clone
987 ICLRMetaHost ICLRMetaHost_iface
;
990 static struct CLRMetaHost GlobalCLRMetaHost
;
992 static HRESULT WINAPI
CLRMetaHost_QueryInterface(ICLRMetaHost
* iface
,
996 TRACE("%s %p\n", debugstr_guid(riid
), ppvObject
);
998 if ( IsEqualGUID( riid
, &IID_ICLRMetaHost
) ||
999 IsEqualGUID( riid
, &IID_IUnknown
) )
1005 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
1006 return E_NOINTERFACE
;
1009 ICLRMetaHost_AddRef( iface
);
1014 static ULONG WINAPI
CLRMetaHost_AddRef(ICLRMetaHost
* iface
)
1019 static ULONG WINAPI
CLRMetaHost_Release(ICLRMetaHost
* iface
)
1024 static BOOL
parse_runtime_version(LPCWSTR version
, DWORD
*major
, DWORD
*minor
, DWORD
*build
)
1030 if (version
[0] == 'v' || version
[0] == 'V')
1033 if (!isdigit(*version
))
1036 while (isdigit(*version
))
1037 *major
= *major
* 10 + (*version
++ - '0');
1042 if (*version
++ != '.' || !isdigit(*version
))
1045 while (isdigit(*version
))
1046 *minor
= *minor
* 10 + (*version
++ - '0');
1051 if (*version
++ != '.' || !isdigit(*version
))
1054 while (isdigit(*version
))
1055 *build
= *build
* 10 + (*version
++ - '0');
1057 return *version
== 0;
1063 HRESULT WINAPI
CLRMetaHost_GetRuntime(ICLRMetaHost
* iface
,
1064 LPCWSTR pwzVersion
, REFIID iid
, LPVOID
*ppRuntime
)
1067 DWORD major
, minor
, build
;
1069 TRACE("%s %s %p\n", debugstr_w(pwzVersion
), debugstr_guid(iid
), ppRuntime
);
1074 if (!parse_runtime_version(pwzVersion
, &major
, &minor
, &build
))
1076 ERR("Cannot parse %s\n", debugstr_w(pwzVersion
));
1077 return CLR_E_SHIM_RUNTIME
;
1082 for (i
=0; i
<NUM_RUNTIMES
; i
++)
1084 if (runtimes
[i
].major
== major
&& runtimes
[i
].minor
== minor
&&
1085 runtimes
[i
].build
== build
)
1087 if (runtimes
[i
].mono_abi_version
)
1088 return ICLRRuntimeInfo_QueryInterface(&runtimes
[i
].ICLRRuntimeInfo_iface
, iid
,
1092 missing_runtime_message();
1093 return CLR_E_SHIM_RUNTIME
;
1098 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion
));
1099 return CLR_E_SHIM_RUNTIME
;
1102 HRESULT WINAPI
CLRMetaHost_GetVersionFromFile(ICLRMetaHost
* iface
,
1103 LPCWSTR pwzFilePath
, LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
1108 ULONG buffer_size
=*pcchBuffer
;
1110 TRACE("%s %p %p\n", debugstr_w(pwzFilePath
), pwzBuffer
, pcchBuffer
);
1112 hr
= assembly_create(&assembly
, pwzFilePath
);
1116 hr
= assembly_get_runtime_version(assembly
, &version
);
1120 *pcchBuffer
= MultiByteToWideChar(CP_UTF8
, 0, version
, -1, NULL
, 0);
1124 if (buffer_size
>= *pcchBuffer
)
1125 MultiByteToWideChar(CP_UTF8
, 0, version
, -1, pwzBuffer
, buffer_size
);
1127 hr
= HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
1131 assembly_release(assembly
);
1137 static HRESULT WINAPI
CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost
* iface
,
1138 IEnumUnknown
**ppEnumerator
)
1140 struct InstalledRuntimeEnum
*new_enum
;
1142 TRACE("%p\n", ppEnumerator
);
1146 new_enum
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum
));
1148 return E_OUTOFMEMORY
;
1150 new_enum
->IEnumUnknown_iface
.lpVtbl
= &InstalledRuntimeEnum_Vtbl
;
1154 *ppEnumerator
= &new_enum
->IEnumUnknown_iface
;
1159 static HRESULT WINAPI
CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost
* iface
,
1160 HANDLE hndProcess
, IEnumUnknown
**ppEnumerator
)
1162 FIXME("%p %p\n", hndProcess
, ppEnumerator
);
1167 static HRESULT WINAPI
CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost
* iface
,
1168 RuntimeLoadedCallbackFnPtr pCallbackFunction
)
1170 FIXME("%p\n", pCallbackFunction
);
1175 static HRESULT WINAPI
CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost
* iface
,
1176 REFIID riid
, LPVOID
*ppUnk
)
1178 FIXME("%s %p\n", debugstr_guid(riid
), ppUnk
);
1183 static HRESULT WINAPI
CLRMetaHost_ExitProcess(ICLRMetaHost
* iface
, INT32 iExitCode
)
1185 FIXME("%i: stub\n", iExitCode
);
1187 ExitProcess(iExitCode
);
1190 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl
=
1192 CLRMetaHost_QueryInterface
,
1194 CLRMetaHost_Release
,
1195 CLRMetaHost_GetRuntime
,
1196 CLRMetaHost_GetVersionFromFile
,
1197 CLRMetaHost_EnumerateInstalledRuntimes
,
1198 CLRMetaHost_EnumerateLoadedRuntimes
,
1199 CLRMetaHost_RequestRuntimeLoadedNotification
,
1200 CLRMetaHost_QueryLegacyV2RuntimeBinding
,
1201 CLRMetaHost_ExitProcess
1204 static struct CLRMetaHost GlobalCLRMetaHost
= {
1205 { &CLRMetaHost_vtbl
}
1208 HRESULT
CLRMetaHost_CreateInstance(REFIID riid
, void **ppobj
)
1210 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost
.ICLRMetaHost_iface
, riid
, ppobj
);
1213 HRESULT
get_file_from_strongname(WCHAR
* stringnameW
, WCHAR
* assemblies_path
, int path_length
)
1216 IAssemblyCache
*asmcache
;
1218 static WCHAR fusiondll
[] = {'f','u','s','i','o','n',0};
1219 HMODULE hfusion
=NULL
;
1220 static HRESULT (WINAPI
*pCreateAssemblyCache
)(IAssemblyCache
**,DWORD
);
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 info
.cbAssemblyInfo
= sizeof(info
);
1240 info
.pszCurrentAssemblyPathBuf
= assemblies_path
;
1241 info
.cchBuf
= path_length
;
1242 assemblies_path
[0] = 0;
1244 hr
= IAssemblyCache_QueryAssemblyInfo(asmcache
, 0, stringnameW
, &info
);
1246 IAssemblyCache_Release(asmcache
);
1252 static MonoAssembly
* mono_assembly_search_hook_fn(MonoAssemblyName
*aname
, char **assemblies_path
, void *user_data
)
1254 loaded_mono
*mono
= user_data
;
1256 MonoAssembly
*result
=NULL
;
1257 char *stringname
=NULL
;
1259 int stringnameW_size
;
1260 WCHAR path
[MAX_PATH
];
1262 MonoImageOpenStatus stat
;
1264 stringname
= mono
->mono_stringify_assembly_name(aname
);
1266 TRACE("%s\n", debugstr_a(stringname
));
1268 if (!stringname
) return NULL
;
1270 /* FIXME: We should search the given paths before the GAC. */
1272 stringnameW_size
= MultiByteToWideChar(CP_UTF8
, 0, stringname
, -1, NULL
, 0);
1274 stringnameW
= HeapAlloc(GetProcessHeap(), 0, stringnameW_size
* sizeof(WCHAR
));
1277 MultiByteToWideChar(CP_UTF8
, 0, stringname
, -1, stringnameW
, stringnameW_size
);
1279 hr
= get_file_from_strongname(stringnameW
, path
, MAX_PATH
);
1281 HeapFree(GetProcessHeap(), 0, stringnameW
);
1288 TRACE("found: %s\n", debugstr_w(path
));
1294 result
= mono
->mono_assembly_open(pathA
, &stat
);
1297 ERR("Failed to load %s, status=%u\n", debugstr_w(path
), stat
);
1299 HeapFree(GetProcessHeap(), 0, pathA
);
1303 mono
->mono_free(stringname
);
1308 HRESULT
get_runtime_info(LPCWSTR exefile
, LPCWSTR version
, LPCWSTR config_file
,
1309 DWORD startup_flags
, DWORD runtimeinfo_flags
, BOOL legacy
, ICLRRuntimeInfo
**result
)
1311 static const WCHAR dotconfig
[] = {'.','c','o','n','f','i','g',0};
1312 static const DWORD supported_startup_flags
= 0;
1313 static const DWORD supported_runtime_flags
= RUNTIME_INFO_UPGRADE_VERSION
;
1315 WCHAR local_version
[MAX_PATH
];
1316 ULONG local_version_size
= MAX_PATH
;
1317 WCHAR local_config_file
[MAX_PATH
];
1319 parsed_config_file parsed_config
;
1321 if (startup_flags
& ~supported_startup_flags
)
1322 FIXME("unsupported startup flags %x\n", startup_flags
& ~supported_startup_flags
);
1324 if (runtimeinfo_flags
& ~supported_runtime_flags
)
1325 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags
& ~supported_runtime_flags
);
1327 if (exefile
&& !config_file
)
1329 strcpyW(local_config_file
, exefile
);
1330 strcatW(local_config_file
, dotconfig
);
1332 config_file
= local_config_file
;
1338 hr
= parse_config_file(config_file
, &parsed_config
);
1342 supported_runtime
*entry
;
1343 LIST_FOR_EACH_ENTRY(entry
, &parsed_config
.supported_runtimes
, supported_runtime
, entry
)
1345 hr
= CLRMetaHost_GetRuntime(0, entry
->version
, &IID_ICLRRuntimeInfo
, (void**)result
);
1355 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file
), hr
);
1358 free_parsed_config_file(&parsed_config
);
1364 if (exefile
&& !version
)
1366 hr
= CLRMetaHost_GetVersionFromFile(0, exefile
, local_version
, &local_version_size
);
1368 version
= local_version
;
1370 if (FAILED(hr
)) return hr
;
1375 hr
= CLRMetaHost_GetRuntime(0, version
, &IID_ICLRRuntimeInfo
, (void**)result
);
1380 if (runtimeinfo_flags
& RUNTIME_INFO_UPGRADE_VERSION
)
1382 DWORD major
, minor
, build
;
1384 if (version
&& !parse_runtime_version(version
, &major
, &minor
, &build
))
1386 ERR("Cannot parse %s\n", debugstr_w(version
));
1387 return CLR_E_SHIM_RUNTIME
;
1399 if (runtimes
[i
].mono_abi_version
)
1401 /* Must be greater or equal to the version passed in. */
1402 if (!version
|| ((runtimes
[i
].major
>= major
&& runtimes
[i
].minor
>= minor
&& runtimes
[i
].build
>= build
) ||
1403 (runtimes
[i
].major
>= major
&& runtimes
[i
].minor
> minor
) ||
1404 (runtimes
[i
].major
> major
)))
1406 return ICLRRuntimeInfo_QueryInterface(&runtimes
[i
].ICLRRuntimeInfo_iface
,
1407 &IID_ICLRRuntimeInfo
, (void **)result
);
1412 missing_runtime_message();
1414 return CLR_E_SHIM_RUNTIME
;
1417 return CLR_E_SHIM_RUNTIME
;