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"
38 #include "wine/list.h"
39 #include "mscoree_private.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL( mscoree
);
45 static const WCHAR net_11_subdir
[] = {'1','.','0',0};
46 static const WCHAR net_20_subdir
[] = {'2','.','0',0};
47 static const WCHAR net_40_subdir
[] = {'4','.','0',0};
49 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl
;
51 #define NUM_RUNTIMES 3
53 static struct CLRRuntimeInfo runtimes
[NUM_RUNTIMES
] = {
54 {{&CLRRuntimeInfoVtbl
}, net_11_subdir
, 1, 1, 4322, 0},
55 {{&CLRRuntimeInfoVtbl
}, net_20_subdir
, 2, 0, 50727, 0},
56 {{&CLRRuntimeInfoVtbl
}, net_40_subdir
, 4, 0, 30319, 0}
59 static int runtimes_initialized
;
61 static CRITICAL_SECTION runtime_list_cs
;
62 static CRITICAL_SECTION_DEBUG runtime_list_cs_debug
=
64 0, 0, &runtime_list_cs
,
65 { &runtime_list_cs_debug
.ProcessLocksList
,
66 &runtime_list_cs_debug
.ProcessLocksList
},
67 0, 0, { (DWORD_PTR
)(__FILE__
": runtime_list_cs") }
69 static CRITICAL_SECTION runtime_list_cs
= { &runtime_list_cs_debug
, -1, 0, 0, 0, 0 };
71 #define NUM_ABI_VERSIONS 2
73 static loaded_mono loaded_monos
[NUM_ABI_VERSIONS
];
75 static BOOL
find_mono_dll(LPCWSTR path
, LPWSTR dll_path
, int abi_version
);
77 static MonoAssembly
* mono_assembly_search_hook_fn(MonoAssemblyName
*aname
, char **assemblies_path
, void *user_data
);
79 static void mono_shutdown_callback_fn(MonoProfiler
*prof
);
81 static void set_environment(LPCWSTR bin_path
)
83 WCHAR path_env
[MAX_PATH
];
86 static const WCHAR pathW
[] = {'P','A','T','H',0};
88 /* We have to modify PATH as Mono loads other DLLs from this directory. */
89 GetEnvironmentVariableW(pathW
, path_env
, sizeof(path_env
)/sizeof(WCHAR
));
90 len
= strlenW(path_env
);
91 path_env
[len
++] = ';';
92 strcpyW(path_env
+len
, bin_path
);
93 SetEnvironmentVariableW(pathW
, path_env
);
96 static void CDECL
do_nothing(void)
100 static void missing_runtime_message(const CLRRuntimeInfo
*This
)
102 if (This
->major
== 1)
103 MESSAGE("wine: Install Mono 2.6 for Windows to run .NET 1.1 applications.\n");
104 else if (This
->major
== 2)
105 MESSAGE("wine: Install Mono for Windows to run .NET 2.0 applications.\n");
106 else if (This
->major
== 4)
107 MESSAGE("wine: Install Mono 2.8 or greater for Windows to run .NET 4.0 applications.\n");
110 static HRESULT
load_mono(CLRRuntimeInfo
*This
, loaded_mono
**result
)
112 static const WCHAR bin
[] = {'\\','b','i','n',0};
113 static const WCHAR lib
[] = {'\\','l','i','b',0};
114 static const WCHAR etc
[] = {'\\','e','t','c',0};
115 static const WCHAR glibdll
[] = {'l','i','b','g','l','i','b','-','2','.','0','-','0','.','d','l','l',0};
116 WCHAR mono_dll_path
[MAX_PATH
+16], mono_bin_path
[MAX_PATH
+4];
117 WCHAR mono_lib_path
[MAX_PATH
+4], mono_etc_path
[MAX_PATH
+4];
118 char mono_lib_path_a
[MAX_PATH
], mono_etc_path_a
[MAX_PATH
];
120 char trace_setting
[256];
122 if (This
->mono_abi_version
<= 0 || This
->mono_abi_version
> NUM_ABI_VERSIONS
)
124 missing_runtime_message(This
);
128 *result
= &loaded_monos
[This
->mono_abi_version
-1];
130 if ((*result
)->is_shutdown
)
132 ERR("Cannot load Mono after it has been shut down.\n");
137 if (!(*result
)->mono_handle
)
139 strcpyW(mono_bin_path
, This
->mono_path
);
140 strcatW(mono_bin_path
, bin
);
141 set_environment(mono_bin_path
);
143 strcpyW(mono_lib_path
, This
->mono_path
);
144 strcatW(mono_lib_path
, lib
);
145 WideCharToMultiByte(CP_UTF8
, 0, mono_lib_path
, -1, mono_lib_path_a
, MAX_PATH
, NULL
, NULL
);
147 strcpyW(mono_etc_path
, This
->mono_path
);
148 strcatW(mono_etc_path
, etc
);
149 WideCharToMultiByte(CP_UTF8
, 0, mono_etc_path
, -1, mono_etc_path_a
, MAX_PATH
, NULL
, NULL
);
151 if (!find_mono_dll(This
->mono_path
, mono_dll_path
, This
->mono_abi_version
)) goto fail
;
153 (*result
)->mono_handle
= LoadLibraryW(mono_dll_path
);
155 if (!(*result
)->mono_handle
) goto fail
;
157 #define LOAD_MONO_FUNCTION(x) do { \
158 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
159 if (!(*result)->x) { \
164 LOAD_MONO_FUNCTION(mono_assembly_get_image
);
165 LOAD_MONO_FUNCTION(mono_assembly_open
);
166 LOAD_MONO_FUNCTION(mono_config_parse
);
167 LOAD_MONO_FUNCTION(mono_class_from_mono_type
);
168 LOAD_MONO_FUNCTION(mono_class_from_name
);
169 LOAD_MONO_FUNCTION(mono_class_get_method_from_name
);
170 LOAD_MONO_FUNCTION(mono_domain_assembly_open
);
171 LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook
);
172 LOAD_MONO_FUNCTION(mono_jit_exec
);
173 LOAD_MONO_FUNCTION(mono_jit_init
);
174 LOAD_MONO_FUNCTION(mono_jit_set_trace_options
);
175 LOAD_MONO_FUNCTION(mono_object_get_domain
);
176 LOAD_MONO_FUNCTION(mono_object_new
);
177 LOAD_MONO_FUNCTION(mono_object_unbox
);
178 LOAD_MONO_FUNCTION(mono_profiler_install
);
179 LOAD_MONO_FUNCTION(mono_reflection_type_from_name
);
180 LOAD_MONO_FUNCTION(mono_runtime_invoke
);
181 LOAD_MONO_FUNCTION(mono_runtime_object_init
);
182 LOAD_MONO_FUNCTION(mono_runtime_quit
);
183 LOAD_MONO_FUNCTION(mono_set_dirs
);
184 LOAD_MONO_FUNCTION(mono_stringify_assembly_name
);
186 /* GLib imports obsoleted by the 2.0 ABI */
187 if (This
->mono_abi_version
== 1)
189 (*result
)->glib_handle
= LoadLibraryW(glibdll
);
190 if (!(*result
)->glib_handle
) goto fail
;
192 (*result
)->mono_free
= (void*)GetProcAddress((*result
)->glib_handle
, "g_free");
193 if (!(*result
)->mono_free
) goto fail
;
197 LOAD_MONO_FUNCTION(mono_free
);
200 #undef LOAD_MONO_FUNCTION
202 #define LOAD_OPT_VOID_MONO_FUNCTION(x) do { \
203 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
204 if (!(*result)->x) { \
205 (*result)->x = do_nothing; \
209 LOAD_OPT_VOID_MONO_FUNCTION(mono_runtime_set_shutting_down
);
210 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_pool_cleanup
);
211 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_suspend_all_other_threads
);
212 LOAD_OPT_VOID_MONO_FUNCTION(mono_threads_set_shutting_down
);
214 #undef LOAD_OPT_VOID_MONO_FUNCTION
216 (*result
)->mono_profiler_install((MonoProfiler
*)*result
, mono_shutdown_callback_fn
);
218 (*result
)->mono_set_dirs(mono_lib_path_a
, mono_etc_path_a
);
220 (*result
)->mono_config_parse(NULL
);
222 (*result
)->mono_install_assembly_preload_hook(mono_assembly_search_hook_fn
, *result
);
224 trace_size
= GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting
, sizeof(trace_setting
));
228 (*result
)->mono_jit_set_trace_options(trace_setting
);
235 ERR("Could not load Mono into this process\n");
236 FreeLibrary((*result
)->mono_handle
);
237 FreeLibrary((*result
)->glib_handle
);
238 (*result
)->mono_handle
= NULL
;
239 (*result
)->glib_handle
= NULL
;
243 static void mono_shutdown_callback_fn(MonoProfiler
*prof
)
245 loaded_mono
*mono
= (loaded_mono
*)prof
;
247 mono
->is_shutdown
= TRUE
;
250 static HRESULT
CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo
*This
, RuntimeHost
**result
)
253 loaded_mono
*ploaded_mono
;
255 if (This
->loaded_runtime
)
257 *result
= This
->loaded_runtime
;
261 EnterCriticalSection(&runtime_list_cs
);
263 hr
= load_mono(This
, &ploaded_mono
);
266 hr
= RuntimeHost_Construct(This
, ploaded_mono
, &This
->loaded_runtime
);
268 LeaveCriticalSection(&runtime_list_cs
);
271 *result
= This
->loaded_runtime
;
276 void unload_all_runtimes(void)
280 for (i
=0; i
<NUM_ABI_VERSIONS
; i
++)
282 loaded_mono
*mono
= &loaded_monos
[i
];
283 if (mono
->mono_handle
&& mono
->is_started
&& !mono
->is_shutdown
)
285 /* Copied from Mono's ves_icall_System_Environment_Exit */
286 mono
->mono_threads_set_shutting_down();
287 mono
->mono_runtime_set_shutting_down();
288 mono
->mono_thread_pool_cleanup();
289 mono
->mono_thread_suspend_all_other_threads();
290 mono
->mono_runtime_quit();
294 for (i
=0; i
<NUM_RUNTIMES
; i
++)
295 if (runtimes
[i
].loaded_runtime
)
296 RuntimeHost_Destroy(runtimes
[i
].loaded_runtime
);
299 void expect_no_runtimes(void)
303 for (i
=0; i
<NUM_ABI_VERSIONS
; i
++)
305 loaded_mono
*mono
= &loaded_monos
[i
];
306 if (mono
->mono_handle
&& mono
->is_started
&& !mono
->is_shutdown
)
308 ERR("Process exited with a Mono runtime loaded.\n");
314 static inline CLRRuntimeInfo
*impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo
*iface
)
316 return CONTAINING_RECORD(iface
, CLRRuntimeInfo
, ICLRRuntimeInfo_iface
);
319 static HRESULT WINAPI
CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo
* iface
,
323 TRACE("%p %s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
325 if ( IsEqualGUID( riid
, &IID_ICLRRuntimeInfo
) ||
326 IsEqualGUID( riid
, &IID_IUnknown
) )
332 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
333 return E_NOINTERFACE
;
336 ICLRRuntimeInfo_AddRef( iface
);
341 static ULONG WINAPI
CLRRuntimeInfo_AddRef(ICLRRuntimeInfo
* iface
)
346 static ULONG WINAPI
CLRRuntimeInfo_Release(ICLRRuntimeInfo
* iface
)
351 static HRESULT WINAPI
CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo
* iface
,
352 LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
354 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
355 DWORD buffer_size
= *pcchBuffer
;
360 TRACE("%p %p %p\n", iface
, pwzBuffer
, pcchBuffer
);
362 size
= snprintf(version
, sizeof(version
), "v%u.%u.%u", This
->major
, This
->minor
, This
->build
);
364 assert(size
<= sizeof(version
));
366 *pcchBuffer
= MultiByteToWideChar(CP_UTF8
, 0, version
, -1, NULL
, 0);
370 if (buffer_size
>= *pcchBuffer
)
371 MultiByteToWideChar(CP_UTF8
, 0, version
, -1, pwzBuffer
, buffer_size
);
373 hr
= HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
379 static BOOL
get_install_root(LPWSTR install_dir
)
381 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};
382 const WCHAR install_root
[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
387 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, dotnet_key
, 0, KEY_READ
, &key
))
391 if (RegQueryValueExW(key
, install_root
, 0, NULL
, (LPBYTE
)install_dir
, &len
))
401 static HRESULT WINAPI
CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo
* iface
,
402 LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
404 static const WCHAR slash
[] = {'\\',0};
405 DWORD buffer_size
= *pcchBuffer
;
406 WCHAR system_dir
[MAX_PATH
];
407 WCHAR version
[MAX_PATH
];
408 DWORD version_size
, size
;
411 TRACE("%p %p %p\n", iface
, pwzBuffer
, pcchBuffer
);
413 if (!get_install_root(system_dir
))
415 ERR("error reading registry key for installroot\n");
420 version_size
= MAX_PATH
;
421 ICLRRuntimeInfo_GetVersionString(iface
, version
, &version_size
);
422 lstrcatW(system_dir
, version
);
423 lstrcatW(system_dir
, slash
);
424 size
= lstrlenW(system_dir
) + 1;
431 if (buffer_size
>= size
)
432 strcpyW(pwzBuffer
, system_dir
);
434 hr
= HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
440 static HRESULT WINAPI
CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo
* iface
,
441 HANDLE hndProcess
, BOOL
*pbLoaded
)
443 FIXME("%p %p %p\n", iface
, hndProcess
, pbLoaded
);
448 static HRESULT WINAPI
CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo
* iface
,
449 UINT iResourceID
, LPWSTR pwzBuffer
, DWORD
*pcchBuffer
, LONG iLocaleid
)
451 FIXME("%p %u %p %p %x\n", iface
, iResourceID
, pwzBuffer
, pcchBuffer
, iLocaleid
);
456 static HRESULT WINAPI
CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo
* iface
,
457 LPCWSTR pwzDllName
, HMODULE
*phndModule
)
459 WCHAR version
[MAX_PATH
];
463 TRACE("%p %s %p\n", iface
, debugstr_w(pwzDllName
), phndModule
);
465 cchBuffer
= MAX_PATH
;
466 hr
= ICLRRuntimeInfo_GetVersionString(iface
, version
, &cchBuffer
);
467 if (FAILED(hr
)) return hr
;
469 return LoadLibraryShim(pwzDllName
, version
, NULL
, phndModule
);
472 static HRESULT WINAPI
CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo
* iface
,
473 LPCSTR pszProcName
, LPVOID
*ppProc
)
475 FIXME("%p %s %p\n", iface
, debugstr_a(pszProcName
), ppProc
);
480 static HRESULT WINAPI
CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo
* iface
,
481 REFCLSID rclsid
, REFIID riid
, LPVOID
*ppUnk
)
483 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
487 TRACE("%p %s %s %p\n", iface
, debugstr_guid(rclsid
), debugstr_guid(riid
), ppUnk
);
489 hr
= CLRRuntimeInfo_GetRuntimeHost(This
, &host
);
492 hr
= RuntimeHost_GetInterface(host
, rclsid
, riid
, ppUnk
);
497 static HRESULT WINAPI
CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo
* iface
,
500 FIXME("%p %p\n", iface
, pbLoadable
);
505 static HRESULT WINAPI
CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo
* iface
,
506 DWORD dwStartupFlags
, LPCWSTR pwzHostConfigFile
)
508 FIXME("%p %x %s\n", iface
, dwStartupFlags
, debugstr_w(pwzHostConfigFile
));
513 static HRESULT WINAPI
CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo
* iface
,
514 DWORD
*pdwStartupFlags
, LPWSTR pwzHostConfigFile
, DWORD
*pcchHostConfigFile
)
516 FIXME("%p %p %p %p\n", iface
, pdwStartupFlags
, pwzHostConfigFile
, pcchHostConfigFile
);
521 static HRESULT WINAPI
CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo
* iface
)
523 FIXME("%p\n", iface
);
528 static HRESULT WINAPI
CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo
* iface
,
529 BOOL
*pbStarted
, DWORD
*pdwStartupFlags
)
531 FIXME("%p %p %p\n", iface
, pbStarted
, pdwStartupFlags
);
536 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl
= {
537 CLRRuntimeInfo_QueryInterface
,
538 CLRRuntimeInfo_AddRef
,
539 CLRRuntimeInfo_Release
,
540 CLRRuntimeInfo_GetVersionString
,
541 CLRRuntimeInfo_GetRuntimeDirectory
,
542 CLRRuntimeInfo_IsLoaded
,
543 CLRRuntimeInfo_LoadErrorString
,
544 CLRRuntimeInfo_LoadLibrary
,
545 CLRRuntimeInfo_GetProcAddress
,
546 CLRRuntimeInfo_GetInterface
,
547 CLRRuntimeInfo_IsLoadable
,
548 CLRRuntimeInfo_SetDefaultStartupFlags
,
549 CLRRuntimeInfo_GetDefaultStartupFlags
,
550 CLRRuntimeInfo_BindAsLegacyV2Runtime
,
551 CLRRuntimeInfo_IsStarted
554 HRESULT
ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo
*iface
, RuntimeHost
**result
)
556 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
558 assert(This
->ICLRRuntimeInfo_iface
.lpVtbl
== &CLRRuntimeInfoVtbl
);
560 return CLRRuntimeInfo_GetRuntimeHost(This
, result
);
563 static BOOL
find_mono_dll(LPCWSTR path
, LPWSTR dll_path
, int abi_version
)
565 static const WCHAR mono_dll
[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
566 static const WCHAR libmono_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
567 static const WCHAR mono2_dll
[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
568 static const WCHAR libmono2_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
569 DWORD attributes
=INVALID_FILE_ATTRIBUTES
;
571 if (abi_version
== 1)
573 strcpyW(dll_path
, path
);
574 strcatW(dll_path
, mono_dll
);
575 attributes
= GetFileAttributesW(dll_path
);
577 if (attributes
== INVALID_FILE_ATTRIBUTES
)
579 strcpyW(dll_path
, path
);
580 strcatW(dll_path
, libmono_dll
);
581 attributes
= GetFileAttributesW(dll_path
);
584 else if (abi_version
== 2)
586 strcpyW(dll_path
, path
);
587 strcatW(dll_path
, mono2_dll
);
588 attributes
= GetFileAttributesW(dll_path
);
590 if (attributes
== INVALID_FILE_ATTRIBUTES
)
592 strcpyW(dll_path
, path
);
593 strcatW(dll_path
, libmono2_dll
);
594 attributes
= GetFileAttributesW(dll_path
);
598 return (attributes
!= INVALID_FILE_ATTRIBUTES
);
601 static BOOL
get_mono_path_from_registry(LPWSTR path
, int abi_version
)
603 static const WCHAR mono_key
[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
604 static const WCHAR defaul_clr
[] = {'D','e','f','a','u','l','t','C','L','R',0};
605 static const WCHAR install_root
[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
606 static const WCHAR slash
[] = {'\\',0};
608 WCHAR version
[64], version_key
[MAX_PATH
];
611 WCHAR dll_path
[MAX_PATH
];
613 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, mono_key
, 0, KEY_READ
, &key
))
616 len
= sizeof(version
);
617 if (RegQueryValueExW(key
, defaul_clr
, 0, NULL
, (LPBYTE
)version
, &len
))
624 lstrcpyW(version_key
, mono_key
);
625 lstrcatW(version_key
, slash
);
626 lstrcatW(version_key
, version
);
628 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, version_key
, 0, KEY_READ
, &key
))
631 len
= sizeof(WCHAR
) * MAX_PATH
;
632 if (RegQueryValueExW(key
, install_root
, 0, NULL
, (LPBYTE
)path
, &len
))
639 return find_mono_dll(path
, dll_path
, abi_version
);
642 static BOOL
get_mono_path_from_folder(LPCWSTR folder
, LPWSTR mono_path
, int abi_version
)
644 static const WCHAR mono_one_dot_zero
[] = {'\\','m','o','n','o','-','1','.','0', 0};
645 static const WCHAR mono_two_dot_zero
[] = {'\\','m','o','n','o','-','2','.','0', 0};
646 WCHAR mono_dll_path
[MAX_PATH
];
649 strcpyW(mono_path
, folder
);
651 if (abi_version
== 1)
652 strcatW(mono_path
, mono_one_dot_zero
);
653 else if (abi_version
== 2)
654 strcatW(mono_path
, mono_two_dot_zero
);
656 found
= find_mono_dll(mono_path
, mono_dll_path
, abi_version
);
661 static BOOL
get_mono_path(LPWSTR path
, int abi_version
)
663 static const WCHAR subdir_mono
[] = {'\\','m','o','n','o',0};
664 static const WCHAR sibling_mono
[] = {'\\','.','.','\\','m','o','n','o',0};
665 WCHAR base_path
[MAX_PATH
];
666 const char *unix_data_dir
;
669 static WCHAR
* (CDECL
*wine_get_dos_file_name
)(const char*);
671 /* First try c:\windows\mono */
672 GetWindowsDirectoryW(base_path
, MAX_PATH
);
673 strcatW(base_path
, subdir_mono
);
675 if (get_mono_path_from_folder(base_path
, path
, abi_version
))
678 /* Next: /usr/share/wine/mono */
679 unix_data_dir
= wine_get_data_dir();
683 unix_data_dir
= wine_get_build_dir();
689 if (!wine_get_dos_file_name
)
690 wine_get_dos_file_name
= (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
692 if (wine_get_dos_file_name
)
694 dos_data_dir
= wine_get_dos_file_name(unix_data_dir
);
698 strcpyW(base_path
, dos_data_dir
);
699 strcatW(base_path
, build_tree
? sibling_mono
: subdir_mono
);
701 HeapFree(GetProcessHeap(), 0, dos_data_dir
);
703 if (get_mono_path_from_folder(base_path
, path
, abi_version
))
709 /* Last: the registry */
710 return get_mono_path_from_registry(path
, abi_version
);
713 static void find_runtimes(void)
716 static const WCHAR libmono
[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
717 static const WCHAR mscorlib
[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
718 WCHAR mono_path
[MAX_PATH
], lib_path
[MAX_PATH
];
719 BOOL any_runtimes_found
= FALSE
;
721 if (runtimes_initialized
) return;
723 EnterCriticalSection(&runtime_list_cs
);
725 if (runtimes_initialized
) goto end
;
727 for (abi_version
=NUM_ABI_VERSIONS
; abi_version
>0; abi_version
--)
729 if (!get_mono_path(mono_path
, abi_version
))
732 for (i
=0; i
<NUM_RUNTIMES
; i
++)
734 if (runtimes
[i
].mono_abi_version
== 0)
736 strcpyW(lib_path
, mono_path
);
737 strcatW(lib_path
, libmono
);
738 strcatW(lib_path
, runtimes
[i
].mono_libdir
);
739 strcatW(lib_path
, mscorlib
);
741 if (GetFileAttributesW(lib_path
) != INVALID_FILE_ATTRIBUTES
)
743 runtimes
[i
].mono_abi_version
= abi_version
;
745 strcpyW(runtimes
[i
].mono_path
, mono_path
);
746 strcpyW(runtimes
[i
].mscorlib_path
, lib_path
);
748 any_runtimes_found
= TRUE
;
754 if (!any_runtimes_found
)
756 /* Report all runtimes are available if Mono isn't installed.
757 * FIXME: Remove this when Mono is properly packaged. */
758 for (i
=0; i
<NUM_RUNTIMES
; i
++)
759 runtimes
[i
].mono_abi_version
= -1;
762 runtimes_initialized
= 1;
765 LeaveCriticalSection(&runtime_list_cs
);
768 struct InstalledRuntimeEnum
770 IEnumUnknown IEnumUnknown_iface
;
775 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl
;
777 static inline struct InstalledRuntimeEnum
*impl_from_IEnumUnknown(IEnumUnknown
*iface
)
779 return CONTAINING_RECORD(iface
, struct InstalledRuntimeEnum
, IEnumUnknown_iface
);
782 static HRESULT WINAPI
InstalledRuntimeEnum_QueryInterface(IEnumUnknown
* iface
, REFIID riid
,
785 TRACE("%p %s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
787 if ( IsEqualGUID( riid
, &IID_IEnumUnknown
) ||
788 IsEqualGUID( riid
, &IID_IUnknown
) )
794 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
795 return E_NOINTERFACE
;
798 IEnumUnknown_AddRef( iface
);
803 static ULONG WINAPI
InstalledRuntimeEnum_AddRef(IEnumUnknown
* iface
)
805 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
806 ULONG ref
= InterlockedIncrement(&This
->ref
);
808 TRACE("(%p) refcount=%u\n", iface
, ref
);
813 static ULONG WINAPI
InstalledRuntimeEnum_Release(IEnumUnknown
* iface
)
815 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
816 ULONG ref
= InterlockedDecrement(&This
->ref
);
818 TRACE("(%p) refcount=%u\n", iface
, ref
);
822 HeapFree(GetProcessHeap(), 0, This
);
828 static HRESULT WINAPI
InstalledRuntimeEnum_Next(IEnumUnknown
*iface
, ULONG celt
,
829 IUnknown
**rgelt
, ULONG
*pceltFetched
)
831 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
836 TRACE("(%p,%u,%p,%p)\n", iface
, celt
, rgelt
, pceltFetched
);
838 while (num_fetched
< celt
)
840 if (This
->pos
>= NUM_RUNTIMES
)
845 if (runtimes
[This
->pos
].mono_abi_version
)
847 item
= (IUnknown
*)&runtimes
[This
->pos
].ICLRRuntimeInfo_iface
;
848 IUnknown_AddRef(item
);
849 rgelt
[num_fetched
] = item
;
856 *pceltFetched
= num_fetched
;
861 static HRESULT WINAPI
InstalledRuntimeEnum_Skip(IEnumUnknown
*iface
, ULONG celt
)
863 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
867 TRACE("(%p,%u)\n", iface
, celt
);
869 while (num_fetched
< celt
)
871 if (This
->pos
>= NUM_RUNTIMES
)
876 if (runtimes
[This
->pos
].mono_abi_version
)
886 static HRESULT WINAPI
InstalledRuntimeEnum_Reset(IEnumUnknown
*iface
)
888 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
890 TRACE("(%p)\n", iface
);
897 static HRESULT WINAPI
InstalledRuntimeEnum_Clone(IEnumUnknown
*iface
, IEnumUnknown
**ppenum
)
899 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
900 struct InstalledRuntimeEnum
*new_enum
;
902 TRACE("(%p)\n", iface
);
904 new_enum
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum
));
906 return E_OUTOFMEMORY
;
908 new_enum
->IEnumUnknown_iface
.lpVtbl
= &InstalledRuntimeEnum_Vtbl
;
910 new_enum
->pos
= This
->pos
;
912 *ppenum
= &new_enum
->IEnumUnknown_iface
;
917 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl
= {
918 InstalledRuntimeEnum_QueryInterface
,
919 InstalledRuntimeEnum_AddRef
,
920 InstalledRuntimeEnum_Release
,
921 InstalledRuntimeEnum_Next
,
922 InstalledRuntimeEnum_Skip
,
923 InstalledRuntimeEnum_Reset
,
924 InstalledRuntimeEnum_Clone
929 ICLRMetaHost ICLRMetaHost_iface
;
932 static struct CLRMetaHost GlobalCLRMetaHost
;
934 static HRESULT WINAPI
CLRMetaHost_QueryInterface(ICLRMetaHost
* iface
,
938 TRACE("%s %p\n", debugstr_guid(riid
), ppvObject
);
940 if ( IsEqualGUID( riid
, &IID_ICLRMetaHost
) ||
941 IsEqualGUID( riid
, &IID_IUnknown
) )
947 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
948 return E_NOINTERFACE
;
951 ICLRMetaHost_AddRef( iface
);
956 static ULONG WINAPI
CLRMetaHost_AddRef(ICLRMetaHost
* iface
)
961 static ULONG WINAPI
CLRMetaHost_Release(ICLRMetaHost
* iface
)
966 static BOOL
parse_runtime_version(LPCWSTR version
, DWORD
*major
, DWORD
*minor
, DWORD
*build
)
972 if (version
[0] == 'v')
975 if (!isdigit(*version
))
978 while (isdigit(*version
))
979 *major
= *major
* 10 + (*version
++ - '0');
984 if (*version
++ != '.' || !isdigit(*version
))
987 while (isdigit(*version
))
988 *minor
= *minor
* 10 + (*version
++ - '0');
993 if (*version
++ != '.' || !isdigit(*version
))
996 while (isdigit(*version
))
997 *build
= *build
* 10 + (*version
++ - '0');
999 return *version
== 0;
1005 static HRESULT WINAPI
CLRMetaHost_GetRuntime(ICLRMetaHost
* iface
,
1006 LPCWSTR pwzVersion
, REFIID iid
, LPVOID
*ppRuntime
)
1009 DWORD major
, minor
, build
;
1011 TRACE("%s %s %p\n", debugstr_w(pwzVersion
), debugstr_guid(iid
), ppRuntime
);
1016 if (!parse_runtime_version(pwzVersion
, &major
, &minor
, &build
))
1018 ERR("Cannot parse %s\n", debugstr_w(pwzVersion
));
1019 return CLR_E_SHIM_RUNTIME
;
1024 for (i
=0; i
<NUM_RUNTIMES
; i
++)
1026 if (runtimes
[i
].major
== major
&& runtimes
[i
].minor
== minor
&&
1027 runtimes
[i
].build
== build
)
1029 if (runtimes
[i
].mono_abi_version
)
1030 return ICLRRuntimeInfo_QueryInterface(&runtimes
[i
].ICLRRuntimeInfo_iface
, iid
,
1034 missing_runtime_message(&runtimes
[i
]);
1035 return CLR_E_SHIM_RUNTIME
;
1040 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion
));
1041 return CLR_E_SHIM_RUNTIME
;
1044 HRESULT WINAPI
CLRMetaHost_GetVersionFromFile(ICLRMetaHost
* iface
,
1045 LPCWSTR pwzFilePath
, LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
1050 ULONG buffer_size
=*pcchBuffer
;
1052 TRACE("%s %p %p\n", debugstr_w(pwzFilePath
), pwzBuffer
, pcchBuffer
);
1054 hr
= assembly_create(&assembly
, pwzFilePath
);
1058 hr
= assembly_get_runtime_version(assembly
, &version
);
1062 *pcchBuffer
= MultiByteToWideChar(CP_UTF8
, 0, version
, -1, NULL
, 0);
1066 if (buffer_size
>= *pcchBuffer
)
1067 MultiByteToWideChar(CP_UTF8
, 0, version
, -1, pwzBuffer
, buffer_size
);
1069 hr
= HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
1073 assembly_release(assembly
);
1079 static HRESULT WINAPI
CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost
* iface
,
1080 IEnumUnknown
**ppEnumerator
)
1082 struct InstalledRuntimeEnum
*new_enum
;
1084 TRACE("%p\n", ppEnumerator
);
1088 new_enum
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum
));
1090 return E_OUTOFMEMORY
;
1092 new_enum
->IEnumUnknown_iface
.lpVtbl
= &InstalledRuntimeEnum_Vtbl
;
1096 *ppEnumerator
= &new_enum
->IEnumUnknown_iface
;
1101 static HRESULT WINAPI
CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost
* iface
,
1102 HANDLE hndProcess
, IEnumUnknown
**ppEnumerator
)
1104 FIXME("%p %p\n", hndProcess
, ppEnumerator
);
1109 static HRESULT WINAPI
CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost
* iface
,
1110 RuntimeLoadedCallbackFnPtr pCallbackFunction
)
1112 FIXME("%p\n", pCallbackFunction
);
1117 static HRESULT WINAPI
CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost
* iface
,
1118 REFIID riid
, LPVOID
*ppUnk
)
1120 FIXME("%s %p\n", debugstr_guid(riid
), ppUnk
);
1125 static HRESULT WINAPI
CLRMetaHost_ExitProcess(ICLRMetaHost
* iface
, INT32 iExitCode
)
1127 FIXME("%i: stub\n", iExitCode
);
1129 ExitProcess(iExitCode
);
1132 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl
=
1134 CLRMetaHost_QueryInterface
,
1136 CLRMetaHost_Release
,
1137 CLRMetaHost_GetRuntime
,
1138 CLRMetaHost_GetVersionFromFile
,
1139 CLRMetaHost_EnumerateInstalledRuntimes
,
1140 CLRMetaHost_EnumerateLoadedRuntimes
,
1141 CLRMetaHost_RequestRuntimeLoadedNotification
,
1142 CLRMetaHost_QueryLegacyV2RuntimeBinding
,
1143 CLRMetaHost_ExitProcess
1146 static struct CLRMetaHost GlobalCLRMetaHost
= {
1147 { &CLRMetaHost_vtbl
}
1150 extern HRESULT
CLRMetaHost_CreateInstance(REFIID riid
, void **ppobj
)
1152 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost
.ICLRMetaHost_iface
, riid
, ppobj
);
1155 static MonoAssembly
* mono_assembly_search_hook_fn(MonoAssemblyName
*aname
, char **assemblies_path
, void *user_data
)
1157 loaded_mono
*mono
= user_data
;
1159 MonoAssembly
*result
=NULL
;
1160 char *stringname
=NULL
;
1162 int stringnameW_size
;
1163 IAssemblyCache
*asmcache
;
1165 WCHAR path
[MAX_PATH
];
1167 MonoImageOpenStatus stat
;
1168 static WCHAR fusiondll
[] = {'f','u','s','i','o','n',0};
1169 HMODULE hfusion
=NULL
;
1170 static HRESULT
WINAPI (*pCreateAssemblyCache
)(IAssemblyCache
**,DWORD
);
1172 stringname
= mono
->mono_stringify_assembly_name(aname
);
1174 TRACE("%s\n", debugstr_a(stringname
));
1176 if (!stringname
) return NULL
;
1178 /* FIXME: We should search the given paths before the GAC. */
1180 if (!pCreateAssemblyCache
)
1182 hr
= LoadLibraryShim(fusiondll
, NULL
, NULL
, &hfusion
);
1186 pCreateAssemblyCache
= (void*)GetProcAddress(hfusion
, "CreateAssemblyCache");
1187 if (!pCreateAssemblyCache
)
1193 hr
= pCreateAssemblyCache(&asmcache
, 0);
1197 stringnameW_size
= MultiByteToWideChar(CP_UTF8
, 0, stringname
, -1, NULL
, 0);
1199 stringnameW
= HeapAlloc(GetProcessHeap(), 0, stringnameW_size
* sizeof(WCHAR
));
1201 MultiByteToWideChar(CP_UTF8
, 0, stringname
, -1, stringnameW
, stringnameW_size
);
1207 info
.cbAssemblyInfo
= sizeof(info
);
1208 info
.pszCurrentAssemblyPathBuf
= path
;
1209 info
.cchBuf
= MAX_PATH
;
1212 hr
= IAssemblyCache_QueryAssemblyInfo(asmcache
, 0, stringnameW
, &info
);
1215 HeapFree(GetProcessHeap(), 0, stringnameW
);
1217 IAssemblyCache_Release(asmcache
);
1222 TRACE("found: %s\n", debugstr_w(path
));
1228 result
= mono
->mono_assembly_open(pathA
, &stat
);
1231 ERR("Failed to load %s, status=%u\n", debugstr_w(path
), stat
);
1233 HeapFree(GetProcessHeap(), 0, pathA
);
1237 mono
->mono_free(stringname
);
1242 HRESULT
get_runtime_info(LPCWSTR exefile
, LPCWSTR version
, LPCWSTR config_file
,
1243 DWORD startup_flags
, DWORD runtimeinfo_flags
, BOOL legacy
, ICLRRuntimeInfo
**result
)
1245 static const WCHAR dotconfig
[] = {'.','c','o','n','f','i','g',0};
1246 static const DWORD supported_startup_flags
= 0;
1247 static const DWORD supported_runtime_flags
= RUNTIME_INFO_UPGRADE_VERSION
;
1249 WCHAR local_version
[MAX_PATH
];
1250 ULONG local_version_size
= MAX_PATH
;
1251 WCHAR local_config_file
[MAX_PATH
];
1253 parsed_config_file parsed_config
;
1255 if (startup_flags
& ~supported_startup_flags
)
1256 FIXME("unsupported startup flags %x\n", startup_flags
& ~supported_startup_flags
);
1258 if (runtimeinfo_flags
& ~supported_runtime_flags
)
1259 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags
& ~supported_runtime_flags
);
1261 if (exefile
&& !config_file
)
1263 strcpyW(local_config_file
, exefile
);
1264 strcatW(local_config_file
, dotconfig
);
1266 config_file
= local_config_file
;
1272 hr
= parse_config_file(config_file
, &parsed_config
);
1276 supported_runtime
*entry
;
1277 LIST_FOR_EACH_ENTRY(entry
, &parsed_config
.supported_runtimes
, supported_runtime
, entry
)
1279 hr
= CLRMetaHost_GetRuntime(0, entry
->version
, &IID_ICLRRuntimeInfo
, (void**)result
);
1289 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file
), hr
);
1292 free_parsed_config_file(&parsed_config
);
1298 if (exefile
&& !version
)
1300 hr
= CLRMetaHost_GetVersionFromFile(0, exefile
, local_version
, &local_version_size
);
1302 version
= local_version
;
1304 if (FAILED(hr
)) return hr
;
1309 return CLRMetaHost_GetRuntime(0, version
, &IID_ICLRRuntimeInfo
, (void**)result
);
1312 if (runtimeinfo_flags
& RUNTIME_INFO_UPGRADE_VERSION
)
1323 if (runtimes
[i
].mono_abi_version
)
1324 return ICLRRuntimeInfo_QueryInterface(&runtimes
[i
].ICLRRuntimeInfo_iface
,
1325 &IID_ICLRRuntimeInfo
, (void **)result
);
1329 missing_runtime_message(&runtimes
[1]);
1331 missing_runtime_message(&runtimes
[NUM_RUNTIMES
-1]);
1333 return CLR_E_SHIM_RUNTIME
;
1336 return CLR_E_SHIM_RUNTIME
;