msvcrt: Use parameter validation macros for mcstowcs_s_l.
[wine] / dlls / 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 #include <stdio.h>
22 #include <stdarg.h>
23 #include <assert.h>
24
25 #define COBJMACROS
26
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"
33
34 #include "corerror.h"
35 #include "mscoree.h"
36 #include "metahost.h"
37 #include "mscoree_private.h"
38
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL( mscoree );
42
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};
46
47 const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl;
48
49 #define NUM_RUNTIMES 3
50
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}
55 };
56
57 static int runtimes_initialized;
58
59 static CRITICAL_SECTION runtime_list_cs;
60 static CRITICAL_SECTION_DEBUG runtime_list_cs_debug =
61 {
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") }
66 };
67 static CRITICAL_SECTION runtime_list_cs = { &runtime_list_cs_debug, -1, 0, 0, 0, 0 };
68
69 static HRESULT load_mono(CLRRuntimeInfo *This, loaded_mono **result)
70 {
71     /* FIXME: stub */
72     *result = NULL;
73
74     return S_OK;
75 }
76
77 static HRESULT CLRRuntimeInfo_GetRuntimeHost(CLRRuntimeInfo *This, RuntimeHost **result)
78 {
79     HRESULT hr = S_OK;
80     loaded_mono *ploaded_mono;
81
82     if (This->loaded_runtime)
83     {
84         *result = This->loaded_runtime;
85         return hr;
86     }
87
88     EnterCriticalSection(&runtime_list_cs);
89
90     hr = load_mono(This, &ploaded_mono);
91
92     if (SUCCEEDED(hr))
93         hr = RuntimeHost_Construct(This, ploaded_mono, &This->loaded_runtime);
94
95     LeaveCriticalSection(&runtime_list_cs);
96
97     if (SUCCEEDED(hr))
98         *result = This->loaded_runtime;
99
100     return hr;
101 }
102
103 void unload_all_runtimes(void)
104 {
105     int i;
106
107     for (i=0; i<NUM_RUNTIMES; i++)
108         if (runtimes[i].loaded_runtime)
109             RuntimeHost_Destroy(runtimes[i].loaded_runtime);
110 }
111
112 static HRESULT WINAPI CLRRuntimeInfo_QueryInterface(ICLRRuntimeInfo* iface,
113         REFIID riid,
114         void **ppvObject)
115 {
116     TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
117
118     if ( IsEqualGUID( riid, &IID_ICLRRuntimeInfo ) ||
119          IsEqualGUID( riid, &IID_IUnknown ) )
120     {
121         *ppvObject = iface;
122     }
123     else
124     {
125         FIXME("Unsupported interface %s\n", debugstr_guid(riid));
126         return E_NOINTERFACE;
127     }
128
129     ICLRRuntimeInfo_AddRef( iface );
130
131     return S_OK;
132 }
133
134 static ULONG WINAPI CLRRuntimeInfo_AddRef(ICLRRuntimeInfo* iface)
135 {
136     return 2;
137 }
138
139 static ULONG WINAPI CLRRuntimeInfo_Release(ICLRRuntimeInfo* iface)
140 {
141     return 1;
142 }
143
144 static HRESULT WINAPI CLRRuntimeInfo_GetVersionString(ICLRRuntimeInfo* iface,
145     LPWSTR pwzBuffer, DWORD *pcchBuffer)
146 {
147     struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
148     DWORD buffer_size = *pcchBuffer;
149     HRESULT hr = S_OK;
150     char version[11];
151     DWORD size;
152
153     TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
154
155     size = snprintf(version, sizeof(version), "v%u.%u.%u", This->major, This->minor, This->build);
156
157     assert(size <= sizeof(version));
158
159     *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
160
161     if (pwzBuffer)
162     {
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);
167     }
168
169     return hr;
170 }
171
172 static BOOL get_install_root(LPWSTR install_dir)
173 {
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};
176
177     DWORD len;
178     HKEY key;
179
180     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
181         return FALSE;
182
183     len = MAX_PATH;
184     if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
185     {
186         RegCloseKey(key);
187         return FALSE;
188     }
189     RegCloseKey(key);
190
191     return TRUE;
192 }
193
194 static HRESULT WINAPI CLRRuntimeInfo_GetRuntimeDirectory(ICLRRuntimeInfo* iface,
195     LPWSTR pwzBuffer, DWORD *pcchBuffer)
196 {
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;
203
204     TRACE("%p %p %p\n", iface, pwzBuffer, pcchBuffer);
205
206     if (!get_install_root(system_dir))
207     {
208         ERR("error reading registry key for installroot\n");
209         return E_FAIL;
210     }
211     else
212     {
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;
218     }
219
220     *pcchBuffer = size;
221
222     if (pwzBuffer)
223     {
224         if (buffer_size >= size)
225             strcpyW(pwzBuffer, system_dir);
226         else
227             hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
228     }
229
230     return hr;
231 }
232
233 static HRESULT WINAPI CLRRuntimeInfo_IsLoaded(ICLRRuntimeInfo* iface,
234     HANDLE hndProcess, BOOL *pbLoaded)
235 {
236     FIXME("%p %p %p\n", iface, hndProcess, pbLoaded);
237
238     return E_NOTIMPL;
239 }
240
241 static HRESULT WINAPI CLRRuntimeInfo_LoadErrorString(ICLRRuntimeInfo* iface,
242     UINT iResourceID, LPWSTR pwzBuffer, DWORD *pcchBuffer, LONG iLocaleid)
243 {
244     FIXME("%p %u %p %p %x\n", iface, iResourceID, pwzBuffer, pcchBuffer, iLocaleid);
245
246     return E_NOTIMPL;
247 }
248
249 static HRESULT WINAPI CLRRuntimeInfo_LoadLibrary(ICLRRuntimeInfo* iface,
250     LPCWSTR pwzDllName, HMODULE *phndModule)
251 {
252     WCHAR version[MAX_PATH];
253     HRESULT hr;
254     DWORD cchBuffer;
255
256     TRACE("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
257
258     cchBuffer = MAX_PATH;
259     hr = ICLRRuntimeInfo_GetVersionString(iface, version, &cchBuffer);
260     if (FAILED(hr)) return hr;
261
262     return LoadLibraryShim(pwzDllName, version, NULL, phndModule);
263 }
264
265 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
266     LPCSTR pszProcName, LPVOID *ppProc)
267 {
268     FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
269
270     return E_NOTIMPL;
271 }
272
273 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
274     REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
275 {
276     struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
277     RuntimeHost *host;
278     HRESULT hr;
279
280     TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
281
282     hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
283
284     if (SUCCEEDED(hr))
285         hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
286
287     return hr;
288 }
289
290 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
291     BOOL *pbLoadable)
292 {
293     FIXME("%p %p\n", iface, pbLoadable);
294
295     return E_NOTIMPL;
296 }
297
298 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
299     DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
300 {
301     FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
302
303     return E_NOTIMPL;
304 }
305
306 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
307     DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
308 {
309     FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
310
311     return E_NOTIMPL;
312 }
313
314 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
315 {
316     FIXME("%p\n", iface);
317
318     return E_NOTIMPL;
319 }
320
321 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
322     BOOL *pbStarted, DWORD *pdwStartupFlags)
323 {
324     FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
325
326     return E_NOTIMPL;
327 }
328
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
345 };
346
347 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version)
348 {
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;
352
353     if (abi_version == 1)
354     {
355         strcpyW(dll_path, path);
356         strcatW(dll_path, mono_dll);
357         attributes = GetFileAttributesW(dll_path);
358
359         if (attributes == INVALID_FILE_ATTRIBUTES)
360         {
361             strcpyW(dll_path, path);
362             strcatW(dll_path, libmono_dll);
363             attributes = GetFileAttributesW(dll_path);
364         }
365     }
366
367     return (attributes != INVALID_FILE_ATTRIBUTES);
368 }
369
370 static BOOL get_mono_path_from_registry(LPWSTR path, int abi_version)
371 {
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};
376
377     WCHAR version[64], version_key[MAX_PATH];
378     DWORD len;
379     HKEY key;
380     WCHAR dll_path[MAX_PATH];
381
382     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
383         return FALSE;
384
385     len = sizeof(version);
386     if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
387     {
388         RegCloseKey(key);
389         return FALSE;
390     }
391     RegCloseKey(key);
392
393     lstrcpyW(version_key, mono_key);
394     lstrcatW(version_key, slash);
395     lstrcatW(version_key, version);
396
397     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
398         return FALSE;
399
400     len = sizeof(WCHAR) * MAX_PATH;
401     if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
402     {
403         RegCloseKey(key);
404         return FALSE;
405     }
406     RegCloseKey(key);
407
408     return find_mono_dll(path, dll_path, abi_version);
409 }
410
411 static BOOL get_mono_path_from_folder(LPCWSTR folder, LPWSTR mono_path, int abi_version)
412 {
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;
416
417     strcpyW(mono_path, folder);
418
419     if (abi_version == 1)
420         strcatW(mono_path, mono_one_dot_zero);
421
422     found = find_mono_dll(mono_path, mono_dll_path, abi_version);
423
424     return found;
425 }
426
427 static BOOL get_mono_path(LPWSTR path, int abi_version)
428 {
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*);
436
437     /* First try c:\windows\mono */
438     GetWindowsDirectoryW(base_path, MAX_PATH);
439     strcatW(base_path, subdir_mono);
440
441     if (get_mono_path_from_folder(base_path, path, abi_version))
442         return TRUE;
443
444     /* Next: /usr/share/wine/mono */
445     unix_data_dir = wine_get_data_dir();
446
447     if (!unix_data_dir)
448     {
449         unix_data_dir = wine_get_build_dir();
450         build_tree = 1;
451     }
452
453     if (unix_data_dir)
454     {
455         if (!wine_get_dos_file_name)
456             wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
457
458         if (wine_get_dos_file_name)
459         {
460             dos_data_dir = wine_get_dos_file_name(unix_data_dir);
461
462             if (dos_data_dir)
463             {
464                 strcpyW(base_path, dos_data_dir);
465                 strcatW(base_path, build_tree ? sibling_mono : subdir_mono);
466
467                 HeapFree(GetProcessHeap(), 0, dos_data_dir);
468
469                 if (get_mono_path_from_folder(base_path, path, abi_version))
470                     return TRUE;
471             }
472         }
473     }
474
475     /* Last: the registry */
476     return get_mono_path_from_registry(path, abi_version);
477 }
478
479 static void find_runtimes(void)
480 {
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;
486
487     if (runtimes_initialized) return;
488
489     EnterCriticalSection(&runtime_list_cs);
490
491     if (runtimes_initialized) goto end;
492
493     for (abi_version=1; abi_version>0; abi_version--)
494     {
495         if (!get_mono_path(mono_path, abi_version))
496             continue;
497
498         for (i=0; i<NUM_RUNTIMES; i++)
499         {
500             if (runtimes[i].mono_abi_version == 0)
501             {
502                 strcpyW(lib_path, mono_path);
503                 strcatW(lib_path, libmono);
504                 strcatW(lib_path, runtimes[i].mono_libdir);
505                 strcatW(lib_path, mscorlib);
506
507                 if (GetFileAttributesW(lib_path) != INVALID_FILE_ATTRIBUTES)
508                 {
509                     runtimes[i].mono_abi_version = abi_version;
510
511                     strcpyW(runtimes[i].mono_path, mono_path);
512                     strcpyW(runtimes[i].mscorlib_path, lib_path);
513
514                     any_runtimes_found = TRUE;
515                 }
516             }
517         }
518     }
519
520     runtimes_initialized = 1;
521
522     if (!any_runtimes_found)
523         MESSAGE("wine: Install the Windows version of Mono to run .NET executables\n");
524
525 end:
526     LeaveCriticalSection(&runtime_list_cs);
527 }
528
529 struct InstalledRuntimeEnum
530 {
531     const struct IEnumUnknownVtbl *Vtbl;
532     LONG ref;
533     ULONG pos;
534 };
535
536 const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
537
538 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface,
539         REFIID riid,
540         void **ppvObject)
541 {
542     TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
543
544     if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
545          IsEqualGUID( riid, &IID_IUnknown ) )
546     {
547         *ppvObject = iface;
548     }
549     else
550     {
551         FIXME("Unsupported interface %s\n", debugstr_guid(riid));
552         return E_NOINTERFACE;
553     }
554
555     IEnumUnknown_AddRef( iface );
556
557     return S_OK;
558 }
559
560 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
561 {
562     struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
563     ULONG ref = InterlockedIncrement(&This->ref);
564
565     TRACE("(%p) refcount=%u\n", iface, ref);
566
567     return ref;
568 }
569
570 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
571 {
572     struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
573     ULONG ref = InterlockedDecrement(&This->ref);
574
575     TRACE("(%p) refcount=%u\n", iface, ref);
576
577     if (ref == 0)
578     {
579         HeapFree(GetProcessHeap(), 0, This);
580     }
581
582     return ref;
583 }
584
585 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
586     IUnknown **rgelt, ULONG *pceltFetched)
587 {
588     struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
589     int num_fetched = 0;
590     HRESULT hr=S_OK;
591     IUnknown *item;
592
593     TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
594
595     while (num_fetched < celt)
596     {
597         if (This->pos >= NUM_RUNTIMES)
598         {
599             hr = S_FALSE;
600             break;
601         }
602         if (runtimes[This->pos].mono_abi_version)
603         {
604             item = (IUnknown*)&runtimes[This->pos];
605             IUnknown_AddRef(item);
606             rgelt[num_fetched] = item;
607             num_fetched++;
608         }
609         This->pos++;
610     }
611
612     if (pceltFetched)
613         *pceltFetched = num_fetched;
614
615     return hr;
616 }
617
618 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
619 {
620     struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
621     int num_fetched = 0;
622     HRESULT hr=S_OK;
623
624     TRACE("(%p,%u)\n", iface, celt);
625
626     while (num_fetched < celt)
627     {
628         if (This->pos >= NUM_RUNTIMES)
629         {
630             hr = S_FALSE;
631             break;
632         }
633         if (runtimes[This->pos].mono_abi_version)
634         {
635             num_fetched++;
636         }
637         This->pos++;
638     }
639
640     return hr;
641 }
642
643 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
644 {
645     struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
646
647     TRACE("(%p)\n", iface);
648
649     This->pos = 0;
650
651     return S_OK;
652 }
653
654 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
655 {
656     struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
657     struct InstalledRuntimeEnum *new_enum;
658
659     TRACE("(%p)\n", iface);
660
661     new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
662     if (!new_enum)
663         return E_OUTOFMEMORY;
664
665     new_enum->Vtbl = &InstalledRuntimeEnum_Vtbl;
666     new_enum->ref = 1;
667     new_enum->pos = This->pos;
668
669     *ppenum = (IEnumUnknown*)new_enum;
670
671     return S_OK;
672 }
673
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
682 };
683
684 struct CLRMetaHost
685 {
686     const struct ICLRMetaHostVtbl *CLRMetaHost_vtbl;
687 };
688
689 static const struct CLRMetaHost GlobalCLRMetaHost;
690
691 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
692         REFIID riid,
693         void **ppvObject)
694 {
695     TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
696
697     if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
698          IsEqualGUID( riid, &IID_IUnknown ) )
699     {
700         *ppvObject = iface;
701     }
702     else
703     {
704         FIXME("Unsupported interface %s\n", debugstr_guid(riid));
705         return E_NOINTERFACE;
706     }
707
708     ICLRMetaHost_AddRef( iface );
709
710     return S_OK;
711 }
712
713 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
714 {
715     return 2;
716 }
717
718 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
719 {
720     return 1;
721 }
722
723 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
724 {
725     *major = 0;
726     *minor = 0;
727     *build = 0;
728
729     if (version[0] == 'v')
730     {
731         version++;
732         if (!isdigit(*version))
733             return FALSE;
734
735         while (isdigit(*version))
736             *major = *major * 10 + (*version++ - '0');
737
738         if (*version == 0)
739             return TRUE;
740
741         if (*version++ != '.' || !isdigit(*version))
742             return FALSE;
743
744         while (isdigit(*version))
745             *minor = *minor * 10 + (*version++ - '0');
746
747         if (*version == 0)
748             return TRUE;
749
750         if (*version++ != '.' || !isdigit(*version))
751             return FALSE;
752
753         while (isdigit(*version))
754             *build = *build * 10 + (*version++ - '0');
755
756         return *version == 0;
757     }
758     else
759         return FALSE;
760 }
761
762 static HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
763     LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
764 {
765     int i;
766     DWORD major, minor, build;
767
768     TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
769
770     if (!pwzVersion)
771         return E_POINTER;
772
773     if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
774     {
775         ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
776         return CLR_E_SHIM_RUNTIME;
777     }
778
779     find_runtimes();
780
781     for (i=0; i<NUM_RUNTIMES; i++)
782     {
783         if (runtimes[i].major == major && runtimes[i].minor == minor &&
784             runtimes[i].build == build)
785         {
786             if (runtimes[i].mono_abi_version)
787                 return IUnknown_QueryInterface((IUnknown*)&runtimes[i], iid, ppRuntime);
788             else
789             {
790                 ERR("Mono is missing %s runtime\n", debugstr_w(pwzVersion));
791                 return CLR_E_SHIM_RUNTIME;
792             }
793         }
794     }
795
796     FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
797     return CLR_E_SHIM_RUNTIME;
798 }
799
800 static HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
801     LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
802 {
803     ASSEMBLY *assembly;
804     HRESULT hr;
805     LPSTR version;
806     ULONG buffer_size=*pcchBuffer;
807
808     TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
809
810     hr = assembly_create(&assembly, pwzFilePath);
811
812     if (SUCCEEDED(hr))
813     {
814         hr = assembly_get_runtime_version(assembly, &version);
815
816         if (SUCCEEDED(hr))
817         {
818             *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
819
820             if (pwzBuffer)
821             {
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);
826             }
827         }
828
829         assembly_release(assembly);
830     }
831
832     return hr;
833 }
834
835 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
836     IEnumUnknown **ppEnumerator)
837 {
838     struct InstalledRuntimeEnum *new_enum;
839
840     TRACE("%p\n", ppEnumerator);
841
842     find_runtimes();
843
844     new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
845     if (!new_enum)
846         return E_OUTOFMEMORY;
847
848     new_enum->Vtbl = &InstalledRuntimeEnum_Vtbl;
849     new_enum->ref = 1;
850     new_enum->pos = 0;
851
852     *ppEnumerator = (IEnumUnknown*)new_enum;
853
854     return S_OK;
855 }
856
857 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
858     HANDLE hndProcess, IEnumUnknown **ppEnumerator)
859 {
860     FIXME("%p %p\n", hndProcess, ppEnumerator);
861
862     return E_NOTIMPL;
863 }
864
865 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
866     RuntimeLoadedCallbackFnPtr pCallbackFunction)
867 {
868     FIXME("%p\n", pCallbackFunction);
869
870     return E_NOTIMPL;
871 }
872
873 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
874     REFIID riid, LPVOID *ppUnk)
875 {
876     FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
877
878     return E_NOTIMPL;
879 }
880
881 static HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
882 {
883     FIXME("%i: stub\n", iExitCode);
884
885     ExitProcess(iExitCode);
886 }
887
888 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
889 {
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
900 };
901
902 static const struct CLRMetaHost GlobalCLRMetaHost = {
903     &CLRMetaHost_vtbl
904 };
905
906 extern HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
907 {
908     return ICLRMetaHost_QueryInterface((ICLRMetaHost*)&GlobalCLRMetaHost, riid, ppobj);
909 }
910
911 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
912     DWORD startup_flags, DWORD runtimeinfo_flags, BOOL legacy, ICLRRuntimeInfo **result)
913 {
914     static const DWORD supported_startup_flags = 0;
915     static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
916     int i;
917
918     if (exefile)
919         FIXME("ignoring exe filename %s\n", debugstr_w(exefile));
920
921     if (config_file)
922         FIXME("ignoring config filename %s\n", debugstr_w(config_file));
923
924     if (startup_flags & ~supported_startup_flags)
925         FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
926
927     if (runtimeinfo_flags & ~supported_runtime_flags)
928         FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
929
930     if (version)
931     {
932         return CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
933     }
934
935     if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
936     {
937         find_runtimes();
938
939         if (legacy)
940             i = 2;
941         else
942             i = NUM_RUNTIMES;
943
944         while (i--)
945         {
946             if (runtimes[i].mono_abi_version)
947                 return IUnknown_QueryInterface((IUnknown*)&runtimes[i],
948                     &IID_ICLRRuntimeInfo, (void**)result);
949         }
950
951         ERR("No %s.NET runtime installed\n", legacy ? "legacy " : "");
952
953         return CLR_E_SHIM_RUNTIME;
954     }
955
956     return CLR_E_SHIM_RUNTIME;
957 }
958
959 HRESULT force_get_runtime_info(ICLRRuntimeInfo **result)
960 {
961     return IUnknown_QueryInterface((IUnknown*)&runtimes[0], &IID_ICLRRuntimeInfo, (void**)result);
962 }