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 #define WIN32_NO_STATUS
23 #define COM_NO_WINDOWS_H
31 #include <wine/unicode.h>
32 #include <wine/library.h>
34 //#include "winbase.h"
38 //#include "corerror.h"
40 //#include "mscoree.h"
45 #include <wine/list.h>
46 #include "mscoree_private.h"
48 #include <wine/debug.h>
50 WINE_DEFAULT_DEBUG_CHANNEL( mscoree
);
52 static const WCHAR net_11_subdir
[] = {'1','.','0',0};
53 static const WCHAR net_20_subdir
[] = {'2','.','0',0};
54 static const WCHAR net_40_subdir
[] = {'4','.','0',0};
56 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl
;
58 #define NUM_RUNTIMES 3
60 static struct CLRRuntimeInfo runtimes
[NUM_RUNTIMES
] = {
61 {{&CLRRuntimeInfoVtbl
}, net_11_subdir
, 1, 1, 4322, 0},
62 {{&CLRRuntimeInfoVtbl
}, net_20_subdir
, 2, 0, 50727, 0},
63 {{&CLRRuntimeInfoVtbl
}, net_40_subdir
, 4, 0, 30319, 0}
66 static int runtimes_initialized
;
68 static CRITICAL_SECTION runtime_list_cs
;
69 static CRITICAL_SECTION_DEBUG runtime_list_cs_debug
=
71 0, 0, &runtime_list_cs
,
72 { &runtime_list_cs_debug
.ProcessLocksList
,
73 &runtime_list_cs_debug
.ProcessLocksList
},
74 0, 0, { (DWORD_PTR
)(__FILE__
": runtime_list_cs") }
76 static CRITICAL_SECTION runtime_list_cs
= { &runtime_list_cs_debug
, -1, 0, 0, 0, 0 };
78 #define NUM_ABI_VERSIONS 2
80 static loaded_mono loaded_monos
[NUM_ABI_VERSIONS
];
82 static BOOL
find_mono_dll(LPCWSTR path
, LPWSTR dll_path
, int abi_version
);
84 static MonoAssembly
* mono_assembly_search_hook_fn(MonoAssemblyName
*aname
, char **assemblies_path
, void *user_data
);
86 static void mono_shutdown_callback_fn(MonoProfiler
*prof
);
88 static void set_environment(LPCWSTR bin_path
)
90 WCHAR path_env
[MAX_PATH
];
93 static const WCHAR pathW
[] = {'P','A','T','H',0};
95 /* We have to modify PATH as Mono loads other DLLs from this directory. */
96 GetEnvironmentVariableW(pathW
, path_env
, sizeof(path_env
)/sizeof(WCHAR
));
97 len
= strlenW(path_env
);
98 path_env
[len
++] = ';';
99 strcpyW(path_env
+len
, bin_path
);
100 SetEnvironmentVariableW(pathW
, path_env
);
103 static void CDECL
do_nothing(void)
107 static void missing_runtime_message(const CLRRuntimeInfo
*This
)
109 if (This
->major
== 1)
110 MESSAGE("wine: Install Mono 2.6 for Windows to run .NET 1.1 applications.\n");
111 else if (This
->major
== 2)
112 MESSAGE("wine: Install Mono for Windows to run .NET 2.0 applications.\n");
113 else if (This
->major
== 4)
114 MESSAGE("wine: Install Mono 2.8 or greater for Windows to run .NET 4.0 applications.\n");
117 static HRESULT
load_mono(CLRRuntimeInfo
*This
, loaded_mono
**result
)
119 static const WCHAR bin
[] = {'\\','b','i','n',0};
120 static const WCHAR lib
[] = {'\\','l','i','b',0};
121 static const WCHAR etc
[] = {'\\','e','t','c',0};
122 static const WCHAR glibdll
[] = {'l','i','b','g','l','i','b','-','2','.','0','-','0','.','d','l','l',0};
123 WCHAR mono_dll_path
[MAX_PATH
+16], mono_bin_path
[MAX_PATH
+4];
124 WCHAR mono_lib_path
[MAX_PATH
+4], mono_etc_path
[MAX_PATH
+4];
125 char mono_lib_path_a
[MAX_PATH
], mono_etc_path_a
[MAX_PATH
];
127 char trace_setting
[256];
129 if (This
->mono_abi_version
<= 0 || This
->mono_abi_version
> NUM_ABI_VERSIONS
)
131 missing_runtime_message(This
);
135 *result
= &loaded_monos
[This
->mono_abi_version
-1];
137 if ((*result
)->is_shutdown
)
139 ERR("Cannot load Mono after it has been shut down.\n");
144 if (!(*result
)->mono_handle
)
146 strcpyW(mono_bin_path
, This
->mono_path
);
147 strcatW(mono_bin_path
, bin
);
148 set_environment(mono_bin_path
);
150 strcpyW(mono_lib_path
, This
->mono_path
);
151 strcatW(mono_lib_path
, lib
);
152 WideCharToMultiByte(CP_UTF8
, 0, mono_lib_path
, -1, mono_lib_path_a
, MAX_PATH
, NULL
, NULL
);
154 strcpyW(mono_etc_path
, This
->mono_path
);
155 strcatW(mono_etc_path
, etc
);
156 WideCharToMultiByte(CP_UTF8
, 0, mono_etc_path
, -1, mono_etc_path_a
, MAX_PATH
, NULL
, NULL
);
158 if (!find_mono_dll(This
->mono_path
, mono_dll_path
, This
->mono_abi_version
)) goto fail
;
160 (*result
)->mono_handle
= LoadLibraryW(mono_dll_path
);
162 if (!(*result
)->mono_handle
) goto fail
;
164 #define LOAD_MONO_FUNCTION(x) do { \
165 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
166 if (!(*result)->x) { \
171 LOAD_MONO_FUNCTION(mono_assembly_get_image
);
172 LOAD_MONO_FUNCTION(mono_assembly_load_from
);
173 LOAD_MONO_FUNCTION(mono_assembly_open
);
174 LOAD_MONO_FUNCTION(mono_config_parse
);
175 LOAD_MONO_FUNCTION(mono_class_from_mono_type
);
176 LOAD_MONO_FUNCTION(mono_class_from_name
);
177 LOAD_MONO_FUNCTION(mono_class_get_method_from_name
);
178 LOAD_MONO_FUNCTION(mono_domain_assembly_open
);
179 LOAD_MONO_FUNCTION(mono_image_open_from_module_handle
);
180 LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook
);
181 LOAD_MONO_FUNCTION(mono_jit_exec
);
182 LOAD_MONO_FUNCTION(mono_jit_init
);
183 LOAD_MONO_FUNCTION(mono_jit_set_trace_options
);
184 LOAD_MONO_FUNCTION(mono_marshal_get_vtfixup_ftnptr
);
185 LOAD_MONO_FUNCTION(mono_object_get_domain
);
186 LOAD_MONO_FUNCTION(mono_object_new
);
187 LOAD_MONO_FUNCTION(mono_object_unbox
);
188 LOAD_MONO_FUNCTION(mono_profiler_install
);
189 LOAD_MONO_FUNCTION(mono_reflection_type_from_name
);
190 LOAD_MONO_FUNCTION(mono_runtime_invoke
);
191 LOAD_MONO_FUNCTION(mono_runtime_object_init
);
192 LOAD_MONO_FUNCTION(mono_runtime_quit
);
193 LOAD_MONO_FUNCTION(mono_set_dirs
);
194 LOAD_MONO_FUNCTION(mono_stringify_assembly_name
);
195 LOAD_MONO_FUNCTION(mono_string_new
);
196 LOAD_MONO_FUNCTION(mono_thread_attach
);
198 /* GLib imports obsoleted by the 2.0 ABI */
199 if (This
->mono_abi_version
== 1)
201 (*result
)->glib_handle
= LoadLibraryW(glibdll
);
202 if (!(*result
)->glib_handle
) goto fail
;
204 (*result
)->mono_free
= (void*)GetProcAddress((*result
)->glib_handle
, "g_free");
205 if (!(*result
)->mono_free
) goto fail
;
209 LOAD_MONO_FUNCTION(mono_free
);
212 #undef LOAD_MONO_FUNCTION
214 #define LOAD_OPT_VOID_MONO_FUNCTION(x) do { \
215 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
216 if (!(*result)->x) { \
217 (*result)->x = do_nothing; \
221 LOAD_OPT_VOID_MONO_FUNCTION(mono_runtime_set_shutting_down
);
222 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_pool_cleanup
);
223 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_suspend_all_other_threads
);
224 LOAD_OPT_VOID_MONO_FUNCTION(mono_threads_set_shutting_down
);
226 #undef LOAD_OPT_VOID_MONO_FUNCTION
228 (*result
)->mono_profiler_install((MonoProfiler
*)*result
, mono_shutdown_callback_fn
);
230 (*result
)->mono_set_dirs(mono_lib_path_a
, mono_etc_path_a
);
232 (*result
)->mono_config_parse(NULL
);
234 (*result
)->mono_install_assembly_preload_hook(mono_assembly_search_hook_fn
, *result
);
236 trace_size
= GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting
, sizeof(trace_setting
));
240 (*result
)->mono_jit_set_trace_options(trace_setting
);
247 ERR("Could not load Mono into this process\n");
248 FreeLibrary((*result
)->mono_handle
);
249 FreeLibrary((*result
)->glib_handle
);
250 (*result
)->mono_handle
= NULL
;
251 (*result
)->glib_handle
= NULL
;
255 static void mono_shutdown_callback_fn(MonoProfiler
*prof
)
257 loaded_mono
*mono
= (loaded_mono
*)prof
;
259 mono
->is_shutdown
= TRUE
;
262 static HRESULT
CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo
*This
, RuntimeHost
**result
)
265 loaded_mono
*ploaded_mono
;
267 if (This
->loaded_runtime
)
269 *result
= This
->loaded_runtime
;
273 EnterCriticalSection(&runtime_list_cs
);
275 hr
= load_mono(This
, &ploaded_mono
);
278 hr
= RuntimeHost_Construct(This
, ploaded_mono
, &This
->loaded_runtime
);
280 LeaveCriticalSection(&runtime_list_cs
);
283 *result
= This
->loaded_runtime
;
288 void unload_all_runtimes(void)
292 for (i
=0; i
<NUM_ABI_VERSIONS
; i
++)
294 loaded_mono
*mono
= &loaded_monos
[i
];
295 if (mono
->mono_handle
&& mono
->is_started
&& !mono
->is_shutdown
)
297 /* Copied from Mono's ves_icall_System_Environment_Exit */
298 mono
->mono_threads_set_shutting_down();
299 mono
->mono_runtime_set_shutting_down();
300 mono
->mono_thread_pool_cleanup();
301 mono
->mono_thread_suspend_all_other_threads();
302 mono
->mono_runtime_quit();
306 for (i
=0; i
<NUM_RUNTIMES
; i
++)
307 if (runtimes
[i
].loaded_runtime
)
308 RuntimeHost_Destroy(runtimes
[i
].loaded_runtime
);
311 void expect_no_runtimes(void)
315 for (i
=0; i
<NUM_ABI_VERSIONS
; i
++)
317 loaded_mono
*mono
= &loaded_monos
[i
];
318 if (mono
->mono_handle
&& mono
->is_started
&& !mono
->is_shutdown
)
320 ERR("Process exited with a Mono runtime loaded.\n");
326 static inline CLRRuntimeInfo
*impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo
*iface
)
328 return CONTAINING_RECORD(iface
, CLRRuntimeInfo
, ICLRRuntimeInfo_iface
);
331 static HRESULT WINAPI
CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo
* iface
,
335 TRACE("%p %s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
337 if ( IsEqualGUID( riid
, &IID_ICLRRuntimeInfo
) ||
338 IsEqualGUID( riid
, &IID_IUnknown
) )
344 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
345 return E_NOINTERFACE
;
348 ICLRRuntimeInfo_AddRef( iface
);
353 static ULONG WINAPI
CLRRuntimeInfo_AddRef(ICLRRuntimeInfo
* iface
)
358 static ULONG WINAPI
CLRRuntimeInfo_Release(ICLRRuntimeInfo
* iface
)
363 static HRESULT WINAPI
CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo
* iface
,
364 LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
366 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
367 DWORD buffer_size
= *pcchBuffer
;
372 TRACE("%p %p %p\n", iface
, pwzBuffer
, pcchBuffer
);
374 size
= snprintf(version
, sizeof(version
), "v%u.%u.%u", This
->major
, This
->minor
, This
->build
);
376 assert(size
<= sizeof(version
));
378 *pcchBuffer
= MultiByteToWideChar(CP_UTF8
, 0, version
, -1, NULL
, 0);
382 if (buffer_size
>= *pcchBuffer
)
383 MultiByteToWideChar(CP_UTF8
, 0, version
, -1, pwzBuffer
, buffer_size
);
385 hr
= HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
391 static BOOL
get_install_root(LPWSTR install_dir
)
393 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};
394 const WCHAR install_root
[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
399 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, dotnet_key
, 0, KEY_READ
, &key
))
403 if (RegQueryValueExW(key
, install_root
, 0, NULL
, (LPBYTE
)install_dir
, &len
))
413 static HRESULT WINAPI
CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo
* iface
,
414 LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
416 static const WCHAR slash
[] = {'\\',0};
417 DWORD buffer_size
= *pcchBuffer
;
418 WCHAR system_dir
[MAX_PATH
];
419 WCHAR version
[MAX_PATH
];
420 DWORD version_size
, size
;
423 TRACE("%p %p %p\n", iface
, pwzBuffer
, pcchBuffer
);
425 if (!get_install_root(system_dir
))
427 ERR("error reading registry key for installroot\n");
432 version_size
= MAX_PATH
;
433 ICLRRuntimeInfo_GetVersionString(iface
, version
, &version_size
);
434 lstrcatW(system_dir
, version
);
435 lstrcatW(system_dir
, slash
);
436 size
= lstrlenW(system_dir
) + 1;
443 if (buffer_size
>= size
)
444 strcpyW(pwzBuffer
, system_dir
);
446 hr
= HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
452 static HRESULT WINAPI
CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo
* iface
,
453 HANDLE hndProcess
, BOOL
*pbLoaded
)
455 FIXME("%p %p %p\n", iface
, hndProcess
, pbLoaded
);
460 static HRESULT WINAPI
CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo
* iface
,
461 UINT iResourceID
, LPWSTR pwzBuffer
, DWORD
*pcchBuffer
, LONG iLocaleid
)
463 FIXME("%p %u %p %p %x\n", iface
, iResourceID
, pwzBuffer
, pcchBuffer
, iLocaleid
);
468 static HRESULT WINAPI
CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo
* iface
,
469 LPCWSTR pwzDllName
, HMODULE
*phndModule
)
471 WCHAR version
[MAX_PATH
];
475 TRACE("%p %s %p\n", iface
, debugstr_w(pwzDllName
), phndModule
);
477 cchBuffer
= MAX_PATH
;
478 hr
= ICLRRuntimeInfo_GetVersionString(iface
, version
, &cchBuffer
);
479 if (FAILED(hr
)) return hr
;
481 return LoadLibraryShim(pwzDllName
, version
, NULL
, phndModule
);
484 static HRESULT WINAPI
CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo
* iface
,
485 LPCSTR pszProcName
, LPVOID
*ppProc
)
487 FIXME("%p %s %p\n", iface
, debugstr_a(pszProcName
), ppProc
);
492 static HRESULT WINAPI
CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo
* iface
,
493 REFCLSID rclsid
, REFIID riid
, LPVOID
*ppUnk
)
495 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
499 TRACE("%p %s %s %p\n", iface
, debugstr_guid(rclsid
), debugstr_guid(riid
), ppUnk
);
501 hr
= CLRRuntimeInfo_GetRuntimeHost(This
, &host
);
504 hr
= RuntimeHost_GetInterface(host
, rclsid
, riid
, ppUnk
);
509 static HRESULT WINAPI
CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo
* iface
,
512 FIXME("%p %p\n", iface
, pbLoadable
);
517 static HRESULT WINAPI
CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo
* iface
,
518 DWORD dwStartupFlags
, LPCWSTR pwzHostConfigFile
)
520 FIXME("%p %x %s\n", iface
, dwStartupFlags
, debugstr_w(pwzHostConfigFile
));
525 static HRESULT WINAPI
CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo
* iface
,
526 DWORD
*pdwStartupFlags
, LPWSTR pwzHostConfigFile
, DWORD
*pcchHostConfigFile
)
528 FIXME("%p %p %p %p\n", iface
, pdwStartupFlags
, pwzHostConfigFile
, pcchHostConfigFile
);
533 static HRESULT WINAPI
CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo
* iface
)
535 FIXME("%p\n", iface
);
540 static HRESULT WINAPI
CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo
* iface
,
541 BOOL
*pbStarted
, DWORD
*pdwStartupFlags
)
543 FIXME("%p %p %p\n", iface
, pbStarted
, pdwStartupFlags
);
548 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl
= {
549 CLRRuntimeInfo_QueryInterface
,
550 CLRRuntimeInfo_AddRef
,
551 CLRRuntimeInfo_Release
,
552 CLRRuntimeInfo_GetVersionString
,
553 CLRRuntimeInfo_GetRuntimeDirectory
,
554 CLRRuntimeInfo_IsLoaded
,
555 CLRRuntimeInfo_LoadErrorString
,
556 CLRRuntimeInfo_LoadLibrary
,
557 CLRRuntimeInfo_GetProcAddress
,
558 CLRRuntimeInfo_GetInterface
,
559 CLRRuntimeInfo_IsLoadable
,
560 CLRRuntimeInfo_SetDefaultStartupFlags
,
561 CLRRuntimeInfo_GetDefaultStartupFlags
,
562 CLRRuntimeInfo_BindAsLegacyV2Runtime
,
563 CLRRuntimeInfo_IsStarted
566 HRESULT
ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo
*iface
, RuntimeHost
**result
)
568 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
570 assert(This
->ICLRRuntimeInfo_iface
.lpVtbl
== &CLRRuntimeInfoVtbl
);
572 return CLRRuntimeInfo_GetRuntimeHost(This
, result
);
576 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};
577 #elif defined(__x86_64__)
578 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};
580 static const WCHAR libmono2_arch_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
583 static BOOL
find_mono_dll(LPCWSTR path
, LPWSTR dll_path
, int abi_version
)
585 static const WCHAR mono_dll
[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
586 static const WCHAR libmono_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
587 static const WCHAR mono2_dll
[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
588 static const WCHAR libmono2_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
589 DWORD attributes
=INVALID_FILE_ATTRIBUTES
;
591 if (abi_version
== 1)
593 strcpyW(dll_path
, path
);
594 strcatW(dll_path
, mono_dll
);
595 attributes
= GetFileAttributesW(dll_path
);
597 if (attributes
== INVALID_FILE_ATTRIBUTES
)
599 strcpyW(dll_path
, path
);
600 strcatW(dll_path
, libmono_dll
);
601 attributes
= GetFileAttributesW(dll_path
);
604 else if (abi_version
== 2)
606 strcpyW(dll_path
, path
);
607 strcatW(dll_path
, libmono2_arch_dll
);
608 attributes
= GetFileAttributesW(dll_path
);
610 if (attributes
== INVALID_FILE_ATTRIBUTES
)
612 strcpyW(dll_path
, path
);
613 strcatW(dll_path
, mono2_dll
);
614 attributes
= GetFileAttributesW(dll_path
);
617 if (attributes
== INVALID_FILE_ATTRIBUTES
)
619 strcpyW(dll_path
, path
);
620 strcatW(dll_path
, libmono2_dll
);
621 attributes
= GetFileAttributesW(dll_path
);
625 return (attributes
!= INVALID_FILE_ATTRIBUTES
);
628 static BOOL
get_mono_path_from_registry(LPWSTR path
, int abi_version
)
630 static const WCHAR mono_key
[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
631 static const WCHAR defaul_clr
[] = {'D','e','f','a','u','l','t','C','L','R',0};
632 static const WCHAR install_root
[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
633 static const WCHAR slash
[] = {'\\',0};
635 WCHAR version
[64], version_key
[MAX_PATH
];
638 WCHAR dll_path
[MAX_PATH
];
640 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, mono_key
, 0, KEY_READ
, &key
))
643 len
= sizeof(version
);
644 if (RegQueryValueExW(key
, defaul_clr
, 0, NULL
, (LPBYTE
)version
, &len
))
651 lstrcpyW(version_key
, mono_key
);
652 lstrcatW(version_key
, slash
);
653 lstrcatW(version_key
, version
);
655 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, version_key
, 0, KEY_READ
, &key
))
658 len
= sizeof(WCHAR
) * MAX_PATH
;
659 if (RegQueryValueExW(key
, install_root
, 0, NULL
, (LPBYTE
)path
, &len
))
666 return find_mono_dll(path
, dll_path
, abi_version
);
669 static BOOL
get_mono_path_from_folder(LPCWSTR folder
, LPWSTR mono_path
, int abi_version
)
671 static const WCHAR mono_one_dot_zero
[] = {'\\','m','o','n','o','-','1','.','0', 0};
672 static const WCHAR mono_two_dot_zero
[] = {'\\','m','o','n','o','-','2','.','0', 0};
673 WCHAR mono_dll_path
[MAX_PATH
];
676 strcpyW(mono_path
, folder
);
678 if (abi_version
== 1)
679 strcatW(mono_path
, mono_one_dot_zero
);
680 else if (abi_version
== 2)
681 strcatW(mono_path
, mono_two_dot_zero
);
683 found
= find_mono_dll(mono_path
, mono_dll_path
, abi_version
);
688 static BOOL
get_mono_path(LPWSTR path
, int abi_version
)
690 static const WCHAR subdir_mono
[] = {'\\','m','o','n','o',0};
691 static const WCHAR sibling_mono
[] = {'\\','.','.','\\','m','o','n','o',0};
692 WCHAR base_path
[MAX_PATH
];
693 const char *unix_data_dir
;
696 static WCHAR
* (CDECL
*wine_get_dos_file_name
)(const char*);
698 /* First try c:\windows\mono */
699 GetWindowsDirectoryW(base_path
, MAX_PATH
);
700 strcatW(base_path
, subdir_mono
);
702 if (get_mono_path_from_folder(base_path
, path
, abi_version
))
705 /* Next: /usr/share/wine/mono */
706 unix_data_dir
= wine_get_data_dir();
710 unix_data_dir
= wine_get_build_dir();
716 if (!wine_get_dos_file_name
)
717 wine_get_dos_file_name
= (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
719 if (wine_get_dos_file_name
)
721 dos_data_dir
= wine_get_dos_file_name(unix_data_dir
);
725 strcpyW(base_path
, dos_data_dir
);
726 strcatW(base_path
, build_tree
? sibling_mono
: subdir_mono
);
728 HeapFree(GetProcessHeap(), 0, dos_data_dir
);
730 if (get_mono_path_from_folder(base_path
, path
, abi_version
))
736 /* Last: the registry */
737 return get_mono_path_from_registry(path
, abi_version
);
740 static void find_runtimes(void)
743 static const WCHAR libmono
[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
744 static const WCHAR mscorlib
[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
745 WCHAR mono_path
[MAX_PATH
], lib_path
[MAX_PATH
];
746 BOOL any_runtimes_found
= FALSE
;
748 if (runtimes_initialized
) return;
750 EnterCriticalSection(&runtime_list_cs
);
752 if (runtimes_initialized
) goto end
;
754 for (abi_version
=NUM_ABI_VERSIONS
; abi_version
>0; abi_version
--)
756 if (!get_mono_path(mono_path
, abi_version
))
759 for (i
=0; i
<NUM_RUNTIMES
; i
++)
761 if (runtimes
[i
].mono_abi_version
== 0)
763 strcpyW(lib_path
, mono_path
);
764 strcatW(lib_path
, libmono
);
765 strcatW(lib_path
, runtimes
[i
].mono_libdir
);
766 strcatW(lib_path
, mscorlib
);
768 if (GetFileAttributesW(lib_path
) != INVALID_FILE_ATTRIBUTES
)
770 runtimes
[i
].mono_abi_version
= abi_version
;
772 strcpyW(runtimes
[i
].mono_path
, mono_path
);
773 strcpyW(runtimes
[i
].mscorlib_path
, lib_path
);
775 any_runtimes_found
= TRUE
;
781 if (!any_runtimes_found
)
783 /* Report all runtimes are available if Mono isn't installed.
784 * FIXME: Remove this when Mono is properly packaged. */
785 for (i
=0; i
<NUM_RUNTIMES
; i
++)
786 runtimes
[i
].mono_abi_version
= -1;
789 runtimes_initialized
= 1;
792 LeaveCriticalSection(&runtime_list_cs
);
795 struct InstalledRuntimeEnum
797 IEnumUnknown IEnumUnknown_iface
;
802 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl
;
804 static inline struct InstalledRuntimeEnum
*impl_from_IEnumUnknown(IEnumUnknown
*iface
)
806 return CONTAINING_RECORD(iface
, struct InstalledRuntimeEnum
, IEnumUnknown_iface
);
809 static HRESULT WINAPI
InstalledRuntimeEnum_QueryInterface(IEnumUnknown
* iface
, REFIID riid
,
812 TRACE("%p %s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
814 if ( IsEqualGUID( riid
, &IID_IEnumUnknown
) ||
815 IsEqualGUID( riid
, &IID_IUnknown
) )
821 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
822 return E_NOINTERFACE
;
825 IEnumUnknown_AddRef( iface
);
830 static ULONG WINAPI
InstalledRuntimeEnum_AddRef(IEnumUnknown
* iface
)
832 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
833 ULONG ref
= InterlockedIncrement(&This
->ref
);
835 TRACE("(%p) refcount=%u\n", iface
, ref
);
840 static ULONG WINAPI
InstalledRuntimeEnum_Release(IEnumUnknown
* iface
)
842 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
843 ULONG ref
= InterlockedDecrement(&This
->ref
);
845 TRACE("(%p) refcount=%u\n", iface
, ref
);
849 HeapFree(GetProcessHeap(), 0, This
);
855 static HRESULT WINAPI
InstalledRuntimeEnum_Next(IEnumUnknown
*iface
, ULONG celt
,
856 IUnknown
**rgelt
, ULONG
*pceltFetched
)
858 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
863 TRACE("(%p,%u,%p,%p)\n", iface
, celt
, rgelt
, pceltFetched
);
865 while (num_fetched
< celt
)
867 if (This
->pos
>= NUM_RUNTIMES
)
872 if (runtimes
[This
->pos
].mono_abi_version
)
874 item
= (IUnknown
*)&runtimes
[This
->pos
].ICLRRuntimeInfo_iface
;
875 IUnknown_AddRef(item
);
876 rgelt
[num_fetched
] = item
;
883 *pceltFetched
= num_fetched
;
888 static HRESULT WINAPI
InstalledRuntimeEnum_Skip(IEnumUnknown
*iface
, ULONG celt
)
890 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
894 TRACE("(%p,%u)\n", iface
, celt
);
896 while (num_fetched
< celt
)
898 if (This
->pos
>= NUM_RUNTIMES
)
903 if (runtimes
[This
->pos
].mono_abi_version
)
913 static HRESULT WINAPI
InstalledRuntimeEnum_Reset(IEnumUnknown
*iface
)
915 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
917 TRACE("(%p)\n", iface
);
924 static HRESULT WINAPI
InstalledRuntimeEnum_Clone(IEnumUnknown
*iface
, IEnumUnknown
**ppenum
)
926 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
927 struct InstalledRuntimeEnum
*new_enum
;
929 TRACE("(%p)\n", iface
);
931 new_enum
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum
));
933 return E_OUTOFMEMORY
;
935 new_enum
->IEnumUnknown_iface
.lpVtbl
= &InstalledRuntimeEnum_Vtbl
;
937 new_enum
->pos
= This
->pos
;
939 *ppenum
= &new_enum
->IEnumUnknown_iface
;
944 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl
= {
945 InstalledRuntimeEnum_QueryInterface
,
946 InstalledRuntimeEnum_AddRef
,
947 InstalledRuntimeEnum_Release
,
948 InstalledRuntimeEnum_Next
,
949 InstalledRuntimeEnum_Skip
,
950 InstalledRuntimeEnum_Reset
,
951 InstalledRuntimeEnum_Clone
956 ICLRMetaHost ICLRMetaHost_iface
;
959 static struct CLRMetaHost GlobalCLRMetaHost
;
961 static HRESULT WINAPI
CLRMetaHost_QueryInterface(ICLRMetaHost
* iface
,
965 TRACE("%s %p\n", debugstr_guid(riid
), ppvObject
);
967 if ( IsEqualGUID( riid
, &IID_ICLRMetaHost
) ||
968 IsEqualGUID( riid
, &IID_IUnknown
) )
974 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
975 return E_NOINTERFACE
;
978 ICLRMetaHost_AddRef( iface
);
983 static ULONG WINAPI
CLRMetaHost_AddRef(ICLRMetaHost
* iface
)
988 static ULONG WINAPI
CLRMetaHost_Release(ICLRMetaHost
* iface
)
993 static BOOL
parse_runtime_version(LPCWSTR version
, DWORD
*major
, DWORD
*minor
, DWORD
*build
)
999 if (version
[0] == 'v' || version
[0] == 'V')
1002 if (!isdigit(*version
))
1005 while (isdigit(*version
))
1006 *major
= *major
* 10 + (*version
++ - '0');
1011 if (*version
++ != '.' || !isdigit(*version
))
1014 while (isdigit(*version
))
1015 *minor
= *minor
* 10 + (*version
++ - '0');
1020 if (*version
++ != '.' || !isdigit(*version
))
1023 while (isdigit(*version
))
1024 *build
= *build
* 10 + (*version
++ - '0');
1026 return *version
== 0;
1032 HRESULT WINAPI
CLRMetaHost_GetRuntime(ICLRMetaHost
* iface
,
1033 LPCWSTR pwzVersion
, REFIID iid
, LPVOID
*ppRuntime
)
1036 DWORD major
, minor
, build
;
1038 TRACE("%s %s %p\n", debugstr_w(pwzVersion
), debugstr_guid(iid
), ppRuntime
);
1043 if (!parse_runtime_version(pwzVersion
, &major
, &minor
, &build
))
1045 ERR("Cannot parse %s\n", debugstr_w(pwzVersion
));
1046 return CLR_E_SHIM_RUNTIME
;
1051 for (i
=0; i
<NUM_RUNTIMES
; i
++)
1053 if (runtimes
[i
].major
== major
&& runtimes
[i
].minor
== minor
&&
1054 runtimes
[i
].build
== build
)
1056 if (runtimes
[i
].mono_abi_version
)
1057 return ICLRRuntimeInfo_QueryInterface(&runtimes
[i
].ICLRRuntimeInfo_iface
, iid
,
1061 missing_runtime_message(&runtimes
[i
]);
1062 return CLR_E_SHIM_RUNTIME
;
1067 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion
));
1068 return CLR_E_SHIM_RUNTIME
;
1071 HRESULT WINAPI
CLRMetaHost_GetVersionFromFile(ICLRMetaHost
* iface
,
1072 LPCWSTR pwzFilePath
, LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
1077 ULONG buffer_size
=*pcchBuffer
;
1079 TRACE("%s %p %p\n", debugstr_w(pwzFilePath
), pwzBuffer
, pcchBuffer
);
1081 hr
= assembly_create(&assembly
, pwzFilePath
);
1085 hr
= assembly_get_runtime_version(assembly
, &version
);
1089 *pcchBuffer
= MultiByteToWideChar(CP_UTF8
, 0, version
, -1, NULL
, 0);
1093 if (buffer_size
>= *pcchBuffer
)
1094 MultiByteToWideChar(CP_UTF8
, 0, version
, -1, pwzBuffer
, buffer_size
);
1096 hr
= HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
1100 assembly_release(assembly
);
1106 static HRESULT WINAPI
CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost
* iface
,
1107 IEnumUnknown
**ppEnumerator
)
1109 struct InstalledRuntimeEnum
*new_enum
;
1111 TRACE("%p\n", ppEnumerator
);
1115 new_enum
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum
));
1117 return E_OUTOFMEMORY
;
1119 new_enum
->IEnumUnknown_iface
.lpVtbl
= &InstalledRuntimeEnum_Vtbl
;
1123 *ppEnumerator
= &new_enum
->IEnumUnknown_iface
;
1128 static HRESULT WINAPI
CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost
* iface
,
1129 HANDLE hndProcess
, IEnumUnknown
**ppEnumerator
)
1131 FIXME("%p %p\n", hndProcess
, ppEnumerator
);
1136 static HRESULT WINAPI
CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost
* iface
,
1137 RuntimeLoadedCallbackFnPtr pCallbackFunction
)
1139 FIXME("%p\n", pCallbackFunction
);
1144 static HRESULT WINAPI
CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost
* iface
,
1145 REFIID riid
, LPVOID
*ppUnk
)
1147 FIXME("%s %p\n", debugstr_guid(riid
), ppUnk
);
1152 static HRESULT WINAPI
CLRMetaHost_ExitProcess(ICLRMetaHost
* iface
, INT32 iExitCode
)
1154 FIXME("%i: stub\n", iExitCode
);
1156 ExitProcess(iExitCode
);
1159 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl
=
1161 CLRMetaHost_QueryInterface
,
1163 CLRMetaHost_Release
,
1164 CLRMetaHost_GetRuntime
,
1165 CLRMetaHost_GetVersionFromFile
,
1166 CLRMetaHost_EnumerateInstalledRuntimes
,
1167 CLRMetaHost_EnumerateLoadedRuntimes
,
1168 CLRMetaHost_RequestRuntimeLoadedNotification
,
1169 CLRMetaHost_QueryLegacyV2RuntimeBinding
,
1170 CLRMetaHost_ExitProcess
1173 static struct CLRMetaHost GlobalCLRMetaHost
= {
1174 { &CLRMetaHost_vtbl
}
1177 HRESULT
CLRMetaHost_CreateInstance(REFIID riid
, void **ppobj
)
1179 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost
.ICLRMetaHost_iface
, riid
, ppobj
);
1182 static MonoAssembly
* mono_assembly_search_hook_fn(MonoAssemblyName
*aname
, char **assemblies_path
, void *user_data
)
1184 loaded_mono
*mono
= user_data
;
1186 MonoAssembly
*result
=NULL
;
1187 char *stringname
=NULL
;
1189 int stringnameW_size
;
1190 IAssemblyCache
*asmcache
;
1192 WCHAR path
[MAX_PATH
];
1194 MonoImageOpenStatus stat
;
1195 static WCHAR fusiondll
[] = {'f','u','s','i','o','n',0};
1196 HMODULE hfusion
=NULL
;
1197 static HRESULT (WINAPI
*pCreateAssemblyCache
)(IAssemblyCache
**,DWORD
);
1199 stringname
= mono
->mono_stringify_assembly_name(aname
);
1201 TRACE("%s\n", debugstr_a(stringname
));
1203 if (!stringname
) return NULL
;
1205 /* FIXME: We should search the given paths before the GAC. */
1207 if (!pCreateAssemblyCache
)
1209 hr
= LoadLibraryShim(fusiondll
, NULL
, NULL
, &hfusion
);
1213 pCreateAssemblyCache
= (void*)GetProcAddress(hfusion
, "CreateAssemblyCache");
1214 if (!pCreateAssemblyCache
)
1220 hr
= pCreateAssemblyCache(&asmcache
, 0);
1224 stringnameW_size
= MultiByteToWideChar(CP_UTF8
, 0, stringname
, -1, NULL
, 0);
1226 stringnameW
= HeapAlloc(GetProcessHeap(), 0, stringnameW_size
* sizeof(WCHAR
));
1228 MultiByteToWideChar(CP_UTF8
, 0, stringname
, -1, stringnameW
, stringnameW_size
);
1234 info
.cbAssemblyInfo
= sizeof(info
);
1235 info
.pszCurrentAssemblyPathBuf
= path
;
1236 info
.cchBuf
= MAX_PATH
;
1239 hr
= IAssemblyCache_QueryAssemblyInfo(asmcache
, 0, stringnameW
, &info
);
1242 HeapFree(GetProcessHeap(), 0, stringnameW
);
1244 IAssemblyCache_Release(asmcache
);
1249 TRACE("found: %s\n", debugstr_w(path
));
1255 result
= mono
->mono_assembly_open(pathA
, &stat
);
1258 ERR("Failed to load %s, status=%u\n", debugstr_w(path
), stat
);
1260 HeapFree(GetProcessHeap(), 0, pathA
);
1264 mono
->mono_free(stringname
);
1269 HRESULT
get_runtime_info(LPCWSTR exefile
, LPCWSTR version
, LPCWSTR config_file
,
1270 DWORD startup_flags
, DWORD runtimeinfo_flags
, BOOL legacy
, ICLRRuntimeInfo
**result
)
1272 static const WCHAR dotconfig
[] = {'.','c','o','n','f','i','g',0};
1273 static const DWORD supported_startup_flags
= 0;
1274 static const DWORD supported_runtime_flags
= RUNTIME_INFO_UPGRADE_VERSION
;
1276 WCHAR local_version
[MAX_PATH
];
1277 ULONG local_version_size
= MAX_PATH
;
1278 WCHAR local_config_file
[MAX_PATH
];
1280 parsed_config_file parsed_config
;
1282 if (startup_flags
& ~supported_startup_flags
)
1283 FIXME("unsupported startup flags %x\n", startup_flags
& ~supported_startup_flags
);
1285 if (runtimeinfo_flags
& ~supported_runtime_flags
)
1286 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags
& ~supported_runtime_flags
);
1288 if (exefile
&& !config_file
)
1290 strcpyW(local_config_file
, exefile
);
1291 strcatW(local_config_file
, dotconfig
);
1293 config_file
= local_config_file
;
1299 hr
= parse_config_file(config_file
, &parsed_config
);
1303 supported_runtime
*entry
;
1304 LIST_FOR_EACH_ENTRY(entry
, &parsed_config
.supported_runtimes
, supported_runtime
, entry
)
1306 hr
= CLRMetaHost_GetRuntime(0, entry
->version
, &IID_ICLRRuntimeInfo
, (void**)result
);
1316 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file
), hr
);
1319 free_parsed_config_file(&parsed_config
);
1325 if (exefile
&& !version
)
1327 hr
= CLRMetaHost_GetVersionFromFile(0, exefile
, local_version
, &local_version_size
);
1329 version
= local_version
;
1331 if (FAILED(hr
)) return hr
;
1336 hr
= CLRMetaHost_GetRuntime(0, version
, &IID_ICLRRuntimeInfo
, (void**)result
);
1341 if (runtimeinfo_flags
& RUNTIME_INFO_UPGRADE_VERSION
)
1343 DWORD major
, minor
, build
;
1345 if (version
&& !parse_runtime_version(version
, &major
, &minor
, &build
))
1347 ERR("Cannot parse %s\n", debugstr_w(version
));
1348 return CLR_E_SHIM_RUNTIME
;
1360 if (runtimes
[i
].mono_abi_version
)
1362 /* Must be greater or equal to the version passed in. */
1363 if (!version
|| ((runtimes
[i
].major
>= major
&& runtimes
[i
].minor
>= minor
&& runtimes
[i
].build
>= build
) ||
1364 (runtimes
[i
].major
>= major
&& runtimes
[i
].minor
> minor
) ||
1365 (runtimes
[i
].major
> major
)))
1367 return ICLRRuntimeInfo_QueryInterface(&runtimes
[i
].ICLRRuntimeInfo_iface
,
1368 &IID_ICLRRuntimeInfo
, (void **)result
);
1374 missing_runtime_message(&runtimes
[1]);
1376 missing_runtime_message(&runtimes
[NUM_RUNTIMES
-1]);
1378 return CLR_E_SHIM_RUNTIME
;
1381 return CLR_E_SHIM_RUNTIME
;