Branching for 0.3.15 release after two days of no response from a certain sphere...
[reactos.git] / dll / win32 / mscoree / metahost.c
1 /*
2 * ICLRMetaHost - discovery and management of available .NET runtimes
3 *
4 * Copyright 2010 Vincent Povirk for CodeWeavers
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #define WIN32_NO_STATUS
22 #define _INC_WINDOWS
23 #define COM_NO_WINDOWS_H
24
25 #include <stdio.h>
26 //#include <stdarg.h>
27 #include <assert.h>
28
29 #define COBJMACROS
30
31 #include <wine/unicode.h>
32 #include <wine/library.h>
33 //#include "windef.h"
34 //#include "winbase.h"
35 #include <winreg.h>
36 #include <ole2.h>
37
38 //#include "corerror.h"
39 #include <cor.h>
40 //#include "mscoree.h"
41 //#include "corhdr.h"
42 #include <cordebug.h>
43 #include <metahost.h>
44 #include <fusion.h>
45 #include <wine/list.h>
46 #include "mscoree_private.h"
47
48 #include <wine/debug.h>
49
50 WINE_DEFAULT_DEBUG_CHANNEL( mscoree );
51
52 static const WCHAR net_11_subdir[] = {'1','.','0',0};
53 static const WCHAR net_20_subdir[] = {'2','.','0',0};
54 static const WCHAR net_40_subdir[] = {'4','.','0',0};
55
56 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl;
57
58 #define NUM_RUNTIMES 3
59
60 static struct CLRRuntimeInfo runtimes[NUM_RUNTIMES] = {
61 {{&CLRRuntimeInfoVtbl}, net_11_subdir, 1, 1, 4322, 0},
62 {{&CLRRuntimeInfoVtbl}, net_20_subdir, 2, 0, 50727, 0},
63 {{&CLRRuntimeInfoVtbl}, net_40_subdir, 4, 0, 30319, 0}
64 };
65
66 static int runtimes_initialized;
67
68 static CRITICAL_SECTION runtime_list_cs;
69 static CRITICAL_SECTION_DEBUG runtime_list_cs_debug =
70 {
71 0, 0, &runtime_list_cs,
72 { &runtime_list_cs_debug.ProcessLocksList,
73 &runtime_list_cs_debug.ProcessLocksList },
74 0, 0, { (DWORD_PTR)(__FILE__ ": runtime_list_cs") }
75 };
76 static CRITICAL_SECTION runtime_list_cs = { &runtime_list_cs_debug, -1, 0, 0, 0, 0 };
77
78 #define NUM_ABI_VERSIONS 2
79
80 static loaded_mono loaded_monos[NUM_ABI_VERSIONS];
81
82 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version);
83
84 static MonoAssembly* mono_assembly_search_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data);
85
86 static void mono_shutdown_callback_fn(MonoProfiler *prof);
87
88 static void set_environment(LPCWSTR bin_path)
89 {
90 WCHAR path_env[MAX_PATH];
91 int len;
92
93 static const WCHAR pathW[] = {'P','A','T','H',0};
94
95 /* We have to modify PATH as Mono loads other DLLs from this directory. */
96 GetEnvironmentVariableW(pathW, path_env, sizeof(path_env)/sizeof(WCHAR));
97 len = strlenW(path_env);
98 path_env[len++] = ';';
99 strcpyW(path_env+len, bin_path);
100 SetEnvironmentVariableW(pathW, path_env);
101 }
102
103 static void CDECL do_nothing(void)
104 {
105 }
106
107 static void missing_runtime_message(const CLRRuntimeInfo *This)
108 {
109 if (This->major == 1)
110 MESSAGE("wine: Install Mono 2.6 for Windows to run .NET 1.1 applications.\n");
111 else if (This->major == 2)
112 MESSAGE("wine: Install Mono for Windows to run .NET 2.0 applications.\n");
113 else if (This->major == 4)
114 MESSAGE("wine: Install Mono 2.8 or greater for Windows to run .NET 4.0 applications.\n");
115 }
116
117 static HRESULT load_mono(CLRRuntimeInfo *This, loaded_mono **result)
118 {
119 static const WCHAR bin[] = {'\\','b','i','n',0};
120 static const WCHAR lib[] = {'\\','l','i','b',0};
121 static const WCHAR etc[] = {'\\','e','t','c',0};
122 static const WCHAR glibdll[] = {'l','i','b','g','l','i','b','-','2','.','0','-','0','.','d','l','l',0};
123 WCHAR mono_dll_path[MAX_PATH+16], mono_bin_path[MAX_PATH+4];
124 WCHAR mono_lib_path[MAX_PATH+4], mono_etc_path[MAX_PATH+4];
125 char mono_lib_path_a[MAX_PATH], mono_etc_path_a[MAX_PATH];
126 int trace_size;
127 char trace_setting[256];
128
129 if (This->mono_abi_version <= 0 || This->mono_abi_version > NUM_ABI_VERSIONS)
130 {
131 missing_runtime_message(This);
132 return E_FAIL;
133 }
134
135 *result = &loaded_monos[This->mono_abi_version-1];
136
137 if ((*result)->is_shutdown)
138 {
139 ERR("Cannot load Mono after it has been shut down.\n");
140 *result = NULL;
141 return E_FAIL;
142 }
143
144 if (!(*result)->mono_handle)
145 {
146 strcpyW(mono_bin_path, This->mono_path);
147 strcatW(mono_bin_path, bin);
148 set_environment(mono_bin_path);
149
150 strcpyW(mono_lib_path, This->mono_path);
151 strcatW(mono_lib_path, lib);
152 WideCharToMultiByte(CP_UTF8, 0, mono_lib_path, -1, mono_lib_path_a, MAX_PATH, NULL, NULL);
153
154 strcpyW(mono_etc_path, This->mono_path);
155 strcatW(mono_etc_path, etc);
156 WideCharToMultiByte(CP_UTF8, 0, mono_etc_path, -1, mono_etc_path_a, MAX_PATH, NULL, NULL);
157
158 if (!find_mono_dll(This->mono_path, mono_dll_path, This->mono_abi_version)) goto fail;
159
160 (*result)->mono_handle = LoadLibraryW(mono_dll_path);
161
162 if (!(*result)->mono_handle) goto fail;
163
164 #define LOAD_MONO_FUNCTION(x) do { \
165 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
166 if (!(*result)->x) { \
167 goto fail; \
168 } \
169 } while (0);
170
171 LOAD_MONO_FUNCTION(mono_assembly_get_image);
172 LOAD_MONO_FUNCTION(mono_assembly_load_from);
173 LOAD_MONO_FUNCTION(mono_assembly_open);
174 LOAD_MONO_FUNCTION(mono_config_parse);
175 LOAD_MONO_FUNCTION(mono_class_from_mono_type);
176 LOAD_MONO_FUNCTION(mono_class_from_name);
177 LOAD_MONO_FUNCTION(mono_class_get_method_from_name);
178 LOAD_MONO_FUNCTION(mono_domain_assembly_open);
179 LOAD_MONO_FUNCTION(mono_image_open_from_module_handle);
180 LOAD_MONO_FUNCTION(mono_install_assembly_preload_hook);
181 LOAD_MONO_FUNCTION(mono_jit_exec);
182 LOAD_MONO_FUNCTION(mono_jit_init);
183 LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
184 LOAD_MONO_FUNCTION(mono_marshal_get_vtfixup_ftnptr);
185 LOAD_MONO_FUNCTION(mono_object_get_domain);
186 LOAD_MONO_FUNCTION(mono_object_new);
187 LOAD_MONO_FUNCTION(mono_object_unbox);
188 LOAD_MONO_FUNCTION(mono_profiler_install);
189 LOAD_MONO_FUNCTION(mono_reflection_type_from_name);
190 LOAD_MONO_FUNCTION(mono_runtime_invoke);
191 LOAD_MONO_FUNCTION(mono_runtime_object_init);
192 LOAD_MONO_FUNCTION(mono_runtime_quit);
193 LOAD_MONO_FUNCTION(mono_set_dirs);
194 LOAD_MONO_FUNCTION(mono_stringify_assembly_name);
195 LOAD_MONO_FUNCTION(mono_string_new);
196 LOAD_MONO_FUNCTION(mono_thread_attach);
197
198 /* GLib imports obsoleted by the 2.0 ABI */
199 if (This->mono_abi_version == 1)
200 {
201 (*result)->glib_handle = LoadLibraryW(glibdll);
202 if (!(*result)->glib_handle) goto fail;
203
204 (*result)->mono_free = (void*)GetProcAddress((*result)->glib_handle, "g_free");
205 if (!(*result)->mono_free) goto fail;
206 }
207 else
208 {
209 LOAD_MONO_FUNCTION(mono_free);
210 }
211
212 #undef LOAD_MONO_FUNCTION
213
214 #define LOAD_OPT_VOID_MONO_FUNCTION(x) do { \
215 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
216 if (!(*result)->x) { \
217 (*result)->x = do_nothing; \
218 } \
219 } while (0);
220
221 LOAD_OPT_VOID_MONO_FUNCTION(mono_runtime_set_shutting_down);
222 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_pool_cleanup);
223 LOAD_OPT_VOID_MONO_FUNCTION(mono_thread_suspend_all_other_threads);
224 LOAD_OPT_VOID_MONO_FUNCTION(mono_threads_set_shutting_down);
225
226 #undef LOAD_OPT_VOID_MONO_FUNCTION
227
228 (*result)->mono_profiler_install((MonoProfiler*)*result, mono_shutdown_callback_fn);
229
230 (*result)->mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
231
232 (*result)->mono_config_parse(NULL);
233
234 (*result)->mono_install_assembly_preload_hook(mono_assembly_search_hook_fn, *result);
235
236 trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
237
238 if (trace_size)
239 {
240 (*result)->mono_jit_set_trace_options(trace_setting);
241 }
242 }
243
244 return S_OK;
245
246 fail:
247 ERR("Could not load Mono into this process\n");
248 FreeLibrary((*result)->mono_handle);
249 FreeLibrary((*result)->glib_handle);
250 (*result)->mono_handle = NULL;
251 (*result)->glib_handle = NULL;
252 return E_FAIL;
253 }
254
255 static void mono_shutdown_callback_fn(MonoProfiler *prof)
256 {
257 loaded_mono *mono = (loaded_mono*)prof;
258
259 mono->is_shutdown = TRUE;
260 }
261
262 static HRESULT CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo *This, RuntimeHost **result)
263 {
264 HRESULT hr = S_OK;
265 loaded_mono *ploaded_mono;
266
267 if (This->loaded_runtime)
268 {
269 *result = This->loaded_runtime;
270 return hr;
271 }
272
273 EnterCriticalSection(&runtime_list_cs);
274
275 hr = load_mono(This, &ploaded_mono);
276
277 if (SUCCEEDED(hr))
278 hr = RuntimeHost_Construct(This, ploaded_mono, &This->loaded_runtime);
279
280 LeaveCriticalSection(&runtime_list_cs);
281
282 if (SUCCEEDED(hr))
283 *result = This->loaded_runtime;
284
285 return hr;
286 }
287
288 void unload_all_runtimes(void)
289 {
290 int i;
291
292 for (i=0; i<NUM_ABI_VERSIONS; i++)
293 {
294 loaded_mono *mono = &loaded_monos[i];
295 if (mono->mono_handle && mono->is_started && !mono->is_shutdown)
296 {
297 /* Copied from Mono's ves_icall_System_Environment_Exit */
298 mono->mono_threads_set_shutting_down();
299 mono->mono_runtime_set_shutting_down();
300 mono->mono_thread_pool_cleanup();
301 mono->mono_thread_suspend_all_other_threads();
302 mono->mono_runtime_quit();
303 }
304 }
305
306 for (i=0; i<NUM_RUNTIMES; i++)
307 if (runtimes[i].loaded_runtime)
308 RuntimeHost_Destroy(runtimes[i].loaded_runtime);
309 }
310
311 void expect_no_runtimes(void)
312 {
313 int i;
314
315 for (i=0; i<NUM_ABI_VERSIONS; i++)
316 {
317 loaded_mono *mono = &loaded_monos[i];
318 if (mono->mono_handle && mono->is_started && !mono->is_shutdown)
319 {
320 ERR("Process exited with a Mono runtime loaded.\n");
321 return;
322 }
323 }
324 }
325
326 static inline CLRRuntimeInfo *impl_from_ICLRRuntimeInfo(ICLRRuntimeInfo *iface)
327 {
328 return CONTAINING_RECORD(iface, CLRRuntimeInfo, ICLRRuntimeInfo_iface);
329 }
330
331 static HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
332 REFIID riid,
333 void **ppvObject)
334 {
335 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
336
337 if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
338 IsEqualGUID( riid, &IID_IUnknown ) )
339 {
340 *ppvObject = iface;
341 }
342 else
343 {
344 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
345 return E_NOINTERFACE;
346 }
347
348 ICLRRuntimeInfo_AddRef( iface );
349
350 return S_OK;
351 }
352
353 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
354 {
355 return 2;
356 }
357
358 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
359 {
360 return 1;
361 }
362
363 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
364 LPWSTR pwzBuffer, DWORD *pcchBuffer)
365 {
366 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
367 DWORD buffer_size = *pcchBuffer;
368 HRESULT hr = S_OK;
369 char version[11];
370 DWORD size;
371
372 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
373
374 size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
375
376 assert(size <= sizeof(version));
377
378 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
379
380 if (pwzBuffer)
381 {
382 if (buffer_size >= *pcchBuffer)
383 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
384 else
385 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
386 }
387
388 return hr;
389 }
390
391 static BOOL get_install_root(LPWSTR install_dir)
392 {
393 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};
394 const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
395
396 DWORD len;
397 HKEY key;
398
399 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
400 return FALSE;
401
402 len = MAX_PATH;
403 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
404 {
405 RegCloseKey(key);
406 return FALSE;
407 }
408 RegCloseKey(key);
409
410 return TRUE;
411 }
412
413 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
414 LPWSTR pwzBuffer, DWORD *pcchBuffer)
415 {
416 static const WCHAR slash[] = {'\\',0};
417 DWORD buffer_size = *pcchBuffer;
418 WCHAR system_dir[MAX_PATH];
419 WCHAR version[MAX_PATH];
420 DWORD version_size, size;
421 HRESULT hr = S_OK;
422
423 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
424
425 if (!get_install_root(system_dir))
426 {
427 ERR("error reading registry key for installroot\n");
428 return E_FAIL;
429 }
430 else
431 {
432 version_size = MAX_PATH;
433 ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
434 lstrcatW(system_dir, version);
435 lstrcatW(system_dir, slash);
436 size = lstrlenW(system_dir) + 1;
437 }
438
439 *pcchBuffer = size;
440
441 if (pwzBuffer)
442 {
443 if (buffer_size >= size)
444 strcpyW(pwzBuffer, system_dir);
445 else
446 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
447 }
448
449 return hr;
450 }
451
452 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
453 HANDLE hndProcess, BOOL *pbLoaded)
454 {
455 FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
456
457 return E_NOTIMPL;
458 }
459
460 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
461 UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
462 {
463 FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
464
465 return E_NOTIMPL;
466 }
467
468 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
469 LPCWSTR pwzDllName, HMODULE *phndModule)
470 {
471 WCHAR version[MAX_PATH];
472 HRESULT hr;
473 DWORD cchBuffer;
474
475 TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
476
477 cchBuffer = MAX_PATH;
478 hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
479 if (FAILED(hr)) return hr;
480
481 return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
482 }
483
484 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
485 LPCSTR pszProcName, LPVOID *ppProc)
486 {
487 FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
488
489 return E_NOTIMPL;
490 }
491
492 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
493 REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
494 {
495 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
496 RuntimeHost *host;
497 HRESULT hr;
498
499 TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
500
501 hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
502
503 if (SUCCEEDED(hr))
504 hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
505
506 return hr;
507 }
508
509 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
510 BOOL *pbLoadable)
511 {
512 FIXME("%p %p\n", iface, pbLoadable);
513
514 return E_NOTIMPL;
515 }
516
517 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
518 DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
519 {
520 FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
521
522 return E_NOTIMPL;
523 }
524
525 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
526 DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
527 {
528 FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
529
530 return E_NOTIMPL;
531 }
532
533 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
534 {
535 FIXME("%p\n", iface);
536
537 return E_NOTIMPL;
538 }
539
540 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
541 BOOL *pbStarted, DWORD *pdwStartupFlags)
542 {
543 FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
544
545 return E_NOTIMPL;
546 }
547
548 static const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
549 CLRRuntimeInfo_QueryInterface,
550 CLRRuntimeInfo_AddRef,
551 CLRRuntimeInfo_Release,
552 CLRRuntimeInfo_GetVersionString,
553 CLRRuntimeInfo_GetRuntimeDirectory,
554 CLRRuntimeInfo_IsLoaded,
555 CLRRuntimeInfo_LoadErrorString,
556 CLRRuntimeInfo_LoadLibrary,
557 CLRRuntimeInfo_GetProcAddress,
558 CLRRuntimeInfo_GetInterface,
559 CLRRuntimeInfo_IsLoadable,
560 CLRRuntimeInfo_SetDefaultStartupFlags,
561 CLRRuntimeInfo_GetDefaultStartupFlags,
562 CLRRuntimeInfo_BindAsLegacyV2Runtime,
563 CLRRuntimeInfo_IsStarted
564 };
565
566 HRESULT ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo *iface, RuntimeHost **result)
567 {
568 struct CLRRuntimeInfo *This = impl_from_ICLRRuntimeInfo(iface);
569
570 assert(This->ICLRRuntimeInfo_iface.lpVtbl == &CLRRuntimeInfoVtbl);
571
572 return CLRRuntimeInfo_GetRuntimeHost(This, result);
573 }
574
575 #ifdef __i386__
576 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};
577 #elif defined(__x86_64__)
578 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 #else
580 static const WCHAR libmono2_arch_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
581 #endif
582
583 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version)
584 {
585 static const WCHAR mono_dll[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
586 static const WCHAR libmono_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
587 static const WCHAR mono2_dll[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
588 static const WCHAR libmono2_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
589 DWORD attributes=INVALID_FILE_ATTRIBUTES;
590
591 if (abi_version == 1)
592 {
593 strcpyW(dll_path, path);
594 strcatW(dll_path, mono_dll);
595 attributes = GetFileAttributesW(dll_path);
596
597 if (attributes == INVALID_FILE_ATTRIBUTES)
598 {
599 strcpyW(dll_path, path);
600 strcatW(dll_path, libmono_dll);
601 attributes = GetFileAttributesW(dll_path);
602 }
603 }
604 else if (abi_version == 2)
605 {
606 strcpyW(dll_path, path);
607 strcatW(dll_path, libmono2_arch_dll);
608 attributes = GetFileAttributesW(dll_path);
609
610 if (attributes == INVALID_FILE_ATTRIBUTES)
611 {
612 strcpyW(dll_path, path);
613 strcatW(dll_path, mono2_dll);
614 attributes = GetFileAttributesW(dll_path);
615 }
616
617 if (attributes == INVALID_FILE_ATTRIBUTES)
618 {
619 strcpyW(dll_path, path);
620 strcatW(dll_path, libmono2_dll);
621 attributes = GetFileAttributesW(dll_path);
622 }
623 }
624
625 return (attributes != INVALID_FILE_ATTRIBUTES);
626 }
627
628 static BOOL get_mono_path_from_registry(LPWSTR path, int abi_version)
629 {
630 static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
631 static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
632 static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
633 static const WCHAR slash[] = {'\\',0};
634
635 WCHAR version[64], version_key[MAX_PATH];
636 DWORD len;
637 HKEY key;
638 WCHAR dll_path[MAX_PATH];
639
640 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
641 return FALSE;
642
643 len = sizeof(version);
644 if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
645 {
646 RegCloseKey(key);
647 return FALSE;
648 }
649 RegCloseKey(key);
650
651 lstrcpyW(version_key, mono_key);
652 lstrcatW(version_key, slash);
653 lstrcatW(version_key, version);
654
655 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
656 return FALSE;
657
658 len = sizeof(WCHAR) * MAX_PATH;
659 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
660 {
661 RegCloseKey(key);
662 return FALSE;
663 }
664 RegCloseKey(key);
665
666 return find_mono_dll(path, dll_path, abi_version);
667 }
668
669 static BOOL get_mono_path_from_folder(LPCWSTR folder, LPWSTR mono_path, int abi_version)
670 {
671 static const WCHAR mono_one_dot_zero[] = {'\\','m','o','n','o','-','1','.','0', 0};
672 static const WCHAR mono_two_dot_zero[] = {'\\','m','o','n','o','-','2','.','0', 0};
673 WCHAR mono_dll_path[MAX_PATH];
674 BOOL found = FALSE;
675
676 strcpyW(mono_path, folder);
677
678 if (abi_version == 1)
679 strcatW(mono_path, mono_one_dot_zero);
680 else if (abi_version == 2)
681 strcatW(mono_path, mono_two_dot_zero);
682
683 found = find_mono_dll(mono_path, mono_dll_path, abi_version);
684
685 return found;
686 }
687
688 static BOOL get_mono_path(LPWSTR path, int abi_version)
689 {
690 static const WCHAR subdir_mono[] = {'\\','m','o','n','o',0};
691 static const WCHAR sibling_mono[] = {'\\','.','.','\\','m','o','n','o',0};
692 WCHAR base_path[MAX_PATH];
693 const char *unix_data_dir;
694 WCHAR *dos_data_dir;
695 int build_tree=0;
696 static WCHAR* (CDECL *wine_get_dos_file_name)(const char*);
697
698 /* First try c:\windows\mono */
699 GetWindowsDirectoryW(base_path, MAX_PATH);
700 strcatW(base_path, subdir_mono);
701
702 if (get_mono_path_from_folder(base_path, path, abi_version))
703 return TRUE;
704
705 /* Next: /usr/share/wine/mono */
706 unix_data_dir = wine_get_data_dir();
707
708 if (!unix_data_dir)
709 {
710 unix_data_dir = wine_get_build_dir();
711 build_tree = 1;
712 }
713
714 if (unix_data_dir)
715 {
716 if (!wine_get_dos_file_name)
717 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
718
719 if (wine_get_dos_file_name)
720 {
721 dos_data_dir = wine_get_dos_file_name(unix_data_dir);
722
723 if (dos_data_dir)
724 {
725 strcpyW(base_path, dos_data_dir);
726 strcatW(base_path, build_tree ? sibling_mono : subdir_mono);
727
728 HeapFree(GetProcessHeap(), 0, dos_data_dir);
729
730 if (get_mono_path_from_folder(base_path, path, abi_version))
731 return TRUE;
732 }
733 }
734 }
735
736 /* Last: the registry */
737 return get_mono_path_from_registry(path, abi_version);
738 }
739
740 static void find_runtimes(void)
741 {
742 int abi_version, i;
743 static const WCHAR libmono[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
744 static const WCHAR mscorlib[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
745 WCHAR mono_path[MAX_PATH], lib_path[MAX_PATH];
746 BOOL any_runtimes_found = FALSE;
747
748 if (runtimes_initialized) return;
749
750 EnterCriticalSection(&runtime_list_cs);
751
752 if (runtimes_initialized) goto end;
753
754 for (abi_version=NUM_ABI_VERSIONS; abi_version>0; abi_version--)
755 {
756 if (!get_mono_path(mono_path, abi_version))
757 continue;
758
759 for (i=0; i<NUM_RUNTIMES; i++)
760 {
761 if (runtimes[i].mono_abi_version == 0)
762 {
763 strcpyW(lib_path, mono_path);
764 strcatW(lib_path, libmono);
765 strcatW(lib_path, runtimes[i].mono_libdir);
766 strcatW(lib_path, mscorlib);
767
768 if (GetFileAttributesW(lib_path) != INVALID_FILE_ATTRIBUTES)
769 {
770 runtimes[i].mono_abi_version = abi_version;
771
772 strcpyW(runtimes[i].mono_path, mono_path);
773 strcpyW(runtimes[i].mscorlib_path, lib_path);
774
775 any_runtimes_found = TRUE;
776 }
777 }
778 }
779 }
780
781 if (!any_runtimes_found)
782 {
783 /* Report all runtimes are available if Mono isn't installed.
784 * FIXME: Remove this when Mono is properly packaged. */
785 for (i=0; i<NUM_RUNTIMES; i++)
786 runtimes[i].mono_abi_version = -1;
787 }
788
789 runtimes_initialized = 1;
790
791 end:
792 LeaveCriticalSection(&runtime_list_cs);
793 }
794
795 struct InstalledRuntimeEnum
796 {
797 IEnumUnknown IEnumUnknown_iface;
798 LONG ref;
799 ULONG pos;
800 };
801
802 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
803
804 static inline struct InstalledRuntimeEnum *impl_from_IEnumUnknown(IEnumUnknown *iface)
805 {
806 return CONTAINING_RECORD(iface, struct InstalledRuntimeEnum, IEnumUnknown_iface);
807 }
808
809 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface, REFIID riid,
810 void **ppvObject)
811 {
812 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
813
814 if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
815 IsEqualGUID( riid, &IID_IUnknown ) )
816 {
817 *ppvObject = iface;
818 }
819 else
820 {
821 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
822 return E_NOINTERFACE;
823 }
824
825 IEnumUnknown_AddRef( iface );
826
827 return S_OK;
828 }
829
830 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
831 {
832 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
833 ULONG ref = InterlockedIncrement(&This->ref);
834
835 TRACE("(%p) refcount=%u\n", iface, ref);
836
837 return ref;
838 }
839
840 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
841 {
842 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
843 ULONG ref = InterlockedDecrement(&This->ref);
844
845 TRACE("(%p) refcount=%u\n", iface, ref);
846
847 if (ref == 0)
848 {
849 HeapFree(GetProcessHeap(), 0, This);
850 }
851
852 return ref;
853 }
854
855 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
856 IUnknown **rgelt, ULONG *pceltFetched)
857 {
858 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
859 int num_fetched = 0;
860 HRESULT hr=S_OK;
861 IUnknown *item;
862
863 TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
864
865 while (num_fetched < celt)
866 {
867 if (This->pos >= NUM_RUNTIMES)
868 {
869 hr = S_FALSE;
870 break;
871 }
872 if (runtimes[This->pos].mono_abi_version)
873 {
874 item = (IUnknown*)&runtimes[This->pos].ICLRRuntimeInfo_iface;
875 IUnknown_AddRef(item);
876 rgelt[num_fetched] = item;
877 num_fetched++;
878 }
879 This->pos++;
880 }
881
882 if (pceltFetched)
883 *pceltFetched = num_fetched;
884
885 return hr;
886 }
887
888 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
889 {
890 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
891 int num_fetched = 0;
892 HRESULT hr=S_OK;
893
894 TRACE("(%p,%u)\n", iface, celt);
895
896 while (num_fetched < celt)
897 {
898 if (This->pos >= NUM_RUNTIMES)
899 {
900 hr = S_FALSE;
901 break;
902 }
903 if (runtimes[This->pos].mono_abi_version)
904 {
905 num_fetched++;
906 }
907 This->pos++;
908 }
909
910 return hr;
911 }
912
913 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
914 {
915 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
916
917 TRACE("(%p)\n", iface);
918
919 This->pos = 0;
920
921 return S_OK;
922 }
923
924 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
925 {
926 struct InstalledRuntimeEnum *This = impl_from_IEnumUnknown(iface);
927 struct InstalledRuntimeEnum *new_enum;
928
929 TRACE("(%p)\n", iface);
930
931 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
932 if (!new_enum)
933 return E_OUTOFMEMORY;
934
935 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
936 new_enum->ref = 1;
937 new_enum->pos = This->pos;
938
939 *ppenum = &new_enum->IEnumUnknown_iface;
940
941 return S_OK;
942 }
943
944 static const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
945 InstalledRuntimeEnum_QueryInterface,
946 InstalledRuntimeEnum_AddRef,
947 InstalledRuntimeEnum_Release,
948 InstalledRuntimeEnum_Next,
949 InstalledRuntimeEnum_Skip,
950 InstalledRuntimeEnum_Reset,
951 InstalledRuntimeEnum_Clone
952 };
953
954 struct CLRMetaHost
955 {
956 ICLRMetaHost ICLRMetaHost_iface;
957 };
958
959 static struct CLRMetaHost GlobalCLRMetaHost;
960
961 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
962 REFIID riid,
963 void **ppvObject)
964 {
965 TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
966
967 if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
968 IsEqualGUID( riid, &IID_IUnknown ) )
969 {
970 *ppvObject = iface;
971 }
972 else
973 {
974 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
975 return E_NOINTERFACE;
976 }
977
978 ICLRMetaHost_AddRef( iface );
979
980 return S_OK;
981 }
982
983 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
984 {
985 return 2;
986 }
987
988 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
989 {
990 return 1;
991 }
992
993 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
994 {
995 *major = 0;
996 *minor = 0;
997 *build = 0;
998
999 if (version[0] == 'v' || version[0] == 'V')
1000 {
1001 version++;
1002 if (!isdigit(*version))
1003 return FALSE;
1004
1005 while (isdigit(*version))
1006 *major = *major * 10 + (*version++ - '0');
1007
1008 if (*version == 0)
1009 return TRUE;
1010
1011 if (*version++ != '.' || !isdigit(*version))
1012 return FALSE;
1013
1014 while (isdigit(*version))
1015 *minor = *minor * 10 + (*version++ - '0');
1016
1017 if (*version == 0)
1018 return TRUE;
1019
1020 if (*version++ != '.' || !isdigit(*version))
1021 return FALSE;
1022
1023 while (isdigit(*version))
1024 *build = *build * 10 + (*version++ - '0');
1025
1026 return *version == 0;
1027 }
1028 else
1029 return FALSE;
1030 }
1031
1032 HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
1033 LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
1034 {
1035 int i;
1036 DWORD major, minor, build;
1037
1038 TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
1039
1040 if (!pwzVersion)
1041 return E_POINTER;
1042
1043 if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
1044 {
1045 ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
1046 return CLR_E_SHIM_RUNTIME;
1047 }
1048
1049 find_runtimes();
1050
1051 for (i=0; i<NUM_RUNTIMES; i++)
1052 {
1053 if (runtimes[i].major == major && runtimes[i].minor == minor &&
1054 runtimes[i].build == build)
1055 {
1056 if (runtimes[i].mono_abi_version)
1057 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface, iid,
1058 ppRuntime);
1059 else
1060 {
1061 missing_runtime_message(&runtimes[i]);
1062 return CLR_E_SHIM_RUNTIME;
1063 }
1064 }
1065 }
1066
1067 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
1068 return CLR_E_SHIM_RUNTIME;
1069 }
1070
1071 HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
1072 LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
1073 {
1074 ASSEMBLY *assembly;
1075 HRESULT hr;
1076 LPSTR version;
1077 ULONG buffer_size=*pcchBuffer;
1078
1079 TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
1080
1081 hr = assembly_create(&assembly, pwzFilePath);
1082
1083 if (SUCCEEDED(hr))
1084 {
1085 hr = assembly_get_runtime_version(assembly, &version);
1086
1087 if (SUCCEEDED(hr))
1088 {
1089 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
1090
1091 if (pwzBuffer)
1092 {
1093 if (buffer_size >= *pcchBuffer)
1094 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
1095 else
1096 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
1097 }
1098 }
1099
1100 assembly_release(assembly);
1101 }
1102
1103 return hr;
1104 }
1105
1106 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
1107 IEnumUnknown **ppEnumerator)
1108 {
1109 struct InstalledRuntimeEnum *new_enum;
1110
1111 TRACE("%p\n", ppEnumerator);
1112
1113 find_runtimes();
1114
1115 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
1116 if (!new_enum)
1117 return E_OUTOFMEMORY;
1118
1119 new_enum->IEnumUnknown_iface.lpVtbl = &InstalledRuntimeEnum_Vtbl;
1120 new_enum->ref = 1;
1121 new_enum->pos = 0;
1122
1123 *ppEnumerator = &new_enum->IEnumUnknown_iface;
1124
1125 return S_OK;
1126 }
1127
1128 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
1129 HANDLE hndProcess, IEnumUnknown **ppEnumerator)
1130 {
1131 FIXME("%p %p\n", hndProcess, ppEnumerator);
1132
1133 return E_NOTIMPL;
1134 }
1135
1136 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
1137 RuntimeLoadedCallbackFnPtr pCallbackFunction)
1138 {
1139 FIXME("%p\n", pCallbackFunction);
1140
1141 return E_NOTIMPL;
1142 }
1143
1144 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
1145 REFIID riid, LPVOID *ppUnk)
1146 {
1147 FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
1148
1149 return E_NOTIMPL;
1150 }
1151
1152 static HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
1153 {
1154 FIXME("%i: stub\n", iExitCode);
1155
1156 ExitProcess(iExitCode);
1157 }
1158
1159 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
1160 {
1161 CLRMetaHost_QueryInterface,
1162 CLRMetaHost_AddRef,
1163 CLRMetaHost_Release,
1164 CLRMetaHost_GetRuntime,
1165 CLRMetaHost_GetVersionFromFile,
1166 CLRMetaHost_EnumerateInstalledRuntimes,
1167 CLRMetaHost_EnumerateLoadedRuntimes,
1168 CLRMetaHost_RequestRuntimeLoadedNotification,
1169 CLRMetaHost_QueryLegacyV2RuntimeBinding,
1170 CLRMetaHost_ExitProcess
1171 };
1172
1173 static struct CLRMetaHost GlobalCLRMetaHost = {
1174 { &CLRMetaHost_vtbl }
1175 };
1176
1177 HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1178 {
1179 return ICLRMetaHost_QueryInterface(&GlobalCLRMetaHost.ICLRMetaHost_iface, riid, ppobj);
1180 }
1181
1182 static MonoAssembly* mono_assembly_search_hook_fn(MonoAssemblyName *aname, char **assemblies_path, void *user_data)
1183 {
1184 loaded_mono *mono = user_data;
1185 HRESULT hr=S_OK;
1186 MonoAssembly *result=NULL;
1187 char *stringname=NULL;
1188 LPWSTR stringnameW;
1189 int stringnameW_size;
1190 IAssemblyCache *asmcache;
1191 ASSEMBLY_INFO info;
1192 WCHAR path[MAX_PATH];
1193 char *pathA;
1194 MonoImageOpenStatus stat;
1195 static WCHAR fusiondll[] = {'f','u','s','i','o','n',0};
1196 HMODULE hfusion=NULL;
1197 static HRESULT (WINAPI *pCreateAssemblyCache)(IAssemblyCache**,DWORD);
1198
1199 stringname = mono->mono_stringify_assembly_name(aname);
1200
1201 TRACE("%s\n", debugstr_a(stringname));
1202
1203 if (!stringname) return NULL;
1204
1205 /* FIXME: We should search the given paths before the GAC. */
1206
1207 if (!pCreateAssemblyCache)
1208 {
1209 hr = LoadLibraryShim(fusiondll, NULL, NULL, &hfusion);
1210
1211 if (SUCCEEDED(hr))
1212 {
1213 pCreateAssemblyCache = (void*)GetProcAddress(hfusion, "CreateAssemblyCache");
1214 if (!pCreateAssemblyCache)
1215 hr = E_FAIL;
1216 }
1217 }
1218
1219 if (SUCCEEDED(hr))
1220 hr = pCreateAssemblyCache(&asmcache, 0);
1221
1222 if (SUCCEEDED(hr))
1223 {
1224 stringnameW_size = MultiByteToWideChar(CP_UTF8, 0, stringname, -1, NULL, 0);
1225
1226 stringnameW = HeapAlloc(GetProcessHeap(), 0, stringnameW_size * sizeof(WCHAR));
1227 if (stringnameW)
1228 MultiByteToWideChar(CP_UTF8, 0, stringname, -1, stringnameW, stringnameW_size);
1229 else
1230 hr = E_OUTOFMEMORY;
1231
1232 if (SUCCEEDED(hr))
1233 {
1234 info.cbAssemblyInfo = sizeof(info);
1235 info.pszCurrentAssemblyPathBuf = path;
1236 info.cchBuf = MAX_PATH;
1237 path[0] = 0;
1238
1239 hr = IAssemblyCache_QueryAssemblyInfo(asmcache, 0, stringnameW, &info);
1240 }
1241
1242 HeapFree(GetProcessHeap(), 0, stringnameW);
1243
1244 IAssemblyCache_Release(asmcache);
1245 }
1246
1247 if (SUCCEEDED(hr))
1248 {
1249 TRACE("found: %s\n", debugstr_w(path));
1250
1251 pathA = WtoA(path);
1252
1253 if (pathA)
1254 {
1255 result = mono->mono_assembly_open(pathA, &stat);
1256
1257 if (!result)
1258 ERR("Failed to load %s, status=%u\n", debugstr_w(path), stat);
1259
1260 HeapFree(GetProcessHeap(), 0, pathA);
1261 }
1262 }
1263
1264 mono->mono_free(stringname);
1265
1266 return result;
1267 }
1268
1269 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1270 DWORD startup_flags, DWORD runtimeinfo_flags, BOOL legacy, ICLRRuntimeInfo **result)
1271 {
1272 static const WCHAR dotconfig[] = {'.','c','o','n','f','i','g',0};
1273 static const DWORD supported_startup_flags = 0;
1274 static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1275 int i;
1276 WCHAR local_version[MAX_PATH];
1277 ULONG local_version_size = MAX_PATH;
1278 WCHAR local_config_file[MAX_PATH];
1279 HRESULT hr;
1280 parsed_config_file parsed_config;
1281
1282 if (startup_flags & ~supported_startup_flags)
1283 FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
1284
1285 if (runtimeinfo_flags & ~supported_runtime_flags)
1286 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
1287
1288 if (exefile && !config_file)
1289 {
1290 strcpyW(local_config_file, exefile);
1291 strcatW(local_config_file, dotconfig);
1292
1293 config_file = local_config_file;
1294 }
1295
1296 if (config_file)
1297 {
1298 int found=0;
1299 hr = parse_config_file(config_file, &parsed_config);
1300
1301 if (SUCCEEDED(hr))
1302 {
1303 supported_runtime *entry;
1304 LIST_FOR_EACH_ENTRY(entry, &parsed_config.supported_runtimes, supported_runtime, entry)
1305 {
1306 hr = CLRMetaHost_GetRuntime(0, entry->version, &IID_ICLRRuntimeInfo, (void**)result);
1307 if (SUCCEEDED(hr))
1308 {
1309 found = 1;
1310 break;
1311 }
1312 }
1313 }
1314 else
1315 {
1316 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file), hr);
1317 }
1318
1319 free_parsed_config_file(&parsed_config);
1320
1321 if (found)
1322 return S_OK;
1323 }
1324
1325 if (exefile && !version)
1326 {
1327 hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1328
1329 version = local_version;
1330
1331 if (FAILED(hr)) return hr;
1332 }
1333
1334 if (version)
1335 {
1336 hr = CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1337 if(SUCCEEDED(hr))
1338 return hr;
1339 }
1340
1341 if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1342 {
1343 DWORD major, minor, build;
1344
1345 if (version && !parse_runtime_version(version, &major, &minor, &build))
1346 {
1347 ERR("Cannot parse %s\n", debugstr_w(version));
1348 return CLR_E_SHIM_RUNTIME;
1349 }
1350
1351 find_runtimes();
1352
1353 if (legacy)
1354 i = 2;
1355 else
1356 i = NUM_RUNTIMES;
1357
1358 while (i--)
1359 {
1360 if (runtimes[i].mono_abi_version)
1361 {
1362 /* Must be greater or equal to the version passed in. */
1363 if (!version || ((runtimes[i].major >= major && runtimes[i].minor >= minor && runtimes[i].build >= build) ||
1364 (runtimes[i].major >= major && runtimes[i].minor > minor) ||
1365 (runtimes[i].major > major)))
1366 {
1367 return ICLRRuntimeInfo_QueryInterface(&runtimes[i].ICLRRuntimeInfo_iface,
1368 &IID_ICLRRuntimeInfo, (void **)result);
1369 }
1370 }
1371 }
1372
1373 if (legacy)
1374 missing_runtime_message(&runtimes[1]);
1375 else
1376 missing_runtime_message(&runtimes[NUM_RUNTIMES-1]);
1377
1378 return CLR_E_SHIM_RUNTIME;
1379 }
1380
1381 return CLR_E_SHIM_RUNTIME;
1382 }