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