wined3d: Get resource info from the texture in wined3d_surface_cleanup().
[wine.git] / dlls / mscoree / metahost.c
blobca6914e99a6e33e6f12cef62e897565caf1bfe41
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_dirs)(const char *assembly_dir, const char *config_dir);
104 static void (CDECL *mono_set_verbose_level)(DWORD level);
105 MonoString* (CDECL *mono_string_new)(MonoDomain *domain, const char *str);
106 static char* (CDECL *mono_stringify_assembly_name)(MonoAssemblyName *aname);
107 MonoThread* (CDECL *mono_thread_attach)(MonoDomain *domain);
108 void (CDECL *mono_thread_manage)(void);
109 void (CDECL *mono_trace_set_assembly)(MonoAssembly *assembly);
110 void (CDECL *mono_trace_set_print_handler)(MonoPrintCallback callback);
111 void (CDECL *mono_trace_set_printerr_handler)(MonoPrintCallback callback);
113 static BOOL get_mono_path(LPWSTR path);
115 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path);
117 static MonoAssembly* CDECL mono_assembly_preload_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data);
119 static void CDECL mono_shutdown_callback_fn(MonoProfiler *prof);
121 static void CDECL mono_print_handler_fn(const char *string, INT is_stdout);
123 static MonoImage* CDECL image_open_module_handle_dummy(HMODULE module_handle,
124 char* fname, UINT has_entry_point, MonoImageOpenStatus* status)
126 return mono_image_open(fname, status);
129 static void CDECL set_print_handler_dummy(MonoPrintCallback callback)
133 static HRESULT load_mono(LPCWSTR mono_path)
135 static const WCHAR lib[] = {'\\','l','i','b',0};
136 static const WCHAR etc[] = {'\\','e','t','c',0};
137 WCHAR mono_dll_path[MAX_PATH+16];
138 WCHAR mono_lib_path[MAX_PATH+4], mono_etc_path[MAX_PATH+4];
139 char mono_lib_path_a[MAX_PATH], mono_etc_path_a[MAX_PATH];
140 int trace_size;
141 char trace_setting[256];
142 int verbose_size;
143 char verbose_setting[256];
145 if (is_mono_shutdown)
147 ERR("Cannot load Mono after it has been shut down.\n");
148 return E_FAIL;
151 if (!mono_handle)
153 strcpyW(mono_lib_path, mono_path);
154 strcatW(mono_lib_path, lib);
155 WideCharToMultiByte(CP_UTF8, 0, mono_lib_path, -1, mono_lib_path_a, MAX_PATH, NULL, NULL);
157 strcpyW(mono_etc_path, mono_path);
158 strcatW(mono_etc_path, etc);
159 WideCharToMultiByte(CP_UTF8, 0, mono_etc_path, -1, mono_etc_path_a, MAX_PATH, NULL, NULL);
161 if (!find_mono_dll(mono_path, mono_dll_path)) goto fail;
163 mono_handle = LoadLibraryW(mono_dll_path);
165 if (!mono_handle) goto fail;
167 #define LOAD_MONO_FUNCTION(x) do { \
168 x = (void*)GetProcAddress(mono_handle, #x); \
169 if (!x) { \
170 goto fail; \
172 } while (0);
174 LOAD_MONO_FUNCTION(mono_assembly_get_image);
175 LOAD_MONO_FUNCTION(mono_assembly_load_from);
176 LOAD_MONO_FUNCTION(mono_assembly_open);
177 LOAD_MONO_FUNCTION(mono_config_parse);
178 LOAD_MONO_FUNCTION(mono_class_from_mono_type);
179 LOAD_MONO_FUNCTION(mono_class_from_name);
180 LOAD_MONO_FUNCTION(mono_class_get_method_from_name);
181 LOAD_MONO_FUNCTION(mono_domain_assembly_open);
182 LOAD_MONO_FUNCTION(mono_domain_get);
183 LOAD_MONO_FUNCTION(mono_domain_get_by_id);
184 LOAD_MONO_FUNCTION(mono_domain_set);
185 LOAD_MONO_FUNCTION(mono_domain_set_config);
186 LOAD_MONO_FUNCTION(mono_free);
187 LOAD_MONO_FUNCTION(mono_image_open);
188 LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook);
189 LOAD_MONO_FUNCTION(mono_jit_exec);
190 LOAD_MONO_FUNCTION(mono_jit_init_version);
191 LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
192 LOAD_MONO_FUNCTION(mono_marshal_get_vtfixup_ftnptr);
193 LOAD_MONO_FUNCTION(mono_object_get_domain);
194 LOAD_MONO_FUNCTION(mono_object_get_virtual_method);
195 LOAD_MONO_FUNCTION(mono_object_new);
196 LOAD_MONO_FUNCTION(mono_object_unbox);
197 LOAD_MONO_FUNCTION(mono_profiler_install);
198 LOAD_MONO_FUNCTION(mono_reflection_type_from_name);
199 LOAD_MONO_FUNCTION(mono_runtime_invoke);
200 LOAD_MONO_FUNCTION(mono_runtime_object_init);
201 LOAD_MONO_FUNCTION(mono_runtime_quit);
202 LOAD_MONO_FUNCTION(mono_set_dirs);
203 LOAD_MONO_FUNCTION(mono_set_verbose_level);
204 LOAD_MONO_FUNCTION(mono_stringify_assembly_name);
205 LOAD_MONO_FUNCTION(mono_string_new);
206 LOAD_MONO_FUNCTION(mono_thread_attach);
207 LOAD_MONO_FUNCTION(mono_thread_manage);
208 LOAD_MONO_FUNCTION(mono_trace_set_assembly);
210 #undef LOAD_MONO_FUNCTION
212 #define LOAD_OPT_MONO_FUNCTION(x, default) do { \
213 x = (void*)GetProcAddress(mono_handle, #x); \
214 if (!x) { \
215 x = default; \
217 } while (0);
219 LOAD_OPT_MONO_FUNCTION(mono_image_open_from_module_handle, image_open_module_handle_dummy);
220 LOAD_OPT_MONO_FUNCTION(mono_trace_set_print_handler, set_print_handler_dummy);
221 LOAD_OPT_MONO_FUNCTION(mono_trace_set_printerr_handler, set_print_handler_dummy);
223 #undef LOAD_OPT_MONO_FUNCTION
225 mono_profiler_install(NULL, mono_shutdown_callback_fn);
227 mono_trace_set_print_handler(mono_print_handler_fn);
228 mono_trace_set_printerr_handler(mono_print_handler_fn);
230 mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
232 mono_config_parse(NULL);
234 mono_install_assembly_preload_hook(mono_assembly_preload_hook_fn, NULL);
236 trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
238 if (trace_size)
240 mono_jit_set_trace_options(trace_setting);
243 verbose_size = GetEnvironmentVariableA("WINE_MONO_VERBOSE", verbose_setting, sizeof(verbose_setting));
245 if (verbose_size)
247 mono_set_verbose_level(verbose_setting[0] - '0');
251 return S_OK;
253 fail:
254 ERR("Could not load Mono into this process\n");
255 FreeLibrary(mono_handle);
256 mono_handle = NULL;
257 return E_FAIL;
260 static void CDECL mono_shutdown_callback_fn(MonoProfiler *prof)
262 is_mono_shutdown = TRUE;
265 static void CDECL mono_print_handler_fn(const char *string, INT is_stdout)
267 wine_dbg_printf("%s", string);
270 static HRESULT CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo *This, RuntimeHost **result)
272 HRESULT hr = S_OK;
273 WCHAR mono_path[MAX_PATH];
275 if (This->loaded_runtime)
277 *result = This->loaded_runtime;
278 return hr;
281 if (!get_mono_path(mono_path))
283 ERR("Wine Mono is not installed\n");
284 return CLR_E_SHIM_RUNTIME;
287 EnterCriticalSection(&runtime_list_cs);
289 hr = load_mono(mono_path);
291 if (SUCCEEDED(hr))
292 hr = RuntimeHost_Construct(This, &This->loaded_runtime);
294 LeaveCriticalSection(&runtime_list_cs);
296 if (SUCCEEDED(hr))
297 *result = This->loaded_runtime;
299 return hr;
302 void expect_no_runtimes(void)
304 if (mono_handle && is_mono_started && !is_mono_shutdown)
306 ERR("Process exited with a Mono runtime loaded.\n");
307 return;
311 static inline CLRRuntimeInfo *impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo *iface)
313 return CONTAINING_RECORD(iface, CLRRuntimeInfo, ICLRRuntimeInfo_iface);
316 static HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
317 REFIID riid,
318 void **ppvObject)
320 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
322 if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
323 IsEqualGUID( riid, &IID_IUnknown ) )
325 *ppvObject = iface;
327 else
329 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
330 return E_NOINTERFACE;
333 ICLRRuntimeInfo_AddRef( iface );
335 return S_OK;
338 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
340 return 2;
343 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
345 return 1;
348 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
349 LPWSTR pwzBuffer, DWORD *pcchBuffer)
351 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
352 DWORD buffer_size = *pcchBuffer;
353 HRESULT hr = S_OK;
354 char version[11];
355 DWORD size;
357 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
359 size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
361 assert(size <= sizeof(version));
363 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
365 if (pwzBuffer)
367 if (buffer_size >= *pcchBuffer)
368 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
369 else
370 hr = E_NOT_SUFFICIENT_BUFFER;
373 return hr;
376 static BOOL get_install_root(LPWSTR install_dir)
378 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};
379 const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
381 DWORD len;
382 HKEY key;
384 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
385 return FALSE;
387 len = MAX_PATH;
388 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
390 RegCloseKey(key);
391 return FALSE;
393 RegCloseKey(key);
395 return TRUE;
398 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
399 LPWSTR pwzBuffer, DWORD *pcchBuffer)
401 static const WCHAR slash[] = {'\\',0};
402 DWORD buffer_size = *pcchBuffer;
403 WCHAR system_dir[MAX_PATH];
404 WCHAR version[MAX_PATH];
405 DWORD version_size, size;
406 HRESULT hr = S_OK;
408 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
410 if (!get_install_root(system_dir))
412 ERR("error reading registry key for installroot\n");
413 return E_FAIL;
415 else
417 version_size = MAX_PATH;
418 ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
419 lstrcatW(system_dir, version);
420 lstrcatW(system_dir, slash);
421 size = lstrlenW(system_dir) + 1;
424 *pcchBuffer = size;
426 if (pwzBuffer)
428 if (buffer_size >= size)
429 strcpyW(pwzBuffer, system_dir);
430 else
431 hr = E_NOT_SUFFICIENT_BUFFER;
434 return hr;
437 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
438 HANDLE hndProcess, BOOL *pbLoaded)
440 FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
442 return E_NOTIMPL;
445 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
446 UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
448 FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
450 return E_NOTIMPL;
453 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
454 LPCWSTR pwzDllName, HMODULE *phndModule)
456 WCHAR version[MAX_PATH];
457 HRESULT hr;
458 DWORD cchBuffer;
460 TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
462 cchBuffer = MAX_PATH;
463 hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
464 if (FAILED(hr)) return hr;
466 return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
469 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
470 LPCSTR pszProcName, LPVOID *ppProc)
472 FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
474 return E_NOTIMPL;
477 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
478 REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
480 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
481 RuntimeHost *host;
482 HRESULT hr;
484 TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
486 hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
488 if (SUCCEEDED(hr))
489 hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
491 return hr;
494 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
495 BOOL *pbLoadable)
497 FIXME("%p %p\n", iface, pbLoadable);
499 return E_NOTIMPL;
502 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
503 DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
505 FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
507 return E_NOTIMPL;
510 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
511 DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
513 FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
515 return E_NOTIMPL;
518 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
520 FIXME("%p\n", iface);
522 return E_NOTIMPL;
525 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
526 BOOL *pbStarted, DWORD *pdwStartupFlags)
528 FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
530 return E_NOTIMPL;
533 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
534 CLRRuntimeInfo_QueryInterface,
535 CLRRuntimeInfo_AddRef,
536 CLRRuntimeInfo_Release,
537 CLRRuntimeInfo_GetVersionString,
538 CLRRuntimeInfo_GetRuntimeDirectory,
539 CLRRuntimeInfo_IsLoaded,
540 CLRRuntimeInfo_LoadErrorString,
541 CLRRuntimeInfo_LoadLibrary,
542 CLRRuntimeInfo_GetProcAddress,
543 CLRRuntimeInfo_GetInterface,
544 CLRRuntimeInfo_IsLoadable,
545 CLRRuntimeInfo_SetDefaultStartupFlags,
546 CLRRuntimeInfo_GetDefaultStartupFlags,
547 CLRRuntimeInfo_BindAsLegacyV2Runtime,
548 CLRRuntimeInfo_IsStarted
551 HRESULT ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo *iface, RuntimeHost **result)
553 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
555 assert(This->ICLRRuntimeInfo_iface.lpVtbl == &CLRRuntimeInfoVtbl);
557 return CLRRuntimeInfo_GetRuntimeHost(This, result);
560 #ifdef __i386__
561 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};
562 #elif defined(__x86_64__)
563 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};
564 #else
565 static const WCHAR libmono2_arch_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
566 #endif
568 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path)
570 static const WCHAR mono2_dll[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
571 static const WCHAR libmono2_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
572 DWORD attributes=INVALID_FILE_ATTRIBUTES;
574 strcpyW(dll_path, path);
575 strcatW(dll_path, libmono2_arch_dll);
576 attributes = GetFileAttributesW(dll_path);
578 if (attributes == INVALID_FILE_ATTRIBUTES)
580 strcpyW(dll_path, path);
581 strcatW(dll_path, mono2_dll);
582 attributes = GetFileAttributesW(dll_path);
585 if (attributes == INVALID_FILE_ATTRIBUTES)
587 strcpyW(dll_path, path);
588 strcatW(dll_path, libmono2_dll);
589 attributes = GetFileAttributesW(dll_path);
592 return (attributes != INVALID_FILE_ATTRIBUTES);
595 static BOOL get_mono_path(LPWSTR path)
597 static const WCHAR subdir_mono[] = {'\\','m','o','n','o','\\','m','o','n','o','-','2','.','0', 0};
598 WCHAR base_path[MAX_PATH], mono_dll_path[MAX_PATH];
600 /* c:\windows\mono\mono-2.0 */
601 GetWindowsDirectoryW(base_path, MAX_PATH);
602 strcatW(base_path, subdir_mono);
604 if (find_mono_dll(base_path, mono_dll_path))
606 strcpyW(path, base_path);
607 return TRUE;
610 return FALSE;
613 struct InstalledRuntimeEnum
615 IEnumUnknown IEnumUnknown_iface;
616 LONG ref;
617 ULONG pos;
620 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
622 static inline struct InstalledRuntimeEnum *impl_from_IEnumUnknown(IEnumUnknown *iface)
624 return CONTAINING_RECORD(iface, struct InstalledRuntimeEnum, IEnumUnknown_iface);
627 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface, REFIID riid,
628 void **ppvObject)
630 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
632 if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
633 IsEqualGUID( riid, &IID_IUnknown ) )
635 *ppvObject = iface;
637 else
639 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
640 return E_NOINTERFACE;
643 IEnumUnknown_AddRef( iface );
645 return S_OK;
648 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
650 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
651 ULONG ref = InterlockedIncrement(&This->ref);
653 TRACE("(%p) refcount=%u\n", iface, ref);
655 return ref;
658 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
660 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
661 ULONG ref = InterlockedDecrement(&This->ref);
663 TRACE("(%p) refcount=%u\n", iface, ref);
665 if (ref == 0)
667 HeapFree(GetProcessHeap(), 0, This);
670 return ref;
673 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
674 IUnknown **rgelt, ULONG *pceltFetched)
676 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
677 ULONG num_fetched = 0;
678 HRESULT hr=S_OK;
679 IUnknown *item;
681 TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
683 while (num_fetched < celt)
685 if (This->pos >= NUM_RUNTIMES)
687 hr = S_FALSE;
688 break;
690 item = (IUnknown*)&runtimes[This->pos].ICLRRuntimeInfo_iface;
691 IUnknown_AddRef(item);
692 rgelt[num_fetched] = item;
693 num_fetched++;
694 This->pos++;
697 if (pceltFetched)
698 *pceltFetched = num_fetched;
700 return hr;
703 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
705 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
706 ULONG num_fetched = 0;
707 HRESULT hr=S_OK;
709 TRACE("(%p,%u)\n", iface, celt);
711 while (num_fetched < celt)
713 if (This->pos >= NUM_RUNTIMES)
715 hr = S_FALSE;
716 break;
718 num_fetched++;
719 This->pos++;
722 return hr;
725 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
727 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
729 TRACE("(%p)\n", iface);
731 This->pos = 0;
733 return S_OK;
736 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
738 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
739 struct InstalledRuntimeEnum *new_enum;
741 TRACE("(%p)\n", iface);
743 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
744 if (!new_enum)
745 return E_OUTOFMEMORY;
747 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
748 new_enum->ref = 1;
749 new_enum->pos = This->pos;
751 *ppenum = &new_enum->IEnumUnknown_iface;
753 return S_OK;
756 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
757 InstalledRuntimeEnum_QueryInterface,
758 InstalledRuntimeEnum_AddRef,
759 InstalledRuntimeEnum_Release,
760 InstalledRuntimeEnum_Next,
761 InstalledRuntimeEnum_Skip,
762 InstalledRuntimeEnum_Reset,
763 InstalledRuntimeEnum_Clone
766 struct CLRMetaHost
768 ICLRMetaHost ICLRMetaHost_iface;
770 RuntimeLoadedCallbackFnPtr callback;
773 static struct CLRMetaHost GlobalCLRMetaHost;
775 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
776 REFIID riid,
777 void **ppvObject)
779 TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
781 if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
782 IsEqualGUID( riid, &IID_IUnknown ) )
784 *ppvObject = iface;
786 else
788 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
789 return E_NOINTERFACE;
792 ICLRMetaHost_AddRef( iface );
794 return S_OK;
797 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
799 return 2;
802 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
804 return 1;
807 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
809 *major = 0;
810 *minor = 0;
811 *build = 0;
813 if (version[0] == 'v' || version[0] == 'V')
815 version++;
816 if (!isdigit(*version))
817 return FALSE;
819 while (isdigit(*version))
820 *major = *major * 10 + (*version++ - '0');
822 if (*version == 0)
823 return TRUE;
825 if (*version++ != '.' || !isdigit(*version))
826 return FALSE;
828 while (isdigit(*version))
829 *minor = *minor * 10 + (*version++ - '0');
831 if (*version == 0)
832 return TRUE;
834 if (*version++ != '.' || !isdigit(*version))
835 return FALSE;
837 while (isdigit(*version))
838 *build = *build * 10 + (*version++ - '0');
840 return *version == 0;
842 else
843 return FALSE;
846 static HRESULT get_runtime(LPCWSTR pwzVersion, BOOL allow_short,
847 REFIID iid, LPVOID *ppRuntime)
849 int i;
850 DWORD major, minor, build;
852 if (!pwzVersion)
853 return E_POINTER;
855 if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
857 ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
858 return CLR_E_SHIM_RUNTIME;
861 for (i=0; i<NUM_RUNTIMES; i++)
863 if (runtimes[i].major == major && runtimes[i].minor == minor &&
864 (runtimes[i].build == build || (allow_short && major >= 4 && build == 0)))
866 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface, iid,
867 ppRuntime);
871 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
872 return CLR_E_SHIM_RUNTIME;
875 HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
876 LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
878 TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
880 return get_runtime(pwzVersion, FALSE, iid, ppRuntime);
883 HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
884 LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
886 ASSEMBLY *assembly;
887 HRESULT hr;
888 LPSTR version;
889 ULONG buffer_size=*pcchBuffer;
891 TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
893 hr = assembly_create(&assembly, pwzFilePath);
895 if (SUCCEEDED(hr))
897 hr = assembly_get_runtime_version(assembly, &version);
899 if (SUCCEEDED(hr))
901 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
903 if (pwzBuffer)
905 if (buffer_size >= *pcchBuffer)
906 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
907 else
908 hr = E_NOT_SUFFICIENT_BUFFER;
912 assembly_release(assembly);
915 return hr;
918 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
919 IEnumUnknown **ppEnumerator)
921 struct InstalledRuntimeEnum *new_enum;
923 TRACE("%p\n", ppEnumerator);
925 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
926 if (!new_enum)
927 return E_OUTOFMEMORY;
929 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
930 new_enum->ref = 1;
931 new_enum->pos = 0;
933 *ppEnumerator = &new_enum->IEnumUnknown_iface;
935 return S_OK;
938 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
939 HANDLE hndProcess, IEnumUnknown **ppEnumerator)
941 FIXME("%p %p\n", hndProcess, ppEnumerator);
943 return E_NOTIMPL;
946 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
947 RuntimeLoadedCallbackFnPtr pCallbackFunction)
949 TRACE("%p\n", pCallbackFunction);
951 if(!pCallbackFunction)
952 return E_POINTER;
954 WARN("Callback currently will not be called.\n");
956 GlobalCLRMetaHost.callback = pCallbackFunction;
958 return S_OK;
961 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
962 REFIID riid, LPVOID *ppUnk)
964 FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
966 return E_NOTIMPL;
969 HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
971 TRACE("%i\n", iExitCode);
973 EnterCriticalSection(&runtime_list_cs);
975 if (is_mono_started && !is_mono_shutdown)
977 /* search for a runtime and call System.Environment.Exit() */
978 int i;
980 for (i=0; i<NUM_RUNTIMES; i++)
981 if (runtimes[i].loaded_runtime)
982 RuntimeHost_ExitProcess(runtimes[i].loaded_runtime, iExitCode);
985 ExitProcess(iExitCode);
988 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
990 CLRMetaHost_QueryInterface,
991 CLRMetaHost_AddRef,
992 CLRMetaHost_Release,
993 CLRMetaHost_GetRuntime,
994 CLRMetaHost_GetVersionFromFile,
995 CLRMetaHost_EnumerateInstalledRuntimes,
996 CLRMetaHost_EnumerateLoadedRuntimes,
997 CLRMetaHost_RequestRuntimeLoadedNotification,
998 CLRMetaHost_QueryLegacyV2RuntimeBinding,
999 CLRMetaHost_ExitProcess
1002 static struct CLRMetaHost GlobalCLRMetaHost = {
1003 { &CLRMetaHost_vtbl }
1006 HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1008 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost.ICLRMetaHost_iface, riid, ppobj);
1011 struct CLRMetaHostPolicy
1013 ICLRMetaHostPolicy ICLRMetaHostPolicy_iface;
1016 static struct CLRMetaHostPolicy GlobalCLRMetaHostPolicy;
1018 static HRESULT WINAPI metahostpolicy_QueryInterface(ICLRMetaHostPolicy *iface, REFIID riid, void **obj)
1020 TRACE("%s %p\n", debugstr_guid(riid), obj);
1022 if ( IsEqualGUID( riid, &IID_ICLRMetaHostPolicy ) ||
1023 IsEqualGUID( riid, &IID_IUnknown ) )
1025 ICLRMetaHostPolicy_AddRef( iface );
1026 *obj = iface;
1027 return S_OK;
1030 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
1032 *obj = NULL;
1033 return E_NOINTERFACE;
1036 static ULONG WINAPI metahostpolicy_AddRef(ICLRMetaHostPolicy *iface)
1038 return 2;
1041 static ULONG WINAPI metahostpolicy_Release(ICLRMetaHostPolicy *iface)
1043 return 1;
1046 static HRESULT WINAPI metahostpolicy_GetRequestedRuntime(ICLRMetaHostPolicy *iface, METAHOST_POLICY_FLAGS dwPolicyFlags,
1047 LPCWSTR pwzBinary, IStream *pCfgStream, LPWSTR pwzVersion, DWORD *pcchVersion,
1048 LPWSTR pwzImageVersion, DWORD *pcchImageVersion, DWORD *pdwConfigFlags, REFIID riid,
1049 LPVOID *ppRuntime)
1051 ICLRRuntimeInfo *result;
1052 HRESULT hr;
1053 WCHAR filename[MAX_PATH];
1054 const WCHAR *path = NULL;
1055 int flags = 0;
1057 TRACE("%d %p %p %p %p %p %p %p %s %p\n", dwPolicyFlags, pwzBinary, pCfgStream,
1058 pwzVersion, pcchVersion, pwzImageVersion, pcchImageVersion, pdwConfigFlags,
1059 debugstr_guid(riid), ppRuntime);
1061 if (pCfgStream)
1062 FIXME("ignoring config file stream\n");
1064 if (pdwConfigFlags)
1065 FIXME("ignoring config flags\n");
1067 if(dwPolicyFlags & METAHOST_POLICY_USE_PROCESS_IMAGE_PATH)
1069 GetModuleFileNameW(0, filename, MAX_PATH);
1070 path = filename;
1072 else if(pwzBinary)
1074 path = pwzBinary;
1077 if(dwPolicyFlags & METAHOST_POLICY_APPLY_UPGRADE_POLICY)
1078 flags |= RUNTIME_INFO_UPGRADE_VERSION;
1080 hr = get_runtime_info(path, pwzImageVersion, NULL, 0, flags, FALSE, &result);
1081 if (SUCCEEDED(hr))
1083 if (pwzImageVersion)
1085 /* Ignoring errors on purpose */
1086 ICLRRuntimeInfo_GetVersionString(result, pwzImageVersion, pcchImageVersion);
1089 hr = ICLRRuntimeInfo_QueryInterface(result, riid, ppRuntime);
1091 ICLRRuntimeInfo_Release(result);
1094 TRACE("<- 0x%08x\n", hr);
1096 return hr;
1099 static const struct ICLRMetaHostPolicyVtbl CLRMetaHostPolicy_vtbl =
1101 metahostpolicy_QueryInterface,
1102 metahostpolicy_AddRef,
1103 metahostpolicy_Release,
1104 metahostpolicy_GetRequestedRuntime
1107 static struct CLRMetaHostPolicy GlobalCLRMetaHostPolicy = {
1108 { &CLRMetaHostPolicy_vtbl }
1111 HRESULT CLRMetaHostPolicy_CreateInstance(REFIID riid, void **ppobj)
1113 return ICLRMetaHostPolicy_QueryInterface(&GlobalCLRMetaHostPolicy.ICLRMetaHostPolicy_iface, riid, ppobj);
1116 HRESULT get_file_from_strongname(WCHAR* stringnameW, WCHAR* assemblies_path, int path_length)
1118 HRESULT hr=S_OK;
1119 IAssemblyCache *asmcache;
1120 ASSEMBLY_INFO info;
1121 static const WCHAR fusiondll[] = {'f','u','s','i','o','n',0};
1122 HMODULE hfusion=NULL;
1123 static HRESULT (WINAPI *pCreateAssemblyCache)(IAssemblyCache**,DWORD);
1125 if (!pCreateAssemblyCache)
1127 hr = LoadLibraryShim(fusiondll, NULL, NULL, &hfusion);
1129 if (SUCCEEDED(hr))
1131 pCreateAssemblyCache = (void*)GetProcAddress(hfusion, "CreateAssemblyCache");
1132 if (!pCreateAssemblyCache)
1133 hr = E_FAIL;
1137 if (SUCCEEDED(hr))
1138 hr = pCreateAssemblyCache(&asmcache, 0);
1140 if (SUCCEEDED(hr))
1142 info.cbAssemblyInfo = sizeof(info);
1143 info.pszCurrentAssemblyPathBuf = assemblies_path;
1144 info.cchBuf = path_length;
1145 assemblies_path[0] = 0;
1147 hr = IAssemblyCache_QueryAssemblyInfo(asmcache, 0, stringnameW, &info);
1149 IAssemblyCache_Release(asmcache);
1152 return hr;
1155 static MonoAssembly* CDECL mono_assembly_preload_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data)
1157 HRESULT hr;
1158 MonoAssembly *result=NULL;
1159 char *stringname=NULL;
1160 LPWSTR stringnameW;
1161 int stringnameW_size;
1162 WCHAR path[MAX_PATH];
1163 char *pathA;
1164 MonoImageOpenStatus stat;
1166 stringname = mono_stringify_assembly_name(aname);
1168 TRACE("%s\n", debugstr_a(stringname));
1170 if (!stringname) return NULL;
1172 /* FIXME: We should search the given paths before the GAC. */
1174 stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, stringname, -1, NULL, 0);
1176 stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1177 if (stringnameW)
1179 MultiByteToWideChar(CP_UTF8, 0, stringname, -1, stringnameW, stringnameW_size);
1181 hr = get_file_from_strongname(stringnameW, path, MAX_PATH);
1183 HeapFree(GetProcessHeap(), 0, stringnameW);
1185 else
1186 hr = E_OUTOFMEMORY;
1188 if (SUCCEEDED(hr))
1190 TRACE("found: %s\n", debugstr_w(path));
1192 pathA = WtoA(path);
1194 if (pathA)
1196 result = mono_assembly_open(pathA, &stat);
1198 if (!result)
1199 ERR("Failed to load %s, status=%u\n", debugstr_w(path), stat);
1201 HeapFree(GetProcessHeap(), 0, pathA);
1205 mono_free(stringname);
1207 return result;
1210 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1211 DWORD startup_flags, DWORD runtimeinfo_flags, BOOL legacy, ICLRRuntimeInfo **result)
1213 static const WCHAR dotconfig[] = {'.','c','o','n','f','i','g',0};
1214 static const DWORD supported_startup_flags = 0;
1215 static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1216 int i;
1217 WCHAR local_version[MAX_PATH];
1218 ULONG local_version_size = MAX_PATH;
1219 WCHAR local_config_file[MAX_PATH];
1220 HRESULT hr;
1221 parsed_config_file parsed_config;
1223 if (startup_flags & ~supported_startup_flags)
1224 FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
1226 if (runtimeinfo_flags & ~supported_runtime_flags)
1227 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
1229 if (exefile && !exefile[0])
1230 exefile = NULL;
1232 if (exefile && !config_file)
1234 strcpyW(local_config_file, exefile);
1235 strcatW(local_config_file, dotconfig);
1237 config_file = local_config_file;
1240 if (config_file)
1242 BOOL found = FALSE;
1243 hr = parse_config_file(config_file, &parsed_config);
1245 if (SUCCEEDED(hr))
1247 supported_runtime *entry;
1248 LIST_FOR_EACH_ENTRY(entry, &parsed_config.supported_runtimes, supported_runtime, entry)
1250 hr = get_runtime(entry->version, TRUE, &IID_ICLRRuntimeInfo, (void**)result);
1251 if (SUCCEEDED(hr))
1253 found = TRUE;
1254 break;
1258 else
1260 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file), hr);
1263 free_parsed_config_file(&parsed_config);
1265 if (found)
1266 return S_OK;
1269 if (exefile && !version)
1271 hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1273 version = local_version;
1275 if (FAILED(hr)) return hr;
1278 if (version)
1280 hr = CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1281 if(SUCCEEDED(hr))
1282 return hr;
1285 if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1287 DWORD major, minor, build;
1289 if (version && !parse_runtime_version(version, &major, &minor, &build))
1291 ERR("Cannot parse %s\n", debugstr_w(version));
1292 return CLR_E_SHIM_RUNTIME;
1295 if (legacy)
1296 i = 3;
1297 else
1298 i = NUM_RUNTIMES;
1300 while (i--)
1302 /* Must be greater or equal to the version passed in. */
1303 if (!version || ((runtimes[i].major >= major && runtimes[i].minor >= minor && runtimes[i].build >= build) ||
1304 (runtimes[i].major >= major && runtimes[i].minor > minor) ||
1305 (runtimes[i].major > major)))
1307 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface,
1308 &IID_ICLRRuntimeInfo, (void **)result);
1312 return CLR_E_SHIM_RUNTIME;
1315 return CLR_E_SHIM_RUNTIME;