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 struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl
;
50 #define NUM_RUNTIMES 4
52 static struct CLRRuntimeInfo runtimes
[NUM_RUNTIMES
] = {
53 {{&CLRRuntimeInfoVtbl
}, 1, 0, 3705, 0},
54 {{&CLRRuntimeInfoVtbl
}, 1, 1, 4322, 0},
55 {{&CLRRuntimeInfoVtbl
}, 2, 0, 50727, 0},
56 {{&CLRRuntimeInfoVtbl
}, 4, 0, 30319, 0}
59 static CRITICAL_SECTION runtime_list_cs
;
60 static CRITICAL_SECTION_DEBUG runtime_list_cs_debug
=
62 0, 0, &runtime_list_cs
,
63 { &runtime_list_cs_debug
.ProcessLocksList
,
64 &runtime_list_cs_debug
.ProcessLocksList
},
65 0, 0, { (DWORD_PTR
)(__FILE__
": runtime_list_cs") }
67 static CRITICAL_SECTION runtime_list_cs
= { &runtime_list_cs_debug
, -1, 0, 0, 0, 0 };
69 static HMODULE mono_handle
;
72 static BOOL is_mono_shutdown
;
74 MonoImage
* (CDECL
*mono_assembly_get_image
)(MonoAssembly
*assembly
);
75 MonoAssembly
* (CDECL
*mono_assembly_load_from
)(MonoImage
*image
, const char *fname
, MonoImageOpenStatus
*status
);
76 MonoAssembly
* (CDECL
*mono_assembly_open
)(const char *filename
, MonoImageOpenStatus
*status
);
77 MonoClass
* (CDECL
*mono_class_from_mono_type
)(MonoType
*type
);
78 MonoClass
* (CDECL
*mono_class_from_name
)(MonoImage
*image
, const char* name_space
, const char *name
);
79 MonoMethod
* (CDECL
*mono_class_get_method_from_name
)(MonoClass
*klass
, const char *name
, int param_count
);
80 static void (CDECL
*mono_config_parse
)(const char *filename
);
81 MonoAssembly
* (CDECL
*mono_domain_assembly_open
)(MonoDomain
*domain
, const char *name
);
82 MonoDomain
* (CDECL
*mono_domain_get
)(void);
83 MonoDomain
* (CDECL
*mono_domain_get_by_id
)(int id
);
84 BOOL (CDECL
*mono_domain_set
)(MonoDomain
*domain
,BOOL force
);
85 void (CDECL
*mono_domain_set_config
)(MonoDomain
*domain
,const char *base_dir
,const char *config_file_name
);
86 static void (CDECL
*mono_free
)(void *);
87 static MonoImage
* (CDECL
*mono_image_open
)(const char *fname
, MonoImageOpenStatus
*status
);
88 MonoImage
* (CDECL
*mono_image_open_from_module_handle
)(HMODULE module_handle
, char* fname
, UINT has_entry_point
, MonoImageOpenStatus
* status
);
89 static void (CDECL
*mono_install_assembly_preload_hook
)(MonoAssemblyPreLoadFunc func
, void *user_data
);
90 int (CDECL
*mono_jit_exec
)(MonoDomain
*domain
, MonoAssembly
*assembly
, int argc
, char *argv
[]);
91 MonoDomain
* (CDECL
*mono_jit_init_version
)(const char *domain_name
, const char *runtime_version
);
92 static int (CDECL
*mono_jit_set_trace_options
)(const char* options
);
93 void* (CDECL
*mono_marshal_get_vtfixup_ftnptr
)(MonoImage
*image
, DWORD token
, WORD type
);
94 MonoDomain
* (CDECL
*mono_object_get_domain
)(MonoObject
*obj
);
95 MonoMethod
* (CDECL
*mono_object_get_virtual_method
)(MonoObject
*obj
, MonoMethod
*method
);
96 MonoObject
* (CDECL
*mono_object_new
)(MonoDomain
*domain
, MonoClass
*klass
);
97 void* (CDECL
*mono_object_unbox
)(MonoObject
*obj
);
98 static void (CDECL
*mono_profiler_install
)(MonoProfiler
*prof
, MonoProfileFunc shutdown_callback
);
99 MonoType
* (CDECL
*mono_reflection_type_from_name
)(char *name
, MonoImage
*image
);
100 MonoObject
* (CDECL
*mono_runtime_invoke
)(MonoMethod
*method
, void *obj
, void **params
, MonoObject
**exc
);
101 void (CDECL
*mono_runtime_object_init
)(MonoObject
*this_obj
);
102 void (CDECL
*mono_runtime_quit
)(void);
103 static void (CDECL
*mono_set_crash_chaining
)(BOOL chain_signals
);
104 static void (CDECL
*mono_set_dirs
)(const char *assembly_dir
, const char *config_dir
);
105 static void (CDECL
*mono_set_verbose_level
)(DWORD level
);
106 MonoString
* (CDECL
*mono_string_new
)(MonoDomain
*domain
, const char *str
);
107 static char* (CDECL
*mono_stringify_assembly_name
)(MonoAssemblyName
*aname
);
108 MonoThread
* (CDECL
*mono_thread_attach
)(MonoDomain
*domain
);
109 void (CDECL
*mono_thread_manage
)(void);
110 void (CDECL
*mono_trace_set_assembly
)(MonoAssembly
*assembly
);
111 void (CDECL
*mono_trace_set_print_handler
)(MonoPrintCallback callback
);
112 void (CDECL
*mono_trace_set_printerr_handler
)(MonoPrintCallback callback
);
114 static BOOL
get_mono_path(LPWSTR path
);
116 static BOOL
find_mono_dll(LPCWSTR path
, LPWSTR dll_path
);
118 static MonoAssembly
* CDECL
mono_assembly_preload_hook_fn(MonoAssemblyName
*aname
, char **assemblies_path
, void *user_data
);
120 static void CDECL
mono_shutdown_callback_fn(MonoProfiler
*prof
);
122 static void CDECL
mono_print_handler_fn(const char *string
, INT is_stdout
);
124 static MonoImage
* CDECL
image_open_module_handle_dummy(HMODULE module_handle
,
125 char* fname
, UINT has_entry_point
, MonoImageOpenStatus
* status
)
127 return mono_image_open(fname
, status
);
130 static void CDECL
set_crash_chaining_dummy(BOOL crash_chaining
)
134 static void CDECL
set_print_handler_dummy(MonoPrintCallback callback
)
138 static HRESULT
load_mono(LPCWSTR mono_path
)
140 static const WCHAR lib
[] = {'\\','l','i','b',0};
141 static const WCHAR etc
[] = {'\\','e','t','c',0};
142 WCHAR mono_dll_path
[MAX_PATH
+16];
143 WCHAR mono_lib_path
[MAX_PATH
+4], mono_etc_path
[MAX_PATH
+4];
144 char mono_lib_path_a
[MAX_PATH
], mono_etc_path_a
[MAX_PATH
];
146 char trace_setting
[256];
148 char verbose_setting
[256];
150 if (is_mono_shutdown
)
152 ERR("Cannot load Mono after it has been shut down.\n");
158 strcpyW(mono_lib_path
, mono_path
);
159 strcatW(mono_lib_path
, lib
);
160 WideCharToMultiByte(CP_UTF8
, 0, mono_lib_path
, -1, mono_lib_path_a
, MAX_PATH
, NULL
, NULL
);
162 strcpyW(mono_etc_path
, mono_path
);
163 strcatW(mono_etc_path
, etc
);
164 WideCharToMultiByte(CP_UTF8
, 0, mono_etc_path
, -1, mono_etc_path_a
, MAX_PATH
, NULL
, NULL
);
166 if (!find_mono_dll(mono_path
, mono_dll_path
)) goto fail
;
168 mono_handle
= LoadLibraryW(mono_dll_path
);
170 if (!mono_handle
) goto fail
;
172 #define LOAD_MONO_FUNCTION(x) do { \
173 x = (void*)GetProcAddress(mono_handle, #x); \
179 LOAD_MONO_FUNCTION(mono_assembly_get_image
);
180 LOAD_MONO_FUNCTION(mono_assembly_load_from
);
181 LOAD_MONO_FUNCTION(mono_assembly_open
);
182 LOAD_MONO_FUNCTION(mono_config_parse
);
183 LOAD_MONO_FUNCTION(mono_class_from_mono_type
);
184 LOAD_MONO_FUNCTION(mono_class_from_name
);
185 LOAD_MONO_FUNCTION(mono_class_get_method_from_name
);
186 LOAD_MONO_FUNCTION(mono_domain_assembly_open
);
187 LOAD_MONO_FUNCTION(mono_domain_get
);
188 LOAD_MONO_FUNCTION(mono_domain_get_by_id
);
189 LOAD_MONO_FUNCTION(mono_domain_set
);
190 LOAD_MONO_FUNCTION(mono_domain_set_config
);
191 LOAD_MONO_FUNCTION(mono_free
);
192 LOAD_MONO_FUNCTION(mono_image_open
);
193 LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook
);
194 LOAD_MONO_FUNCTION(mono_jit_exec
);
195 LOAD_MONO_FUNCTION(mono_jit_init_version
);
196 LOAD_MONO_FUNCTION(mono_jit_set_trace_options
);
197 LOAD_MONO_FUNCTION(mono_marshal_get_vtfixup_ftnptr
);
198 LOAD_MONO_FUNCTION(mono_object_get_domain
);
199 LOAD_MONO_FUNCTION(mono_object_get_virtual_method
);
200 LOAD_MONO_FUNCTION(mono_object_new
);
201 LOAD_MONO_FUNCTION(mono_object_unbox
);
202 LOAD_MONO_FUNCTION(mono_profiler_install
);
203 LOAD_MONO_FUNCTION(mono_reflection_type_from_name
);
204 LOAD_MONO_FUNCTION(mono_runtime_invoke
);
205 LOAD_MONO_FUNCTION(mono_runtime_object_init
);
206 LOAD_MONO_FUNCTION(mono_runtime_quit
);
207 LOAD_MONO_FUNCTION(mono_set_dirs
);
208 LOAD_MONO_FUNCTION(mono_set_verbose_level
);
209 LOAD_MONO_FUNCTION(mono_stringify_assembly_name
);
210 LOAD_MONO_FUNCTION(mono_string_new
);
211 LOAD_MONO_FUNCTION(mono_thread_attach
);
212 LOAD_MONO_FUNCTION(mono_thread_manage
);
213 LOAD_MONO_FUNCTION(mono_trace_set_assembly
);
215 #undef LOAD_MONO_FUNCTION
217 #define LOAD_OPT_MONO_FUNCTION(x, default) do { \
218 x = (void*)GetProcAddress(mono_handle, #x); \
224 LOAD_OPT_MONO_FUNCTION(mono_image_open_from_module_handle
, image_open_module_handle_dummy
);
225 LOAD_OPT_MONO_FUNCTION(mono_set_crash_chaining
, set_crash_chaining_dummy
);
226 LOAD_OPT_MONO_FUNCTION(mono_trace_set_print_handler
, set_print_handler_dummy
);
227 LOAD_OPT_MONO_FUNCTION(mono_trace_set_printerr_handler
, set_print_handler_dummy
);
229 #undef LOAD_OPT_MONO_FUNCTION
231 mono_profiler_install(NULL
, mono_shutdown_callback_fn
);
233 mono_set_crash_chaining(TRUE
);
235 mono_trace_set_print_handler(mono_print_handler_fn
);
236 mono_trace_set_printerr_handler(mono_print_handler_fn
);
238 mono_set_dirs(mono_lib_path_a
, mono_etc_path_a
);
240 mono_config_parse(NULL
);
242 mono_install_assembly_preload_hook(mono_assembly_preload_hook_fn
, NULL
);
244 trace_size
= GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting
, sizeof(trace_setting
));
248 mono_jit_set_trace_options(trace_setting
);
251 verbose_size
= GetEnvironmentVariableA("WINE_MONO_VERBOSE", verbose_setting
, sizeof(verbose_setting
));
255 mono_set_verbose_level(verbose_setting
[0] - '0');
262 ERR("Could not load Mono into this process\n");
263 FreeLibrary(mono_handle
);
268 static void CDECL
mono_shutdown_callback_fn(MonoProfiler
*prof
)
270 is_mono_shutdown
= TRUE
;
273 static void CDECL
mono_print_handler_fn(const char *string
, INT is_stdout
)
276 for (; *string
; string
= p
)
278 if ((p
= strstr(string
, "\n"))) p
++;
279 else p
= string
+ strlen(string
);
280 wine_dbg_printf("%.*s", (int)(p
- string
), string
);
284 static HRESULT
CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo
*This
, RuntimeHost
**result
)
287 WCHAR mono_path
[MAX_PATH
];
289 if (This
->loaded_runtime
)
291 *result
= This
->loaded_runtime
;
295 if (!get_mono_path(mono_path
))
297 ERR("Wine Mono is not installed\n");
298 return CLR_E_SHIM_RUNTIME
;
301 EnterCriticalSection(&runtime_list_cs
);
303 hr
= load_mono(mono_path
);
306 hr
= RuntimeHost_Construct(This
, &This
->loaded_runtime
);
308 LeaveCriticalSection(&runtime_list_cs
);
311 *result
= This
->loaded_runtime
;
316 void expect_no_runtimes(void)
318 if (mono_handle
&& is_mono_started
&& !is_mono_shutdown
)
320 ERR("Process exited with a Mono runtime loaded.\n");
325 static inline CLRRuntimeInfo
*impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo
*iface
)
327 return CONTAINING_RECORD(iface
, CLRRuntimeInfo
, ICLRRuntimeInfo_iface
);
330 static HRESULT WINAPI
CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo
* iface
,
334 TRACE("%p %s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
336 if ( IsEqualGUID( riid
, &IID_ICLRRuntimeInfo
) ||
337 IsEqualGUID( riid
, &IID_IUnknown
) )
343 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
344 return E_NOINTERFACE
;
347 ICLRRuntimeInfo_AddRef( iface
);
352 static ULONG WINAPI
CLRRuntimeInfo_AddRef(ICLRRuntimeInfo
* iface
)
357 static ULONG WINAPI
CLRRuntimeInfo_Release(ICLRRuntimeInfo
* iface
)
362 static HRESULT WINAPI
CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo
* iface
,
363 LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
365 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
366 DWORD buffer_size
= *pcchBuffer
;
371 TRACE("%p %p %p\n", iface
, pwzBuffer
, pcchBuffer
);
373 size
= snprintf(version
, sizeof(version
), "v%u.%u.%u", This
->major
, This
->minor
, This
->build
);
375 assert(size
<= sizeof(version
));
377 *pcchBuffer
= MultiByteToWideChar(CP_UTF8
, 0, version
, -1, NULL
, 0);
381 if (buffer_size
>= *pcchBuffer
)
382 MultiByteToWideChar(CP_UTF8
, 0, version
, -1, pwzBuffer
, buffer_size
);
384 hr
= E_NOT_SUFFICIENT_BUFFER
;
390 static BOOL
get_install_root(LPWSTR install_dir
)
392 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};
393 const WCHAR install_root
[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
398 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, dotnet_key
, 0, KEY_READ
, &key
))
402 if (RegQueryValueExW(key
, install_root
, 0, NULL
, (LPBYTE
)install_dir
, &len
))
412 static HRESULT WINAPI
CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo
* iface
,
413 LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
415 static const WCHAR slash
[] = {'\\',0};
416 DWORD buffer_size
= *pcchBuffer
;
417 WCHAR system_dir
[MAX_PATH
];
418 WCHAR version
[MAX_PATH
];
419 DWORD version_size
, size
;
422 TRACE("%p %p %p\n", iface
, pwzBuffer
, pcchBuffer
);
424 if (!get_install_root(system_dir
))
426 ERR("error reading registry key for installroot\n");
431 version_size
= MAX_PATH
;
432 ICLRRuntimeInfo_GetVersionString(iface
, version
, &version_size
);
433 lstrcatW(system_dir
, version
);
434 lstrcatW(system_dir
, slash
);
435 size
= lstrlenW(system_dir
) + 1;
442 if (buffer_size
>= size
)
443 strcpyW(pwzBuffer
, system_dir
);
445 hr
= E_NOT_SUFFICIENT_BUFFER
;
451 static HRESULT WINAPI
CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo
* iface
,
452 HANDLE hndProcess
, BOOL
*pbLoaded
)
454 FIXME("%p %p %p\n", iface
, hndProcess
, pbLoaded
);
459 static HRESULT WINAPI
CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo
* iface
,
460 UINT iResourceID
, LPWSTR pwzBuffer
, DWORD
*pcchBuffer
, LONG iLocaleid
)
462 FIXME("%p %u %p %p %x\n", iface
, iResourceID
, pwzBuffer
, pcchBuffer
, iLocaleid
);
467 static HRESULT WINAPI
CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo
* iface
,
468 LPCWSTR pwzDllName
, HMODULE
*phndModule
)
470 WCHAR version
[MAX_PATH
];
474 TRACE("%p %s %p\n", iface
, debugstr_w(pwzDllName
), phndModule
);
476 cchBuffer
= MAX_PATH
;
477 hr
= ICLRRuntimeInfo_GetVersionString(iface
, version
, &cchBuffer
);
478 if (FAILED(hr
)) return hr
;
480 return LoadLibraryShim(pwzDllName
, version
, NULL
, phndModule
);
483 static HRESULT WINAPI
CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo
* iface
,
484 LPCSTR pszProcName
, LPVOID
*ppProc
)
486 FIXME("%p %s %p\n", iface
, debugstr_a(pszProcName
), ppProc
);
491 static HRESULT WINAPI
CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo
* iface
,
492 REFCLSID rclsid
, REFIID riid
, LPVOID
*ppUnk
)
494 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
498 TRACE("%p %s %s %p\n", iface
, debugstr_guid(rclsid
), debugstr_guid(riid
), ppUnk
);
500 hr
= CLRRuntimeInfo_GetRuntimeHost(This
, &host
);
503 hr
= RuntimeHost_GetInterface(host
, rclsid
, riid
, ppUnk
);
508 static HRESULT WINAPI
CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo
* iface
,
511 FIXME("%p %p\n", iface
, pbLoadable
);
516 static HRESULT WINAPI
CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo
* iface
,
517 DWORD dwStartupFlags
, LPCWSTR pwzHostConfigFile
)
519 FIXME("%p %x %s\n", iface
, dwStartupFlags
, debugstr_w(pwzHostConfigFile
));
524 static HRESULT WINAPI
CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo
* iface
,
525 DWORD
*pdwStartupFlags
, LPWSTR pwzHostConfigFile
, DWORD
*pcchHostConfigFile
)
527 FIXME("%p %p %p %p\n", iface
, pdwStartupFlags
, pwzHostConfigFile
, pcchHostConfigFile
);
532 static HRESULT WINAPI
CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo
* iface
)
534 FIXME("%p\n", iface
);
539 static HRESULT WINAPI
CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo
* iface
,
540 BOOL
*pbStarted
, DWORD
*pdwStartupFlags
)
542 FIXME("%p %p %p\n", iface
, pbStarted
, pdwStartupFlags
);
547 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl
= {
548 CLRRuntimeInfo_QueryInterface
,
549 CLRRuntimeInfo_AddRef
,
550 CLRRuntimeInfo_Release
,
551 CLRRuntimeInfo_GetVersionString
,
552 CLRRuntimeInfo_GetRuntimeDirectory
,
553 CLRRuntimeInfo_IsLoaded
,
554 CLRRuntimeInfo_LoadErrorString
,
555 CLRRuntimeInfo_LoadLibrary
,
556 CLRRuntimeInfo_GetProcAddress
,
557 CLRRuntimeInfo_GetInterface
,
558 CLRRuntimeInfo_IsLoadable
,
559 CLRRuntimeInfo_SetDefaultStartupFlags
,
560 CLRRuntimeInfo_GetDefaultStartupFlags
,
561 CLRRuntimeInfo_BindAsLegacyV2Runtime
,
562 CLRRuntimeInfo_IsStarted
565 HRESULT
ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo
*iface
, RuntimeHost
**result
)
567 struct CLRRuntimeInfo
*This
= impl_from_ICLRRuntimeInfo(iface
);
569 assert(This
->ICLRRuntimeInfo_iface
.lpVtbl
== &CLRRuntimeInfoVtbl
);
571 return CLRRuntimeInfo_GetRuntimeHost(This
, result
);
575 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};
576 #elif defined(__x86_64__)
577 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};
579 static const WCHAR libmono2_arch_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
582 static BOOL
find_mono_dll(LPCWSTR path
, LPWSTR dll_path
)
584 static const WCHAR mono2_dll
[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
585 static const WCHAR libmono2_dll
[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
586 DWORD attributes
=INVALID_FILE_ATTRIBUTES
;
588 strcpyW(dll_path
, path
);
589 strcatW(dll_path
, libmono2_arch_dll
);
590 attributes
= GetFileAttributesW(dll_path
);
592 if (attributes
== INVALID_FILE_ATTRIBUTES
)
594 strcpyW(dll_path
, path
);
595 strcatW(dll_path
, mono2_dll
);
596 attributes
= GetFileAttributesW(dll_path
);
599 if (attributes
== INVALID_FILE_ATTRIBUTES
)
601 strcpyW(dll_path
, path
);
602 strcatW(dll_path
, libmono2_dll
);
603 attributes
= GetFileAttributesW(dll_path
);
606 return (attributes
!= INVALID_FILE_ATTRIBUTES
);
609 static BOOL
get_mono_path(LPWSTR path
)
611 static const WCHAR subdir_mono
[] = {'\\','m','o','n','o','\\','m','o','n','o','-','2','.','0', 0};
612 WCHAR base_path
[MAX_PATH
], mono_dll_path
[MAX_PATH
];
614 /* c:\windows\mono\mono-2.0 */
615 GetWindowsDirectoryW(base_path
, MAX_PATH
);
616 strcatW(base_path
, subdir_mono
);
618 if (find_mono_dll(base_path
, mono_dll_path
))
620 strcpyW(path
, base_path
);
627 struct InstalledRuntimeEnum
629 IEnumUnknown IEnumUnknown_iface
;
634 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl
;
636 static inline struct InstalledRuntimeEnum
*impl_from_IEnumUnknown(IEnumUnknown
*iface
)
638 return CONTAINING_RECORD(iface
, struct InstalledRuntimeEnum
, IEnumUnknown_iface
);
641 static HRESULT WINAPI
InstalledRuntimeEnum_QueryInterface(IEnumUnknown
* iface
, REFIID riid
,
644 TRACE("%p %s %p\n", iface
, debugstr_guid(riid
), ppvObject
);
646 if ( IsEqualGUID( riid
, &IID_IEnumUnknown
) ||
647 IsEqualGUID( riid
, &IID_IUnknown
) )
653 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
654 return E_NOINTERFACE
;
657 IEnumUnknown_AddRef( iface
);
662 static ULONG WINAPI
InstalledRuntimeEnum_AddRef(IEnumUnknown
* iface
)
664 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
665 ULONG ref
= InterlockedIncrement(&This
->ref
);
667 TRACE("(%p) refcount=%u\n", iface
, ref
);
672 static ULONG WINAPI
InstalledRuntimeEnum_Release(IEnumUnknown
* iface
)
674 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
675 ULONG ref
= InterlockedDecrement(&This
->ref
);
677 TRACE("(%p) refcount=%u\n", iface
, ref
);
681 HeapFree(GetProcessHeap(), 0, This
);
687 static HRESULT WINAPI
InstalledRuntimeEnum_Next(IEnumUnknown
*iface
, ULONG celt
,
688 IUnknown
**rgelt
, ULONG
*pceltFetched
)
690 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
691 ULONG num_fetched
= 0;
695 TRACE("(%p,%u,%p,%p)\n", iface
, celt
, rgelt
, pceltFetched
);
697 while (num_fetched
< celt
)
699 if (This
->pos
>= NUM_RUNTIMES
)
704 item
= (IUnknown
*)&runtimes
[This
->pos
].ICLRRuntimeInfo_iface
;
705 IUnknown_AddRef(item
);
706 rgelt
[num_fetched
] = item
;
712 *pceltFetched
= num_fetched
;
717 static HRESULT WINAPI
InstalledRuntimeEnum_Skip(IEnumUnknown
*iface
, ULONG celt
)
719 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
720 ULONG num_fetched
= 0;
723 TRACE("(%p,%u)\n", iface
, celt
);
725 while (num_fetched
< celt
)
727 if (This
->pos
>= NUM_RUNTIMES
)
739 static HRESULT WINAPI
InstalledRuntimeEnum_Reset(IEnumUnknown
*iface
)
741 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
743 TRACE("(%p)\n", iface
);
750 static HRESULT WINAPI
InstalledRuntimeEnum_Clone(IEnumUnknown
*iface
, IEnumUnknown
**ppenum
)
752 struct InstalledRuntimeEnum
*This
= impl_from_IEnumUnknown(iface
);
753 struct InstalledRuntimeEnum
*new_enum
;
755 TRACE("(%p)\n", iface
);
757 new_enum
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum
));
759 return E_OUTOFMEMORY
;
761 new_enum
->IEnumUnknown_iface
.lpVtbl
= &InstalledRuntimeEnum_Vtbl
;
763 new_enum
->pos
= This
->pos
;
765 *ppenum
= &new_enum
->IEnumUnknown_iface
;
770 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl
= {
771 InstalledRuntimeEnum_QueryInterface
,
772 InstalledRuntimeEnum_AddRef
,
773 InstalledRuntimeEnum_Release
,
774 InstalledRuntimeEnum_Next
,
775 InstalledRuntimeEnum_Skip
,
776 InstalledRuntimeEnum_Reset
,
777 InstalledRuntimeEnum_Clone
782 ICLRMetaHost ICLRMetaHost_iface
;
784 RuntimeLoadedCallbackFnPtr callback
;
787 static struct CLRMetaHost GlobalCLRMetaHost
;
789 static HRESULT WINAPI
CLRMetaHost_QueryInterface(ICLRMetaHost
* iface
,
793 TRACE("%s %p\n", debugstr_guid(riid
), ppvObject
);
795 if ( IsEqualGUID( riid
, &IID_ICLRMetaHost
) ||
796 IsEqualGUID( riid
, &IID_IUnknown
) )
802 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
803 return E_NOINTERFACE
;
806 ICLRMetaHost_AddRef( iface
);
811 static ULONG WINAPI
CLRMetaHost_AddRef(ICLRMetaHost
* iface
)
816 static ULONG WINAPI
CLRMetaHost_Release(ICLRMetaHost
* iface
)
821 static inline BOOL
isDigit(WCHAR c
)
823 return c
>= '0' && c
<= '9';
826 static BOOL
parse_runtime_version(LPCWSTR version
, DWORD
*major
, DWORD
*minor
, DWORD
*build
)
832 if (version
[0] == 'v' || version
[0] == 'V')
835 if (!isDigit(*version
))
838 while (isDigit(*version
))
839 *major
= *major
* 10 + (*version
++ - '0');
844 if (*version
++ != '.' || !isDigit(*version
))
847 while (isDigit(*version
))
848 *minor
= *minor
* 10 + (*version
++ - '0');
853 if (*version
++ != '.' || !isDigit(*version
))
856 while (isDigit(*version
))
857 *build
= *build
* 10 + (*version
++ - '0');
859 return *version
== 0;
865 static HRESULT
get_runtime(LPCWSTR pwzVersion
, BOOL allow_short
,
866 REFIID iid
, LPVOID
*ppRuntime
)
869 DWORD major
, minor
, build
;
874 if (!parse_runtime_version(pwzVersion
, &major
, &minor
, &build
))
876 ERR("Cannot parse %s\n", debugstr_w(pwzVersion
));
877 return CLR_E_SHIM_RUNTIME
;
880 for (i
=0; i
<NUM_RUNTIMES
; i
++)
882 if (runtimes
[i
].major
== major
&& runtimes
[i
].minor
== minor
&&
883 (runtimes
[i
].build
== build
|| (allow_short
&& major
>= 4 && build
== 0)))
885 return ICLRRuntimeInfo_QueryInterface(&runtimes
[i
].ICLRRuntimeInfo_iface
, iid
,
890 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion
));
891 return CLR_E_SHIM_RUNTIME
;
894 HRESULT WINAPI
CLRMetaHost_GetRuntime(ICLRMetaHost
* iface
,
895 LPCWSTR pwzVersion
, REFIID iid
, LPVOID
*ppRuntime
)
897 TRACE("%s %s %p\n", debugstr_w(pwzVersion
), debugstr_guid(iid
), ppRuntime
);
899 return get_runtime(pwzVersion
, FALSE
, iid
, ppRuntime
);
902 HRESULT WINAPI
CLRMetaHost_GetVersionFromFile(ICLRMetaHost
* iface
,
903 LPCWSTR pwzFilePath
, LPWSTR pwzBuffer
, DWORD
*pcchBuffer
)
908 ULONG buffer_size
=*pcchBuffer
;
910 TRACE("%s %p %p\n", debugstr_w(pwzFilePath
), pwzBuffer
, pcchBuffer
);
912 hr
= assembly_create(&assembly
, pwzFilePath
);
916 hr
= assembly_get_runtime_version(assembly
, &version
);
920 *pcchBuffer
= MultiByteToWideChar(CP_UTF8
, 0, version
, -1, NULL
, 0);
924 if (buffer_size
>= *pcchBuffer
)
925 MultiByteToWideChar(CP_UTF8
, 0, version
, -1, pwzBuffer
, buffer_size
);
927 hr
= E_NOT_SUFFICIENT_BUFFER
;
931 assembly_release(assembly
);
937 static HRESULT WINAPI
CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost
* iface
,
938 IEnumUnknown
**ppEnumerator
)
940 struct InstalledRuntimeEnum
*new_enum
;
942 TRACE("%p\n", ppEnumerator
);
944 new_enum
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum
));
946 return E_OUTOFMEMORY
;
948 new_enum
->IEnumUnknown_iface
.lpVtbl
= &InstalledRuntimeEnum_Vtbl
;
952 *ppEnumerator
= &new_enum
->IEnumUnknown_iface
;
957 static HRESULT WINAPI
CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost
* iface
,
958 HANDLE hndProcess
, IEnumUnknown
**ppEnumerator
)
960 FIXME("%p %p\n", hndProcess
, ppEnumerator
);
965 static HRESULT WINAPI
CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost
* iface
,
966 RuntimeLoadedCallbackFnPtr pCallbackFunction
)
968 TRACE("%p\n", pCallbackFunction
);
970 if(!pCallbackFunction
)
973 WARN("Callback currently will not be called.\n");
975 GlobalCLRMetaHost
.callback
= pCallbackFunction
;
980 static HRESULT WINAPI
CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost
* iface
,
981 REFIID riid
, LPVOID
*ppUnk
)
983 FIXME("%s %p\n", debugstr_guid(riid
), ppUnk
);
988 HRESULT WINAPI
CLRMetaHost_ExitProcess(ICLRMetaHost
* iface
, INT32 iExitCode
)
990 TRACE("%i\n", iExitCode
);
992 EnterCriticalSection(&runtime_list_cs
);
994 if (is_mono_started
&& !is_mono_shutdown
)
996 /* search for a runtime and call System.Environment.Exit() */
999 for (i
=0; i
<NUM_RUNTIMES
; i
++)
1000 if (runtimes
[i
].loaded_runtime
)
1001 RuntimeHost_ExitProcess(runtimes
[i
].loaded_runtime
, iExitCode
);
1004 ExitProcess(iExitCode
);
1007 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl
=
1009 CLRMetaHost_QueryInterface
,
1011 CLRMetaHost_Release
,
1012 CLRMetaHost_GetRuntime
,
1013 CLRMetaHost_GetVersionFromFile
,
1014 CLRMetaHost_EnumerateInstalledRuntimes
,
1015 CLRMetaHost_EnumerateLoadedRuntimes
,
1016 CLRMetaHost_RequestRuntimeLoadedNotification
,
1017 CLRMetaHost_QueryLegacyV2RuntimeBinding
,
1018 CLRMetaHost_ExitProcess
1021 static struct CLRMetaHost GlobalCLRMetaHost
= {
1022 { &CLRMetaHost_vtbl
}
1025 HRESULT
CLRMetaHost_CreateInstance(REFIID riid
, void **ppobj
)
1027 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost
.ICLRMetaHost_iface
, riid
, ppobj
);
1030 struct CLRMetaHostPolicy
1032 ICLRMetaHostPolicy ICLRMetaHostPolicy_iface
;
1035 static struct CLRMetaHostPolicy GlobalCLRMetaHostPolicy
;
1037 static HRESULT WINAPI
metahostpolicy_QueryInterface(ICLRMetaHostPolicy
*iface
, REFIID riid
, void **obj
)
1039 TRACE("%s %p\n", debugstr_guid(riid
), obj
);
1041 if ( IsEqualGUID( riid
, &IID_ICLRMetaHostPolicy
) ||
1042 IsEqualGUID( riid
, &IID_IUnknown
) )
1044 ICLRMetaHostPolicy_AddRef( iface
);
1049 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
1052 return E_NOINTERFACE
;
1055 static ULONG WINAPI
metahostpolicy_AddRef(ICLRMetaHostPolicy
*iface
)
1060 static ULONG WINAPI
metahostpolicy_Release(ICLRMetaHostPolicy
*iface
)
1065 static HRESULT WINAPI
metahostpolicy_GetRequestedRuntime(ICLRMetaHostPolicy
*iface
, METAHOST_POLICY_FLAGS dwPolicyFlags
,
1066 LPCWSTR pwzBinary
, IStream
*pCfgStream
, LPWSTR pwzVersion
, DWORD
*pcchVersion
,
1067 LPWSTR pwzImageVersion
, DWORD
*pcchImageVersion
, DWORD
*pdwConfigFlags
, REFIID riid
,
1070 ICLRRuntimeInfo
*result
;
1072 WCHAR filename
[MAX_PATH
];
1073 const WCHAR
*path
= NULL
;
1076 TRACE("%d %p %p %p %p %p %p %p %s %p\n", dwPolicyFlags
, pwzBinary
, pCfgStream
,
1077 pwzVersion
, pcchVersion
, pwzImageVersion
, pcchImageVersion
, pdwConfigFlags
,
1078 debugstr_guid(riid
), ppRuntime
);
1081 FIXME("ignoring config file stream\n");
1084 FIXME("ignoring config flags\n");
1086 if(dwPolicyFlags
& METAHOST_POLICY_USE_PROCESS_IMAGE_PATH
)
1088 GetModuleFileNameW(0, filename
, MAX_PATH
);
1096 if(dwPolicyFlags
& METAHOST_POLICY_APPLY_UPGRADE_POLICY
)
1097 flags
|= RUNTIME_INFO_UPGRADE_VERSION
;
1099 hr
= get_runtime_info(path
, pwzImageVersion
, NULL
, 0, flags
, FALSE
, &result
);
1102 if (pwzImageVersion
)
1104 /* Ignoring errors on purpose */
1105 ICLRRuntimeInfo_GetVersionString(result
, pwzImageVersion
, pcchImageVersion
);
1108 hr
= ICLRRuntimeInfo_QueryInterface(result
, riid
, ppRuntime
);
1110 ICLRRuntimeInfo_Release(result
);
1113 TRACE("<- 0x%08x\n", hr
);
1118 static const struct ICLRMetaHostPolicyVtbl CLRMetaHostPolicy_vtbl
=
1120 metahostpolicy_QueryInterface
,
1121 metahostpolicy_AddRef
,
1122 metahostpolicy_Release
,
1123 metahostpolicy_GetRequestedRuntime
1126 static struct CLRMetaHostPolicy GlobalCLRMetaHostPolicy
= {
1127 { &CLRMetaHostPolicy_vtbl
}
1130 HRESULT
CLRMetaHostPolicy_CreateInstance(REFIID riid
, void **ppobj
)
1132 return ICLRMetaHostPolicy_QueryInterface(&GlobalCLRMetaHostPolicy
.ICLRMetaHostPolicy_iface
, riid
, ppobj
);
1135 HRESULT
get_file_from_strongname(WCHAR
* stringnameW
, WCHAR
* assemblies_path
, int path_length
)
1138 IAssemblyCache
*asmcache
;
1140 static const WCHAR fusiondll
[] = {'f','u','s','i','o','n',0};
1141 HMODULE hfusion
=NULL
;
1142 static HRESULT (WINAPI
*pCreateAssemblyCache
)(IAssemblyCache
**,DWORD
);
1144 if (!pCreateAssemblyCache
)
1146 hr
= LoadLibraryShim(fusiondll
, NULL
, NULL
, &hfusion
);
1150 pCreateAssemblyCache
= (void*)GetProcAddress(hfusion
, "CreateAssemblyCache");
1151 if (!pCreateAssemblyCache
)
1157 hr
= pCreateAssemblyCache(&asmcache
, 0);
1161 info
.cbAssemblyInfo
= sizeof(info
);
1162 info
.pszCurrentAssemblyPathBuf
= assemblies_path
;
1163 info
.cchBuf
= path_length
;
1164 assemblies_path
[0] = 0;
1166 hr
= IAssemblyCache_QueryAssemblyInfo(asmcache
, 0, stringnameW
, &info
);
1168 IAssemblyCache_Release(asmcache
);
1174 static MonoAssembly
* CDECL
mono_assembly_preload_hook_fn(MonoAssemblyName
*aname
, char **assemblies_path
, void *user_data
)
1177 MonoAssembly
*result
=NULL
;
1178 char *stringname
=NULL
;
1180 int stringnameW_size
;
1181 WCHAR path
[MAX_PATH
];
1183 MonoImageOpenStatus stat
;
1185 stringname
= mono_stringify_assembly_name(aname
);
1187 TRACE("%s\n", debugstr_a(stringname
));
1189 if (!stringname
) return NULL
;
1191 /* FIXME: We should search the given paths before the GAC. */
1193 stringnameW_size
= MultiByteToWideChar(CP_UTF8
, 0, stringname
, -1, NULL
, 0);
1195 stringnameW
= HeapAlloc(GetProcessHeap(), 0, stringnameW_size
* sizeof(WCHAR
));
1198 MultiByteToWideChar(CP_UTF8
, 0, stringname
, -1, stringnameW
, stringnameW_size
);
1200 hr
= get_file_from_strongname(stringnameW
, path
, MAX_PATH
);
1202 HeapFree(GetProcessHeap(), 0, stringnameW
);
1209 TRACE("found: %s\n", debugstr_w(path
));
1215 result
= mono_assembly_open(pathA
, &stat
);
1218 ERR("Failed to load %s, status=%u\n", debugstr_w(path
), stat
);
1220 HeapFree(GetProcessHeap(), 0, pathA
);
1224 mono_free(stringname
);
1229 HRESULT
get_runtime_info(LPCWSTR exefile
, LPCWSTR version
, LPCWSTR config_file
,
1230 DWORD startup_flags
, DWORD runtimeinfo_flags
, BOOL legacy
, ICLRRuntimeInfo
**result
)
1232 static const WCHAR dotconfig
[] = {'.','c','o','n','f','i','g',0};
1233 static const DWORD supported_startup_flags
= 0;
1234 static const DWORD supported_runtime_flags
= RUNTIME_INFO_UPGRADE_VERSION
;
1236 WCHAR local_version
[MAX_PATH
];
1237 ULONG local_version_size
= MAX_PATH
;
1238 WCHAR local_config_file
[MAX_PATH
];
1240 parsed_config_file parsed_config
;
1242 if (startup_flags
& ~supported_startup_flags
)
1243 FIXME("unsupported startup flags %x\n", startup_flags
& ~supported_startup_flags
);
1245 if (runtimeinfo_flags
& ~supported_runtime_flags
)
1246 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags
& ~supported_runtime_flags
);
1248 if (exefile
&& !exefile
[0])
1251 if (exefile
&& !config_file
)
1253 strcpyW(local_config_file
, exefile
);
1254 strcatW(local_config_file
, dotconfig
);
1256 config_file
= local_config_file
;
1262 hr
= parse_config_file(config_file
, &parsed_config
);
1266 supported_runtime
*entry
;
1267 LIST_FOR_EACH_ENTRY(entry
, &parsed_config
.supported_runtimes
, supported_runtime
, entry
)
1269 hr
= get_runtime(entry
->version
, TRUE
, &IID_ICLRRuntimeInfo
, (void**)result
);
1279 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file
), hr
);
1282 free_parsed_config_file(&parsed_config
);
1288 if (exefile
&& !version
)
1290 hr
= CLRMetaHost_GetVersionFromFile(0, exefile
, local_version
, &local_version_size
);
1292 version
= local_version
;
1294 if (FAILED(hr
)) return hr
;
1299 hr
= CLRMetaHost_GetRuntime(0, version
, &IID_ICLRRuntimeInfo
, (void**)result
);
1304 if (runtimeinfo_flags
& RUNTIME_INFO_UPGRADE_VERSION
)
1306 DWORD major
, minor
, build
;
1308 if (version
&& !parse_runtime_version(version
, &major
, &minor
, &build
))
1310 ERR("Cannot parse %s\n", debugstr_w(version
));
1311 return CLR_E_SHIM_RUNTIME
;
1321 /* Must be greater or equal to the version passed in. */
1322 if (!version
|| ((runtimes
[i
].major
>= major
&& runtimes
[i
].minor
>= minor
&& runtimes
[i
].build
>= build
) ||
1323 (runtimes
[i
].major
>= major
&& runtimes
[i
].minor
> minor
) ||
1324 (runtimes
[i
].major
> major
)))
1326 return ICLRRuntimeInfo_QueryInterface(&runtimes
[i
].ICLRRuntimeInfo_iface
,
1327 &IID_ICLRRuntimeInfo
, (void **)result
);
1331 return CLR_E_SHIM_RUNTIME
;
1334 return CLR_E_SHIM_RUNTIME
;