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_11_subdir
[] = {'1','.','0',0};
49 static const WCHAR net_20_subdir
[] = {'2','.','0',0};
50 static const WCHAR net_40_subdir
[] = {'4','.','0',0};
52 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl
;
54 #define NUM_RUNTIMES 3
56 static struct CLRRuntimeInfo runtimes
[NUM_RUNTIMES
] = {
57 {{&CLRRuntimeInfoVtbl
}, net_11_subdir
, 1, 1, 4322, 0},
58 {{&CLRRuntimeInfoVtbl
}, net_20_subdir
, 2, 0, 50727, 0},
59 {{&CLRRuntimeInfoVtbl
}, net_40_subdir
, 4, 0, 30319, 0}
62 static int runtimes_initialized
;
64 static CRITICAL_SECTION runtime_list_cs
;
65 static CRITICAL_SECTION_DEBUG runtime_list_cs_debug
=
67 0, 0, &runtime_list_cs
,
68 { &runtime_list_cs_debug
.ProcessLocksList
,
69 &runtime_list_cs_debug
.ProcessLocksList
},
70 0, 0, { (DWORD_PTR
)(__FILE__
": runtime_list_cs") }
72 static CRITICAL_SECTION runtime_list_cs
= { &runtime_list_cs_debug
, -1, 0, 0, 0, 0 };
74 #define NUM_ABI_VERSIONS 2
76 static loaded_mono loaded_monos
[NUM_ABI_VERSIONS
];
78 static BOOL
find_mono_dll(LPCWSTR path
, LPWSTR dll_path
, int abi_version
);
80 static MonoAssembly
* mono_assembly_search_hook_fn(MonoAssemblyName
*aname
, char **assemblies_path
, void *user_data
);
82 static void mono_shutdown_callback_fn(MonoProfiler
*prof
);
84 static void set_environment(LPCWSTR bin_path
)
86 WCHAR path_env
[MAX_PATH
];
89 static const WCHAR pathW
[] = {'P','A','T','H',0};
91 /* We have to modify PATH as Mono loads other DLLs from this directory. */
92 GetEnvironmentVariableW(pathW
, path_env
, sizeof(path_env
)/sizeof(WCHAR
));
93 len
= strlenW(path_env
);
94 path_env
[len
++] = ';';
95 strcpyW(path_env
+len
, bin_path
);
96 SetEnvironmentVariableW(pathW
, path_env
);
99 static void CDECL
do_nothing(void)
103 static void missing_runtime_message(const CLRRuntimeInfo
*This
)
105 if (This
->major
== 1)
106 MESSAGE("wine: Install Mono 2.6 for Windows to run .NET 1.1 applications.\n");
107 else if (This
->major
== 2)
108 MESSAGE("wine: Install Mono for Windows to run .NET 2.0 applications.\n");
109 else if (This
->major
== 4)
110 MESSAGE("wine: Install Mono 2.8 or greater for Windows to run .NET 4.0 applications.\n");
113 static HRESULT
load_mono(CLRRuntimeInfo
*This
, loaded_mono
**result
)
115 static const WCHAR bin
[] = {'\\','b','i','n',0};
116 static const WCHAR lib
[] = {'\\','l','i','b',0};
117 static const WCHAR etc
[] = {'\\','e','t','c',0};
118 static const WCHAR glibdll
[] = {'l','i','b','g','l','i','b','-','2','.','0','-','0','.','d','l','l',0};
119 WCHAR mono_dll_path
[MAX_PATH
+16], mono_bin_path
[MAX_PATH
+4];
120 WCHAR mono_lib_path
[MAX_PATH
+4], mono_etc_path
[MAX_PATH
+4];
121 char mono_lib_path_a
[MAX_PATH
], mono_etc_path_a
[MAX_PATH
];
123 char trace_setting
[256];
125 if (This
->mono_abi_version
<= 0 || This
->mono_abi_version
> NUM_ABI_VERSIONS
)
127 missing_runtime_message(This
);
131 *result
= &loaded_monos
[This
->mono_abi_version
-1];
133 if ((*result
)->is_shutdown
)
135 ERR("Cannot load Mono after it has been shut down.\n");
140 if (!(*result
)->mono_handle
)
142 strcpyW(mono_bin_path
, This
->mono_path
);
143 strcatW(mono_bin_path
, bin
);
144 set_environment(mono_bin_path
);
146 strcpyW(mono_lib_path
, This
->mono_path
);
147 strcatW(mono_lib_path
, lib
);
148 WideCharToMultiByte(CP_UTF8
, 0, mono_lib_path
, -1, mono_lib_path_a
, MAX_PATH
, NULL
, NULL
);
150 strcpyW(mono_etc_path
, This
->mono_path
);
151 strcatW(mono_etc_path
, etc
);
152 WideCharToMultiByte(CP_UTF8
, 0, mono_etc_path
, -1, mono_etc_path_a
, MAX_PATH
, NULL
, NULL
);
154 if (!find_mono_dll(This
->mono_path
, mono_dll_path
, This
->mono_abi_version
)) goto fail
;
156 (*result
)->mono_handle
= LoadLibraryW(mono_dll_path
);
158 if (!(*result
)->mono_handle
) goto fail
;
160 #define LOAD_MONO_FUNCTION(x) do { \
161 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
162 if (!(*result)->x) { \
167 LOAD_MONO_FUNCTION(mono_assembly_get_image
);
168 LOAD_MONO_FUNCTION(mono_assembly_load_from
);
169 LOAD_MONO_FUNCTION(mono_assembly_open
);
170 LOAD_MONO_FUNCTION(mono_config_parse
);
171 LOAD_MONO_FUNCTION(mono_class_from_mono_type
);
172 LOAD_MONO_FUNCTION(mono_class_from_name
);
173 LOAD_MONO_FUNCTION(mono_class_get_method_from_name
);
174 LOAD_MONO_FUNCTION(mono_domain_assembly_open
);
175 LOAD_MONO_FUNCTION(mono_image_open_from_module_handle
);
176 LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook
);
177 LOAD_MONO_FUNCTION(mono_jit_exec
);
178 LOAD_MONO_FUNCTION(mono_jit_init
);
179 LOAD_MONO_FUNCTION(mono_jit_set_trace_options
);
180 LOAD_MONO_FUNCTION(mono_marshal_get_vtfixup_ftnptr
);
181 LOAD_MONO_FUNCTION(mono_object_get_domain
);
182 LOAD_MONO_FUNCTION(mono_object_new
);
183 LOAD_MONO_FUNCTION(mono_object_unbox
);
184 LOAD_MONO_FUNCTION(mono_profiler_install
);
185 LOAD_MONO_FUNCTION(mono_reflection_type_from_name
);
186 LOAD_MONO_FUNCTION(mono_runtime_invoke
);
187 LOAD_MONO_FUNCTION(mono_runtime_object_init
);
188 LOAD_MONO_FUNCTION(mono_runtime_quit
);
189 LOAD_MONO_FUNCTION(mono_set_dirs
);
190 LOAD_MONO_FUNCTION(mono_stringify_assembly_name
);
191 LOAD_MONO_FUNCTION(mono_string_new
);
192 LOAD_MONO_FUNCTION(mono_thread_attach
);
194 /* GLib imports obsoleted by the 2.0 ABI */
195 if (This
->mono_abi_version
== 1)
197 (*result
)->glib_handle
= LoadLibraryW(glibdll
);
198 if (!(*result
)->glib_handle
) goto fail
;
200 (*result
)->mono_free
= (void*)GetProcAddress((*result
)->glib_handle
, "g_free");
201 if (!(*result
)->mono_free
) goto fail
;
205 LOAD_MONO_FUNCTION(mono_free
);
208 #undef LOAD_MONO_FUNCTION
210 #define LOAD_OPT_VOID_MONO_FUNCTION(x) do { \
211 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
212 if (!(*result)->x) { \
213 (*result)->x = do_nothing; \
217 LOAD_OPT_VOID_MONO_FUNCTION(mono_runtime_set_shutting_down
);
218 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_pool_cleanup
);
219 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_suspend_all_other_threads
);
220 LOAD_OPT_VOID_MONO_FUNCTION(mono_threads_set_shutting_down
);
222 #undef LOAD_OPT_VOID_MONO_FUNCTION
224 (*result
)->mono_profiler_install((MonoProfiler
*)*result
, mono_shutdown_callback_fn
);
226 (*result
)->mono_set_dirs(mono_lib_path_a
, mono_etc_path_a
);
228 (*result
)->mono_config_parse(NULL
);
230 (*result
)->mono_install_assembly_preload_hook(mono_assembly_search_hook_fn
, *result
);
232 trace_size
= GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting
, sizeof(trace_setting
));
236 (*result
)->mono_jit_set_trace_options(trace_setting
);
243 ERR("Could not load Mono into this process\n");
244 FreeLibrary((*result
)->mono_handle
);
245 FreeLibrary((*result
)->glib_handle
);
246 (*result
)->mono_handle
= NULL
;
247 (*result
)->glib_handle
= NULL
;
251 static void mono_shutdown_callback_fn(MonoProfiler
*prof
)
253 loaded_mono
*mono
= (loaded_mono
*)prof
;
255 mono
->is_shutdown
= TRUE
;
258 static HRESULT
CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo
*This
, RuntimeHost
**result
)
261 loaded_mono
*ploaded_mono
;
263 if (This
->loaded_runtime
)
265 *result
= This
->loaded_runtime
;
269 EnterCriticalSection(&runtime_list_cs
);
271 hr
= load_mono(This
, &ploaded_mono
);
274 hr
= RuntimeHost_Construct(This
, ploaded_mono
, &This
->loaded_runtime
);
276 LeaveCriticalSection(&runtime_list_cs
);
279 *result
= This
->loaded_runtime
;
284 void unload_all_runtimes(void)
288 for (i
=0; i
<NUM_ABI_VERSIONS
; i
++)
290 loaded_mono
*mono
= &loaded_monos
[i
];
291 if (mono
->mono_handle
&& mono
->is_started
&& !mono
->is_shutdown
)
293 /* Copied from Mono's ves_icall_System_Environment_Exit */
294 mono
->mono_threads_set_shutting_down();
295 mono
->mono_runtime_set_shutting_down();
296 mono
->mono_thread_pool_cleanup();
297 mono
->mono_thread_suspend_all_other_threads();
298 mono
->mono_runtime_quit();
302 for (i
=0; i
<NUM_RUNTIMES
; i
++)
303 if (runtimes
[i
].loaded_runtime
)
304 RuntimeHost_Destroy(runtimes
[i
].loaded_runtime
);
307 void expect_no_runtimes(void)
311 for (i
=0; i
<NUM_ABI_VERSIONS
; i
++)
313 loaded_mono
*mono
= &loaded_monos
[i
];
314 if (mono
->mono_handle
&& mono
->is_started
&& !mono
->is_shutdown
)
316 ERR("Process exited with a Mono runtime loaded.\n");
322 static inline CLRRuntimeInfo
*impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo
*iface
)
324 return CONTAINING_RECORD(iface
, CLRRuntimeInfo
, ICLRRuntimeInfo_iface
);
327 static HRESULT WINAPI
CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo
* iface
,
331 TRACE("%p %s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
333 if ( IsEqualGUID( riid
, &IID_ICLRRuntimeInfo
) ||
334 IsEqualGUID( riid
, &IID_IUnknown
) )
340 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
341 return E_NOINTERFACE
;
344 ICLRRuntimeInfo_AddRef( iface
);
349 static ULONG WINAPI
CLRRuntimeInfo_AddRef(ICLRRuntimeInfo
* iface
)
354 static ULONG WINAPI
CLRRuntimeInfo_Release(ICLRRuntimeInfo
* iface
)
359 static HRESULT WINAPI
CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo
* iface
,
360 LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
362 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
363 DWORD buffer_size
= *pcchBuffer
;
368 TRACE("%p %p %p\n", iface
, pwzBuffer
, pcchBuffer
);
370 size
= snprintf(version
, sizeof(version
), "v%u.%u.%u", This
->major
, This
->minor
, This
->build
);
372 assert(size
<= sizeof(version
));
374 *pcchBuffer
= MultiByteToWideChar(CP_UTF8
, 0, version
, -1, NULL
, 0);
378 if (buffer_size
>= *pcchBuffer
)
379 MultiByteToWideChar(CP_UTF8
, 0, version
, -1, pwzBuffer
, buffer_size
);
381 hr
= HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
387 static BOOL
get_install_root(LPWSTR install_dir
)
389 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};
390 const WCHAR install_root
[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
395 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, dotnet_key
, 0, KEY_READ
, &key
))
399 if (RegQueryValueExW(key
, install_root
, 0, NULL
, (LPBYTE
)install_dir
, &len
))
409 static HRESULT WINAPI
CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo
* iface
,
410 LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
412 static const WCHAR slash
[] = {'\\',0};
413 DWORD buffer_size
= *pcchBuffer
;
414 WCHAR system_dir
[MAX_PATH
];
415 WCHAR version
[MAX_PATH
];
416 DWORD version_size
, size
;
419 TRACE("%p %p %p\n", iface
, pwzBuffer
, pcchBuffer
);
421 if (!get_install_root(system_dir
))
423 ERR("error reading registry key for installroot\n");
428 version_size
= MAX_PATH
;
429 ICLRRuntimeInfo_GetVersionString(iface
, version
, &version_size
);
430 lstrcatW(system_dir
, version
);
431 lstrcatW(system_dir
, slash
);
432 size
= lstrlenW(system_dir
) + 1;
439 if (buffer_size
>= size
)
440 strcpyW(pwzBuffer
, system_dir
);
442 hr
= HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
448 static HRESULT WINAPI
CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo
* iface
,
449 HANDLE hndProcess
, BOOL
*pbLoaded
)
451 FIXME("%p %p %p\n", iface
, hndProcess
, pbLoaded
);
456 static HRESULT WINAPI
CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo
* iface
,
457 UINT iResourceID
, LPWSTR pwzBuffer
, DWORD
*pcchBuffer
, LONG iLocaleid
)
459 FIXME("%p %u %p %p %x\n", iface
, iResourceID
, pwzBuffer
, pcchBuffer
, iLocaleid
);
464 static HRESULT WINAPI
CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo
* iface
,
465 LPCWSTR pwzDllName
, HMODULE
*phndModule
)
467 WCHAR version
[MAX_PATH
];
471 TRACE("%p %s %p\n", iface
, debugstr_w(pwzDllName
), phndModule
);
473 cchBuffer
= MAX_PATH
;
474 hr
= ICLRRuntimeInfo_GetVersionString(iface
, version
, &cchBuffer
);
475 if (FAILED(hr
)) return hr
;
477 return LoadLibraryShim(pwzDllName
, version
, NULL
, phndModule
);
480 static HRESULT WINAPI
CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo
* iface
,
481 LPCSTR pszProcName
, LPVOID
*ppProc
)
483 FIXME("%p %s %p\n", iface
, debugstr_a(pszProcName
), ppProc
);
488 static HRESULT WINAPI
CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo
* iface
,
489 REFCLSID rclsid
, REFIID riid
, LPVOID
*ppUnk
)
491 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
495 TRACE("%p %s %s %p\n", iface
, debugstr_guid(rclsid
), debugstr_guid(riid
), ppUnk
);
497 hr
= CLRRuntimeInfo_GetRuntimeHost(This
, &host
);
500 hr
= RuntimeHost_GetInterface(host
, rclsid
, riid
, ppUnk
);
505 static HRESULT WINAPI
CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo
* iface
,
508 FIXME("%p %p\n", iface
, pbLoadable
);
513 static HRESULT WINAPI
CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo
* iface
,
514 DWORD dwStartupFlags
, LPCWSTR pwzHostConfigFile
)
516 FIXME("%p %x %s\n", iface
, dwStartupFlags
, debugstr_w(pwzHostConfigFile
));
521 static HRESULT WINAPI
CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo
* iface
,
522 DWORD
*pdwStartupFlags
, LPWSTR pwzHostConfigFile
, DWORD
*pcchHostConfigFile
)
524 FIXME("%p %p %p %p\n", iface
, pdwStartupFlags
, pwzHostConfigFile
, pcchHostConfigFile
);
529 static HRESULT WINAPI
CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo
* iface
)
531 FIXME("%p\n", iface
);
536 static HRESULT WINAPI
CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo
* iface
,
537 BOOL
*pbStarted
, DWORD
*pdwStartupFlags
)
539 FIXME("%p %p %p\n", iface
, pbStarted
, pdwStartupFlags
);
544 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl
= {
545 CLRRuntimeInfo_QueryInterface
,
546 CLRRuntimeInfo_AddRef
,
547 CLRRuntimeInfo_Release
,
548 CLRRuntimeInfo_GetVersionString
,
549 CLRRuntimeInfo_GetRuntimeDirectory
,
550 CLRRuntimeInfo_IsLoaded
,
551 CLRRuntimeInfo_LoadErrorString
,
552 CLRRuntimeInfo_LoadLibrary
,
553 CLRRuntimeInfo_GetProcAddress
,
554 CLRRuntimeInfo_GetInterface
,
555 CLRRuntimeInfo_IsLoadable
,
556 CLRRuntimeInfo_SetDefaultStartupFlags
,
557 CLRRuntimeInfo_GetDefaultStartupFlags
,
558 CLRRuntimeInfo_BindAsLegacyV2Runtime
,
559 CLRRuntimeInfo_IsStarted
562 HRESULT
ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo
*iface
, RuntimeHost
**result
)
564 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
566 assert(This
->ICLRRuntimeInfo_iface
.lpVtbl
== &CLRRuntimeInfoVtbl
);
568 return CLRRuntimeInfo_GetRuntimeHost(This
, result
);
572 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};
573 #elif defined(__x86_64__)
574 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};
576 static const WCHAR libmono2_arch_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
579 static BOOL
find_mono_dll(LPCWSTR path
, LPWSTR dll_path
, int abi_version
)
581 static const WCHAR mono_dll
[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
582 static const WCHAR libmono_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
583 static const WCHAR mono2_dll
[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
584 static const WCHAR libmono2_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
585 DWORD attributes
=INVALID_FILE_ATTRIBUTES
;
587 if (abi_version
== 1)
589 strcpyW(dll_path
, path
);
590 strcatW(dll_path
, mono_dll
);
591 attributes
= GetFileAttributesW(dll_path
);
593 if (attributes
== INVALID_FILE_ATTRIBUTES
)
595 strcpyW(dll_path
, path
);
596 strcatW(dll_path
, libmono_dll
);
597 attributes
= GetFileAttributesW(dll_path
);
600 else if (abi_version
== 2)
602 strcpyW(dll_path
, path
);
603 strcatW(dll_path
, libmono2_arch_dll
);
604 attributes
= GetFileAttributesW(dll_path
);
606 if (attributes
== INVALID_FILE_ATTRIBUTES
)
608 strcpyW(dll_path
, path
);
609 strcatW(dll_path
, mono2_dll
);
610 attributes
= GetFileAttributesW(dll_path
);
613 if (attributes
== INVALID_FILE_ATTRIBUTES
)
615 strcpyW(dll_path
, path
);
616 strcatW(dll_path
, libmono2_dll
);
617 attributes
= GetFileAttributesW(dll_path
);
621 return (attributes
!= INVALID_FILE_ATTRIBUTES
);
624 static BOOL
get_mono_path_from_registry(LPWSTR path
, int abi_version
)
626 static const WCHAR mono_key
[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
627 static const WCHAR defaul_clr
[] = {'D','e','f','a','u','l','t','C','L','R',0};
628 static const WCHAR install_root
[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
629 static const WCHAR slash
[] = {'\\',0};
631 WCHAR version
[64], version_key
[MAX_PATH
];
634 WCHAR dll_path
[MAX_PATH
];
636 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, mono_key
, 0, KEY_READ
, &key
))
639 len
= sizeof(version
);
640 if (RegQueryValueExW(key
, defaul_clr
, 0, NULL
, (LPBYTE
)version
, &len
))
647 lstrcpyW(version_key
, mono_key
);
648 lstrcatW(version_key
, slash
);
649 lstrcatW(version_key
, version
);
651 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, version_key
, 0, KEY_READ
, &key
))
654 len
= sizeof(WCHAR
) * MAX_PATH
;
655 if (RegQueryValueExW(key
, install_root
, 0, NULL
, (LPBYTE
)path
, &len
))
662 return find_mono_dll(path
, dll_path
, abi_version
);
665 static BOOL
get_mono_path_from_folder(LPCWSTR folder
, LPWSTR mono_path
, int abi_version
)
667 static const WCHAR mono_one_dot_zero
[] = {'\\','m','o','n','o','-','1','.','0', 0};
668 static const WCHAR mono_two_dot_zero
[] = {'\\','m','o','n','o','-','2','.','0', 0};
669 WCHAR mono_dll_path
[MAX_PATH
];
672 strcpyW(mono_path
, folder
);
674 if (abi_version
== 1)
675 strcatW(mono_path
, mono_one_dot_zero
);
676 else if (abi_version
== 2)
677 strcatW(mono_path
, mono_two_dot_zero
);
679 found
= find_mono_dll(mono_path
, mono_dll_path
, abi_version
);
684 static BOOL
get_mono_path(LPWSTR path
, int abi_version
)
686 static const WCHAR subdir_mono
[] = {'\\','m','o','n','o',0};
687 static const WCHAR sibling_mono
[] = {'\\','.','.','\\','m','o','n','o',0};
688 WCHAR base_path
[MAX_PATH
];
689 const char *unix_data_dir
;
692 static WCHAR
* (CDECL
*wine_get_dos_file_name
)(const char*);
694 /* First try c:\windows\mono */
695 GetWindowsDirectoryW(base_path
, MAX_PATH
);
696 strcatW(base_path
, subdir_mono
);
698 if (get_mono_path_from_folder(base_path
, path
, abi_version
))
701 /* Next: /usr/share/wine/mono */
702 unix_data_dir
= wine_get_data_dir();
706 unix_data_dir
= wine_get_build_dir();
712 if (!wine_get_dos_file_name
)
713 wine_get_dos_file_name
= (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
715 if (wine_get_dos_file_name
)
717 dos_data_dir
= wine_get_dos_file_name(unix_data_dir
);
721 strcpyW(base_path
, dos_data_dir
);
722 strcatW(base_path
, build_tree
? sibling_mono
: subdir_mono
);
724 HeapFree(GetProcessHeap(), 0, dos_data_dir
);
726 if (get_mono_path_from_folder(base_path
, path
, abi_version
))
732 /* Last: the registry */
733 return get_mono_path_from_registry(path
, abi_version
);
736 static void find_runtimes(void)
739 static const WCHAR libmono
[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
740 static const WCHAR mscorlib
[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
741 WCHAR mono_path
[MAX_PATH
], lib_path
[MAX_PATH
];
742 BOOL any_runtimes_found
= FALSE
;
744 if (runtimes_initialized
) return;
746 EnterCriticalSection(&runtime_list_cs
);
748 if (runtimes_initialized
) goto end
;
750 for (abi_version
=NUM_ABI_VERSIONS
; abi_version
>0; abi_version
--)
752 if (!get_mono_path(mono_path
, abi_version
))
755 for (i
=0; i
<NUM_RUNTIMES
; i
++)
757 if (runtimes
[i
].mono_abi_version
== 0)
759 strcpyW(lib_path
, mono_path
);
760 strcatW(lib_path
, libmono
);
761 strcatW(lib_path
, runtimes
[i
].mono_libdir
);
762 strcatW(lib_path
, mscorlib
);
764 if (GetFileAttributesW(lib_path
) != INVALID_FILE_ATTRIBUTES
)
766 runtimes
[i
].mono_abi_version
= abi_version
;
768 strcpyW(runtimes
[i
].mono_path
, mono_path
);
769 strcpyW(runtimes
[i
].mscorlib_path
, lib_path
);
771 any_runtimes_found
= TRUE
;
777 if (!any_runtimes_found
)
779 /* Report all runtimes are available if Mono isn't installed.
780 * FIXME: Remove this when Mono is properly packaged. */
781 for (i
=0; i
<NUM_RUNTIMES
; i
++)
782 runtimes
[i
].mono_abi_version
= -1;
785 runtimes_initialized
= 1;
788 LeaveCriticalSection(&runtime_list_cs
);
791 struct InstalledRuntimeEnum
793 IEnumUnknown IEnumUnknown_iface
;
798 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl
;
800 static inline struct InstalledRuntimeEnum
*impl_from_IEnumUnknown(IEnumUnknown
*iface
)
802 return CONTAINING_RECORD(iface
, struct InstalledRuntimeEnum
, IEnumUnknown_iface
);
805 static HRESULT WINAPI
InstalledRuntimeEnum_QueryInterface(IEnumUnknown
* iface
, REFIID riid
,
808 TRACE("%p %s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
810 if ( IsEqualGUID( riid
, &IID_IEnumUnknown
) ||
811 IsEqualGUID( riid
, &IID_IUnknown
) )
817 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
818 return E_NOINTERFACE
;
821 IEnumUnknown_AddRef( iface
);
826 static ULONG WINAPI
InstalledRuntimeEnum_AddRef(IEnumUnknown
* iface
)
828 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
829 ULONG ref
= InterlockedIncrement(&This
->ref
);
831 TRACE("(%p) refcount=%u\n", iface
, ref
);
836 static ULONG WINAPI
InstalledRuntimeEnum_Release(IEnumUnknown
* iface
)
838 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
839 ULONG ref
= InterlockedDecrement(&This
->ref
);
841 TRACE("(%p) refcount=%u\n", iface
, ref
);
845 HeapFree(GetProcessHeap(), 0, This
);
851 static HRESULT WINAPI
InstalledRuntimeEnum_Next(IEnumUnknown
*iface
, ULONG celt
,
852 IUnknown
**rgelt
, ULONG
*pceltFetched
)
854 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
859 TRACE("(%p,%u,%p,%p)\n", iface
, celt
, rgelt
, pceltFetched
);
861 while (num_fetched
< celt
)
863 if (This
->pos
>= NUM_RUNTIMES
)
868 if (runtimes
[This
->pos
].mono_abi_version
)
870 item
= (IUnknown
*)&runtimes
[This
->pos
].ICLRRuntimeInfo_iface
;
871 IUnknown_AddRef(item
);
872 rgelt
[num_fetched
] = item
;
879 *pceltFetched
= num_fetched
;
884 static HRESULT WINAPI
InstalledRuntimeEnum_Skip(IEnumUnknown
*iface
, ULONG celt
)
886 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
890 TRACE("(%p,%u)\n", iface
, celt
);
892 while (num_fetched
< celt
)
894 if (This
->pos
>= NUM_RUNTIMES
)
899 if (runtimes
[This
->pos
].mono_abi_version
)
909 static HRESULT WINAPI
InstalledRuntimeEnum_Reset(IEnumUnknown
*iface
)
911 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
913 TRACE("(%p)\n", iface
);
920 static HRESULT WINAPI
InstalledRuntimeEnum_Clone(IEnumUnknown
*iface
, IEnumUnknown
**ppenum
)
922 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
923 struct InstalledRuntimeEnum
*new_enum
;
925 TRACE("(%p)\n", iface
);
927 new_enum
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum
));
929 return E_OUTOFMEMORY
;
931 new_enum
->IEnumUnknown_iface
.lpVtbl
= &InstalledRuntimeEnum_Vtbl
;
933 new_enum
->pos
= This
->pos
;
935 *ppenum
= &new_enum
->IEnumUnknown_iface
;
940 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl
= {
941 InstalledRuntimeEnum_QueryInterface
,
942 InstalledRuntimeEnum_AddRef
,
943 InstalledRuntimeEnum_Release
,
944 InstalledRuntimeEnum_Next
,
945 InstalledRuntimeEnum_Skip
,
946 InstalledRuntimeEnum_Reset
,
947 InstalledRuntimeEnum_Clone
952 ICLRMetaHost ICLRMetaHost_iface
;
955 static struct CLRMetaHost GlobalCLRMetaHost
;
957 static HRESULT WINAPI
CLRMetaHost_QueryInterface(ICLRMetaHost
* iface
,
961 TRACE("%s %p\n", debugstr_guid(riid
), ppvObject
);
963 if ( IsEqualGUID( riid
, &IID_ICLRMetaHost
) ||
964 IsEqualGUID( riid
, &IID_IUnknown
) )
970 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
971 return E_NOINTERFACE
;
974 ICLRMetaHost_AddRef( iface
);
979 static ULONG WINAPI
CLRMetaHost_AddRef(ICLRMetaHost
* iface
)
984 static ULONG WINAPI
CLRMetaHost_Release(ICLRMetaHost
* iface
)
989 static BOOL
parse_runtime_version(LPCWSTR version
, DWORD
*major
, DWORD
*minor
, DWORD
*build
)
995 if (version
[0] == 'v' || version
[0] == 'V')
998 if (!isdigit(*version
))
1001 while (isdigit(*version
))
1002 *major
= *major
* 10 + (*version
++ - '0');
1007 if (*version
++ != '.' || !isdigit(*version
))
1010 while (isdigit(*version
))
1011 *minor
= *minor
* 10 + (*version
++ - '0');
1016 if (*version
++ != '.' || !isdigit(*version
))
1019 while (isdigit(*version
))
1020 *build
= *build
* 10 + (*version
++ - '0');
1022 return *version
== 0;
1028 HRESULT WINAPI
CLRMetaHost_GetRuntime(ICLRMetaHost
* iface
,
1029 LPCWSTR pwzVersion
, REFIID iid
, LPVOID
*ppRuntime
)
1032 DWORD major
, minor
, build
;
1034 TRACE("%s %s %p\n", debugstr_w(pwzVersion
), debugstr_guid(iid
), ppRuntime
);
1039 if (!parse_runtime_version(pwzVersion
, &major
, &minor
, &build
))
1041 ERR("Cannot parse %s\n", debugstr_w(pwzVersion
));
1042 return CLR_E_SHIM_RUNTIME
;
1047 for (i
=0; i
<NUM_RUNTIMES
; i
++)
1049 if (runtimes
[i
].major
== major
&& runtimes
[i
].minor
== minor
&&
1050 runtimes
[i
].build
== build
)
1052 if (runtimes
[i
].mono_abi_version
)
1053 return ICLRRuntimeInfo_QueryInterface(&runtimes
[i
].ICLRRuntimeInfo_iface
, iid
,
1057 missing_runtime_message(&runtimes
[i
]);
1058 return CLR_E_SHIM_RUNTIME
;
1063 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion
));
1064 return CLR_E_SHIM_RUNTIME
;
1067 HRESULT WINAPI
CLRMetaHost_GetVersionFromFile(ICLRMetaHost
* iface
,
1068 LPCWSTR pwzFilePath
, LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
1073 ULONG buffer_size
=*pcchBuffer
;
1075 TRACE("%s %p %p\n", debugstr_w(pwzFilePath
), pwzBuffer
, pcchBuffer
);
1077 hr
= assembly_create(&assembly
, pwzFilePath
);
1081 hr
= assembly_get_runtime_version(assembly
, &version
);
1085 *pcchBuffer
= MultiByteToWideChar(CP_UTF8
, 0, version
, -1, NULL
, 0);
1089 if (buffer_size
>= *pcchBuffer
)
1090 MultiByteToWideChar(CP_UTF8
, 0, version
, -1, pwzBuffer
, buffer_size
);
1092 hr
= HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
1096 assembly_release(assembly
);
1102 static HRESULT WINAPI
CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost
* iface
,
1103 IEnumUnknown
**ppEnumerator
)
1105 struct InstalledRuntimeEnum
*new_enum
;
1107 TRACE("%p\n", ppEnumerator
);
1111 new_enum
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum
));
1113 return E_OUTOFMEMORY
;
1115 new_enum
->IEnumUnknown_iface
.lpVtbl
= &InstalledRuntimeEnum_Vtbl
;
1119 *ppEnumerator
= &new_enum
->IEnumUnknown_iface
;
1124 static HRESULT WINAPI
CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost
* iface
,
1125 HANDLE hndProcess
, IEnumUnknown
**ppEnumerator
)
1127 FIXME("%p %p\n", hndProcess
, ppEnumerator
);
1132 static HRESULT WINAPI
CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost
* iface
,
1133 RuntimeLoadedCallbackFnPtr pCallbackFunction
)
1135 FIXME("%p\n", pCallbackFunction
);
1140 static HRESULT WINAPI
CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost
* iface
,
1141 REFIID riid
, LPVOID
*ppUnk
)
1143 FIXME("%s %p\n", debugstr_guid(riid
), ppUnk
);
1148 static HRESULT WINAPI
CLRMetaHost_ExitProcess(ICLRMetaHost
* iface
, INT32 iExitCode
)
1150 FIXME("%i: stub\n", iExitCode
);
1152 ExitProcess(iExitCode
);
1155 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl
=
1157 CLRMetaHost_QueryInterface
,
1159 CLRMetaHost_Release
,
1160 CLRMetaHost_GetRuntime
,
1161 CLRMetaHost_GetVersionFromFile
,
1162 CLRMetaHost_EnumerateInstalledRuntimes
,
1163 CLRMetaHost_EnumerateLoadedRuntimes
,
1164 CLRMetaHost_RequestRuntimeLoadedNotification
,
1165 CLRMetaHost_QueryLegacyV2RuntimeBinding
,
1166 CLRMetaHost_ExitProcess
1169 static struct CLRMetaHost GlobalCLRMetaHost
= {
1170 { &CLRMetaHost_vtbl
}
1173 HRESULT
CLRMetaHost_CreateInstance(REFIID riid
, void **ppobj
)
1175 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost
.ICLRMetaHost_iface
, riid
, ppobj
);
1178 static MonoAssembly
* mono_assembly_search_hook_fn(MonoAssemblyName
*aname
, char **assemblies_path
, void *user_data
)
1180 loaded_mono
*mono
= user_data
;
1182 MonoAssembly
*result
=NULL
;
1183 char *stringname
=NULL
;
1185 int stringnameW_size
;
1186 IAssemblyCache
*asmcache
;
1188 WCHAR path
[MAX_PATH
];
1190 MonoImageOpenStatus stat
;
1191 static WCHAR fusiondll
[] = {'f','u','s','i','o','n',0};
1192 HMODULE hfusion
=NULL
;
1193 static HRESULT (WINAPI
*pCreateAssemblyCache
)(IAssemblyCache
**,DWORD
);
1195 stringname
= mono
->mono_stringify_assembly_name(aname
);
1197 TRACE("%s\n", debugstr_a(stringname
));
1199 if (!stringname
) return NULL
;
1201 /* FIXME: We should search the given paths before the GAC. */
1203 if (!pCreateAssemblyCache
)
1205 hr
= LoadLibraryShim(fusiondll
, NULL
, NULL
, &hfusion
);
1209 pCreateAssemblyCache
= (void*)GetProcAddress(hfusion
, "CreateAssemblyCache");
1210 if (!pCreateAssemblyCache
)
1216 hr
= pCreateAssemblyCache(&asmcache
, 0);
1220 stringnameW_size
= MultiByteToWideChar(CP_UTF8
, 0, stringname
, -1, NULL
, 0);
1222 stringnameW
= HeapAlloc(GetProcessHeap(), 0, stringnameW_size
* sizeof(WCHAR
));
1224 MultiByteToWideChar(CP_UTF8
, 0, stringname
, -1, stringnameW
, stringnameW_size
);
1230 info
.cbAssemblyInfo
= sizeof(info
);
1231 info
.pszCurrentAssemblyPathBuf
= path
;
1232 info
.cchBuf
= MAX_PATH
;
1235 hr
= IAssemblyCache_QueryAssemblyInfo(asmcache
, 0, stringnameW
, &info
);
1238 HeapFree(GetProcessHeap(), 0, stringnameW
);
1240 IAssemblyCache_Release(asmcache
);
1245 TRACE("found: %s\n", debugstr_w(path
));
1251 result
= mono
->mono_assembly_open(pathA
, &stat
);
1254 ERR("Failed to load %s, status=%u\n", debugstr_w(path
), stat
);
1256 HeapFree(GetProcessHeap(), 0, pathA
);
1260 mono
->mono_free(stringname
);
1265 HRESULT
get_runtime_info(LPCWSTR exefile
, LPCWSTR version
, LPCWSTR config_file
,
1266 DWORD startup_flags
, DWORD runtimeinfo_flags
, BOOL legacy
, ICLRRuntimeInfo
**result
)
1268 static const WCHAR dotconfig
[] = {'.','c','o','n','f','i','g',0};
1269 static const DWORD supported_startup_flags
= 0;
1270 static const DWORD supported_runtime_flags
= RUNTIME_INFO_UPGRADE_VERSION
;
1272 WCHAR local_version
[MAX_PATH
];
1273 ULONG local_version_size
= MAX_PATH
;
1274 WCHAR local_config_file
[MAX_PATH
];
1276 parsed_config_file parsed_config
;
1278 if (startup_flags
& ~supported_startup_flags
)
1279 FIXME("unsupported startup flags %x\n", startup_flags
& ~supported_startup_flags
);
1281 if (runtimeinfo_flags
& ~supported_runtime_flags
)
1282 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags
& ~supported_runtime_flags
);
1284 if (exefile
&& !config_file
)
1286 strcpyW(local_config_file
, exefile
);
1287 strcatW(local_config_file
, dotconfig
);
1289 config_file
= local_config_file
;
1295 hr
= parse_config_file(config_file
, &parsed_config
);
1299 supported_runtime
*entry
;
1300 LIST_FOR_EACH_ENTRY(entry
, &parsed_config
.supported_runtimes
, supported_runtime
, entry
)
1302 hr
= CLRMetaHost_GetRuntime(0, entry
->version
, &IID_ICLRRuntimeInfo
, (void**)result
);
1312 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file
), hr
);
1315 free_parsed_config_file(&parsed_config
);
1321 if (exefile
&& !version
)
1323 hr
= CLRMetaHost_GetVersionFromFile(0, exefile
, local_version
, &local_version_size
);
1325 version
= local_version
;
1327 if (FAILED(hr
)) return hr
;
1332 hr
= CLRMetaHost_GetRuntime(0, version
, &IID_ICLRRuntimeInfo
, (void**)result
);
1337 if (runtimeinfo_flags
& RUNTIME_INFO_UPGRADE_VERSION
)
1339 DWORD major
, minor
, build
;
1341 if (version
&& !parse_runtime_version(version
, &major
, &minor
, &build
))
1343 ERR("Cannot parse %s\n", debugstr_w(version
));
1344 return CLR_E_SHIM_RUNTIME
;
1356 if (runtimes
[i
].mono_abi_version
)
1358 /* Must be greater or equal to the version passed in. */
1359 if (!version
|| ((runtimes
[i
].major
>= major
&& runtimes
[i
].minor
>= minor
&& runtimes
[i
].build
>= build
) ||
1360 (runtimes
[i
].major
>= major
&& runtimes
[i
].minor
> minor
) ||
1361 (runtimes
[i
].major
> major
)))
1363 return ICLRRuntimeInfo_QueryInterface(&runtimes
[i
].ICLRRuntimeInfo_iface
,
1364 &IID_ICLRRuntimeInfo
, (void **)result
);
1370 missing_runtime_message(&runtimes
[1]);
1372 missing_runtime_message(&runtimes
[NUM_RUNTIMES
-1]);
1374 return CLR_E_SHIM_RUNTIME
;
1377 return CLR_E_SHIM_RUNTIME
;