wined3d: IWineD3DBuffer_Unmap() can't fail.
[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     FIXME("%p %s %p\n", iface, debugstr_w(pwzDllName), phndModule);
253
254     return E_NOTIMPL;
255 }
256
257 static HRESULT WINAPI CLRRuntimeInfo_GetProcAddress(ICLRRuntimeInfo* iface,
258     LPCSTR pszProcName, LPVOID *ppProc)
259 {
260     FIXME("%p %s %p\n", iface, debugstr_a(pszProcName), ppProc);
261
262     return E_NOTIMPL;
263 }
264
265 static HRESULT WINAPI CLRRuntimeInfo_GetInterface(ICLRRuntimeInfo* iface,
266     REFCLSID rclsid, REFIID riid, LPVOID *ppUnk)
267 {
268     struct CLRRuntimeInfo *This = (struct CLRRuntimeInfo*)iface;
269     RuntimeHost *host;
270     HRESULT hr;
271
272     TRACE("%p %s %s %p\n", iface, debugstr_guid(rclsid), debugstr_guid(riid), ppUnk);
273
274     hr = CLRRuntimeInfo_GetRuntimeHost(This, &host);
275
276     if (SUCCEEDED(hr))
277         hr = RuntimeHost_GetInterface(host, rclsid, riid, ppUnk);
278
279     return hr;
280 }
281
282 static HRESULT WINAPI CLRRuntimeInfo_IsLoadable(ICLRRuntimeInfo* iface,
283     BOOL *pbLoadable)
284 {
285     FIXME("%p %p\n", iface, pbLoadable);
286
287     return E_NOTIMPL;
288 }
289
290 static HRESULT WINAPI CLRRuntimeInfo_SetDefaultStartupFlags(ICLRRuntimeInfo* iface,
291     DWORD dwStartupFlags, LPCWSTR pwzHostConfigFile)
292 {
293     FIXME("%p %x %s\n", iface, dwStartupFlags, debugstr_w(pwzHostConfigFile));
294
295     return E_NOTIMPL;
296 }
297
298 static HRESULT WINAPI CLRRuntimeInfo_GetDefaultStartupFlags(ICLRRuntimeInfo* iface,
299     DWORD *pdwStartupFlags, LPWSTR pwzHostConfigFile, DWORD *pcchHostConfigFile)
300 {
301     FIXME("%p %p %p %p\n", iface, pdwStartupFlags, pwzHostConfigFile, pcchHostConfigFile);
302
303     return E_NOTIMPL;
304 }
305
306 static HRESULT WINAPI CLRRuntimeInfo_BindAsLegacyV2Runtime(ICLRRuntimeInfo* iface)
307 {
308     FIXME("%p\n", iface);
309
310     return E_NOTIMPL;
311 }
312
313 static HRESULT WINAPI CLRRuntimeInfo_IsStarted(ICLRRuntimeInfo* iface,
314     BOOL *pbStarted, DWORD *pdwStartupFlags)
315 {
316     FIXME("%p %p %p\n", iface, pbStarted, pdwStartupFlags);
317
318     return E_NOTIMPL;
319 }
320
321 const struct ICLRRuntimeInfoVtbl CLRRuntimeInfoVtbl = {
322     CLRRuntimeInfo_QueryInterface,
323     CLRRuntimeInfo_AddRef,
324     CLRRuntimeInfo_Release,
325     CLRRuntimeInfo_GetVersionString,
326     CLRRuntimeInfo_GetRuntimeDirectory,
327     CLRRuntimeInfo_IsLoaded,
328     CLRRuntimeInfo_LoadErrorString,
329     CLRRuntimeInfo_LoadLibrary,
330     CLRRuntimeInfo_GetProcAddress,
331     CLRRuntimeInfo_GetInterface,
332     CLRRuntimeInfo_IsLoadable,
333     CLRRuntimeInfo_SetDefaultStartupFlags,
334     CLRRuntimeInfo_GetDefaultStartupFlags,
335     CLRRuntimeInfo_BindAsLegacyV2Runtime,
336     CLRRuntimeInfo_IsStarted
337 };
338
339 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int abi_version)
340 {
341     static const WCHAR mono_dll[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
342     static const WCHAR libmono_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
343     DWORD attributes=INVALID_FILE_ATTRIBUTES;
344
345     if (abi_version == 1)
346     {
347         strcpyW(dll_path, path);
348         strcatW(dll_path, mono_dll);
349         attributes = GetFileAttributesW(dll_path);
350
351         if (attributes == INVALID_FILE_ATTRIBUTES)
352         {
353             strcpyW(dll_path, path);
354             strcatW(dll_path, libmono_dll);
355             attributes = GetFileAttributesW(dll_path);
356         }
357     }
358
359     return (attributes != INVALID_FILE_ATTRIBUTES);
360 }
361
362 static BOOL get_mono_path_from_registry(LPWSTR path, int abi_version)
363 {
364     static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
365     static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
366     static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
367     static const WCHAR slash[] = {'\\',0};
368
369     WCHAR version[64], version_key[MAX_PATH];
370     DWORD len;
371     HKEY key;
372     WCHAR dll_path[MAX_PATH];
373
374     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
375         return FALSE;
376
377     len = sizeof(version);
378     if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
379     {
380         RegCloseKey(key);
381         return FALSE;
382     }
383     RegCloseKey(key);
384
385     lstrcpyW(version_key, mono_key);
386     lstrcatW(version_key, slash);
387     lstrcatW(version_key, version);
388
389     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
390         return FALSE;
391
392     len = sizeof(WCHAR) * MAX_PATH;
393     if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
394     {
395         RegCloseKey(key);
396         return FALSE;
397     }
398     RegCloseKey(key);
399
400     return find_mono_dll(path, dll_path, abi_version);
401 }
402
403 static BOOL get_mono_path_from_folder(LPCWSTR folder, LPWSTR mono_path, int abi_version)
404 {
405     static const WCHAR mono_one_dot_zero[] = {'\\','m','o','n','o','-','1','.','0', 0};
406     WCHAR mono_dll_path[MAX_PATH];
407     BOOL found = FALSE;
408
409     strcpyW(mono_path, folder);
410
411     if (abi_version == 1)
412         strcatW(mono_path, mono_one_dot_zero);
413
414     found = find_mono_dll(mono_path, mono_dll_path, abi_version);
415
416     return found;
417 }
418
419 static BOOL get_mono_path(LPWSTR path, int abi_version)
420 {
421     static const WCHAR subdir_mono[] = {'\\','m','o','n','o',0};
422     static const WCHAR sibling_mono[] = {'\\','.','.','\\','m','o','n','o',0};
423     WCHAR base_path[MAX_PATH];
424     const char *unix_data_dir;
425     WCHAR *dos_data_dir;
426     int build_tree=0;
427     static WCHAR* (CDECL *wine_get_dos_file_name)(const char*);
428
429     /* First try c:\windows\mono */
430     GetWindowsDirectoryW(base_path, MAX_PATH);
431     strcatW(base_path, subdir_mono);
432
433     if (get_mono_path_from_folder(base_path, path, abi_version))
434         return TRUE;
435
436     /* Next: /usr/share/wine/mono */
437     unix_data_dir = wine_get_data_dir();
438
439     if (!unix_data_dir)
440     {
441         unix_data_dir = wine_get_build_dir();
442         build_tree = 1;
443     }
444
445     if (unix_data_dir)
446     {
447         if (!wine_get_dos_file_name)
448             wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
449
450         if (wine_get_dos_file_name)
451         {
452             dos_data_dir = wine_get_dos_file_name(unix_data_dir);
453
454             if (dos_data_dir)
455             {
456                 strcpyW(base_path, dos_data_dir);
457                 strcatW(base_path, build_tree ? sibling_mono : subdir_mono);
458
459                 HeapFree(GetProcessHeap(), 0, dos_data_dir);
460
461                 if (get_mono_path_from_folder(base_path, path, abi_version))
462                     return TRUE;
463             }
464         }
465     }
466
467     /* Last: the registry */
468     return get_mono_path_from_registry(path, abi_version);
469 }
470
471 static void find_runtimes(void)
472 {
473     int abi_version, i;
474     static const WCHAR libmono[] = {'\\','l','i','b','\\','m','o','n','o','\\',0};
475     static const WCHAR mscorlib[] = {'\\','m','s','c','o','r','l','i','b','.','d','l','l',0};
476     WCHAR mono_path[MAX_PATH], lib_path[MAX_PATH];
477     BOOL any_runtimes_found = FALSE;
478
479     if (runtimes_initialized) return;
480
481     EnterCriticalSection(&runtime_list_cs);
482
483     if (runtimes_initialized) goto end;
484
485     for (abi_version=1; abi_version>0; abi_version--)
486     {
487         if (!get_mono_path(mono_path, abi_version))
488             continue;
489
490         for (i=0; i<NUM_RUNTIMES; i++)
491         {
492             if (runtimes[i].mono_abi_version == 0)
493             {
494                 strcpyW(lib_path, mono_path);
495                 strcatW(lib_path, libmono);
496                 strcatW(lib_path, runtimes[i].mono_libdir);
497                 strcatW(lib_path, mscorlib);
498
499                 if (GetFileAttributesW(lib_path) != INVALID_FILE_ATTRIBUTES)
500                 {
501                     runtimes[i].mono_abi_version = abi_version;
502
503                     strcpyW(runtimes[i].mono_path, mono_path);
504                     strcpyW(runtimes[i].mscorlib_path, lib_path);
505
506                     any_runtimes_found = TRUE;
507                 }
508             }
509         }
510     }
511
512     runtimes_initialized = 1;
513
514     if (!any_runtimes_found)
515         MESSAGE("wine: Install the Windows version of Mono to run .NET executables\n");
516
517 end:
518     LeaveCriticalSection(&runtime_list_cs);
519 }
520
521 struct InstalledRuntimeEnum
522 {
523     const struct IEnumUnknownVtbl *Vtbl;
524     LONG ref;
525     ULONG pos;
526 };
527
528 const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl;
529
530 static HRESULT WINAPI InstalledRuntimeEnum_QueryInterface(IEnumUnknown* iface,
531         REFIID riid,
532         void **ppvObject)
533 {
534     TRACE("%p %s %p\n", iface, debugstr_guid(riid), ppvObject);
535
536     if ( IsEqualGUID( riid, &IID_IEnumUnknown ) ||
537          IsEqualGUID( riid, &IID_IUnknown ) )
538     {
539         *ppvObject = iface;
540     }
541     else
542     {
543         FIXME("Unsupported interface %s\n", debugstr_guid(riid));
544         return E_NOINTERFACE;
545     }
546
547     IEnumUnknown_AddRef( iface );
548
549     return S_OK;
550 }
551
552 static ULONG WINAPI InstalledRuntimeEnum_AddRef(IEnumUnknown* iface)
553 {
554     struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
555     ULONG ref = InterlockedIncrement(&This->ref);
556
557     TRACE("(%p) refcount=%u\n", iface, ref);
558
559     return ref;
560 }
561
562 static ULONG WINAPI InstalledRuntimeEnum_Release(IEnumUnknown* iface)
563 {
564     struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
565     ULONG ref = InterlockedDecrement(&This->ref);
566
567     TRACE("(%p) refcount=%u\n", iface, ref);
568
569     if (ref == 0)
570     {
571         HeapFree(GetProcessHeap(), 0, This);
572     }
573
574     return ref;
575 }
576
577 static HRESULT WINAPI InstalledRuntimeEnum_Next(IEnumUnknown *iface, ULONG celt,
578     IUnknown **rgelt, ULONG *pceltFetched)
579 {
580     struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
581     int num_fetched = 0;
582     HRESULT hr=S_OK;
583     IUnknown *item;
584
585     TRACE("(%p,%u,%p,%p)\n", iface, celt, rgelt, pceltFetched);
586
587     while (num_fetched < celt)
588     {
589         if (This->pos >= NUM_RUNTIMES)
590         {
591             hr = S_FALSE;
592             break;
593         }
594         if (runtimes[This->pos].mono_abi_version)
595         {
596             item = (IUnknown*)&runtimes[This->pos];
597             IUnknown_AddRef(item);
598             rgelt[num_fetched] = item;
599             num_fetched++;
600         }
601         This->pos++;
602     }
603
604     if (pceltFetched)
605         *pceltFetched = num_fetched;
606
607     return hr;
608 }
609
610 static HRESULT WINAPI InstalledRuntimeEnum_Skip(IEnumUnknown *iface, ULONG celt)
611 {
612     struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
613     int num_fetched = 0;
614     HRESULT hr=S_OK;
615
616     TRACE("(%p,%u)\n", iface, celt);
617
618     while (num_fetched < celt)
619     {
620         if (This->pos >= NUM_RUNTIMES)
621         {
622             hr = S_FALSE;
623             break;
624         }
625         if (runtimes[This->pos].mono_abi_version)
626         {
627             num_fetched++;
628         }
629         This->pos++;
630     }
631
632     return hr;
633 }
634
635 static HRESULT WINAPI InstalledRuntimeEnum_Reset(IEnumUnknown *iface)
636 {
637     struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
638
639     TRACE("(%p)\n", iface);
640
641     This->pos = 0;
642
643     return S_OK;
644 }
645
646 static HRESULT WINAPI InstalledRuntimeEnum_Clone(IEnumUnknown *iface, IEnumUnknown **ppenum)
647 {
648     struct InstalledRuntimeEnum *This = (struct InstalledRuntimeEnum*)iface;
649     struct InstalledRuntimeEnum *new_enum;
650
651     TRACE("(%p)\n", iface);
652
653     new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
654     if (!new_enum)
655         return E_OUTOFMEMORY;
656
657     new_enum->Vtbl = &InstalledRuntimeEnum_Vtbl;
658     new_enum->ref = 1;
659     new_enum->pos = This->pos;
660
661     *ppenum = (IEnumUnknown*)new_enum;
662
663     return S_OK;
664 }
665
666 const struct IEnumUnknownVtbl InstalledRuntimeEnum_Vtbl = {
667     InstalledRuntimeEnum_QueryInterface,
668     InstalledRuntimeEnum_AddRef,
669     InstalledRuntimeEnum_Release,
670     InstalledRuntimeEnum_Next,
671     InstalledRuntimeEnum_Skip,
672     InstalledRuntimeEnum_Reset,
673     InstalledRuntimeEnum_Clone
674 };
675
676 struct CLRMetaHost
677 {
678     const struct ICLRMetaHostVtbl *CLRMetaHost_vtbl;
679 };
680
681 static const struct CLRMetaHost GlobalCLRMetaHost;
682
683 static HRESULT WINAPI CLRMetaHost_QueryInterface(ICLRMetaHost* iface,
684         REFIID riid,
685         void **ppvObject)
686 {
687     TRACE("%s %p\n", debugstr_guid(riid), ppvObject);
688
689     if ( IsEqualGUID( riid, &IID_ICLRMetaHost ) ||
690          IsEqualGUID( riid, &IID_IUnknown ) )
691     {
692         *ppvObject = iface;
693     }
694     else
695     {
696         FIXME("Unsupported interface %s\n", debugstr_guid(riid));
697         return E_NOINTERFACE;
698     }
699
700     ICLRMetaHost_AddRef( iface );
701
702     return S_OK;
703 }
704
705 static ULONG WINAPI CLRMetaHost_AddRef(ICLRMetaHost* iface)
706 {
707     return 2;
708 }
709
710 static ULONG WINAPI CLRMetaHost_Release(ICLRMetaHost* iface)
711 {
712     return 1;
713 }
714
715 static BOOL parse_runtime_version(LPCWSTR version, DWORD *major, DWORD *minor, DWORD *build)
716 {
717     *major = 0;
718     *minor = 0;
719     *build = 0;
720
721     if (version[0] == 'v')
722     {
723         version++;
724         if (!isdigit(*version))
725             return FALSE;
726
727         while (isdigit(*version))
728             *major = *major * 10 + (*version++ - '0');
729
730         if (*version == 0)
731             return TRUE;
732
733         if (*version++ != '.' || !isdigit(*version))
734             return FALSE;
735
736         while (isdigit(*version))
737             *minor = *minor * 10 + (*version++ - '0');
738
739         if (*version == 0)
740             return TRUE;
741
742         if (*version++ != '.' || !isdigit(*version))
743             return FALSE;
744
745         while (isdigit(*version))
746             *build = *build * 10 + (*version++ - '0');
747
748         return *version == 0;
749     }
750     else
751         return FALSE;
752 }
753
754 static HRESULT WINAPI CLRMetaHost_GetRuntime(ICLRMetaHost* iface,
755     LPCWSTR pwzVersion, REFIID iid, LPVOID *ppRuntime)
756 {
757     int i;
758     DWORD major, minor, build;
759
760     TRACE("%s %s %p\n", debugstr_w(pwzVersion), debugstr_guid(iid), ppRuntime);
761
762     if (!pwzVersion)
763         return E_POINTER;
764
765     if (!parse_runtime_version(pwzVersion, &major, &minor, &build))
766     {
767         ERR("Cannot parse %s\n", debugstr_w(pwzVersion));
768         return CLR_E_SHIM_RUNTIME;
769     }
770
771     find_runtimes();
772
773     for (i=0; i<NUM_RUNTIMES; i++)
774     {
775         if (runtimes[i].major == major && runtimes[i].minor == minor &&
776             runtimes[i].build == build)
777         {
778             if (runtimes[i].mono_abi_version)
779                 return IUnknown_QueryInterface((IUnknown*)&runtimes[i], iid, ppRuntime);
780             else
781             {
782                 ERR("Mono is missing %s runtime\n", debugstr_w(pwzVersion));
783                 return CLR_E_SHIM_RUNTIME;
784             }
785         }
786     }
787
788     FIXME("Unrecognized version %s\n", debugstr_w(pwzVersion));
789     return CLR_E_SHIM_RUNTIME;
790 }
791
792 static HRESULT WINAPI CLRMetaHost_GetVersionFromFile(ICLRMetaHost* iface,
793     LPCWSTR pwzFilePath, LPWSTR pwzBuffer, DWORD *pcchBuffer)
794 {
795     ASSEMBLY *assembly;
796     HRESULT hr;
797     LPSTR version;
798     ULONG buffer_size=*pcchBuffer;
799
800     TRACE("%s %p %p\n", debugstr_w(pwzFilePath), pwzBuffer, pcchBuffer);
801
802     hr = assembly_create(&assembly, pwzFilePath);
803
804     if (SUCCEEDED(hr))
805     {
806         hr = assembly_get_runtime_version(assembly, &version);
807
808         if (SUCCEEDED(hr))
809         {
810             *pcchBuffer = MultiByteToWideChar(CP_UTF8, 0, version, -1, NULL, 0);
811
812             if (pwzBuffer)
813             {
814                 if (buffer_size >= *pcchBuffer)
815                     MultiByteToWideChar(CP_UTF8, 0, version, -1, pwzBuffer, buffer_size);
816                 else
817                     hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
818             }
819         }
820
821         assembly_release(assembly);
822     }
823
824     return hr;
825 }
826
827 static HRESULT WINAPI CLRMetaHost_EnumerateInstalledRuntimes(ICLRMetaHost* iface,
828     IEnumUnknown **ppEnumerator)
829 {
830     struct InstalledRuntimeEnum *new_enum;
831
832     TRACE("%p\n", ppEnumerator);
833
834     find_runtimes();
835
836     new_enum = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_enum));
837     if (!new_enum)
838         return E_OUTOFMEMORY;
839
840     new_enum->Vtbl = &InstalledRuntimeEnum_Vtbl;
841     new_enum->ref = 1;
842     new_enum->pos = 0;
843
844     *ppEnumerator = (IEnumUnknown*)new_enum;
845
846     return S_OK;
847 }
848
849 static HRESULT WINAPI CLRMetaHost_EnumerateLoadedRuntimes(ICLRMetaHost* iface,
850     HANDLE hndProcess, IEnumUnknown **ppEnumerator)
851 {
852     FIXME("%p %p\n", hndProcess, ppEnumerator);
853
854     return E_NOTIMPL;
855 }
856
857 static HRESULT WINAPI CLRMetaHost_RequestRuntimeLoadedNotification(ICLRMetaHost* iface,
858     RuntimeLoadedCallbackFnPtr pCallbackFunction)
859 {
860     FIXME("%p\n", pCallbackFunction);
861
862     return E_NOTIMPL;
863 }
864
865 static HRESULT WINAPI CLRMetaHost_QueryLegacyV2RuntimeBinding(ICLRMetaHost* iface,
866     REFIID riid, LPVOID *ppUnk)
867 {
868     FIXME("%s %p\n", debugstr_guid(riid), ppUnk);
869
870     return E_NOTIMPL;
871 }
872
873 static HRESULT WINAPI CLRMetaHost_ExitProcess(ICLRMetaHost* iface, INT32 iExitCode)
874 {
875     FIXME("%i: stub\n", iExitCode);
876
877     ExitProcess(iExitCode);
878 }
879
880 static const struct ICLRMetaHostVtbl CLRMetaHost_vtbl =
881 {
882     CLRMetaHost_QueryInterface,
883     CLRMetaHost_AddRef,
884     CLRMetaHost_Release,
885     CLRMetaHost_GetRuntime,
886     CLRMetaHost_GetVersionFromFile,
887     CLRMetaHost_EnumerateInstalledRuntimes,
888     CLRMetaHost_EnumerateLoadedRuntimes,
889     CLRMetaHost_RequestRuntimeLoadedNotification,
890     CLRMetaHost_QueryLegacyV2RuntimeBinding,
891     CLRMetaHost_ExitProcess
892 };
893
894 static const struct CLRMetaHost GlobalCLRMetaHost = {
895     &CLRMetaHost_vtbl
896 };
897
898 extern HRESULT CLRMetaHost_CreateInstance(REFIID riid, void **ppobj)
899 {
900     return ICLRMetaHost_QueryInterface((ICLRMetaHost*)&GlobalCLRMetaHost, riid, ppobj);
901 }
902
903 HRESULT get_runtime_info(LPCWSTR exefile, LPCWSTR version, LPCWSTR config_file,
904     DWORD startup_flags, DWORD runtimeinfo_flags, BOOL legacy, ICLRRuntimeInfo **result)
905 {
906     static const DWORD supported_startup_flags = 0;
907     static const DWORD supported_runtime_flags = RUNTIME_INFO_UPGRADE_VERSION;
908     int i;
909
910     if (exefile)
911         FIXME("ignoring exe filename %s\n", debugstr_w(exefile));
912
913     if (config_file)
914         FIXME("ignoring config filename %s\n", debugstr_w(config_file));
915
916     if (startup_flags & ~supported_startup_flags)
917         FIXME("unsupported startup flags %x\n", startup_flags & ~supported_startup_flags);
918
919     if (runtimeinfo_flags & ~supported_runtime_flags)
920         FIXME("unsupported runtimeinfo flags %x\n", runtimeinfo_flags & ~supported_runtime_flags);
921
922     if (version)
923     {
924         return CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)result);
925     }
926
927     if (runtimeinfo_flags & RUNTIME_INFO_UPGRADE_VERSION)
928     {
929         find_runtimes();
930
931         if (legacy)
932             i = 2;
933         else
934             i = NUM_RUNTIMES;
935
936         while (i--)
937         {
938             if (runtimes[i].mono_abi_version)
939                 return IUnknown_QueryInterface((IUnknown*)&runtimes[i],
940                     &IID_ICLRRuntimeInfo, (void**)result);
941         }
942
943         ERR("No %s.NET runtime installed\n", legacy ? "legacy " : "");
944
945         return CLR_E_SHIM_RUNTIME;
946     }
947
948     return CLR_E_SHIM_RUNTIME;
949 }