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"
36 #include "mscoree_private.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL( mscoree );
42 static BOOL get_mono_path(LPWSTR path)
44 static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
45 static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
46 static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
47 static const WCHAR slash[] = {'\\',0};
49 WCHAR version[64], version_key[MAX_PATH];
53 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
56 len = sizeof(version);
57 if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
64 lstrcpyW(version_key, mono_key);
65 lstrcatW(version_key, slash);
66 lstrcatW(version_key, version);
68 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
71 len = sizeof(WCHAR) * MAX_PATH;
72 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
82 static BOOL get_install_root(LPWSTR install_dir)
84 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};
85 const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
90 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
94 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
104 static CRITICAL_SECTION mono_lib_cs;
105 static CRITICAL_SECTION_DEBUG mono_lib_cs_debug =
108 { &mono_lib_cs_debug.ProcessLocksList,
109 &mono_lib_cs_debug.ProcessLocksList },
110 0, 0, { (DWORD_PTR)(__FILE__ ": mono_lib_cs") }
112 static CRITICAL_SECTION mono_lib_cs = { &mono_lib_cs_debug, -1, 0, 0, 0, 0 };
116 void (*mono_config_parse)(const char *filename);
117 MonoAssembly* (*mono_domain_assembly_open) (MonoDomain *domain, const char *name);
118 void (*mono_jit_cleanup)(MonoDomain *domain);
119 int (*mono_jit_exec)(MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[]);
120 MonoDomain* (*mono_jit_init)(const char *file);
121 int (*mono_jit_set_trace_options)(const char* options);
122 void (*mono_set_dirs)(const char *assembly_dir, const char *config_dir);
124 static void set_environment(LPCWSTR bin_path)
126 WCHAR path_env[MAX_PATH];
129 static const WCHAR pathW[] = {'P','A','T','H',0};
131 /* We have to modify PATH as Mono loads other DLLs from this directory. */
132 GetEnvironmentVariableW(pathW, path_env, sizeof(path_env)/sizeof(WCHAR));
133 len = strlenW(path_env);
134 path_env[len++] = ';';
135 strcpyW(path_env+len, bin_path);
136 SetEnvironmentVariableW(pathW, path_env);
139 static HMODULE load_mono(void)
141 static const WCHAR mono_dll[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
142 static const WCHAR libmono_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
143 static const WCHAR bin[] = {'\\','b','i','n',0};
144 static const WCHAR lib[] = {'\\','l','i','b',0};
145 static const WCHAR etc[] = {'\\','e','t','c',0};
147 WCHAR mono_path[MAX_PATH], mono_dll_path[MAX_PATH+16], mono_bin_path[MAX_PATH+4];
148 WCHAR mono_lib_path[MAX_PATH+4], mono_etc_path[MAX_PATH+4];
149 char mono_lib_path_a[MAX_PATH], mono_etc_path_a[MAX_PATH];
151 EnterCriticalSection(&mono_lib_cs);
155 if (!get_mono_path(mono_path)) goto end;
157 strcpyW(mono_bin_path, mono_path);
158 strcatW(mono_bin_path, bin);
159 set_environment(mono_bin_path);
161 strcpyW(mono_lib_path, mono_path);
162 strcatW(mono_lib_path, lib);
163 WideCharToMultiByte(CP_UTF8, 0, mono_lib_path, -1, mono_lib_path_a, MAX_PATH, NULL, NULL);
165 strcpyW(mono_etc_path, mono_path);
166 strcatW(mono_etc_path, etc);
167 WideCharToMultiByte(CP_UTF8, 0, mono_etc_path, -1, mono_etc_path_a, MAX_PATH, NULL, NULL);
169 strcpyW(mono_dll_path, mono_path);
170 strcatW(mono_dll_path, mono_dll);
171 mono_handle = LoadLibraryW(mono_dll_path);
175 strcpyW(mono_dll_path, mono_path);
176 strcatW(mono_dll_path, libmono_dll);
177 mono_handle = LoadLibraryW(mono_dll_path);
180 if (!mono_handle) goto end;
182 #define LOAD_MONO_FUNCTION(x) do { \
183 x = (void*)GetProcAddress(mono_handle, #x); \
185 mono_handle = NULL; \
190 LOAD_MONO_FUNCTION(mono_config_parse);
191 LOAD_MONO_FUNCTION(mono_domain_assembly_open);
192 LOAD_MONO_FUNCTION(mono_jit_cleanup);
193 LOAD_MONO_FUNCTION(mono_jit_exec);
194 LOAD_MONO_FUNCTION(mono_jit_init);
195 LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
196 LOAD_MONO_FUNCTION(mono_set_dirs);
198 #undef LOAD_MONO_FUNCTION
200 mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
202 mono_config_parse(NULL);
206 result = mono_handle;
208 LeaveCriticalSection(&mono_lib_cs);
211 MESSAGE("wine: Install the Windows version of Mono to run .NET executables\n");
216 HRESULT WINAPI CorBindToRuntimeHost(LPCWSTR pwszVersion, LPCWSTR pwszBuildFlavor,
217 LPCWSTR pwszHostConfigFile, VOID *pReserved,
218 DWORD startupFlags, REFCLSID rclsid,
219 REFIID riid, LPVOID *ppv)
221 FIXME("(%s, %s, %s, %p, %d, %s, %s, %p): semi-stub!\n", debugstr_w(pwszVersion),
222 debugstr_w(pwszBuildFlavor), debugstr_w(pwszHostConfigFile), pReserved,
223 startupFlags, debugstr_guid(rclsid), debugstr_guid(riid), ppv);
225 if (!get_mono_path(NULL))
227 MESSAGE("wine: Install the Windows version of Mono to run .NET executables\n");
234 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
236 TRACE("(%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
240 case DLL_WINE_PREATTACH:
241 return FALSE; /* prefer native version */
242 case DLL_PROCESS_ATTACH:
243 DisableThreadLibraryCalls(hinstDLL);
245 case DLL_PROCESS_DETACH:
251 BOOL WINAPI _CorDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
253 FIXME("(%p, %d, %p): stub\n", hinstDLL, fdwReason, lpvReserved);
257 case DLL_PROCESS_ATTACH:
258 DisableThreadLibraryCalls(hinstDLL);
260 case DLL_PROCESS_DETACH:
266 static void get_utf8_args(int *argc, char ***argv)
272 argvw = CommandLineToArgvW(GetCommandLineW(), argc);
274 for (i=0; i<*argc; i++)
276 size += sizeof(char*);
277 size += WideCharToMultiByte(CP_UTF8, 0, argvw[i], -1, NULL, 0, NULL, NULL);
279 size += sizeof(char*);
281 *argv = HeapAlloc(GetProcessHeap(), 0, size);
282 current_arg = (char*)(*argv + *argc + 1);
284 for (i=0; i<*argc; i++)
286 (*argv)[i] = current_arg;
287 current_arg += WideCharToMultiByte(CP_UTF8, 0, argvw[i], -1, current_arg, size, NULL, NULL);
290 (*argv)[*argc] = NULL;
292 HeapFree(GetProcessHeap(), 0, argvw);
295 __int32 WINAPI _CorExeMain(void)
299 char trace_setting[256];
303 MonoAssembly *assembly;
304 char filename[MAX_PATH];
311 get_utf8_args(&argc, &argv);
313 trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
317 mono_jit_set_trace_options(trace_setting);
320 GetModuleFileNameA(NULL, filename, MAX_PATH);
322 domain = mono_jit_init(filename);
324 assembly = mono_domain_assembly_open(domain, filename);
326 exit_code = mono_jit_exec(domain, assembly, argc, argv);
328 mono_jit_cleanup(domain);
330 HeapFree(GetProcessHeap(), 0, argv);
335 __int32 WINAPI _CorExeMain2(PBYTE ptrMemory, DWORD cntMemory, LPWSTR imageName, LPWSTR loaderName, LPWSTR cmdLine)
337 TRACE("(%p, %u, %s, %s, %s)\n", ptrMemory, cntMemory, debugstr_w(imageName), debugstr_w(loaderName), debugstr_w(cmdLine));
338 FIXME("Directly running .NET applications not supported.\n");
342 void WINAPI CorExitProcess(int exitCode)
344 FIXME("(%x) stub\n", exitCode);
345 ExitProcess(exitCode);
348 VOID WINAPI _CorImageUnloading(PVOID imageBase)
350 TRACE("(%p): stub\n", imageBase);
353 HRESULT WINAPI _CorValidateImage(PVOID* imageBase, LPCWSTR imageName)
355 TRACE("(%p, %s): stub\n", imageBase, debugstr_w(imageName));
359 HRESULT WINAPI GetCORSystemDirectory(LPWSTR pbuffer, DWORD cchBuffer, DWORD *dwLength)
361 static const WCHAR slash[] = {'\\',0};
362 WCHAR system_dir[MAX_PATH];
363 WCHAR version[MAX_PATH];
365 FIXME("(%p, %d, %p): semi-stub!\n", pbuffer, cchBuffer, dwLength);
371 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
373 if (!get_install_root(system_dir))
375 ERR("error reading registry key for installroot, returning empty path\n");
380 GetCORVersion(version, MAX_PATH, dwLength);
381 lstrcatW(system_dir, version);
382 lstrcatW(system_dir, slash);
383 *dwLength = lstrlenW(system_dir) + 1;
385 if (cchBuffer < *dwLength)
386 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
388 lstrcpyW(pbuffer, system_dir);
394 HRESULT WINAPI GetCORVersion(LPWSTR pbuffer, DWORD cchBuffer, DWORD *dwLength)
396 static const WCHAR version[] = {'v','2','.','0','.','5','0','7','2','7',0};
398 FIXME("(%p, %d, %p): semi-stub!\n", pbuffer, cchBuffer, dwLength);
400 if (!dwLength || !pbuffer)
403 *dwLength = lstrlenW(version) + 1;
405 if (cchBuffer < *dwLength)
406 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
408 lstrcpyW(pbuffer, version);
413 HRESULT WINAPI GetRequestedRuntimeInfo(LPCWSTR pExe, LPCWSTR pwszVersion, LPCWSTR pConfigurationFile,
414 DWORD startupFlags, DWORD runtimeInfoFlags, LPWSTR pDirectory, DWORD dwDirectory, DWORD *dwDirectoryLength,
415 LPWSTR pVersion, DWORD cchBuffer, DWORD *dwlength)
417 FIXME("(%s, %s, %s, 0x%08x, 0x%08x, %p, 0x%08x, %p, %p, 0x%08x, %p) stub\n", debugstr_w(pExe),
418 debugstr_w(pwszVersion), debugstr_w(pConfigurationFile), startupFlags, runtimeInfoFlags, pDirectory,
419 dwDirectory, dwDirectoryLength, pVersion, cchBuffer, dwlength);
420 return GetCORVersion(pVersion, cchBuffer, dwlength);
423 HRESULT WINAPI LoadLibraryShim( LPCWSTR szDllName, LPCWSTR szVersion, LPVOID pvReserved, HMODULE * phModDll)
425 FIXME("(%p %s, %p, %p, %p): semi-stub\n", szDllName, debugstr_w(szDllName), szVersion, pvReserved, phModDll);
427 if (phModDll) *phModDll = LoadLibraryW(szDllName);
431 HRESULT WINAPI LockClrVersion(FLockClrVersionCallback hostCallback, FLockClrVersionCallback *pBeginHostSetup, FLockClrVersionCallback *pEndHostSetup)
433 FIXME("(%p %p %p): stub\n", hostCallback, pBeginHostSetup, pEndHostSetup);
437 HRESULT WINAPI CoInitializeCor(DWORD fFlags)
439 FIXME("(0x%08x): stub\n", fFlags);
443 HRESULT WINAPI GetAssemblyMDImport(LPCWSTR szFileName, REFIID riid, IUnknown **ppIUnk)
445 FIXME("(%p %s, %s, %p): stub\n", szFileName, debugstr_w(szFileName), debugstr_guid(riid), *ppIUnk);
446 return ERROR_CALL_NOT_IMPLEMENTED;
449 HRESULT WINAPI GetVersionFromProcess(HANDLE hProcess, LPWSTR pVersion, DWORD cchBuffer, DWORD *dwLength)
451 FIXME("(%p, %p, %d, %p): stub\n", hProcess, pVersion, cchBuffer, dwLength);
455 HRESULT WINAPI LoadStringRCEx(LCID culture, UINT resId, LPWSTR pBuffer, int iBufLen, int bQuiet, int* pBufLen)
458 if ((iBufLen <= 0) || !pBuffer)
462 FIXME("(%d, %x, %p, %d, %d, %p): semi-stub\n", culture, resId, pBuffer, iBufLen, bQuiet, pBufLen);
468 *pBufLen = lstrlenW(pBuffer);
472 HRESULT WINAPI LoadStringRC(UINT resId, LPWSTR pBuffer, int iBufLen, int bQuiet)
474 return LoadStringRCEx(-1, resId, pBuffer, iBufLen, bQuiet, NULL);
477 HRESULT WINAPI CorBindToRuntimeEx(LPWSTR szVersion, LPWSTR szBuildFlavor, DWORD nflags, REFCLSID rslsid,
478 REFIID riid, LPVOID *ppv)
480 FIXME("%s %s %d %s %s %p\n", debugstr_w(szVersion), debugstr_w(szBuildFlavor), nflags, debugstr_guid( rslsid ),
481 debugstr_guid( riid ), ppv);
483 if(IsEqualGUID( riid, &IID_ICorRuntimeHost ))
485 *ppv = create_corruntimehost();
492 HRESULT WINAPI CorBindToCurrentRuntime(LPCWSTR filename, REFCLSID rclsid, REFIID riid, LPVOID *ppv)
494 FIXME("(%s, %s, %s, %p): stub\n", debugstr_w(filename), debugstr_guid(rclsid), debugstr_guid(riid), ppv);
498 STDAPI ClrCreateManagedInstance(LPCWSTR pTypeName, REFIID riid, void **ppObject)
500 FIXME("(%s,%s,%p)\n", debugstr_w(pTypeName), debugstr_guid(riid), ppObject);
504 BOOL WINAPI StrongNameSignatureVerification(LPCWSTR filename, DWORD inFlags, DWORD* pOutFlags)
506 FIXME("(%s, 0x%X, %p): stub\n", debugstr_w(filename), inFlags, pOutFlags);
510 BOOL WINAPI StrongNameSignatureVerificationEx(LPCWSTR filename, BOOL forceVerification, BOOL* pVerified)
512 FIXME("(%s, %u, %p): stub\n", debugstr_w(filename), forceVerification, pVerified);
516 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
518 FIXME("(%s, %s, %p): stub\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
525 HRESULT WINAPI DllRegisterServer(void)
531 HRESULT WINAPI DllUnregisterServer(void)
537 HRESULT WINAPI DllCanUnloadNow(VOID)
542 INT WINAPI ND_RU1( const void *ptr, INT offset )
544 return *((const BYTE *)ptr + offset);
547 INT WINAPI ND_RI2( const void *ptr, INT offset )
549 return *(const SHORT *)((const BYTE *)ptr + offset);
552 INT WINAPI ND_RI4( const void *ptr, INT offset )
554 return *(const INT *)((const BYTE *)ptr + offset);
557 INT64 WINAPI ND_RI8( const void *ptr, INT offset )
559 return *(const INT64 *)((const BYTE *)ptr + offset);
562 void WINAPI ND_WU1( void *ptr, INT offset, BYTE val )
564 *((BYTE *)ptr + offset) = val;
567 void WINAPI ND_WI2( void *ptr, INT offset, SHORT val )
569 *(SHORT *)((BYTE *)ptr + offset) = val;
572 void WINAPI ND_WI4( void *ptr, INT offset, INT val )
574 *(INT *)((BYTE *)ptr + offset) = val;
577 void WINAPI ND_WI8( void *ptr, INT offset, INT64 val )
579 *(INT64 *)((BYTE *)ptr + offset) = val;
582 void WINAPI ND_CopyObjDst( const void *src, void *dst, INT offset, INT size )
584 memcpy( (BYTE *)dst + offset, src, size );
587 void WINAPI ND_CopyObjSrc( const void *src, INT offset, void *dst, INT size )
589 memcpy( dst, (const BYTE *)src + offset, size );