msctf: Fix spelling of a private struct's field name.
[wine.git] / dlls / mscoree / metahost.c
blob252740d29b3bd1b5c17bed40c0b4326215041093
1 /*
2 * ICLRMetaHost - discovery and management of available .NET runtimes
4 * Copyright 2010 Vincent Povirk for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <assert.h>
25 #define COBJMACROS
27 #include "wine/unicode.h"
28 #include "wine/library.h"
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winreg.h"
32 #include "ole2.h"
34 #include "corerror.h"
35 #include "cor.h"
36 #include "mscoree.h"
37 #include "corhdr.h"
38 #include "cordebug.h"
39 #include "metahost.h"
40 #include "fusion.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;
71 BOOL is_mono_started;
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];
145 int trace_size;
146 char trace_setting[256];
147 int verbose_size;
148 char verbose_setting[256];
150 if (is_mono_shutdown)
152 ERR("Cannot load Mono after it has been shut down.\n");
153 return E_FAIL;
156 if (!mono_handle)
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); \
174 if (!x) { \
175 goto fail; \
177 } while (0);
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); \
219 if (!x) { \
220 x = default; \
222 } while (0);
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));
246 if (trace_size)
248 mono_jit_set_trace_options(trace_setting);
251 verbose_size = GetEnvironmentVariableA("WINE_MONO_VERBOSE", verbose_setting, sizeof(verbose_setting));
253 if (verbose_size)
255 mono_set_verbose_level(verbose_setting[0] - '0');
259 return S_OK;
261 fail:
262 ERR("Could not load Mono into this process\n");
263 FreeLibrary(mono_handle);
264 mono_handle = NULL;
265 return E_FAIL;
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)
275 wine_dbg_printf("%s", string);
278 static HRESULT CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo *This, RuntimeHost **result)
280 HRESULT hr = S_OK;
281 WCHAR mono_path[MAX_PATH];
283 if (This->loaded_runtime)
285 *result = This->loaded_runtime;
286 return hr;
289 if (!get_mono_path(mono_path))
291 ERR("Wine Mono is not installed\n");
292 return CLR_E_SHIM_RUNTIME;
295 EnterCriticalSection(&runtime_list_cs);
297 hr = load_mono(mono_path);
299 if (SUCCEEDED(hr))
300 hr = RuntimeHost_Construct(This, &This->loaded_runtime);
302 LeaveCriticalSection(&runtime_list_cs);
304 if (SUCCEEDED(hr))
305 *result = This->loaded_runtime;
307 return hr;
310 void expect_no_runtimes(void)
312 if (mono_handle && is_mono_started && !is_mono_shutdown)
314 ERR("Process exited with a Mono runtime loaded.\n");
315 return;
319 static inline CLRRuntimeInfo *impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo *iface)
321 return CONTAINING_RECORD(iface, CLRRuntimeInfo, ICLRRuntimeInfo_iface);
324 static HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
325 REFIID riid,
326 void **ppvObject)
328 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
330 if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
331 IsEqualGUID( riid, &IID_IUnknown ) )
333 *ppvObject = iface;
335 else
337 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
338 return E_NOINTERFACE;
341 ICLRRuntimeInfo_AddRef( iface );
343 return S_OK;
346 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
348 return 2;
351 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
353 return 1;
356 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
357 LPWSTR pwzBuffer, DWORD *pcchBuffer)
359 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
360 DWORD buffer_size = *pcchBuffer;
361 HRESULT hr = S_OK;
362 char version[11];
363 DWORD size;
365 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
367 size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
369 assert(size <= sizeof(version));
371 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
373 if (pwzBuffer)
375 if (buffer_size >= *pcchBuffer)
376 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
377 else
378 hr = E_NOT_SUFFICIENT_BUFFER;
381 return hr;
384 static BOOL get_install_root(LPWSTR install_dir)
386 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};
387 const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
389 DWORD len;
390 HKEY key;
392 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
393 return FALSE;
395 len = MAX_PATH;
396 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
398 RegCloseKey(key);
399 return FALSE;
401 RegCloseKey(key);
403 return TRUE;
406 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
407 LPWSTR pwzBuffer, DWORD *pcchBuffer)
409 static const WCHAR slash[] = {'\\',0};
410 DWORD buffer_size = *pcchBuffer;
411 WCHAR system_dir[MAX_PATH];
412 WCHAR version[MAX_PATH];
413 DWORD version_size, size;
414 HRESULT hr = S_OK;
416 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
418 if (!get_install_root(system_dir))
420 ERR("error reading registry key for installroot\n");
421 return E_FAIL;
423 else
425 version_size = MAX_PATH;
426 ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
427 lstrcatW(system_dir, version);
428 lstrcatW(system_dir, slash);
429 size = lstrlenW(system_dir) + 1;
432 *pcchBuffer = size;
434 if (pwzBuffer)
436 if (buffer_size >= size)
437 strcpyW(pwzBuffer, system_dir);
438 else
439 hr = E_NOT_SUFFICIENT_BUFFER;
442 return hr;
445 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
446 HANDLE hndProcess, BOOL *pbLoaded)
448 FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
450 return E_NOTIMPL;
453 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
454 UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
456 FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
458 return E_NOTIMPL;
461 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
462 LPCWSTR pwzDllName, HMODULE *phndModule)
464 WCHAR version[MAX_PATH];
465 HRESULT hr;
466 DWORD cchBuffer;
468 TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
470 cchBuffer = MAX_PATH;
471 hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
472 if (FAILED(hr)) return hr;
474 return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
477 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
478 LPCSTR pszProcName, LPVOID *ppProc)
480 FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
482 return E_NOTIMPL;
485 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
486 REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
488 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
489 RuntimeHost *host;
490 HRESULT hr;
492 TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
494 hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
496 if (SUCCEEDED(hr))
497 hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
499 return hr;
502 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
503 BOOL *pbLoadable)
505 FIXME("%p %p\n", iface, pbLoadable);
507 return E_NOTIMPL;
510 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
511 DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
513 FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
515 return E_NOTIMPL;
518 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
519 DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
521 FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
523 return E_NOTIMPL;
526 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
528 FIXME("%p\n", iface);
530 return E_NOTIMPL;
533 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
534 BOOL *pbStarted, DWORD *pdwStartupFlags)
536 FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
538 return E_NOTIMPL;
541 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
542 CLRRuntimeInfo_QueryInterface,
543 CLRRuntimeInfo_AddRef,
544 CLRRuntimeInfo_Release,
545 CLRRuntimeInfo_GetVersionString,
546 CLRRuntimeInfo_GetRuntimeDirectory,
547 CLRRuntimeInfo_IsLoaded,
548 CLRRuntimeInfo_LoadErrorString,
549 CLRRuntimeInfo_LoadLibrary,
550 CLRRuntimeInfo_GetProcAddress,
551 CLRRuntimeInfo_GetInterface,
552 CLRRuntimeInfo_IsLoadable,
553 CLRRuntimeInfo_SetDefaultStartupFlags,
554 CLRRuntimeInfo_GetDefaultStartupFlags,
555 CLRRuntimeInfo_BindAsLegacyV2Runtime,
556 CLRRuntimeInfo_IsStarted
559 HRESULT ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo *iface, RuntimeHost **result)
561 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
563 assert(This->ICLRRuntimeInfo_iface.lpVtbl == &CLRRuntimeInfoVtbl);
565 return CLRRuntimeInfo_GetRuntimeHost(This, result);
568 #ifdef __i386__
569 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};
570 #elif defined(__x86_64__)
571 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};
572 #else
573 static const WCHAR libmono2_arch_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
574 #endif
576 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path)
578 static const WCHAR mono2_dll[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
579 static const WCHAR libmono2_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
580 DWORD attributes=INVALID_FILE_ATTRIBUTES;
582 strcpyW(dll_path, path);
583 strcatW(dll_path, libmono2_arch_dll);
584 attributes = GetFileAttributesW(dll_path);
586 if (attributes == INVALID_FILE_ATTRIBUTES)
588 strcpyW(dll_path, path);
589 strcatW(dll_path, mono2_dll);
590 attributes = GetFileAttributesW(dll_path);
593 if (attributes == INVALID_FILE_ATTRIBUTES)
595 strcpyW(dll_path, path);
596 strcatW(dll_path, libmono2_dll);
597 attributes = GetFileAttributesW(dll_path);
600 return (attributes != INVALID_FILE_ATTRIBUTES);
603 static BOOL get_mono_path(LPWSTR path)
605 static const WCHAR subdir_mono[] = {'\\','m','o','n','o','\\','m','o','n','o','-','2','.','0', 0};
606 WCHAR base_path[MAX_PATH], mono_dll_path[MAX_PATH];
608 /* c:\windows\mono\mono-2.0 */
609 GetWindowsDirectoryW(base_path, MAX_PATH);
610 strcatW(base_path, subdir_mono);
612 if (find_mono_dll(base_path, mono_dll_path))
614 strcpyW(path, base_path);
615 return TRUE;
618 return FALSE;
621 struct InstalledRuntimeEnum
623 IEnumUnknown IEnumUnknown_iface;
624 LONG ref;
625 ULONG pos;
628 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
630 static inline struct InstalledRuntimeEnum *impl_from_IEnumUnknown(IEnumUnknown *iface)
632 return CONTAINING_RECORD(iface, struct InstalledRuntimeEnum, IEnumUnknown_iface);
635 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface, REFIID riid,
636 void **ppvObject)
638 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
640 if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
641 IsEqualGUID( riid, &IID_IUnknown ) )
643 *ppvObject = iface;
645 else
647 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
648 return E_NOINTERFACE;
651 IEnumUnknown_AddRef( iface );
653 return S_OK;
656 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
658 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
659 ULONG ref = InterlockedIncrement(&This->ref);
661 TRACE("(%p) refcount=%u\n", iface, ref);
663 return ref;
666 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
668 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
669 ULONG ref = InterlockedDecrement(&This->ref);
671 TRACE("(%p) refcount=%u\n", iface, ref);
673 if (ref == 0)
675 HeapFree(GetProcessHeap(), 0, This);
678 return ref;
681 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
682 IUnknown **rgelt, ULONG *pceltFetched)
684 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
685 ULONG num_fetched = 0;
686 HRESULT hr=S_OK;
687 IUnknown *item;
689 TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
691 while (num_fetched < celt)
693 if (This->pos >= NUM_RUNTIMES)
695 hr = S_FALSE;
696 break;
698 item = (IUnknown*)&runtimes[This->pos].ICLRRuntimeInfo_iface;
699 IUnknown_AddRef(item);
700 rgelt[num_fetched] = item;
701 num_fetched++;
702 This->pos++;
705 if (pceltFetched)
706 *pceltFetched = num_fetched;
708 return hr;
711 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
713 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
714 ULONG num_fetched = 0;
715 HRESULT hr=S_OK;
717 TRACE("(%p,%u)\n", iface, celt);
719 while (num_fetched < celt)
721 if (This->pos >= NUM_RUNTIMES)
723 hr = S_FALSE;
724 break;
726 num_fetched++;
727 This->pos++;
730 return hr;
733 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
735 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
737 TRACE("(%p)\n", iface);
739 This->pos = 0;
741 return S_OK;
744 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
746 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
747 struct InstalledRuntimeEnum *new_enum;
749 TRACE("(%p)\n", iface);
751 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
752 if (!new_enum)
753 return E_OUTOFMEMORY;
755 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
756 new_enum->ref = 1;
757 new_enum->pos = This->pos;
759 *ppenum = &new_enum->IEnumUnknown_iface;
761 return S_OK;
764 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
765 InstalledRuntimeEnum_QueryInterface,
766 InstalledRuntimeEnum_AddRef,
767 InstalledRuntimeEnum_Release,
768 InstalledRuntimeEnum_Next,
769 InstalledRuntimeEnum_Skip,
770 InstalledRuntimeEnum_Reset,
771 InstalledRuntimeEnum_Clone
774 struct CLRMetaHost
776 ICLRMetaHost ICLRMetaHost_iface;
778 RuntimeLoadedCallbackFnPtr callback;
781 static struct CLRMetaHost GlobalCLRMetaHost;
783 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
784 REFIID riid,
785 void **ppvObject)
787 TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
789 if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
790 IsEqualGUID( riid, &IID_IUnknown ) )
792 *ppvObject = iface;
794 else
796 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
797 return E_NOINTERFACE;
800 ICLRMetaHost_AddRef( iface );
802 return S_OK;
805 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
807 return 2;
810 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
812 return 1;
815 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
817 *major = 0;
818 *minor = 0;
819 *build = 0;
821 if (version[0] == 'v' || version[0] == 'V')
823 version++;
824 if (!isdigit(*version))
825 return FALSE;
827 while (isdigit(*version))
828 *major = *major * 10 + (*version++ - '0');
830 if (*version == 0)
831 return TRUE;
833 if (*version++ != '.' || !isdigit(*version))
834 return FALSE;
836 while (isdigit(*version))
837 *minor = *minor * 10 + (*version++ - '0');
839 if (*version == 0)
840 return TRUE;
842 if (*version++ != '.' || !isdigit(*version))
843 return FALSE;
845 while (isdigit(*version))
846 *build = *build * 10 + (*version++ - '0');
848 return *version == 0;
850 else
851 return FALSE;
854 static HRESULT get_runtime(LPCWSTR pwzVersion, BOOL allow_short,
855 REFIID iid, LPVOID *ppRuntime)
857 int i;
858 DWORD major, minor, build;
860 if (!pwzVersion)
861 return E_POINTER;
863 if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
865 ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
866 return CLR_E_SHIM_RUNTIME;
869 for (i=0; i<NUM_RUNTIMES; i++)
871 if (runtimes[i].major == major && runtimes[i].minor == minor &&
872 (runtimes[i].build == build || (allow_short && major >= 4 && build == 0)))
874 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface, iid,
875 ppRuntime);
879 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
880 return CLR_E_SHIM_RUNTIME;
883 HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
884 LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
886 TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
888 return get_runtime(pwzVersion, FALSE, iid, ppRuntime);
891 HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
892 LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
894 ASSEMBLY *assembly;
895 HRESULT hr;
896 LPSTR version;
897 ULONG buffer_size=*pcchBuffer;
899 TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
901 hr = assembly_create(&assembly, pwzFilePath);
903 if (SUCCEEDED(hr))
905 hr = assembly_get_runtime_version(assembly, &version);
907 if (SUCCEEDED(hr))
909 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
911 if (pwzBuffer)
913 if (buffer_size >= *pcchBuffer)
914 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
915 else
916 hr = E_NOT_SUFFICIENT_BUFFER;
920 assembly_release(assembly);
923 return hr;
926 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
927 IEnumUnknown **ppEnumerator)
929 struct InstalledRuntimeEnum *new_enum;
931 TRACE("%p\n", ppEnumerator);
933 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
934 if (!new_enum)
935 return E_OUTOFMEMORY;
937 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
938 new_enum->ref = 1;
939 new_enum->pos = 0;
941 *ppEnumerator = &new_enum->IEnumUnknown_iface;
943 return S_OK;
946 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
947 HANDLE hndProcess, IEnumUnknown **ppEnumerator)
949 FIXME("%p %p\n", hndProcess, ppEnumerator);
951 return E_NOTIMPL;
954 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
955 RuntimeLoadedCallbackFnPtr pCallbackFunction)
957 TRACE("%p\n", pCallbackFunction);
959 if(!pCallbackFunction)
960 return E_POINTER;
962 WARN("Callback currently will not be called.\n");
964 GlobalCLRMetaHost.callback = pCallbackFunction;
966 return S_OK;
969 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
970 REFIID riid, LPVOID *ppUnk)
972 FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
974 return E_NOTIMPL;
977 HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
979 TRACE("%i\n", iExitCode);
981 EnterCriticalSection(&runtime_list_cs);
983 if (is_mono_started && !is_mono_shutdown)
985 /* search for a runtime and call System.Environment.Exit() */
986 int i;
988 for (i=0; i<NUM_RUNTIMES; i++)
989 if (runtimes[i].loaded_runtime)
990 RuntimeHost_ExitProcess(runtimes[i].loaded_runtime, iExitCode);
993 ExitProcess(iExitCode);
996 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
998 CLRMetaHost_QueryInterface,
999 CLRMetaHost_AddRef,
1000 CLRMetaHost_Release,
1001 CLRMetaHost_GetRuntime,
1002 CLRMetaHost_GetVersionFromFile,
1003 CLRMetaHost_EnumerateInstalledRuntimes,
1004 CLRMetaHost_EnumerateLoadedRuntimes,
1005 CLRMetaHost_RequestRuntimeLoadedNotification,
1006 CLRMetaHost_QueryLegacyV2RuntimeBinding,
1007 CLRMetaHost_ExitProcess
1010 static struct CLRMetaHost GlobalCLRMetaHost = {
1011 { &CLRMetaHost_vtbl }
1014 HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1016 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost.ICLRMetaHost_iface, riid, ppobj);
1019 struct CLRMetaHostPolicy
1021 ICLRMetaHostPolicy ICLRMetaHostPolicy_iface;
1024 static struct CLRMetaHostPolicy GlobalCLRMetaHostPolicy;
1026 static HRESULT WINAPI metahostpolicy_QueryInterface(ICLRMetaHostPolicy *iface, REFIID riid, void **obj)
1028 TRACE("%s %p\n", debugstr_guid(riid), obj);
1030 if ( IsEqualGUID( riid, &IID_ICLRMetaHostPolicy ) ||
1031 IsEqualGUID( riid, &IID_IUnknown ) )
1033 ICLRMetaHostPolicy_AddRef( iface );
1034 *obj = iface;
1035 return S_OK;
1038 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
1040 *obj = NULL;
1041 return E_NOINTERFACE;
1044 static ULONG WINAPI metahostpolicy_AddRef(ICLRMetaHostPolicy *iface)
1046 return 2;
1049 static ULONG WINAPI metahostpolicy_Release(ICLRMetaHostPolicy *iface)
1051 return 1;
1054 static HRESULT WINAPI metahostpolicy_GetRequestedRuntime(ICLRMetaHostPolicy *iface, METAHOST_POLICY_FLAGS dwPolicyFlags,
1055 LPCWSTR pwzBinary, IStream *pCfgStream, LPWSTR pwzVersion, DWORD *pcchVersion,
1056 LPWSTR pwzImageVersion, DWORD *pcchImageVersion, DWORD *pdwConfigFlags, REFIID riid,
1057 LPVOID *ppRuntime)
1059 ICLRRuntimeInfo *result;
1060 HRESULT hr;
1061 WCHAR filename[MAX_PATH];
1062 const WCHAR *path = NULL;
1063 int flags = 0;
1065 TRACE("%d %p %p %p %p %p %p %p %s %p\n", dwPolicyFlags, pwzBinary, pCfgStream,
1066 pwzVersion, pcchVersion, pwzImageVersion, pcchImageVersion, pdwConfigFlags,
1067 debugstr_guid(riid), ppRuntime);
1069 if (pCfgStream)
1070 FIXME("ignoring config file stream\n");
1072 if (pdwConfigFlags)
1073 FIXME("ignoring config flags\n");
1075 if(dwPolicyFlags & METAHOST_POLICY_USE_PROCESS_IMAGE_PATH)
1077 GetModuleFileNameW(0, filename, MAX_PATH);
1078 path = filename;
1080 else if(pwzBinary)
1082 path = pwzBinary;
1085 if(dwPolicyFlags & METAHOST_POLICY_APPLY_UPGRADE_POLICY)
1086 flags |= RUNTIME_INFO_UPGRADE_VERSION;
1088 hr = get_runtime_info(path, pwzImageVersion, NULL, 0, flags, FALSE, &result);
1089 if (SUCCEEDED(hr))
1091 if (pwzImageVersion)
1093 /* Ignoring errors on purpose */
1094 ICLRRuntimeInfo_GetVersionString(result, pwzImageVersion, pcchImageVersion);
1097 hr = ICLRRuntimeInfo_QueryInterface(result, riid, ppRuntime);
1099 ICLRRuntimeInfo_Release(result);
1102 TRACE("<- 0x%08x\n", hr);
1104 return hr;
1107 static const struct ICLRMetaHostPolicyVtbl CLRMetaHostPolicy_vtbl =
1109 metahostpolicy_QueryInterface,
1110 metahostpolicy_AddRef,
1111 metahostpolicy_Release,
1112 metahostpolicy_GetRequestedRuntime
1115 static struct CLRMetaHostPolicy GlobalCLRMetaHostPolicy = {
1116 { &CLRMetaHostPolicy_vtbl }
1119 HRESULT CLRMetaHostPolicy_CreateInstance(REFIID riid, void **ppobj)
1121 return ICLRMetaHostPolicy_QueryInterface(&GlobalCLRMetaHostPolicy.ICLRMetaHostPolicy_iface, riid, ppobj);
1124 HRESULT get_file_from_strongname(WCHAR* stringnameW, WCHAR* assemblies_path, int path_length)
1126 HRESULT hr=S_OK;
1127 IAssemblyCache *asmcache;
1128 ASSEMBLY_INFO info;
1129 static const WCHAR fusiondll[] = {'f','u','s','i','o','n',0};
1130 HMODULE hfusion=NULL;
1131 static HRESULT (WINAPI *pCreateAssemblyCache)(IAssemblyCache**,DWORD);
1133 if (!pCreateAssemblyCache)
1135 hr = LoadLibraryShim(fusiondll, NULL, NULL, &hfusion);
1137 if (SUCCEEDED(hr))
1139 pCreateAssemblyCache = (void*)GetProcAddress(hfusion, "CreateAssemblyCache");
1140 if (!pCreateAssemblyCache)
1141 hr = E_FAIL;
1145 if (SUCCEEDED(hr))
1146 hr = pCreateAssemblyCache(&asmcache, 0);
1148 if (SUCCEEDED(hr))
1150 info.cbAssemblyInfo = sizeof(info);
1151 info.pszCurrentAssemblyPathBuf = assemblies_path;
1152 info.cchBuf = path_length;
1153 assemblies_path[0] = 0;
1155 hr = IAssemblyCache_QueryAssemblyInfo(asmcache, 0, stringnameW, &info);
1157 IAssemblyCache_Release(asmcache);
1160 return hr;
1163 static MonoAssembly* CDECL mono_assembly_preload_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data)
1165 HRESULT hr;
1166 MonoAssembly *result=NULL;
1167 char *stringname=NULL;
1168 LPWSTR stringnameW;
1169 int stringnameW_size;
1170 WCHAR path[MAX_PATH];
1171 char *pathA;
1172 MonoImageOpenStatus stat;
1174 stringname = mono_stringify_assembly_name(aname);
1176 TRACE("%s\n", debugstr_a(stringname));
1178 if (!stringname) return NULL;
1180 /* FIXME: We should search the given paths before the GAC. */
1182 stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, stringname, -1, NULL, 0);
1184 stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1185 if (stringnameW)
1187 MultiByteToWideChar(CP_UTF8, 0, stringname, -1, stringnameW, stringnameW_size);
1189 hr = get_file_from_strongname(stringnameW, path, MAX_PATH);
1191 HeapFree(GetProcessHeap(), 0, stringnameW);
1193 else
1194 hr = E_OUTOFMEMORY;
1196 if (SUCCEEDED(hr))
1198 TRACE("found: %s\n", debugstr_w(path));
1200 pathA = WtoA(path);
1202 if (pathA)
1204 result = mono_assembly_open(pathA, &stat);
1206 if (!result)
1207 ERR("Failed to load %s, status=%u\n", debugstr_w(path), stat);
1209 HeapFree(GetProcessHeap(), 0, pathA);
1213 mono_free(stringname);
1215 return result;
1218 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1219 DWORD startup_flags, DWORD runtimeinfo_flags, BOOL legacy, ICLRRuntimeInfo **result)
1221 static const WCHAR dotconfig[] = {'.','c','o','n','f','i','g',0};
1222 static const DWORD supported_startup_flags = 0;
1223 static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1224 int i;
1225 WCHAR local_version[MAX_PATH];
1226 ULONG local_version_size = MAX_PATH;
1227 WCHAR local_config_file[MAX_PATH];
1228 HRESULT hr;
1229 parsed_config_file parsed_config;
1231 if (startup_flags & ~supported_startup_flags)
1232 FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
1234 if (runtimeinfo_flags & ~supported_runtime_flags)
1235 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
1237 if (exefile && !exefile[0])
1238 exefile = NULL;
1240 if (exefile && !config_file)
1242 strcpyW(local_config_file, exefile);
1243 strcatW(local_config_file, dotconfig);
1245 config_file = local_config_file;
1248 if (config_file)
1250 BOOL found = FALSE;
1251 hr = parse_config_file(config_file, &parsed_config);
1253 if (SUCCEEDED(hr))
1255 supported_runtime *entry;
1256 LIST_FOR_EACH_ENTRY(entry, &parsed_config.supported_runtimes, supported_runtime, entry)
1258 hr = get_runtime(entry->version, TRUE, &IID_ICLRRuntimeInfo, (void**)result);
1259 if (SUCCEEDED(hr))
1261 found = TRUE;
1262 break;
1266 else
1268 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file), hr);
1271 free_parsed_config_file(&parsed_config);
1273 if (found)
1274 return S_OK;
1277 if (exefile && !version)
1279 hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1281 version = local_version;
1283 if (FAILED(hr)) return hr;
1286 if (version)
1288 hr = CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1289 if(SUCCEEDED(hr))
1290 return hr;
1293 if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1295 DWORD major, minor, build;
1297 if (version && !parse_runtime_version(version, &major, &minor, &build))
1299 ERR("Cannot parse %s\n", debugstr_w(version));
1300 return CLR_E_SHIM_RUNTIME;
1303 if (legacy)
1304 i = 3;
1305 else
1306 i = NUM_RUNTIMES;
1308 while (i--)
1310 /* Must be greater or equal to the version passed in. */
1311 if (!version || ((runtimes[i].major >= major && runtimes[i].minor >= minor && runtimes[i].build >= build) ||
1312 (runtimes[i].major >= major && runtimes[i].minor > minor) ||
1313 (runtimes[i].major > major)))
1315 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface,
1316 &IID_ICLRRuntimeInfo, (void **)result);
1320 return CLR_E_SHIM_RUNTIME;
1323 return CLR_E_SHIM_RUNTIME;