mscoree: Implement config file parsing.
[wine/multimedia.git] / dlls / mscoree / metahost.c
blob3f572f7cd938fa690bb05d8507c666c99047829a
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 "mscoree.h"
36 #include "metahost.h"
37 #include "wine/list.h"
38 #include "mscoree_private.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL( mscoree );
44 static const WCHAR net_11_subdir[] = {'1','.','0',0};
45 static const WCHAR net_20_subdir[] = {'2','.','0',0};
46 static const WCHAR net_40_subdir[] = {'4','.','0',0};
48 const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl;
50 #define NUM_RUNTIMES 3
52 static struct CLRRuntimeInfo runtimes[NUM_RUNTIMES] = {
53 {&CLRRuntimeInfoVtbl, net_11_subdir, 1, 1, 4322, 0},
54 {&CLRRuntimeInfoVtbl, net_20_subdir, 2, 0, 50727, 0},
55 {&CLRRuntimeInfoVtbl, net_40_subdir, 4, 0, 30319, 0}
58 static int runtimes_initialized;
60 static CRITICAL_SECTION runtime_list_cs;
61 static CRITICAL_SECTION_DEBUG runtime_list_cs_debug =
63 0, 0, &runtime_list_cs,
64 { &runtime_list_cs_debug.ProcessLocksList,
65 &runtime_list_cs_debug.ProcessLocksList },
66 0, 0, { (DWORD_PTR)(__FILE__ ": runtime_list_cs") }
68 static CRITICAL_SECTION runtime_list_cs = { &runtime_list_cs_debug, -1, 0, 0, 0, 0 };
70 #define NUM_ABI_VERSIONS 2
72 static loaded_mono loaded_monos[NUM_ABI_VERSIONS];
74 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version);
76 static void set_environment(LPCWSTR bin_path)
78 WCHAR path_env[MAX_PATH];
79 int len;
81 static const WCHAR pathW[] = {'P','A','T','H',0};
83 /* We have to modify PATH as Mono loads other DLLs from this directory. */
84 GetEnvironmentVariableW(pathW, path_env, sizeof(path_env)/sizeof(WCHAR));
85 len = strlenW(path_env);
86 path_env[len++] = ';';
87 strcpyW(path_env+len, bin_path);
88 SetEnvironmentVariableW(pathW, path_env);
91 static HRESULT load_mono(CLRRuntimeInfo *This, loaded_mono **result)
93 static const WCHAR bin[] = {'\\','b','i','n',0};
94 static const WCHAR lib[] = {'\\','l','i','b',0};
95 static const WCHAR etc[] = {'\\','e','t','c',0};
96 WCHAR mono_dll_path[MAX_PATH+16], mono_bin_path[MAX_PATH+4];
97 WCHAR mono_lib_path[MAX_PATH+4], mono_etc_path[MAX_PATH+4];
98 char mono_lib_path_a[MAX_PATH], mono_etc_path_a[MAX_PATH];
99 int trace_size;
100 char trace_setting[256];
102 if (This->mono_abi_version == -1)
103 MESSAGE("wine: Install the Windows version of Mono to run .NET executables\n");
105 if (This->mono_abi_version <= 0 || This->mono_abi_version > NUM_ABI_VERSIONS)
106 return E_FAIL;
108 *result = &loaded_monos[This->mono_abi_version-1];
110 if (!(*result)->mono_handle)
112 strcpyW(mono_bin_path, This->mono_path);
113 strcatW(mono_bin_path, bin);
114 set_environment(mono_bin_path);
116 strcpyW(mono_lib_path, This->mono_path);
117 strcatW(mono_lib_path, lib);
118 WideCharToMultiByte(CP_UTF8, 0, mono_lib_path, -1, mono_lib_path_a, MAX_PATH, NULL, NULL);
120 strcpyW(mono_etc_path, This->mono_path);
121 strcatW(mono_etc_path, etc);
122 WideCharToMultiByte(CP_UTF8, 0, mono_etc_path, -1, mono_etc_path_a, MAX_PATH, NULL, NULL);
124 if (!find_mono_dll(This->mono_path, mono_dll_path, This->mono_abi_version)) goto fail;
126 (*result)->mono_handle = LoadLibraryW(mono_dll_path);
128 if (!(*result)->mono_handle) goto fail;
130 #define LOAD_MONO_FUNCTION(x) do { \
131 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
132 if (!(*result)->x) { \
133 goto fail; \
135 } while (0);
137 LOAD_MONO_FUNCTION(mono_config_parse);
138 LOAD_MONO_FUNCTION(mono_domain_assembly_open);
139 LOAD_MONO_FUNCTION(mono_jit_cleanup);
140 LOAD_MONO_FUNCTION(mono_jit_exec);
141 LOAD_MONO_FUNCTION(mono_jit_init);
142 LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
143 LOAD_MONO_FUNCTION(mono_set_dirs);
145 #undef LOAD_MONO_FUNCTION
147 (*result)->mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
149 (*result)->mono_config_parse(NULL);
151 trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
153 if (trace_size)
155 (*result)->mono_jit_set_trace_options(trace_setting);
159 return S_OK;
161 fail:
162 ERR("Could not load Mono into this process\n");
163 FreeLibrary((*result)->mono_handle);
164 (*result)->mono_handle = NULL;
165 return E_FAIL;
168 static HRESULT CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo *This, RuntimeHost **result)
170 HRESULT hr = S_OK;
171 loaded_mono *ploaded_mono;
173 if (This->loaded_runtime)
175 *result = This->loaded_runtime;
176 return hr;
179 EnterCriticalSection(&runtime_list_cs);
181 hr = load_mono(This, &ploaded_mono);
183 if (SUCCEEDED(hr))
184 hr = RuntimeHost_Construct(This, ploaded_mono, &This->loaded_runtime);
186 LeaveCriticalSection(&runtime_list_cs);
188 if (SUCCEEDED(hr))
189 *result = This->loaded_runtime;
191 return hr;
194 void unload_all_runtimes(void)
196 int i;
198 for (i=0; i<NUM_RUNTIMES; i++)
199 if (runtimes[i].loaded_runtime)
200 RuntimeHost_Destroy(runtimes[i].loaded_runtime);
203 static HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
204 REFIID riid,
205 void **ppvObject)
207 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
209 if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
210 IsEqualGUID( riid, &IID_IUnknown ) )
212 *ppvObject = iface;
214 else
216 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
217 return E_NOINTERFACE;
220 ICLRRuntimeInfo_AddRef( iface );
222 return S_OK;
225 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
227 MSCOREE_LockModule();
228 return 2;
231 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
233 MSCOREE_UnlockModule();
234 return 1;
237 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
238 LPWSTR pwzBuffer, DWORD *pcchBuffer)
240 struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
241 DWORD buffer_size = *pcchBuffer;
242 HRESULT hr = S_OK;
243 char version[11];
244 DWORD size;
246 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
248 size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
250 assert(size <= sizeof(version));
252 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
254 if (pwzBuffer)
256 if (buffer_size >= *pcchBuffer)
257 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
258 else
259 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
262 return hr;
265 static BOOL get_install_root(LPWSTR install_dir)
267 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};
268 const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
270 DWORD len;
271 HKEY key;
273 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
274 return FALSE;
276 len = MAX_PATH;
277 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
279 RegCloseKey(key);
280 return FALSE;
282 RegCloseKey(key);
284 return TRUE;
287 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
288 LPWSTR pwzBuffer, DWORD *pcchBuffer)
290 static const WCHAR slash[] = {'\\',0};
291 DWORD buffer_size = *pcchBuffer;
292 WCHAR system_dir[MAX_PATH];
293 WCHAR version[MAX_PATH];
294 DWORD version_size, size;
295 HRESULT hr = S_OK;
297 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
299 if (!get_install_root(system_dir))
301 ERR("error reading registry key for installroot\n");
302 return E_FAIL;
304 else
306 version_size = MAX_PATH;
307 ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
308 lstrcatW(system_dir, version);
309 lstrcatW(system_dir, slash);
310 size = lstrlenW(system_dir) + 1;
313 *pcchBuffer = size;
315 if (pwzBuffer)
317 if (buffer_size >= size)
318 strcpyW(pwzBuffer, system_dir);
319 else
320 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
323 return hr;
326 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
327 HANDLE hndProcess, BOOL *pbLoaded)
329 FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
331 return E_NOTIMPL;
334 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
335 UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
337 FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
339 return E_NOTIMPL;
342 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
343 LPCWSTR pwzDllName, HMODULE *phndModule)
345 WCHAR version[MAX_PATH];
346 HRESULT hr;
347 DWORD cchBuffer;
349 TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
351 cchBuffer = MAX_PATH;
352 hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
353 if (FAILED(hr)) return hr;
355 return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
358 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
359 LPCSTR pszProcName, LPVOID *ppProc)
361 FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
363 return E_NOTIMPL;
366 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
367 REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
369 struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
370 RuntimeHost *host;
371 HRESULT hr;
373 TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
375 hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
377 if (SUCCEEDED(hr))
378 hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
380 return hr;
383 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
384 BOOL *pbLoadable)
386 FIXME("%p %p\n", iface, pbLoadable);
388 return E_NOTIMPL;
391 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
392 DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
394 FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
396 return E_NOTIMPL;
399 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
400 DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
402 FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
404 return E_NOTIMPL;
407 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
409 FIXME("%p\n", iface);
411 return E_NOTIMPL;
414 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
415 BOOL *pbStarted, DWORD *pdwStartupFlags)
417 FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
419 return E_NOTIMPL;
422 const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
423 CLRRuntimeInfo_QueryInterface,
424 CLRRuntimeInfo_AddRef,
425 CLRRuntimeInfo_Release,
426 CLRRuntimeInfo_GetVersionString,
427 CLRRuntimeInfo_GetRuntimeDirectory,
428 CLRRuntimeInfo_IsLoaded,
429 CLRRuntimeInfo_LoadErrorString,
430 CLRRuntimeInfo_LoadLibrary,
431 CLRRuntimeInfo_GetProcAddress,
432 CLRRuntimeInfo_GetInterface,
433 CLRRuntimeInfo_IsLoadable,
434 CLRRuntimeInfo_SetDefaultStartupFlags,
435 CLRRuntimeInfo_GetDefaultStartupFlags,
436 CLRRuntimeInfo_BindAsLegacyV2Runtime,
437 CLRRuntimeInfo_IsStarted
440 HRESULT ICLRRuntimeInfo_GetRuntimeHost(ICLRRuntimeInfo *iface, RuntimeHost **result)
442 struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
444 assert(This->ICLRRuntimeInfo_vtbl == &CLRRuntimeInfoVtbl);
446 return CLRRuntimeInfo_GetRuntimeHost(This, result);
449 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version)
451 static const WCHAR mono_dll[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
452 static const WCHAR libmono_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
453 static const WCHAR mono2_dll[] = {'\\','b','i','n','\\','m','o','n','o','-','2','.','0','.','d','l','l',0};
454 static const WCHAR libmono2_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','-','2','.','0','.','d','l','l',0};
455 DWORD attributes=INVALID_FILE_ATTRIBUTES;
457 if (abi_version == 1)
459 strcpyW(dll_path, path);
460 strcatW(dll_path, mono_dll);
461 attributes = GetFileAttributesW(dll_path);
463 if (attributes == INVALID_FILE_ATTRIBUTES)
465 strcpyW(dll_path, path);
466 strcatW(dll_path, libmono_dll);
467 attributes = GetFileAttributesW(dll_path);
470 else if (abi_version == 2)
472 strcpyW(dll_path, path);
473 strcatW(dll_path, mono2_dll);
474 attributes = GetFileAttributesW(dll_path);
476 if (attributes == INVALID_FILE_ATTRIBUTES)
478 strcpyW(dll_path, path);
479 strcatW(dll_path, libmono2_dll);
480 attributes = GetFileAttributesW(dll_path);
484 return (attributes != INVALID_FILE_ATTRIBUTES);
487 static BOOL get_mono_path_from_registry(LPWSTR path, int abi_version)
489 static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
490 static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
491 static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
492 static const WCHAR slash[] = {'\\',0};
494 WCHAR version[64], version_key[MAX_PATH];
495 DWORD len;
496 HKEY key;
497 WCHAR dll_path[MAX_PATH];
499 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
500 return FALSE;
502 len = sizeof(version);
503 if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
505 RegCloseKey(key);
506 return FALSE;
508 RegCloseKey(key);
510 lstrcpyW(version_key, mono_key);
511 lstrcatW(version_key, slash);
512 lstrcatW(version_key, version);
514 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
515 return FALSE;
517 len = sizeof(WCHAR) * MAX_PATH;
518 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
520 RegCloseKey(key);
521 return FALSE;
523 RegCloseKey(key);
525 return find_mono_dll(path, dll_path, abi_version);
528 static BOOL get_mono_path_from_folder(LPCWSTR folder, LPWSTR mono_path, int abi_version)
530 static const WCHAR mono_one_dot_zero[] = {'\\','m','o','n','o','-','1','.','0', 0};
531 static const WCHAR mono_two_dot_zero[] = {'\\','m','o','n','o','-','2','.','0', 0};
532 WCHAR mono_dll_path[MAX_PATH];
533 BOOL found = FALSE;
535 strcpyW(mono_path, folder);
537 if (abi_version == 1)
538 strcatW(mono_path, mono_one_dot_zero);
539 else if (abi_version == 2)
540 strcatW(mono_path, mono_two_dot_zero);
542 found = find_mono_dll(mono_path, mono_dll_path, abi_version);
544 return found;
547 static BOOL get_mono_path(LPWSTR path, int abi_version)
549 static const WCHAR subdir_mono[] = {'\\','m','o','n','o',0};
550 static const WCHAR sibling_mono[] = {'\\','.','.','\\','m','o','n','o',0};
551 WCHAR base_path[MAX_PATH];
552 const char *unix_data_dir;
553 WCHAR *dos_data_dir;
554 int build_tree=0;
555 static WCHAR* (CDECL *wine_get_dos_file_name)(const char*);
557 /* First try c:\windows\mono */
558 GetWindowsDirectoryW(base_path, MAX_PATH);
559 strcatW(base_path, subdir_mono);
561 if (get_mono_path_from_folder(base_path, path, abi_version))
562 return TRUE;
564 /* Next: /usr/share/wine/mono */
565 unix_data_dir = wine_get_data_dir();
567 if (!unix_data_dir)
569 unix_data_dir = wine_get_build_dir();
570 build_tree = 1;
573 if (unix_data_dir)
575 if (!wine_get_dos_file_name)
576 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
578 if (wine_get_dos_file_name)
580 dos_data_dir = wine_get_dos_file_name(unix_data_dir);
582 if (dos_data_dir)
584 strcpyW(base_path, dos_data_dir);
585 strcatW(base_path, build_tree ? sibling_mono : subdir_mono);
587 HeapFree(GetProcessHeap(), 0, dos_data_dir);
589 if (get_mono_path_from_folder(base_path, path, abi_version))
590 return TRUE;
595 /* Last: the registry */
596 return get_mono_path_from_registry(path, abi_version);
599 static void find_runtimes(void)
601 int abi_version, i;
602 static const WCHAR libmono[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
603 static const WCHAR mscorlib[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
604 WCHAR mono_path[MAX_PATH], lib_path[MAX_PATH];
605 BOOL any_runtimes_found = FALSE;
607 if (runtimes_initialized) return;
609 EnterCriticalSection(&runtime_list_cs);
611 if (runtimes_initialized) goto end;
613 for (abi_version=NUM_ABI_VERSIONS; abi_version>0; abi_version--)
615 if (!get_mono_path(mono_path, abi_version))
616 continue;
618 for (i=0; i<NUM_RUNTIMES; i++)
620 if (runtimes[i].mono_abi_version == 0)
622 strcpyW(lib_path, mono_path);
623 strcatW(lib_path, libmono);
624 strcatW(lib_path, runtimes[i].mono_libdir);
625 strcatW(lib_path, mscorlib);
627 if (GetFileAttributesW(lib_path) != INVALID_FILE_ATTRIBUTES)
629 runtimes[i].mono_abi_version = abi_version;
631 strcpyW(runtimes[i].mono_path, mono_path);
632 strcpyW(runtimes[i].mscorlib_path, lib_path);
634 any_runtimes_found = TRUE;
640 if (!any_runtimes_found)
642 /* Report all runtimes are available if Mono isn't installed.
643 * FIXME: Remove this when Mono is properly packaged. */
644 for (i=0; i<NUM_RUNTIMES; i++)
645 runtimes[i].mono_abi_version = -1;
648 runtimes_initialized = 1;
650 end:
651 LeaveCriticalSection(&runtime_list_cs);
654 struct InstalledRuntimeEnum
656 const struct IEnumUnknownVtbl *Vtbl;
657 LONG ref;
658 ULONG pos;
661 const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
663 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface,
664 REFIID riid,
665 void **ppvObject)
667 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
669 if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
670 IsEqualGUID( riid, &IID_IUnknown ) )
672 *ppvObject = iface;
674 else
676 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
677 return E_NOINTERFACE;
680 IEnumUnknown_AddRef( iface );
682 return S_OK;
685 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
687 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
688 ULONG ref = InterlockedIncrement(&This->ref);
690 MSCOREE_LockModule();
692 TRACE("(%p) refcount=%u\n", iface, ref);
694 return ref;
697 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
699 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
700 ULONG ref = InterlockedDecrement(&This->ref);
702 MSCOREE_UnlockModule();
704 TRACE("(%p) refcount=%u\n", iface, ref);
706 if (ref == 0)
708 HeapFree(GetProcessHeap(), 0, This);
711 return ref;
714 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
715 IUnknown **rgelt, ULONG *pceltFetched)
717 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
718 int num_fetched = 0;
719 HRESULT hr=S_OK;
720 IUnknown *item;
722 TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
724 while (num_fetched < celt)
726 if (This->pos >= NUM_RUNTIMES)
728 hr = S_FALSE;
729 break;
731 if (runtimes[This->pos].mono_abi_version)
733 item = (IUnknown*)&runtimes[This->pos];
734 IUnknown_AddRef(item);
735 rgelt[num_fetched] = item;
736 num_fetched++;
738 This->pos++;
741 if (pceltFetched)
742 *pceltFetched = num_fetched;
744 return hr;
747 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
749 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
750 int num_fetched = 0;
751 HRESULT hr=S_OK;
753 TRACE("(%p,%u)\n", iface, celt);
755 while (num_fetched < celt)
757 if (This->pos >= NUM_RUNTIMES)
759 hr = S_FALSE;
760 break;
762 if (runtimes[This->pos].mono_abi_version)
764 num_fetched++;
766 This->pos++;
769 return hr;
772 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
774 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
776 TRACE("(%p)\n", iface);
778 This->pos = 0;
780 return S_OK;
783 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
785 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
786 struct InstalledRuntimeEnum *new_enum;
788 TRACE("(%p)\n", iface);
790 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
791 if (!new_enum)
792 return E_OUTOFMEMORY;
794 new_enum->Vtbl = &InstalledRuntimeEnum_Vtbl;
795 new_enum->ref = 1;
796 new_enum->pos = This->pos;
798 *ppenum = (IEnumUnknown*)new_enum;
800 return S_OK;
803 const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
804 InstalledRuntimeEnum_QueryInterface,
805 InstalledRuntimeEnum_AddRef,
806 InstalledRuntimeEnum_Release,
807 InstalledRuntimeEnum_Next,
808 InstalledRuntimeEnum_Skip,
809 InstalledRuntimeEnum_Reset,
810 InstalledRuntimeEnum_Clone
813 struct CLRMetaHost
815 const struct ICLRMetaHostVtbl *CLRMetaHost_vtbl;
818 static const struct CLRMetaHost GlobalCLRMetaHost;
820 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
821 REFIID riid,
822 void **ppvObject)
824 TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
826 if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
827 IsEqualGUID( riid, &IID_IUnknown ) )
829 *ppvObject = iface;
831 else
833 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
834 return E_NOINTERFACE;
837 ICLRMetaHost_AddRef( iface );
839 return S_OK;
842 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
844 MSCOREE_LockModule();
845 return 2;
848 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
850 MSCOREE_UnlockModule();
851 return 1;
854 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
856 *major = 0;
857 *minor = 0;
858 *build = 0;
860 if (version[0] == 'v')
862 version++;
863 if (!isdigit(*version))
864 return FALSE;
866 while (isdigit(*version))
867 *major = *major * 10 + (*version++ - '0');
869 if (*version == 0)
870 return TRUE;
872 if (*version++ != '.' || !isdigit(*version))
873 return FALSE;
875 while (isdigit(*version))
876 *minor = *minor * 10 + (*version++ - '0');
878 if (*version == 0)
879 return TRUE;
881 if (*version++ != '.' || !isdigit(*version))
882 return FALSE;
884 while (isdigit(*version))
885 *build = *build * 10 + (*version++ - '0');
887 return *version == 0;
889 else
890 return FALSE;
893 static HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
894 LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
896 int i;
897 DWORD major, minor, build;
899 TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
901 if (!pwzVersion)
902 return E_POINTER;
904 if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
906 ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
907 return CLR_E_SHIM_RUNTIME;
910 find_runtimes();
912 for (i=0; i<NUM_RUNTIMES; i++)
914 if (runtimes[i].major == major && runtimes[i].minor == minor &&
915 runtimes[i].build == build)
917 if (runtimes[i].mono_abi_version)
918 return IUnknown_QueryInterface((IUnknown*)&runtimes[i], iid, ppRuntime);
919 else
921 ERR("Mono is missing %s runtime\n", debugstr_w(pwzVersion));
922 return CLR_E_SHIM_RUNTIME;
927 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
928 return CLR_E_SHIM_RUNTIME;
931 static HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
932 LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
934 ASSEMBLY *assembly;
935 HRESULT hr;
936 LPSTR version;
937 ULONG buffer_size=*pcchBuffer;
939 TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
941 hr = assembly_create(&assembly, pwzFilePath);
943 if (SUCCEEDED(hr))
945 hr = assembly_get_runtime_version(assembly, &version);
947 if (SUCCEEDED(hr))
949 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
951 if (pwzBuffer)
953 if (buffer_size >= *pcchBuffer)
954 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
955 else
956 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
960 assembly_release(assembly);
963 return hr;
966 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
967 IEnumUnknown **ppEnumerator)
969 struct InstalledRuntimeEnum *new_enum;
971 TRACE("%p\n", ppEnumerator);
973 find_runtimes();
975 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
976 if (!new_enum)
977 return E_OUTOFMEMORY;
979 new_enum->Vtbl = &InstalledRuntimeEnum_Vtbl;
980 new_enum->ref = 1;
981 new_enum->pos = 0;
983 *ppEnumerator = (IEnumUnknown*)new_enum;
985 return S_OK;
988 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
989 HANDLE hndProcess, IEnumUnknown **ppEnumerator)
991 FIXME("%p %p\n", hndProcess, ppEnumerator);
993 return E_NOTIMPL;
996 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
997 RuntimeLoadedCallbackFnPtr pCallbackFunction)
999 FIXME("%p\n", pCallbackFunction);
1001 return E_NOTIMPL;
1004 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
1005 REFIID riid, LPVOID *ppUnk)
1007 FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
1009 return E_NOTIMPL;
1012 static HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
1014 FIXME("%i: stub\n", iExitCode);
1016 ExitProcess(iExitCode);
1019 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
1021 CLRMetaHost_QueryInterface,
1022 CLRMetaHost_AddRef,
1023 CLRMetaHost_Release,
1024 CLRMetaHost_GetRuntime,
1025 CLRMetaHost_GetVersionFromFile,
1026 CLRMetaHost_EnumerateInstalledRuntimes,
1027 CLRMetaHost_EnumerateLoadedRuntimes,
1028 CLRMetaHost_RequestRuntimeLoadedNotification,
1029 CLRMetaHost_QueryLegacyV2RuntimeBinding,
1030 CLRMetaHost_ExitProcess
1033 static const struct CLRMetaHost GlobalCLRMetaHost = {
1034 &CLRMetaHost_vtbl
1037 extern HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1039 return ICLRMetaHost_QueryInterface((ICLRMetaHost*)&GlobalCLRMetaHost, riid, ppobj);
1042 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1043 DWORD startup_flags, DWORD runtimeinfo_flags, BOOL legacy, ICLRRuntimeInfo **result)
1045 static const WCHAR dotconfig[] = {'.','c','o','n','f','i','g',0};
1046 static const DWORD supported_startup_flags = 0;
1047 static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1048 int i;
1049 WCHAR local_version[MAX_PATH];
1050 ULONG local_version_size = MAX_PATH;
1051 WCHAR local_config_file[MAX_PATH];
1052 HRESULT hr;
1053 parsed_config_file parsed_config;
1055 if (startup_flags & ~supported_startup_flags)
1056 FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
1058 if (runtimeinfo_flags & ~supported_runtime_flags)
1059 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
1061 if (exefile && !config_file)
1063 strcpyW(local_config_file, exefile);
1064 strcatW(local_config_file, dotconfig);
1066 config_file = local_config_file;
1069 if (config_file)
1071 int found=0;
1072 hr = parse_config_file(config_file, &parsed_config);
1074 if (SUCCEEDED(hr))
1076 supported_runtime *entry;
1077 LIST_FOR_EACH_ENTRY(entry, &parsed_config.supported_runtimes, supported_runtime, entry)
1079 hr = CLRMetaHost_GetRuntime(0, entry->version, &IID_ICLRRuntimeInfo, (void**)result);
1080 if (SUCCEEDED(hr))
1082 found = 1;
1083 break;
1087 else
1089 WARN("failed to parse config file %s, hr=%x\n", debugstr_w(config_file), hr);
1092 free_parsed_config_file(&parsed_config);
1094 if (found)
1095 return S_OK;
1098 if (exefile && !version)
1100 hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1102 version = local_version;
1104 if (FAILED(hr)) return hr;
1107 if (version)
1109 return CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1112 if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1114 find_runtimes();
1116 if (legacy)
1117 i = 2;
1118 else
1119 i = NUM_RUNTIMES;
1121 while (i--)
1123 if (runtimes[i].mono_abi_version)
1124 return IUnknown_QueryInterface((IUnknown*)&runtimes[i],
1125 &IID_ICLRRuntimeInfo, (void**)result);
1128 ERR("No %s.NET runtime installed\n", legacy ? "legacy " : "");
1130 return CLR_E_SHIM_RUNTIME;
1133 return CLR_E_SHIM_RUNTIME;
1136 HRESULT force_get_runtime_info(ICLRRuntimeInfo **result)
1138 return IUnknown_QueryInterface((IUnknown*)&runtimes[0], &IID_ICLRRuntimeInfo, (void**)result);