mscoree: Load mono when creating a runtime host interface.
[wine/multimedia.git] / dlls / mscoree / metahost.c
blobd781afda92981a877af2c7cde174e9e26e2d8516
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 "mscoree_private.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL( mscoree );
43 static const WCHAR net_11_subdir[] = {'1','.','0',0};
44 static const WCHAR net_20_subdir[] = {'2','.','0',0};
45 static const WCHAR net_40_subdir[] = {'4','.','0',0};
47 const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl;
49 #define NUM_RUNTIMES 3
51 static struct CLRRuntimeInfo runtimes[NUM_RUNTIMES] = {
52 {&CLRRuntimeInfoVtbl, net_11_subdir, 1, 1, 4322, 0},
53 {&CLRRuntimeInfoVtbl, net_20_subdir, 2, 0, 50727, 0},
54 {&CLRRuntimeInfoVtbl, net_40_subdir, 4, 0, 30319, 0}
57 static int runtimes_initialized;
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 #define NUM_ABI_VERSIONS 1
71 static loaded_mono loaded_monos[NUM_ABI_VERSIONS];
73 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version);
75 static void set_environment(LPCWSTR bin_path)
77 WCHAR path_env[MAX_PATH];
78 int len;
80 static const WCHAR pathW[] = {'P','A','T','H',0};
82 /* We have to modify PATH as Mono loads other DLLs from this directory. */
83 GetEnvironmentVariableW(pathW, path_env, sizeof(path_env)/sizeof(WCHAR));
84 len = strlenW(path_env);
85 path_env[len++] = ';';
86 strcpyW(path_env+len, bin_path);
87 SetEnvironmentVariableW(pathW, path_env);
90 static HRESULT load_mono(CLRRuntimeInfo *This, loaded_mono **result)
92 static const WCHAR bin[] = {'\\','b','i','n',0};
93 static const WCHAR lib[] = {'\\','l','i','b',0};
94 static const WCHAR etc[] = {'\\','e','t','c',0};
95 WCHAR mono_dll_path[MAX_PATH+16], mono_bin_path[MAX_PATH+4];
96 WCHAR mono_lib_path[MAX_PATH+4], mono_etc_path[MAX_PATH+4];
97 char mono_lib_path_a[MAX_PATH], mono_etc_path_a[MAX_PATH];
98 int trace_size;
99 char trace_setting[256];
101 if (This->mono_abi_version == -1)
102 MESSAGE("wine: Install the Windows version of Mono to run .NET executables\n");
104 if (This->mono_abi_version <= 0 || This->mono_abi_version > NUM_ABI_VERSIONS)
105 return E_FAIL;
107 *result = &loaded_monos[This->mono_abi_version-1];
109 if (!(*result)->mono_handle)
111 strcpyW(mono_bin_path, This->mono_path);
112 strcatW(mono_bin_path, bin);
113 set_environment(mono_bin_path);
115 strcpyW(mono_lib_path, This->mono_path);
116 strcatW(mono_lib_path, lib);
117 WideCharToMultiByte(CP_UTF8, 0, mono_lib_path, -1, mono_lib_path_a, MAX_PATH, NULL, NULL);
119 strcpyW(mono_etc_path, This->mono_path);
120 strcatW(mono_etc_path, etc);
121 WideCharToMultiByte(CP_UTF8, 0, mono_etc_path, -1, mono_etc_path_a, MAX_PATH, NULL, NULL);
123 if (!find_mono_dll(This->mono_path, mono_dll_path, This->mono_abi_version)) goto fail;
125 if (This->mono_abi_version != 1) goto fail;
127 (*result)->mono_handle = LoadLibraryW(mono_dll_path);
129 if (!(*result)->mono_handle) goto fail;
131 #define LOAD_MONO_FUNCTION(x) do { \
132 (*result)->x = (void*)GetProcAddress((*result)->mono_handle, #x); \
133 if (!(*result)->x) { \
134 goto fail; \
136 } while (0);
138 LOAD_MONO_FUNCTION(mono_config_parse);
139 LOAD_MONO_FUNCTION(mono_domain_assembly_open);
140 LOAD_MONO_FUNCTION(mono_jit_cleanup);
141 LOAD_MONO_FUNCTION(mono_jit_exec);
142 LOAD_MONO_FUNCTION(mono_jit_init);
143 LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
144 LOAD_MONO_FUNCTION(mono_set_dirs);
146 #undef LOAD_MONO_FUNCTION
148 (*result)->mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
150 (*result)->mono_config_parse(NULL);
152 trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
154 if (trace_size)
156 (*result)->mono_jit_set_trace_options(trace_setting);
160 return S_OK;
162 fail:
163 ERR("Could not load Mono into this process\n");
164 FreeLibrary((*result)->mono_handle);
165 (*result)->mono_handle = NULL;
166 return E_FAIL;
169 static HRESULT CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo *This, RuntimeHost **result)
171 HRESULT hr = S_OK;
172 loaded_mono *ploaded_mono;
174 if (This->loaded_runtime)
176 *result = This->loaded_runtime;
177 return hr;
180 EnterCriticalSection(&runtime_list_cs);
182 hr = load_mono(This, &ploaded_mono);
184 if (SUCCEEDED(hr))
185 hr = RuntimeHost_Construct(This, ploaded_mono, &This->loaded_runtime);
187 LeaveCriticalSection(&runtime_list_cs);
189 if (SUCCEEDED(hr))
190 *result = This->loaded_runtime;
192 return hr;
195 void unload_all_runtimes(void)
197 int i;
199 for (i=0; i<NUM_RUNTIMES; i++)
200 if (runtimes[i].loaded_runtime)
201 RuntimeHost_Destroy(runtimes[i].loaded_runtime);
204 static HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
205 REFIID riid,
206 void **ppvObject)
208 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
210 if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
211 IsEqualGUID( riid, &IID_IUnknown ) )
213 *ppvObject = iface;
215 else
217 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
218 return E_NOINTERFACE;
221 ICLRRuntimeInfo_AddRef( iface );
223 return S_OK;
226 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
228 return 2;
231 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
233 return 1;
236 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
237 LPWSTR pwzBuffer, DWORD *pcchBuffer)
239 struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
240 DWORD buffer_size = *pcchBuffer;
241 HRESULT hr = S_OK;
242 char version[11];
243 DWORD size;
245 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
247 size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
249 assert(size <= sizeof(version));
251 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
253 if (pwzBuffer)
255 if (buffer_size >= *pcchBuffer)
256 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
257 else
258 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
261 return hr;
264 static BOOL get_install_root(LPWSTR install_dir)
266 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};
267 const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
269 DWORD len;
270 HKEY key;
272 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
273 return FALSE;
275 len = MAX_PATH;
276 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
278 RegCloseKey(key);
279 return FALSE;
281 RegCloseKey(key);
283 return TRUE;
286 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
287 LPWSTR pwzBuffer, DWORD *pcchBuffer)
289 static const WCHAR slash[] = {'\\',0};
290 DWORD buffer_size = *pcchBuffer;
291 WCHAR system_dir[MAX_PATH];
292 WCHAR version[MAX_PATH];
293 DWORD version_size, size;
294 HRESULT hr = S_OK;
296 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
298 if (!get_install_root(system_dir))
300 ERR("error reading registry key for installroot\n");
301 return E_FAIL;
303 else
305 version_size = MAX_PATH;
306 ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
307 lstrcatW(system_dir, version);
308 lstrcatW(system_dir, slash);
309 size = lstrlenW(system_dir) + 1;
312 *pcchBuffer = size;
314 if (pwzBuffer)
316 if (buffer_size >= size)
317 strcpyW(pwzBuffer, system_dir);
318 else
319 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
322 return hr;
325 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
326 HANDLE hndProcess, BOOL *pbLoaded)
328 FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
330 return E_NOTIMPL;
333 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
334 UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
336 FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
338 return E_NOTIMPL;
341 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
342 LPCWSTR pwzDllName, HMODULE *phndModule)
344 WCHAR version[MAX_PATH];
345 HRESULT hr;
346 DWORD cchBuffer;
348 TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
350 cchBuffer = MAX_PATH;
351 hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
352 if (FAILED(hr)) return hr;
354 return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
357 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
358 LPCSTR pszProcName, LPVOID *ppProc)
360 FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
362 return E_NOTIMPL;
365 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
366 REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
368 struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
369 RuntimeHost *host;
370 HRESULT hr;
372 TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
374 hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
376 if (SUCCEEDED(hr))
377 hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
379 return hr;
382 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
383 BOOL *pbLoadable)
385 FIXME("%p %p\n", iface, pbLoadable);
387 return E_NOTIMPL;
390 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
391 DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
393 FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
395 return E_NOTIMPL;
398 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
399 DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
401 FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
403 return E_NOTIMPL;
406 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
408 FIXME("%p\n", iface);
410 return E_NOTIMPL;
413 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
414 BOOL *pbStarted, DWORD *pdwStartupFlags)
416 FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
418 return E_NOTIMPL;
421 const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
422 CLRRuntimeInfo_QueryInterface,
423 CLRRuntimeInfo_AddRef,
424 CLRRuntimeInfo_Release,
425 CLRRuntimeInfo_GetVersionString,
426 CLRRuntimeInfo_GetRuntimeDirectory,
427 CLRRuntimeInfo_IsLoaded,
428 CLRRuntimeInfo_LoadErrorString,
429 CLRRuntimeInfo_LoadLibrary,
430 CLRRuntimeInfo_GetProcAddress,
431 CLRRuntimeInfo_GetInterface,
432 CLRRuntimeInfo_IsLoadable,
433 CLRRuntimeInfo_SetDefaultStartupFlags,
434 CLRRuntimeInfo_GetDefaultStartupFlags,
435 CLRRuntimeInfo_BindAsLegacyV2Runtime,
436 CLRRuntimeInfo_IsStarted
439 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version)
441 static const WCHAR mono_dll[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
442 static const WCHAR libmono_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
443 DWORD attributes=INVALID_FILE_ATTRIBUTES;
445 if (abi_version == 1)
447 strcpyW(dll_path, path);
448 strcatW(dll_path, mono_dll);
449 attributes = GetFileAttributesW(dll_path);
451 if (attributes == INVALID_FILE_ATTRIBUTES)
453 strcpyW(dll_path, path);
454 strcatW(dll_path, libmono_dll);
455 attributes = GetFileAttributesW(dll_path);
459 return (attributes != INVALID_FILE_ATTRIBUTES);
462 static BOOL get_mono_path_from_registry(LPWSTR path, int abi_version)
464 static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
465 static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
466 static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
467 static const WCHAR slash[] = {'\\',0};
469 WCHAR version[64], version_key[MAX_PATH];
470 DWORD len;
471 HKEY key;
472 WCHAR dll_path[MAX_PATH];
474 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
475 return FALSE;
477 len = sizeof(version);
478 if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
480 RegCloseKey(key);
481 return FALSE;
483 RegCloseKey(key);
485 lstrcpyW(version_key, mono_key);
486 lstrcatW(version_key, slash);
487 lstrcatW(version_key, version);
489 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
490 return FALSE;
492 len = sizeof(WCHAR) * MAX_PATH;
493 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
495 RegCloseKey(key);
496 return FALSE;
498 RegCloseKey(key);
500 return find_mono_dll(path, dll_path, abi_version);
503 static BOOL get_mono_path_from_folder(LPCWSTR folder, LPWSTR mono_path, int abi_version)
505 static const WCHAR mono_one_dot_zero[] = {'\\','m','o','n','o','-','1','.','0', 0};
506 WCHAR mono_dll_path[MAX_PATH];
507 BOOL found = FALSE;
509 strcpyW(mono_path, folder);
511 if (abi_version == 1)
512 strcatW(mono_path, mono_one_dot_zero);
514 found = find_mono_dll(mono_path, mono_dll_path, abi_version);
516 return found;
519 static BOOL get_mono_path(LPWSTR path, int abi_version)
521 static const WCHAR subdir_mono[] = {'\\','m','o','n','o',0};
522 static const WCHAR sibling_mono[] = {'\\','.','.','\\','m','o','n','o',0};
523 WCHAR base_path[MAX_PATH];
524 const char *unix_data_dir;
525 WCHAR *dos_data_dir;
526 int build_tree=0;
527 static WCHAR* (CDECL *wine_get_dos_file_name)(const char*);
529 /* First try c:\windows\mono */
530 GetWindowsDirectoryW(base_path, MAX_PATH);
531 strcatW(base_path, subdir_mono);
533 if (get_mono_path_from_folder(base_path, path, abi_version))
534 return TRUE;
536 /* Next: /usr/share/wine/mono */
537 unix_data_dir = wine_get_data_dir();
539 if (!unix_data_dir)
541 unix_data_dir = wine_get_build_dir();
542 build_tree = 1;
545 if (unix_data_dir)
547 if (!wine_get_dos_file_name)
548 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
550 if (wine_get_dos_file_name)
552 dos_data_dir = wine_get_dos_file_name(unix_data_dir);
554 if (dos_data_dir)
556 strcpyW(base_path, dos_data_dir);
557 strcatW(base_path, build_tree ? sibling_mono : subdir_mono);
559 HeapFree(GetProcessHeap(), 0, dos_data_dir);
561 if (get_mono_path_from_folder(base_path, path, abi_version))
562 return TRUE;
567 /* Last: the registry */
568 return get_mono_path_from_registry(path, abi_version);
571 static void find_runtimes(void)
573 int abi_version, i;
574 static const WCHAR libmono[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
575 static const WCHAR mscorlib[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
576 WCHAR mono_path[MAX_PATH], lib_path[MAX_PATH];
577 BOOL any_runtimes_found = FALSE;
579 if (runtimes_initialized) return;
581 EnterCriticalSection(&runtime_list_cs);
583 if (runtimes_initialized) goto end;
585 for (abi_version=1; abi_version>0; abi_version--)
587 if (!get_mono_path(mono_path, abi_version))
588 continue;
590 for (i=0; i<NUM_RUNTIMES; i++)
592 if (runtimes[i].mono_abi_version == 0)
594 strcpyW(lib_path, mono_path);
595 strcatW(lib_path, libmono);
596 strcatW(lib_path, runtimes[i].mono_libdir);
597 strcatW(lib_path, mscorlib);
599 if (GetFileAttributesW(lib_path) != INVALID_FILE_ATTRIBUTES)
601 runtimes[i].mono_abi_version = abi_version;
603 strcpyW(runtimes[i].mono_path, mono_path);
604 strcpyW(runtimes[i].mscorlib_path, lib_path);
606 any_runtimes_found = TRUE;
612 if (!any_runtimes_found)
614 /* Report all runtimes are available if Mono isn't installed.
615 * FIXME: Remove this when Mono is properly packaged. */
616 for (i=0; i<NUM_RUNTIMES; i++)
617 runtimes[i].mono_abi_version = -1;
620 runtimes_initialized = 1;
622 end:
623 LeaveCriticalSection(&runtime_list_cs);
626 struct InstalledRuntimeEnum
628 const struct IEnumUnknownVtbl *Vtbl;
629 LONG ref;
630 ULONG pos;
633 const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
635 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface,
636 REFIID riid,
637 void **ppvObject)
639 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
641 if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
642 IsEqualGUID( riid, &IID_IUnknown ) )
644 *ppvObject = iface;
646 else
648 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
649 return E_NOINTERFACE;
652 IEnumUnknown_AddRef( iface );
654 return S_OK;
657 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
659 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
660 ULONG ref = InterlockedIncrement(&This->ref);
662 TRACE("(%p) refcount=%u\n", iface, ref);
664 return ref;
667 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
669 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
670 ULONG ref = InterlockedDecrement(&This->ref);
672 TRACE("(%p) refcount=%u\n", iface, ref);
674 if (ref == 0)
676 HeapFree(GetProcessHeap(), 0, This);
679 return ref;
682 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
683 IUnknown **rgelt, ULONG *pceltFetched)
685 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
686 int num_fetched = 0;
687 HRESULT hr=S_OK;
688 IUnknown *item;
690 TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
692 while (num_fetched < celt)
694 if (This->pos >= NUM_RUNTIMES)
696 hr = S_FALSE;
697 break;
699 if (runtimes[This->pos].mono_abi_version)
701 item = (IUnknown*)&runtimes[This->pos];
702 IUnknown_AddRef(item);
703 rgelt[num_fetched] = item;
704 num_fetched++;
706 This->pos++;
709 if (pceltFetched)
710 *pceltFetched = num_fetched;
712 return hr;
715 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
717 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
718 int num_fetched = 0;
719 HRESULT hr=S_OK;
721 TRACE("(%p,%u)\n", iface, celt);
723 while (num_fetched < celt)
725 if (This->pos >= NUM_RUNTIMES)
727 hr = S_FALSE;
728 break;
730 if (runtimes[This->pos].mono_abi_version)
732 num_fetched++;
734 This->pos++;
737 return hr;
740 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
742 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
744 TRACE("(%p)\n", iface);
746 This->pos = 0;
748 return S_OK;
751 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
753 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
754 struct InstalledRuntimeEnum *new_enum;
756 TRACE("(%p)\n", iface);
758 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
759 if (!new_enum)
760 return E_OUTOFMEMORY;
762 new_enum->Vtbl = &InstalledRuntimeEnum_Vtbl;
763 new_enum->ref = 1;
764 new_enum->pos = This->pos;
766 *ppenum = (IEnumUnknown*)new_enum;
768 return S_OK;
771 const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
772 InstalledRuntimeEnum_QueryInterface,
773 InstalledRuntimeEnum_AddRef,
774 InstalledRuntimeEnum_Release,
775 InstalledRuntimeEnum_Next,
776 InstalledRuntimeEnum_Skip,
777 InstalledRuntimeEnum_Reset,
778 InstalledRuntimeEnum_Clone
781 struct CLRMetaHost
783 const struct ICLRMetaHostVtbl *CLRMetaHost_vtbl;
786 static const struct CLRMetaHost GlobalCLRMetaHost;
788 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
789 REFIID riid,
790 void **ppvObject)
792 TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
794 if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
795 IsEqualGUID( riid, &IID_IUnknown ) )
797 *ppvObject = iface;
799 else
801 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
802 return E_NOINTERFACE;
805 ICLRMetaHost_AddRef( iface );
807 return S_OK;
810 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
812 return 2;
815 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
817 return 1;
820 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
822 *major = 0;
823 *minor = 0;
824 *build = 0;
826 if (version[0] == 'v')
828 version++;
829 if (!isdigit(*version))
830 return FALSE;
832 while (isdigit(*version))
833 *major = *major * 10 + (*version++ - '0');
835 if (*version == 0)
836 return TRUE;
838 if (*version++ != '.' || !isdigit(*version))
839 return FALSE;
841 while (isdigit(*version))
842 *minor = *minor * 10 + (*version++ - '0');
844 if (*version == 0)
845 return TRUE;
847 if (*version++ != '.' || !isdigit(*version))
848 return FALSE;
850 while (isdigit(*version))
851 *build = *build * 10 + (*version++ - '0');
853 return *version == 0;
855 else
856 return FALSE;
859 static HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
860 LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
862 int i;
863 DWORD major, minor, build;
865 TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
867 if (!pwzVersion)
868 return E_POINTER;
870 if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
872 ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
873 return CLR_E_SHIM_RUNTIME;
876 find_runtimes();
878 for (i=0; i<NUM_RUNTIMES; i++)
880 if (runtimes[i].major == major && runtimes[i].minor == minor &&
881 runtimes[i].build == build)
883 if (runtimes[i].mono_abi_version)
884 return IUnknown_QueryInterface((IUnknown*)&runtimes[i], iid, ppRuntime);
885 else
887 ERR("Mono is missing %s runtime\n", debugstr_w(pwzVersion));
888 return CLR_E_SHIM_RUNTIME;
893 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
894 return CLR_E_SHIM_RUNTIME;
897 static HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
898 LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
900 ASSEMBLY *assembly;
901 HRESULT hr;
902 LPSTR version;
903 ULONG buffer_size=*pcchBuffer;
905 TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
907 hr = assembly_create(&assembly, pwzFilePath);
909 if (SUCCEEDED(hr))
911 hr = assembly_get_runtime_version(assembly, &version);
913 if (SUCCEEDED(hr))
915 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
917 if (pwzBuffer)
919 if (buffer_size >= *pcchBuffer)
920 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
921 else
922 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
926 assembly_release(assembly);
929 return hr;
932 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
933 IEnumUnknown **ppEnumerator)
935 struct InstalledRuntimeEnum *new_enum;
937 TRACE("%p\n", ppEnumerator);
939 find_runtimes();
941 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
942 if (!new_enum)
943 return E_OUTOFMEMORY;
945 new_enum->Vtbl = &InstalledRuntimeEnum_Vtbl;
946 new_enum->ref = 1;
947 new_enum->pos = 0;
949 *ppEnumerator = (IEnumUnknown*)new_enum;
951 return S_OK;
954 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
955 HANDLE hndProcess, IEnumUnknown **ppEnumerator)
957 FIXME("%p %p\n", hndProcess, ppEnumerator);
959 return E_NOTIMPL;
962 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
963 RuntimeLoadedCallbackFnPtr pCallbackFunction)
965 FIXME("%p\n", pCallbackFunction);
967 return E_NOTIMPL;
970 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
971 REFIID riid, LPVOID *ppUnk)
973 FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
975 return E_NOTIMPL;
978 static HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
980 FIXME("%i: stub\n", iExitCode);
982 ExitProcess(iExitCode);
985 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
987 CLRMetaHost_QueryInterface,
988 CLRMetaHost_AddRef,
989 CLRMetaHost_Release,
990 CLRMetaHost_GetRuntime,
991 CLRMetaHost_GetVersionFromFile,
992 CLRMetaHost_EnumerateInstalledRuntimes,
993 CLRMetaHost_EnumerateLoadedRuntimes,
994 CLRMetaHost_RequestRuntimeLoadedNotification,
995 CLRMetaHost_QueryLegacyV2RuntimeBinding,
996 CLRMetaHost_ExitProcess
999 static const struct CLRMetaHost GlobalCLRMetaHost = {
1000 &CLRMetaHost_vtbl
1003 extern HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
1005 return ICLRMetaHost_QueryInterface((ICLRMetaHost*)&GlobalCLRMetaHost, riid, ppobj);
1008 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
1009 DWORD startup_flags, DWORD runtimeinfo_flags, BOOL legacy, ICLRRuntimeInfo **result)
1011 static const DWORD supported_startup_flags = 0;
1012 static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
1013 int i;
1014 WCHAR local_version[MAX_PATH];
1015 ULONG local_version_size = MAX_PATH;
1016 HRESULT hr;
1018 if (config_file)
1019 FIXME("ignoring config filename %s\n", debugstr_w(config_file));
1021 if (startup_flags & ~supported_startup_flags)
1022 FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
1024 if (runtimeinfo_flags & ~supported_runtime_flags)
1025 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
1027 if (exefile && !version)
1029 hr = CLRMetaHost_GetVersionFromFile(0, exefile, local_version, &local_version_size);
1031 version = local_version;
1033 if (FAILED(hr)) return hr;
1036 if (version)
1038 return CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
1041 if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
1043 find_runtimes();
1045 if (legacy)
1046 i = 2;
1047 else
1048 i = NUM_RUNTIMES;
1050 while (i--)
1052 if (runtimes[i].mono_abi_version)
1053 return IUnknown_QueryInterface((IUnknown*)&runtimes[i],
1054 &IID_ICLRRuntimeInfo, (void**)result);
1057 ERR("No %s.NET runtime installed\n", legacy ? "legacy " : "");
1059 return CLR_E_SHIM_RUNTIME;
1062 return CLR_E_SHIM_RUNTIME;
1065 HRESULT force_get_runtime_info(ICLRRuntimeInfo **result)
1067 return IUnknown_QueryInterface((IUnknown*)&runtimes[0], &IID_ICLRRuntimeInfo, (void**)result);