2 * Implementation of mscoree.dll
3 * Microsoft Component Object Runtime Execution Engine
5 * Copyright 2006 Paul Chitescu
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/unicode.h"
26 #include "wine/library.h"
41 #include "mscoree_private.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL( mscoree );
47 static BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int* abi_version)
49 static const WCHAR mono_dll[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
50 static const WCHAR libmono_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
53 strcpyW(dll_path, path);
54 strcatW(dll_path, mono_dll);
55 attributes = GetFileAttributesW(dll_path);
57 if (attributes == INVALID_FILE_ATTRIBUTES)
59 strcpyW(dll_path, path);
60 strcatW(dll_path, libmono_dll);
61 attributes = GetFileAttributesW(dll_path);
64 if (attributes != INVALID_FILE_ATTRIBUTES)
66 /* FIXME: Test for appropriate architecture. */
74 static BOOL get_mono_path_from_registry(LPWSTR path)
76 static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
77 static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
78 static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
79 static const WCHAR slash[] = {'\\',0};
81 WCHAR version[64], version_key[MAX_PATH];
84 WCHAR dll_path[MAX_PATH];
87 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
90 len = sizeof(version);
91 if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
98 lstrcpyW(version_key, mono_key);
99 lstrcatW(version_key, slash);
100 lstrcatW(version_key, version);
102 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
105 len = sizeof(WCHAR) * MAX_PATH;
106 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
113 return find_mono_dll(path, dll_path, &abi_version);
116 static BOOL get_mono_path_from_folder(LPCWSTR folder, LPWSTR mono_path)
118 static const WCHAR mono_one_dot_zero[] = {'\\','m','o','n','o','-','1','.','0', 0};
119 WCHAR mono_dll_path[MAX_PATH];
123 strcpyW(mono_path, folder);
124 strcatW(mono_path, mono_one_dot_zero);
126 found = find_mono_dll(mono_path, mono_dll_path, &abi_version);
128 if (found && abi_version != 1)
130 ERR("found wrong ABI in %s\n", debugstr_w(mono_path));
137 static BOOL get_mono_path(LPWSTR path)
139 static const WCHAR subdir_mono[] = {'\\','m','o','n','o',0};
140 static const WCHAR sibling_mono[] = {'\\','.','.','\\','m','o','n','o',0};
141 WCHAR base_path[MAX_PATH];
142 const char *unix_data_dir;
145 static WCHAR* (CDECL *wine_get_dos_file_name)(const char*);
147 /* First try c:\windows\mono */
148 GetWindowsDirectoryW(base_path, MAX_PATH);
149 strcatW(base_path, subdir_mono);
151 if (get_mono_path_from_folder(base_path, path))
154 /* Next: /usr/share/wine/mono */
155 unix_data_dir = wine_get_data_dir();
159 unix_data_dir = wine_get_build_dir();
165 if (!wine_get_dos_file_name)
166 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
168 if (wine_get_dos_file_name)
170 dos_data_dir = wine_get_dos_file_name(unix_data_dir);
174 strcpyW(base_path, dos_data_dir);
175 strcatW(base_path, build_tree ? sibling_mono : subdir_mono);
177 HeapFree(GetProcessHeap(), 0, dos_data_dir);
179 if (get_mono_path_from_folder(base_path, path))
185 /* Last: the registry */
186 return get_mono_path_from_registry(path);
189 static BOOL get_install_root(LPWSTR install_dir)
191 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};
192 const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
197 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
201 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
211 static CRITICAL_SECTION mono_lib_cs;
212 static CRITICAL_SECTION_DEBUG mono_lib_cs_debug =
215 { &mono_lib_cs_debug.ProcessLocksList,
216 &mono_lib_cs_debug.ProcessLocksList },
217 0, 0, { (DWORD_PTR)(__FILE__ ": mono_lib_cs") }
219 static CRITICAL_SECTION mono_lib_cs = { &mono_lib_cs_debug, -1, 0, 0, 0, 0 };
223 void (*mono_config_parse)(const char *filename);
224 MonoAssembly* (*mono_domain_assembly_open) (MonoDomain *domain, const char *name);
225 void (*mono_jit_cleanup)(MonoDomain *domain);
226 int (*mono_jit_exec)(MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[]);
227 MonoDomain* (*mono_jit_init)(const char *file);
228 int (*mono_jit_set_trace_options)(const char* options);
229 void (*mono_set_dirs)(const char *assembly_dir, const char *config_dir);
231 static void set_environment(LPCWSTR bin_path)
233 WCHAR path_env[MAX_PATH];
236 static const WCHAR pathW[] = {'P','A','T','H',0};
238 /* We have to modify PATH as Mono loads other DLLs from this directory. */
239 GetEnvironmentVariableW(pathW, path_env, sizeof(path_env)/sizeof(WCHAR));
240 len = strlenW(path_env);
241 path_env[len++] = ';';
242 strcpyW(path_env+len, bin_path);
243 SetEnvironmentVariableW(pathW, path_env);
246 static HMODULE load_mono(void)
248 static const WCHAR bin[] = {'\\','b','i','n',0};
249 static const WCHAR lib[] = {'\\','l','i','b',0};
250 static const WCHAR etc[] = {'\\','e','t','c',0};
252 WCHAR mono_path[MAX_PATH], mono_dll_path[MAX_PATH+16], mono_bin_path[MAX_PATH+4];
253 WCHAR mono_lib_path[MAX_PATH+4], mono_etc_path[MAX_PATH+4];
254 char mono_lib_path_a[MAX_PATH], mono_etc_path_a[MAX_PATH];
257 EnterCriticalSection(&mono_lib_cs);
261 if (!get_mono_path(mono_path)) goto end;
263 strcpyW(mono_bin_path, mono_path);
264 strcatW(mono_bin_path, bin);
265 set_environment(mono_bin_path);
267 strcpyW(mono_lib_path, mono_path);
268 strcatW(mono_lib_path, lib);
269 WideCharToMultiByte(CP_UTF8, 0, mono_lib_path, -1, mono_lib_path_a, MAX_PATH, NULL, NULL);
271 strcpyW(mono_etc_path, mono_path);
272 strcatW(mono_etc_path, etc);
273 WideCharToMultiByte(CP_UTF8, 0, mono_etc_path, -1, mono_etc_path_a, MAX_PATH, NULL, NULL);
275 if (!find_mono_dll(mono_path, mono_dll_path, &abi_version)) goto end;
277 if (abi_version != 1) goto end;
279 mono_handle = LoadLibraryW(mono_dll_path);
281 if (!mono_handle) goto end;
283 #define LOAD_MONO_FUNCTION(x) do { \
284 x = (void*)GetProcAddress(mono_handle, #x); \
286 mono_handle = NULL; \
291 LOAD_MONO_FUNCTION(mono_config_parse);
292 LOAD_MONO_FUNCTION(mono_domain_assembly_open);
293 LOAD_MONO_FUNCTION(mono_jit_cleanup);
294 LOAD_MONO_FUNCTION(mono_jit_exec);
295 LOAD_MONO_FUNCTION(mono_jit_init);
296 LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
297 LOAD_MONO_FUNCTION(mono_set_dirs);
299 #undef LOAD_MONO_FUNCTION
301 mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
303 mono_config_parse(NULL);
307 result = mono_handle;
309 LeaveCriticalSection(&mono_lib_cs);
312 MESSAGE("wine: Install the Windows version of Mono to run .NET executables\n");
317 HRESULT WINAPI CorBindToRuntimeHost(LPCWSTR pwszVersion, LPCWSTR pwszBuildFlavor,
318 LPCWSTR pwszHostConfigFile, VOID *pReserved,
319 DWORD startupFlags, REFCLSID rclsid,
320 REFIID riid, LPVOID *ppv)
322 FIXME("(%s, %s, %s, %p, %d, %s, %s, %p): semi-stub!\n", debugstr_w(pwszVersion),
323 debugstr_w(pwszBuildFlavor), debugstr_w(pwszHostConfigFile), pReserved,
324 startupFlags, debugstr_guid(rclsid), debugstr_guid(riid), ppv);
326 if (!get_mono_path(NULL))
328 MESSAGE("wine: Install the Windows version of Mono to run .NET executables\n");
335 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
337 TRACE("(%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
341 case DLL_WINE_PREATTACH:
342 return FALSE; /* prefer native version */
343 case DLL_PROCESS_ATTACH:
344 DisableThreadLibraryCalls(hinstDLL);
346 case DLL_PROCESS_DETACH:
347 unload_all_runtimes();
353 BOOL WINAPI _CorDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
355 FIXME("(%p, %d, %p): stub\n", hinstDLL, fdwReason, lpvReserved);
359 case DLL_PROCESS_ATTACH:
360 DisableThreadLibraryCalls(hinstDLL);
362 case DLL_PROCESS_DETACH:
368 static void get_utf8_args(int *argc, char ***argv)
374 argvw = CommandLineToArgvW(GetCommandLineW(), argc);
376 for (i=0; i<*argc; i++)
378 size += sizeof(char*);
379 size += WideCharToMultiByte(CP_UTF8, 0, argvw[i], -1, NULL, 0, NULL, NULL);
381 size += sizeof(char*);
383 *argv = HeapAlloc(GetProcessHeap(), 0, size);
384 current_arg = (char*)(*argv + *argc + 1);
386 for (i=0; i<*argc; i++)
388 (*argv)[i] = current_arg;
389 current_arg += WideCharToMultiByte(CP_UTF8, 0, argvw[i], -1, current_arg, size, NULL, NULL);
392 (*argv)[*argc] = NULL;
394 HeapFree(GetProcessHeap(), 0, argvw);
397 __int32 WINAPI _CorExeMain(void)
401 char trace_setting[256];
405 MonoAssembly *assembly;
406 char filename[MAX_PATH];
413 get_utf8_args(&argc, &argv);
415 trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
419 mono_jit_set_trace_options(trace_setting);
422 GetModuleFileNameA(NULL, filename, MAX_PATH);
424 domain = mono_jit_init(filename);
426 assembly = mono_domain_assembly_open(domain, filename);
428 exit_code = mono_jit_exec(domain, assembly, argc, argv);
430 mono_jit_cleanup(domain);
432 HeapFree(GetProcessHeap(), 0, argv);
437 __int32 WINAPI _CorExeMain2(PBYTE ptrMemory, DWORD cntMemory, LPWSTR imageName, LPWSTR loaderName, LPWSTR cmdLine)
439 TRACE("(%p, %u, %s, %s, %s)\n", ptrMemory, cntMemory, debugstr_w(imageName), debugstr_w(loaderName), debugstr_w(cmdLine));
440 FIXME("Directly running .NET applications not supported.\n");
444 void WINAPI CorExitProcess(int exitCode)
446 FIXME("(%x) stub\n", exitCode);
447 ExitProcess(exitCode);
450 VOID WINAPI _CorImageUnloading(PVOID imageBase)
452 TRACE("(%p): stub\n", imageBase);
455 HRESULT WINAPI _CorValidateImage(PVOID* imageBase, LPCWSTR imageName)
457 TRACE("(%p, %s): stub\n", imageBase, debugstr_w(imageName));
461 HRESULT WINAPI GetCORSystemDirectory(LPWSTR pbuffer, DWORD cchBuffer, DWORD *dwLength)
463 static const WCHAR slash[] = {'\\',0};
464 WCHAR system_dir[MAX_PATH];
465 WCHAR version[MAX_PATH];
467 FIXME("(%p, %d, %p): semi-stub!\n", pbuffer, cchBuffer, dwLength);
473 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
475 if (!get_install_root(system_dir))
477 ERR("error reading registry key for installroot, returning empty path\n");
482 GetCORVersion(version, MAX_PATH, dwLength);
483 lstrcatW(system_dir, version);
484 lstrcatW(system_dir, slash);
485 *dwLength = lstrlenW(system_dir) + 1;
487 if (cchBuffer < *dwLength)
488 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
490 lstrcpyW(pbuffer, system_dir);
496 HRESULT WINAPI GetCORVersion(LPWSTR pbuffer, DWORD cchBuffer, DWORD *dwLength)
498 static const WCHAR version[] = {'v','2','.','0','.','5','0','7','2','7',0};
500 FIXME("(%p, %d, %p): semi-stub!\n", pbuffer, cchBuffer, dwLength);
502 if (!dwLength || !pbuffer)
505 *dwLength = lstrlenW(version) + 1;
507 if (cchBuffer < *dwLength)
508 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
510 lstrcpyW(pbuffer, version);
515 HRESULT WINAPI GetRequestedRuntimeInfo(LPCWSTR pExe, LPCWSTR pwszVersion, LPCWSTR pConfigurationFile,
516 DWORD startupFlags, DWORD runtimeInfoFlags, LPWSTR pDirectory, DWORD dwDirectory, DWORD *dwDirectoryLength,
517 LPWSTR pVersion, DWORD cchBuffer, DWORD *dwlength)
520 ICLRRuntimeInfo *info;
523 TRACE("(%s, %s, %s, 0x%08x, 0x%08x, %p, 0x%08x, %p, %p, 0x%08x, %p)\n", debugstr_w(pExe),
524 debugstr_w(pwszVersion), debugstr_w(pConfigurationFile), startupFlags, runtimeInfoFlags, pDirectory,
525 dwDirectory, dwDirectoryLength, pVersion, cchBuffer, dwlength);
527 if (!dwDirectoryLength) dwDirectoryLength = &length_dummy;
529 if (!dwlength) dwlength = &length_dummy;
531 ret = get_runtime_info(pExe, pwszVersion, pConfigurationFile, startupFlags, runtimeInfoFlags, TRUE, &info);
535 *dwlength = cchBuffer;
536 ret = ICLRRuntimeInfo_GetVersionString(info, pVersion, dwlength);
540 *dwDirectoryLength = dwDirectory;
541 ret = ICLRRuntimeInfo_GetRuntimeDirectory(info, pDirectory, dwDirectoryLength);
544 ICLRRuntimeInfo_Release(info);
550 HRESULT WINAPI LoadLibraryShim( LPCWSTR szDllName, LPCWSTR szVersion, LPVOID pvReserved, HMODULE * phModDll)
553 WCHAR dll_filename[MAX_PATH];
554 WCHAR version[MAX_PATH];
555 static const WCHAR default_version[] = {'v','1','.','1','.','4','3','2','2',0};
556 static const WCHAR slash[] = {'\\',0};
559 TRACE("(%p %s, %p, %p, %p)\n", szDllName, debugstr_w(szDllName), szVersion, pvReserved, phModDll);
561 if (!szDllName || !phModDll)
564 if (!get_install_root(dll_filename))
566 ERR("error reading registry key for installroot\n");
573 ret = GetCORVersion(version, MAX_PATH, &dummy);
577 szVersion = default_version;
579 strcatW(dll_filename, szVersion);
580 strcatW(dll_filename, slash);
583 strcatW(dll_filename, szDllName);
585 *phModDll = LoadLibraryW(dll_filename);
587 return *phModDll ? S_OK : E_HANDLE;
590 HRESULT WINAPI LockClrVersion(FLockClrVersionCallback hostCallback, FLockClrVersionCallback *pBeginHostSetup, FLockClrVersionCallback *pEndHostSetup)
592 FIXME("(%p %p %p): stub\n", hostCallback, pBeginHostSetup, pEndHostSetup);
596 HRESULT WINAPI CoInitializeCor(DWORD fFlags)
598 FIXME("(0x%08x): stub\n", fFlags);
602 HRESULT WINAPI GetAssemblyMDImport(LPCWSTR szFileName, REFIID riid, IUnknown **ppIUnk)
604 FIXME("(%p %s, %s, %p): stub\n", szFileName, debugstr_w(szFileName), debugstr_guid(riid), *ppIUnk);
605 return ERROR_CALL_NOT_IMPLEMENTED;
608 HRESULT WINAPI GetVersionFromProcess(HANDLE hProcess, LPWSTR pVersion, DWORD cchBuffer, DWORD *dwLength)
610 FIXME("(%p, %p, %d, %p): stub\n", hProcess, pVersion, cchBuffer, dwLength);
614 HRESULT WINAPI LoadStringRCEx(LCID culture, UINT resId, LPWSTR pBuffer, int iBufLen, int bQuiet, int* pBufLen)
617 if ((iBufLen <= 0) || !pBuffer)
621 FIXME("(%d, %x, %p, %d, %d, %p): semi-stub\n", culture, resId, pBuffer, iBufLen, bQuiet, pBufLen);
627 *pBufLen = lstrlenW(pBuffer);
631 HRESULT WINAPI LoadStringRC(UINT resId, LPWSTR pBuffer, int iBufLen, int bQuiet)
633 return LoadStringRCEx(-1, resId, pBuffer, iBufLen, bQuiet, NULL);
636 HRESULT WINAPI CorBindToRuntimeEx(LPWSTR szVersion, LPWSTR szBuildFlavor, DWORD nflags, REFCLSID rslsid,
637 REFIID riid, LPVOID *ppv)
639 FIXME("%s %s %d %s %s %p\n", debugstr_w(szVersion), debugstr_w(szBuildFlavor), nflags, debugstr_guid( rslsid ),
640 debugstr_guid( riid ), ppv);
642 if(IsEqualGUID( riid, &IID_ICorRuntimeHost ))
644 *ppv = create_corruntimehost();
651 HRESULT WINAPI CorBindToCurrentRuntime(LPCWSTR filename, REFCLSID rclsid, REFIID riid, LPVOID *ppv)
653 FIXME("(%s, %s, %s, %p): stub\n", debugstr_w(filename), debugstr_guid(rclsid), debugstr_guid(riid), ppv);
657 STDAPI ClrCreateManagedInstance(LPCWSTR pTypeName, REFIID riid, void **ppObject)
659 FIXME("(%s,%s,%p)\n", debugstr_w(pTypeName), debugstr_guid(riid), ppObject);
663 BOOL WINAPI StrongNameSignatureVerification(LPCWSTR filename, DWORD inFlags, DWORD* pOutFlags)
665 FIXME("(%s, 0x%X, %p): stub\n", debugstr_w(filename), inFlags, pOutFlags);
669 BOOL WINAPI StrongNameSignatureVerificationEx(LPCWSTR filename, BOOL forceVerification, BOOL* pVerified)
671 FIXME("(%s, %u, %p): stub\n", debugstr_w(filename), forceVerification, pVerified);
675 HRESULT WINAPI CLRCreateInstance(REFCLSID clsid, REFIID riid, LPVOID *ppInterface)
677 TRACE("(%s,%s,%p)\n", debugstr_guid(clsid), debugstr_guid(riid), ppInterface);
679 if (IsEqualGUID(clsid, &CLSID_CLRMetaHost))
680 return CLRMetaHost_CreateInstance(riid, ppInterface);
682 FIXME("not implemented for class %s\n", debugstr_guid(clsid));
684 return CLASS_E_CLASSNOTAVAILABLE;
687 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
689 FIXME("(%s, %s, %p): stub\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
696 HRESULT WINAPI DllRegisterServer(void)
702 HRESULT WINAPI DllUnregisterServer(void)
708 HRESULT WINAPI DllCanUnloadNow(VOID)
713 INT WINAPI ND_RU1( const void *ptr, INT offset )
715 return *((const BYTE *)ptr + offset);
718 INT WINAPI ND_RI2( const void *ptr, INT offset )
720 return *(const SHORT *)((const BYTE *)ptr + offset);
723 INT WINAPI ND_RI4( const void *ptr, INT offset )
725 return *(const INT *)((const BYTE *)ptr + offset);
728 INT64 WINAPI ND_RI8( const void *ptr, INT offset )
730 return *(const INT64 *)((const BYTE *)ptr + offset);
733 void WINAPI ND_WU1( void *ptr, INT offset, BYTE val )
735 *((BYTE *)ptr + offset) = val;
738 void WINAPI ND_WI2( void *ptr, INT offset, SHORT val )
740 *(SHORT *)((BYTE *)ptr + offset) = val;
743 void WINAPI ND_WI4( void *ptr, INT offset, INT val )
745 *(INT *)((BYTE *)ptr + offset) = val;
748 void WINAPI ND_WI8( void *ptr, INT offset, INT64 val )
750 *(INT64 *)((BYTE *)ptr + offset) = val;
753 void WINAPI ND_CopyObjDst( const void *src, void *dst, INT offset, INT size )
755 memcpy( (BYTE *)dst + offset, src, size );
758 void WINAPI ND_CopyObjSrc( const void *src, INT offset, void *dst, INT size )
760 memcpy( dst, (const BYTE *)src + offset, size );