msvcrt: Use parameter checking macros in vsn(w)printf_s_l.
[wine.git] / dlls / mscoree / metahost.c
blob0f2878834d780c70a1896169586d054e7684d422
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 static HRESULT load_mono(CLRRuntimeInfo *This, loaded_mono **result)
71 /* FIXME: stub */
72 *result = NULL;
74 return S_OK;
77 static HRESULT CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo *This, RuntimeHost **result)
79 HRESULT hr = S_OK;
80 loaded_mono *ploaded_mono;
82 if (This->loaded_runtime)
84 *result = This->loaded_runtime;
85 return hr;
88 EnterCriticalSection(&runtime_list_cs);
90 hr = load_mono(This, &ploaded_mono);
92 if (SUCCEEDED(hr))
93 hr = RuntimeHost_Construct(This, ploaded_mono, &This->loaded_runtime);
95 LeaveCriticalSection(&runtime_list_cs);
97 if (SUCCEEDED(hr))
98 *result = This->loaded_runtime;
100 return hr;
103 void unload_all_runtimes(void)
105 int i;
107 for (i=0; i<NUM_RUNTIMES; i++)
108 if (runtimes[i].loaded_runtime)
109 RuntimeHost_Destroy(runtimes[i].loaded_runtime);
112 static HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
113 REFIID riid,
114 void **ppvObject)
116 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
118 if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
119 IsEqualGUID( riid, &IID_IUnknown ) )
121 *ppvObject = iface;
123 else
125 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
126 return E_NOINTERFACE;
129 ICLRRuntimeInfo_AddRef( iface );
131 return S_OK;
134 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
136 return 2;
139 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
141 return 1;
144 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
145 LPWSTR pwzBuffer, DWORD *pcchBuffer)
147 struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
148 DWORD buffer_size = *pcchBuffer;
149 HRESULT hr = S_OK;
150 char version[11];
151 DWORD size;
153 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
155 size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
157 assert(size <= sizeof(version));
159 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
161 if (pwzBuffer)
163 if (buffer_size >= *pcchBuffer)
164 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
165 else
166 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
169 return hr;
172 static BOOL get_install_root(LPWSTR install_dir)
174 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};
175 const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
177 DWORD len;
178 HKEY key;
180 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
181 return FALSE;
183 len = MAX_PATH;
184 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
186 RegCloseKey(key);
187 return FALSE;
189 RegCloseKey(key);
191 return TRUE;
194 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
195 LPWSTR pwzBuffer, DWORD *pcchBuffer)
197 static const WCHAR slash[] = {'\\',0};
198 DWORD buffer_size = *pcchBuffer;
199 WCHAR system_dir[MAX_PATH];
200 WCHAR version[MAX_PATH];
201 DWORD version_size, size;
202 HRESULT hr = S_OK;
204 TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
206 if (!get_install_root(system_dir))
208 ERR("error reading registry key for installroot\n");
209 return E_FAIL;
211 else
213 version_size = MAX_PATH;
214 ICLRRuntimeInfo_GetVersionString(iface, version, &version_size);
215 lstrcatW(system_dir, version);
216 lstrcatW(system_dir, slash);
217 size = lstrlenW(system_dir) + 1;
220 *pcchBuffer = size;
222 if (pwzBuffer)
224 if (buffer_size >= size)
225 strcpyW(pwzBuffer, system_dir);
226 else
227 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
230 return hr;
233 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
234 HANDLE hndProcess, BOOL *pbLoaded)
236 FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
238 return E_NOTIMPL;
241 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
242 UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
244 FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
246 return E_NOTIMPL;
249 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
250 LPCWSTR pwzDllName, HMODULE *phndModule)
252 WCHAR version[MAX_PATH];
253 HRESULT hr;
254 DWORD cchBuffer;
256 TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
258 cchBuffer = MAX_PATH;
259 hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
260 if (FAILED(hr)) return hr;
262 return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
265 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
266 LPCSTR pszProcName, LPVOID *ppProc)
268 FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
270 return E_NOTIMPL;
273 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
274 REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
276 struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
277 RuntimeHost *host;
278 HRESULT hr;
280 TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
282 hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
284 if (SUCCEEDED(hr))
285 hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
287 return hr;
290 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
291 BOOL *pbLoadable)
293 FIXME("%p %p\n", iface, pbLoadable);
295 return E_NOTIMPL;
298 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
299 DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
301 FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
303 return E_NOTIMPL;
306 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
307 DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
309 FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
311 return E_NOTIMPL;
314 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
316 FIXME("%p\n", iface);
318 return E_NOTIMPL;
321 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
322 BOOL *pbStarted, DWORD *pdwStartupFlags)
324 FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
326 return E_NOTIMPL;
329 const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
330 CLRRuntimeInfo_QueryInterface,
331 CLRRuntimeInfo_AddRef,
332 CLRRuntimeInfo_Release,
333 CLRRuntimeInfo_GetVersionString,
334 CLRRuntimeInfo_GetRuntimeDirectory,
335 CLRRuntimeInfo_IsLoaded,
336 CLRRuntimeInfo_LoadErrorString,
337 CLRRuntimeInfo_LoadLibrary,
338 CLRRuntimeInfo_GetProcAddress,
339 CLRRuntimeInfo_GetInterface,
340 CLRRuntimeInfo_IsLoadable,
341 CLRRuntimeInfo_SetDefaultStartupFlags,
342 CLRRuntimeInfo_GetDefaultStartupFlags,
343 CLRRuntimeInfo_BindAsLegacyV2Runtime,
344 CLRRuntimeInfo_IsStarted
347 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version)
349 static const WCHAR mono_dll[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
350 static const WCHAR libmono_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
351 DWORD attributes=INVALID_FILE_ATTRIBUTES;
353 if (abi_version == 1)
355 strcpyW(dll_path, path);
356 strcatW(dll_path, mono_dll);
357 attributes = GetFileAttributesW(dll_path);
359 if (attributes == INVALID_FILE_ATTRIBUTES)
361 strcpyW(dll_path, path);
362 strcatW(dll_path, libmono_dll);
363 attributes = GetFileAttributesW(dll_path);
367 return (attributes != INVALID_FILE_ATTRIBUTES);
370 static BOOL get_mono_path_from_registry(LPWSTR path, int abi_version)
372 static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
373 static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
374 static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
375 static const WCHAR slash[] = {'\\',0};
377 WCHAR version[64], version_key[MAX_PATH];
378 DWORD len;
379 HKEY key;
380 WCHAR dll_path[MAX_PATH];
382 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
383 return FALSE;
385 len = sizeof(version);
386 if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
388 RegCloseKey(key);
389 return FALSE;
391 RegCloseKey(key);
393 lstrcpyW(version_key, mono_key);
394 lstrcatW(version_key, slash);
395 lstrcatW(version_key, version);
397 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
398 return FALSE;
400 len = sizeof(WCHAR) * MAX_PATH;
401 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
403 RegCloseKey(key);
404 return FALSE;
406 RegCloseKey(key);
408 return find_mono_dll(path, dll_path, abi_version);
411 static BOOL get_mono_path_from_folder(LPCWSTR folder, LPWSTR mono_path, int abi_version)
413 static const WCHAR mono_one_dot_zero[] = {'\\','m','o','n','o','-','1','.','0', 0};
414 WCHAR mono_dll_path[MAX_PATH];
415 BOOL found = FALSE;
417 strcpyW(mono_path, folder);
419 if (abi_version == 1)
420 strcatW(mono_path, mono_one_dot_zero);
422 found = find_mono_dll(mono_path, mono_dll_path, abi_version);
424 return found;
427 static BOOL get_mono_path(LPWSTR path, int abi_version)
429 static const WCHAR subdir_mono[] = {'\\','m','o','n','o',0};
430 static const WCHAR sibling_mono[] = {'\\','.','.','\\','m','o','n','o',0};
431 WCHAR base_path[MAX_PATH];
432 const char *unix_data_dir;
433 WCHAR *dos_data_dir;
434 int build_tree=0;
435 static WCHAR* (CDECL *wine_get_dos_file_name)(const char*);
437 /* First try c:\windows\mono */
438 GetWindowsDirectoryW(base_path, MAX_PATH);
439 strcatW(base_path, subdir_mono);
441 if (get_mono_path_from_folder(base_path, path, abi_version))
442 return TRUE;
444 /* Next: /usr/share/wine/mono */
445 unix_data_dir = wine_get_data_dir();
447 if (!unix_data_dir)
449 unix_data_dir = wine_get_build_dir();
450 build_tree = 1;
453 if (unix_data_dir)
455 if (!wine_get_dos_file_name)
456 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
458 if (wine_get_dos_file_name)
460 dos_data_dir = wine_get_dos_file_name(unix_data_dir);
462 if (dos_data_dir)
464 strcpyW(base_path, dos_data_dir);
465 strcatW(base_path, build_tree ? sibling_mono : subdir_mono);
467 HeapFree(GetProcessHeap(), 0, dos_data_dir);
469 if (get_mono_path_from_folder(base_path, path, abi_version))
470 return TRUE;
475 /* Last: the registry */
476 return get_mono_path_from_registry(path, abi_version);
479 static void find_runtimes(void)
481 int abi_version, i;
482 static const WCHAR libmono[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
483 static const WCHAR mscorlib[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
484 WCHAR mono_path[MAX_PATH], lib_path[MAX_PATH];
485 BOOL any_runtimes_found = FALSE;
487 if (runtimes_initialized) return;
489 EnterCriticalSection(&runtime_list_cs);
491 if (runtimes_initialized) goto end;
493 for (abi_version=1; abi_version>0; abi_version--)
495 if (!get_mono_path(mono_path, abi_version))
496 continue;
498 for (i=0; i<NUM_RUNTIMES; i++)
500 if (runtimes[i].mono_abi_version == 0)
502 strcpyW(lib_path, mono_path);
503 strcatW(lib_path, libmono);
504 strcatW(lib_path, runtimes[i].mono_libdir);
505 strcatW(lib_path, mscorlib);
507 if (GetFileAttributesW(lib_path) != INVALID_FILE_ATTRIBUTES)
509 runtimes[i].mono_abi_version = abi_version;
511 strcpyW(runtimes[i].mono_path, mono_path);
512 strcpyW(runtimes[i].mscorlib_path, lib_path);
514 any_runtimes_found = TRUE;
520 runtimes_initialized = 1;
522 if (!any_runtimes_found)
523 MESSAGE("wine: Install the Windows version of Mono to run .NET executables\n");
525 end:
526 LeaveCriticalSection(&runtime_list_cs);
529 struct InstalledRuntimeEnum
531 const struct IEnumUnknownVtbl *Vtbl;
532 LONG ref;
533 ULONG pos;
536 const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
538 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface,
539 REFIID riid,
540 void **ppvObject)
542 TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
544 if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
545 IsEqualGUID( riid, &IID_IUnknown ) )
547 *ppvObject = iface;
549 else
551 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
552 return E_NOINTERFACE;
555 IEnumUnknown_AddRef( iface );
557 return S_OK;
560 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
562 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
563 ULONG ref = InterlockedIncrement(&This->ref);
565 TRACE("(%p) refcount=%u\n", iface, ref);
567 return ref;
570 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
572 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
573 ULONG ref = InterlockedDecrement(&This->ref);
575 TRACE("(%p) refcount=%u\n", iface, ref);
577 if (ref == 0)
579 HeapFree(GetProcessHeap(), 0, This);
582 return ref;
585 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
586 IUnknown **rgelt, ULONG *pceltFetched)
588 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
589 int num_fetched = 0;
590 HRESULT hr=S_OK;
591 IUnknown *item;
593 TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
595 while (num_fetched < celt)
597 if (This->pos >= NUM_RUNTIMES)
599 hr = S_FALSE;
600 break;
602 if (runtimes[This->pos].mono_abi_version)
604 item = (IUnknown*)&runtimes[This->pos];
605 IUnknown_AddRef(item);
606 rgelt[num_fetched] = item;
607 num_fetched++;
609 This->pos++;
612 if (pceltFetched)
613 *pceltFetched = num_fetched;
615 return hr;
618 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
620 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
621 int num_fetched = 0;
622 HRESULT hr=S_OK;
624 TRACE("(%p,%u)\n", iface, celt);
626 while (num_fetched < celt)
628 if (This->pos >= NUM_RUNTIMES)
630 hr = S_FALSE;
631 break;
633 if (runtimes[This->pos].mono_abi_version)
635 num_fetched++;
637 This->pos++;
640 return hr;
643 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
645 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
647 TRACE("(%p)\n", iface);
649 This->pos = 0;
651 return S_OK;
654 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
656 struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
657 struct InstalledRuntimeEnum *new_enum;
659 TRACE("(%p)\n", iface);
661 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
662 if (!new_enum)
663 return E_OUTOFMEMORY;
665 new_enum->Vtbl = &InstalledRuntimeEnum_Vtbl;
666 new_enum->ref = 1;
667 new_enum->pos = This->pos;
669 *ppenum = (IEnumUnknown*)new_enum;
671 return S_OK;
674 const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
675 InstalledRuntimeEnum_QueryInterface,
676 InstalledRuntimeEnum_AddRef,
677 InstalledRuntimeEnum_Release,
678 InstalledRuntimeEnum_Next,
679 InstalledRuntimeEnum_Skip,
680 InstalledRuntimeEnum_Reset,
681 InstalledRuntimeEnum_Clone
684 struct CLRMetaHost
686 const struct ICLRMetaHostVtbl *CLRMetaHost_vtbl;
689 static const struct CLRMetaHost GlobalCLRMetaHost;
691 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
692 REFIID riid,
693 void **ppvObject)
695 TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
697 if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
698 IsEqualGUID( riid, &IID_IUnknown ) )
700 *ppvObject = iface;
702 else
704 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
705 return E_NOINTERFACE;
708 ICLRMetaHost_AddRef( iface );
710 return S_OK;
713 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
715 return 2;
718 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
720 return 1;
723 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
725 *major = 0;
726 *minor = 0;
727 *build = 0;
729 if (version[0] == 'v')
731 version++;
732 if (!isdigit(*version))
733 return FALSE;
735 while (isdigit(*version))
736 *major = *major * 10 + (*version++ - '0');
738 if (*version == 0)
739 return TRUE;
741 if (*version++ != '.' || !isdigit(*version))
742 return FALSE;
744 while (isdigit(*version))
745 *minor = *minor * 10 + (*version++ - '0');
747 if (*version == 0)
748 return TRUE;
750 if (*version++ != '.' || !isdigit(*version))
751 return FALSE;
753 while (isdigit(*version))
754 *build = *build * 10 + (*version++ - '0');
756 return *version == 0;
758 else
759 return FALSE;
762 static HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
763 LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
765 int i;
766 DWORD major, minor, build;
768 TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
770 if (!pwzVersion)
771 return E_POINTER;
773 if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
775 ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
776 return CLR_E_SHIM_RUNTIME;
779 find_runtimes();
781 for (i=0; i<NUM_RUNTIMES; i++)
783 if (runtimes[i].major == major && runtimes[i].minor == minor &&
784 runtimes[i].build == build)
786 if (runtimes[i].mono_abi_version)
787 return IUnknown_QueryInterface((IUnknown*)&runtimes[i], iid, ppRuntime);
788 else
790 ERR("Mono is missing %s runtime\n", debugstr_w(pwzVersion));
791 return CLR_E_SHIM_RUNTIME;
796 FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
797 return CLR_E_SHIM_RUNTIME;
800 static HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
801 LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
803 ASSEMBLY *assembly;
804 HRESULT hr;
805 LPSTR version;
806 ULONG buffer_size=*pcchBuffer;
808 TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
810 hr = assembly_create(&assembly, pwzFilePath);
812 if (SUCCEEDED(hr))
814 hr = assembly_get_runtime_version(assembly, &version);
816 if (SUCCEEDED(hr))
818 *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
820 if (pwzBuffer)
822 if (buffer_size >= *pcchBuffer)
823 MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
824 else
825 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
829 assembly_release(assembly);
832 return hr;
835 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
836 IEnumUnknown **ppEnumerator)
838 struct InstalledRuntimeEnum *new_enum;
840 TRACE("%p\n", ppEnumerator);
842 find_runtimes();
844 new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
845 if (!new_enum)
846 return E_OUTOFMEMORY;
848 new_enum->Vtbl = &InstalledRuntimeEnum_Vtbl;
849 new_enum->ref = 1;
850 new_enum->pos = 0;
852 *ppEnumerator = (IEnumUnknown*)new_enum;
854 return S_OK;
857 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
858 HANDLE hndProcess, IEnumUnknown **ppEnumerator)
860 FIXME("%p %p\n", hndProcess, ppEnumerator);
862 return E_NOTIMPL;
865 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
866 RuntimeLoadedCallbackFnPtr pCallbackFunction)
868 FIXME("%p\n", pCallbackFunction);
870 return E_NOTIMPL;
873 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
874 REFIID riid, LPVOID *ppUnk)
876 FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
878 return E_NOTIMPL;
881 static HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
883 FIXME("%i: stub\n", iExitCode);
885 ExitProcess(iExitCode);
888 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
890 CLRMetaHost_QueryInterface,
891 CLRMetaHost_AddRef,
892 CLRMetaHost_Release,
893 CLRMetaHost_GetRuntime,
894 CLRMetaHost_GetVersionFromFile,
895 CLRMetaHost_EnumerateInstalledRuntimes,
896 CLRMetaHost_EnumerateLoadedRuntimes,
897 CLRMetaHost_RequestRuntimeLoadedNotification,
898 CLRMetaHost_QueryLegacyV2RuntimeBinding,
899 CLRMetaHost_ExitProcess
902 static const struct CLRMetaHost GlobalCLRMetaHost = {
903 &CLRMetaHost_vtbl
906 extern HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
908 return ICLRMetaHost_QueryInterface((ICLRMetaHost*)&GlobalCLRMetaHost, riid, ppobj);
911 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
912 DWORD startup_flags, DWORD runtimeinfo_flags, BOOL legacy, ICLRRuntimeInfo **result)
914 static const DWORD supported_startup_flags = 0;
915 static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
916 int i;
918 if (exefile)
919 FIXME("ignoring exe filename %s\n", debugstr_w(exefile));
921 if (config_file)
922 FIXME("ignoring config filename %s\n", debugstr_w(config_file));
924 if (startup_flags & ~supported_startup_flags)
925 FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
927 if (runtimeinfo_flags & ~supported_runtime_flags)
928 FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
930 if (version)
932 return CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
935 if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
937 find_runtimes();
939 if (legacy)
940 i = 2;
941 else
942 i = NUM_RUNTIMES;
944 while (i--)
946 if (runtimes[i].mono_abi_version)
947 return IUnknown_QueryInterface((IUnknown*)&runtimes[i],
948 &IID_ICLRRuntimeInfo, (void**)result);
951 ERR("No %s.NET runtime installed\n", legacy ? "legacy " : "");
953 return CLR_E_SHIM_RUNTIME;
956 return CLR_E_SHIM_RUNTIME;
959 HRESULT force_get_runtime_info(ICLRRuntimeInfo **result)
961 return IUnknown_QueryInterface((IUnknown*)&runtimes[0], &IID_ICLRRuntimeInfo, (void**)result);