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
24 #include "wine/unicode.h"
25 #include "wine/library.h"
38 #include "mscoree_private.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL( mscoree );
44 BOOL find_mono_dll(LPCWSTR path, LPWSTR dll_path, int* abi_version)
46 static const WCHAR mono_dll[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
47 static const WCHAR libmono_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
50 strcpyW(dll_path, path);
51 strcatW(dll_path, mono_dll);
52 attributes = GetFileAttributesW(dll_path);
54 if (attributes == INVALID_FILE_ATTRIBUTES)
56 strcpyW(dll_path, path);
57 strcatW(dll_path, libmono_dll);
58 attributes = GetFileAttributesW(dll_path);
61 if (attributes != INVALID_FILE_ATTRIBUTES)
63 /* FIXME: Test for appropriate architecture. */
71 static BOOL get_mono_path_from_registry(LPWSTR path)
73 static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
74 static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
75 static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
76 static const WCHAR slash[] = {'\\',0};
78 WCHAR version[64], version_key[MAX_PATH];
81 WCHAR dll_path[MAX_PATH];
84 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
87 len = sizeof(version);
88 if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
95 lstrcpyW(version_key, mono_key);
96 lstrcatW(version_key, slash);
97 lstrcatW(version_key, version);
99 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
102 len = sizeof(WCHAR) * MAX_PATH;
103 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
110 return find_mono_dll(path, dll_path, &abi_version);
113 static BOOL get_mono_path_from_folder(LPCWSTR folder, LPWSTR mono_path)
115 static const WCHAR mono_one_dot_zero[] = {'\\','m','o','n','o','-','1','.','0', 0};
116 WCHAR mono_dll_path[MAX_PATH];
120 strcpyW(mono_path, folder);
121 strcatW(mono_path, mono_one_dot_zero);
123 found = find_mono_dll(mono_path, mono_dll_path, &abi_version);
125 if (found && abi_version != 1)
127 ERR("found wrong ABI in %s\n", debugstr_w(mono_path));
134 static BOOL get_mono_path(LPWSTR path)
136 static const WCHAR subdir_mono[] = {'\\','m','o','n','o',0};
137 static const WCHAR sibling_mono[] = {'\\','.','.','\\','m','o','n','o',0};
138 WCHAR base_path[MAX_PATH];
139 const char *unix_data_dir;
142 static WCHAR* (CDECL *wine_get_dos_file_name)(const char*);
144 /* First try c:\windows\mono */
145 GetWindowsDirectoryW(base_path, MAX_PATH);
146 strcatW(base_path, subdir_mono);
148 if (get_mono_path_from_folder(base_path, path))
151 /* Next: /usr/share/wine/mono */
152 unix_data_dir = wine_get_data_dir();
156 unix_data_dir = wine_get_build_dir();
162 if (!wine_get_dos_file_name)
163 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleA("kernel32"), "wine_get_dos_file_name");
165 if (wine_get_dos_file_name)
167 dos_data_dir = wine_get_dos_file_name(unix_data_dir);
171 strcpyW(base_path, dos_data_dir);
172 strcatW(base_path, build_tree ? sibling_mono : subdir_mono);
174 HeapFree(GetProcessHeap(), 0, dos_data_dir);
176 if (get_mono_path_from_folder(base_path, path))
182 /* Last: the registry */
183 return get_mono_path_from_registry(path);
186 static BOOL get_install_root(LPWSTR install_dir)
188 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};
189 const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
194 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
198 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
208 static CRITICAL_SECTION mono_lib_cs;
209 static CRITICAL_SECTION_DEBUG mono_lib_cs_debug =
212 { &mono_lib_cs_debug.ProcessLocksList,
213 &mono_lib_cs_debug.ProcessLocksList },
214 0, 0, { (DWORD_PTR)(__FILE__ ": mono_lib_cs") }
216 static CRITICAL_SECTION mono_lib_cs = { &mono_lib_cs_debug, -1, 0, 0, 0, 0 };
220 void (*mono_config_parse)(const char *filename);
221 MonoAssembly* (*mono_domain_assembly_open) (MonoDomain *domain, const char *name);
222 void (*mono_jit_cleanup)(MonoDomain *domain);
223 int (*mono_jit_exec)(MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[]);
224 MonoDomain* (*mono_jit_init)(const char *file);
225 int (*mono_jit_set_trace_options)(const char* options);
226 void (*mono_set_dirs)(const char *assembly_dir, const char *config_dir);
228 static void set_environment(LPCWSTR bin_path)
230 WCHAR path_env[MAX_PATH];
233 static const WCHAR pathW[] = {'P','A','T','H',0};
235 /* We have to modify PATH as Mono loads other DLLs from this directory. */
236 GetEnvironmentVariableW(pathW, path_env, sizeof(path_env)/sizeof(WCHAR));
237 len = strlenW(path_env);
238 path_env[len++] = ';';
239 strcpyW(path_env+len, bin_path);
240 SetEnvironmentVariableW(pathW, path_env);
243 static HMODULE load_mono(void)
245 static const WCHAR bin[] = {'\\','b','i','n',0};
246 static const WCHAR lib[] = {'\\','l','i','b',0};
247 static const WCHAR etc[] = {'\\','e','t','c',0};
249 WCHAR mono_path[MAX_PATH], mono_dll_path[MAX_PATH+16], mono_bin_path[MAX_PATH+4];
250 WCHAR mono_lib_path[MAX_PATH+4], mono_etc_path[MAX_PATH+4];
251 char mono_lib_path_a[MAX_PATH], mono_etc_path_a[MAX_PATH];
254 EnterCriticalSection(&mono_lib_cs);
258 if (!get_mono_path(mono_path)) goto end;
260 strcpyW(mono_bin_path, mono_path);
261 strcatW(mono_bin_path, bin);
262 set_environment(mono_bin_path);
264 strcpyW(mono_lib_path, mono_path);
265 strcatW(mono_lib_path, lib);
266 WideCharToMultiByte(CP_UTF8, 0, mono_lib_path, -1, mono_lib_path_a, MAX_PATH, NULL, NULL);
268 strcpyW(mono_etc_path, mono_path);
269 strcatW(mono_etc_path, etc);
270 WideCharToMultiByte(CP_UTF8, 0, mono_etc_path, -1, mono_etc_path_a, MAX_PATH, NULL, NULL);
272 if (!find_mono_dll(mono_path, mono_dll_path, &abi_version)) goto end;
274 if (abi_version != 1) goto end;
276 mono_handle = LoadLibraryW(mono_dll_path);
278 if (!mono_handle) goto end;
280 #define LOAD_MONO_FUNCTION(x) do { \
281 x = (void*)GetProcAddress(mono_handle, #x); \
283 mono_handle = NULL; \
288 LOAD_MONO_FUNCTION(mono_config_parse);
289 LOAD_MONO_FUNCTION(mono_domain_assembly_open);
290 LOAD_MONO_FUNCTION(mono_jit_cleanup);
291 LOAD_MONO_FUNCTION(mono_jit_exec);
292 LOAD_MONO_FUNCTION(mono_jit_init);
293 LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
294 LOAD_MONO_FUNCTION(mono_set_dirs);
296 #undef LOAD_MONO_FUNCTION
298 mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
300 mono_config_parse(NULL);
304 result = mono_handle;
306 LeaveCriticalSection(&mono_lib_cs);
309 MESSAGE("wine: Install the Windows version of Mono to run .NET executables\n");
314 HRESULT WINAPI CorBindToRuntimeHost(LPCWSTR pwszVersion, LPCWSTR pwszBuildFlavor,
315 LPCWSTR pwszHostConfigFile, VOID *pReserved,
316 DWORD startupFlags, REFCLSID rclsid,
317 REFIID riid, LPVOID *ppv)
319 FIXME("(%s, %s, %s, %p, %d, %s, %s, %p): semi-stub!\n", debugstr_w(pwszVersion),
320 debugstr_w(pwszBuildFlavor), debugstr_w(pwszHostConfigFile), pReserved,
321 startupFlags, debugstr_guid(rclsid), debugstr_guid(riid), ppv);
323 if (!get_mono_path(NULL))
325 MESSAGE("wine: Install the Windows version of Mono to run .NET executables\n");
332 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
334 TRACE("(%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
338 case DLL_WINE_PREATTACH:
339 return FALSE; /* prefer native version */
340 case DLL_PROCESS_ATTACH:
341 DisableThreadLibraryCalls(hinstDLL);
343 case DLL_PROCESS_DETACH:
349 BOOL WINAPI _CorDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
351 FIXME("(%p, %d, %p): stub\n", hinstDLL, fdwReason, lpvReserved);
355 case DLL_PROCESS_ATTACH:
356 DisableThreadLibraryCalls(hinstDLL);
358 case DLL_PROCESS_DETACH:
364 static void get_utf8_args(int *argc, char ***argv)
370 argvw = CommandLineToArgvW(GetCommandLineW(), argc);
372 for (i=0; i<*argc; i++)
374 size += sizeof(char*);
375 size += WideCharToMultiByte(CP_UTF8, 0, argvw[i], -1, NULL, 0, NULL, NULL);
377 size += sizeof(char*);
379 *argv = HeapAlloc(GetProcessHeap(), 0, size);
380 current_arg = (char*)(*argv + *argc + 1);
382 for (i=0; i<*argc; i++)
384 (*argv)[i] = current_arg;
385 current_arg += WideCharToMultiByte(CP_UTF8, 0, argvw[i], -1, current_arg, size, NULL, NULL);
388 (*argv)[*argc] = NULL;
390 HeapFree(GetProcessHeap(), 0, argvw);
393 __int32 WINAPI _CorExeMain(void)
397 char trace_setting[256];
401 MonoAssembly *assembly;
402 char filename[MAX_PATH];
409 get_utf8_args(&argc, &argv);
411 trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
415 mono_jit_set_trace_options(trace_setting);
418 GetModuleFileNameA(NULL, filename, MAX_PATH);
420 domain = mono_jit_init(filename);
422 assembly = mono_domain_assembly_open(domain, filename);
424 exit_code = mono_jit_exec(domain, assembly, argc, argv);
426 mono_jit_cleanup(domain);
428 HeapFree(GetProcessHeap(), 0, argv);
433 __int32 WINAPI _CorExeMain2(PBYTE ptrMemory, DWORD cntMemory, LPWSTR imageName, LPWSTR loaderName, LPWSTR cmdLine)
435 TRACE("(%p, %u, %s, %s, %s)\n", ptrMemory, cntMemory, debugstr_w(imageName), debugstr_w(loaderName), debugstr_w(cmdLine));
436 FIXME("Directly running .NET applications not supported.\n");
440 void WINAPI CorExitProcess(int exitCode)
442 FIXME("(%x) stub\n", exitCode);
443 ExitProcess(exitCode);
446 VOID WINAPI _CorImageUnloading(PVOID imageBase)
448 TRACE("(%p): stub\n", imageBase);
451 HRESULT WINAPI _CorValidateImage(PVOID* imageBase, LPCWSTR imageName)
453 TRACE("(%p, %s): stub\n", imageBase, debugstr_w(imageName));
457 HRESULT WINAPI GetCORSystemDirectory(LPWSTR pbuffer, DWORD cchBuffer, DWORD *dwLength)
459 static const WCHAR slash[] = {'\\',0};
460 WCHAR system_dir[MAX_PATH];
461 WCHAR version[MAX_PATH];
463 FIXME("(%p, %d, %p): semi-stub!\n", pbuffer, cchBuffer, dwLength);
469 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
471 if (!get_install_root(system_dir))
473 ERR("error reading registry key for installroot, returning empty path\n");
478 GetCORVersion(version, MAX_PATH, dwLength);
479 lstrcatW(system_dir, version);
480 lstrcatW(system_dir, slash);
481 *dwLength = lstrlenW(system_dir) + 1;
483 if (cchBuffer < *dwLength)
484 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
486 lstrcpyW(pbuffer, system_dir);
492 HRESULT WINAPI GetCORVersion(LPWSTR pbuffer, DWORD cchBuffer, DWORD *dwLength)
494 static const WCHAR version[] = {'v','2','.','0','.','5','0','7','2','7',0};
496 FIXME("(%p, %d, %p): semi-stub!\n", pbuffer, cchBuffer, dwLength);
498 if (!dwLength || !pbuffer)
501 *dwLength = lstrlenW(version) + 1;
503 if (cchBuffer < *dwLength)
504 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
506 lstrcpyW(pbuffer, version);
511 HRESULT WINAPI GetRequestedRuntimeInfo(LPCWSTR pExe, LPCWSTR pwszVersion, LPCWSTR pConfigurationFile,
512 DWORD startupFlags, DWORD runtimeInfoFlags, LPWSTR pDirectory, DWORD dwDirectory, DWORD *dwDirectoryLength,
513 LPWSTR pVersion, DWORD cchBuffer, DWORD *dwlength)
516 DWORD ver_len, dir_len;
517 WCHAR dirW[MAX_PATH], verW[MAX_PATH];
519 FIXME("(%s, %s, %s, 0x%08x, 0x%08x, %p, 0x%08x, %p, %p, 0x%08x, %p) semi-stub\n", debugstr_w(pExe),
520 debugstr_w(pwszVersion), debugstr_w(pConfigurationFile), startupFlags, runtimeInfoFlags, pDirectory,
521 dwDirectory, dwDirectoryLength, pVersion, cchBuffer, dwlength);
523 if (!pwszVersion && !(runtimeInfoFlags & RUNTIME_INFO_UPGRADE_VERSION))
524 return CLR_E_SHIM_RUNTIME;
526 ret = GetCORSystemDirectory(dirW, dwDirectory, &dir_len);
530 if (dwDirectoryLength)
531 *dwDirectoryLength = dir_len;
533 lstrcpyW(pDirectory, dirW);
535 ret = GetCORVersion(verW, cchBuffer, &ver_len);
542 lstrcpyW(pVersion, verW);
548 HRESULT WINAPI LoadLibraryShim( LPCWSTR szDllName, LPCWSTR szVersion, LPVOID pvReserved, HMODULE * phModDll)
550 FIXME("(%p %s, %p, %p, %p): semi-stub\n", szDllName, debugstr_w(szDllName), szVersion, pvReserved, phModDll);
552 if (phModDll) *phModDll = LoadLibraryW(szDllName);
556 HRESULT WINAPI LockClrVersion(FLockClrVersionCallback hostCallback, FLockClrVersionCallback *pBeginHostSetup, FLockClrVersionCallback *pEndHostSetup)
558 FIXME("(%p %p %p): stub\n", hostCallback, pBeginHostSetup, pEndHostSetup);
562 HRESULT WINAPI CoInitializeCor(DWORD fFlags)
564 FIXME("(0x%08x): stub\n", fFlags);
568 HRESULT WINAPI GetAssemblyMDImport(LPCWSTR szFileName, REFIID riid, IUnknown **ppIUnk)
570 FIXME("(%p %s, %s, %p): stub\n", szFileName, debugstr_w(szFileName), debugstr_guid(riid), *ppIUnk);
571 return ERROR_CALL_NOT_IMPLEMENTED;
574 HRESULT WINAPI GetVersionFromProcess(HANDLE hProcess, LPWSTR pVersion, DWORD cchBuffer, DWORD *dwLength)
576 FIXME("(%p, %p, %d, %p): stub\n", hProcess, pVersion, cchBuffer, dwLength);
580 HRESULT WINAPI LoadStringRCEx(LCID culture, UINT resId, LPWSTR pBuffer, int iBufLen, int bQuiet, int* pBufLen)
583 if ((iBufLen <= 0) || !pBuffer)
587 FIXME("(%d, %x, %p, %d, %d, %p): semi-stub\n", culture, resId, pBuffer, iBufLen, bQuiet, pBufLen);
593 *pBufLen = lstrlenW(pBuffer);
597 HRESULT WINAPI LoadStringRC(UINT resId, LPWSTR pBuffer, int iBufLen, int bQuiet)
599 return LoadStringRCEx(-1, resId, pBuffer, iBufLen, bQuiet, NULL);
602 HRESULT WINAPI CorBindToRuntimeEx(LPWSTR szVersion, LPWSTR szBuildFlavor, DWORD nflags, REFCLSID rslsid,
603 REFIID riid, LPVOID *ppv)
605 FIXME("%s %s %d %s %s %p\n", debugstr_w(szVersion), debugstr_w(szBuildFlavor), nflags, debugstr_guid( rslsid ),
606 debugstr_guid( riid ), ppv);
608 if(IsEqualGUID( riid, &IID_ICorRuntimeHost ))
610 *ppv = create_corruntimehost();
617 HRESULT WINAPI CorBindToCurrentRuntime(LPCWSTR filename, REFCLSID rclsid, REFIID riid, LPVOID *ppv)
619 FIXME("(%s, %s, %s, %p): stub\n", debugstr_w(filename), debugstr_guid(rclsid), debugstr_guid(riid), ppv);
623 STDAPI ClrCreateManagedInstance(LPCWSTR pTypeName, REFIID riid, void **ppObject)
625 FIXME("(%s,%s,%p)\n", debugstr_w(pTypeName), debugstr_guid(riid), ppObject);
629 BOOL WINAPI StrongNameSignatureVerification(LPCWSTR filename, DWORD inFlags, DWORD* pOutFlags)
631 FIXME("(%s, 0x%X, %p): stub\n", debugstr_w(filename), inFlags, pOutFlags);
635 BOOL WINAPI StrongNameSignatureVerificationEx(LPCWSTR filename, BOOL forceVerification, BOOL* pVerified)
637 FIXME("(%s, %u, %p): stub\n", debugstr_w(filename), forceVerification, pVerified);
641 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
643 FIXME("(%s, %s, %p): stub\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
650 HRESULT WINAPI DllRegisterServer(void)
656 HRESULT WINAPI DllUnregisterServer(void)
662 HRESULT WINAPI DllCanUnloadNow(VOID)
667 INT WINAPI ND_RU1( const void *ptr, INT offset )
669 return *((const BYTE *)ptr + offset);
672 INT WINAPI ND_RI2( const void *ptr, INT offset )
674 return *(const SHORT *)((const BYTE *)ptr + offset);
677 INT WINAPI ND_RI4( const void *ptr, INT offset )
679 return *(const INT *)((const BYTE *)ptr + offset);
682 INT64 WINAPI ND_RI8( const void *ptr, INT offset )
684 return *(const INT64 *)((const BYTE *)ptr + offset);
687 void WINAPI ND_WU1( void *ptr, INT offset, BYTE val )
689 *((BYTE *)ptr + offset) = val;
692 void WINAPI ND_WI2( void *ptr, INT offset, SHORT val )
694 *(SHORT *)((BYTE *)ptr + offset) = val;
697 void WINAPI ND_WI4( void *ptr, INT offset, INT val )
699 *(INT *)((BYTE *)ptr + offset) = val;
702 void WINAPI ND_WI8( void *ptr, INT offset, INT64 val )
704 *(INT64 *)((BYTE *)ptr + offset) = val;
707 void WINAPI ND_CopyObjDst( const void *src, void *dst, INT offset, INT size )
709 memcpy( (BYTE *)dst + offset, src, size );
712 void WINAPI ND_CopyObjSrc( const void *src, INT offset, void *dst, INT size )
714 memcpy( dst, (const BYTE *)src + offset, size );