8b72ec83c08656a87f76ad2023eabd5ec0872e46
[reactos.git] / reactos / dll / win32 / mscoree / mscoree_main.c
1 /*
2 * Implementation of mscoree.dll
3 * Microsoft Component Object Runtime Execution Engine
4 *
5 * Copyright 2006 Paul Chitescu
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include <stdarg.h>
23
24 #define COBJMACROS
25 #include "wine/unicode.h"
26 #include "wine/library.h"
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "winnls.h"
31 #include "winreg.h"
32 #include "ole2.h"
33 #include "ocidl.h"
34 #include "shellapi.h"
35
36 #include "initguid.h"
37 #include "msxml2.h"
38 #include "corerror.h"
39 #include "cor.h"
40 #include "mscoree.h"
41 #include "corhdr.h"
42 #include "cordebug.h"
43 #include "metahost.h"
44 #include "fusion.h"
45 #include "wine/list.h"
46 #include "mscoree_private.h"
47 #include "rpcproxy.h"
48
49 #include "wine/debug.h"
50
51 WINE_DEFAULT_DEBUG_CHANNEL( mscoree );
52
53 static HINSTANCE MSCOREE_hInstance;
54
55 char *WtoA(LPCWSTR wstr)
56 {
57 int length;
58 char *result;
59
60 length = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
61
62 result = HeapAlloc(GetProcessHeap(), 0, length);
63
64 if (result)
65 WideCharToMultiByte(CP_UTF8, 0, wstr, -1, result, length, NULL, NULL);
66
67 return result;
68 }
69
70 static BOOL get_install_root(LPWSTR install_dir)
71 {
72 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};
73 const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
74
75 DWORD len;
76 HKEY key;
77
78 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
79 return FALSE;
80
81 len = MAX_PATH;
82 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
83 {
84 RegCloseKey(key);
85 return FALSE;
86 }
87 RegCloseKey(key);
88
89 return TRUE;
90 }
91
92 HRESULT WINAPI CorBindToRuntimeHost(LPCWSTR pwszVersion, LPCWSTR pwszBuildFlavor,
93 LPCWSTR pwszHostConfigFile, VOID *pReserved,
94 DWORD startupFlags, REFCLSID rclsid,
95 REFIID riid, LPVOID *ppv)
96 {
97 HRESULT ret;
98 ICLRRuntimeInfo *info;
99
100 TRACE("(%s, %s, %s, %p, %d, %s, %s, %p)\n", debugstr_w(pwszVersion),
101 debugstr_w(pwszBuildFlavor), debugstr_w(pwszHostConfigFile), pReserved,
102 startupFlags, debugstr_guid(rclsid), debugstr_guid(riid), ppv);
103
104 *ppv = NULL;
105
106 ret = get_runtime_info(NULL, pwszVersion, pwszHostConfigFile, startupFlags, 0, TRUE, &info);
107
108 if (SUCCEEDED(ret))
109 {
110 ret = ICLRRuntimeInfo_GetInterface(info, rclsid, riid, ppv);
111
112 ICLRRuntimeInfo_Release(info);
113 }
114
115 return ret;
116 }
117
118 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
119 {
120 TRACE("(%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
121
122 MSCOREE_hInstance = hinstDLL;
123
124 switch (fdwReason)
125 {
126 case DLL_WINE_PREATTACH:
127 return FALSE; /* prefer native version */
128 case DLL_PROCESS_ATTACH:
129 DisableThreadLibraryCalls(hinstDLL);
130 break;
131 case DLL_PROCESS_DETACH:
132 expect_no_runtimes();
133 break;
134 }
135 return TRUE;
136 }
137
138 BOOL WINAPI _CorDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
139 {
140 FIXME("(%p, %d, %p): stub\n", hinstDLL, fdwReason, lpvReserved);
141
142 switch (fdwReason)
143 {
144 case DLL_PROCESS_ATTACH:
145 DisableThreadLibraryCalls(hinstDLL);
146 break;
147 case DLL_PROCESS_DETACH:
148 break;
149 }
150 return TRUE;
151 }
152
153 __int32 WINAPI _CorExeMain2(PBYTE ptrMemory, DWORD cntMemory, LPWSTR imageName, LPWSTR loaderName, LPWSTR cmdLine)
154 {
155 TRACE("(%p, %u, %s, %s, %s)\n", ptrMemory, cntMemory, debugstr_w(imageName), debugstr_w(loaderName), debugstr_w(cmdLine));
156 FIXME("Directly running .NET applications not supported.\n");
157 return -1;
158 }
159
160 void WINAPI CorExitProcess(int exitCode)
161 {
162 TRACE("(%x)\n", exitCode);
163 unload_all_runtimes();
164 ExitProcess(exitCode);
165 }
166
167 VOID WINAPI _CorImageUnloading(PVOID imageBase)
168 {
169 TRACE("(%p): stub\n", imageBase);
170 }
171
172 HRESULT WINAPI _CorValidateImage(PVOID* imageBase, LPCWSTR imageName)
173 {
174 TRACE("(%p, %s): stub\n", imageBase, debugstr_w(imageName));
175 return E_FAIL;
176 }
177
178 HRESULT WINAPI GetCORSystemDirectory(LPWSTR pbuffer, DWORD cchBuffer, DWORD *dwLength)
179 {
180 ICLRRuntimeInfo *info;
181 HRESULT ret;
182
183 TRACE("(%p, %d, %p)!\n", pbuffer, cchBuffer, dwLength);
184
185 if (!dwLength || !pbuffer)
186 return E_POINTER;
187
188 ret = get_runtime_info(NULL, NULL, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, TRUE, &info);
189
190 if (SUCCEEDED(ret))
191 {
192 *dwLength = cchBuffer;
193 ret = ICLRRuntimeInfo_GetRuntimeDirectory(info, pbuffer, dwLength);
194
195 ICLRRuntimeInfo_Release(info);
196 }
197
198 return ret;
199 }
200
201 HRESULT WINAPI GetCORVersion(LPWSTR pbuffer, DWORD cchBuffer, DWORD *dwLength)
202 {
203 ICLRRuntimeInfo *info;
204 HRESULT ret;
205
206 TRACE("(%p, %d, %p)!\n", pbuffer, cchBuffer, dwLength);
207
208 if (!dwLength || !pbuffer)
209 return E_POINTER;
210
211 ret = get_runtime_info(NULL, NULL, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, TRUE, &info);
212
213 if (SUCCEEDED(ret))
214 {
215 *dwLength = cchBuffer;
216 ret = ICLRRuntimeInfo_GetVersionString(info, pbuffer, dwLength);
217
218 ICLRRuntimeInfo_Release(info);
219 }
220
221 return ret;
222 }
223
224 HRESULT WINAPI GetRequestedRuntimeInfo(LPCWSTR pExe, LPCWSTR pwszVersion, LPCWSTR pConfigurationFile,
225 DWORD startupFlags, DWORD runtimeInfoFlags, LPWSTR pDirectory, DWORD dwDirectory, DWORD *dwDirectoryLength,
226 LPWSTR pVersion, DWORD cchBuffer, DWORD *dwlength)
227 {
228 HRESULT ret;
229 ICLRRuntimeInfo *info;
230 DWORD length_dummy;
231
232 TRACE("(%s, %s, %s, 0x%08x, 0x%08x, %p, 0x%08x, %p, %p, 0x%08x, %p)\n", debugstr_w(pExe),
233 debugstr_w(pwszVersion), debugstr_w(pConfigurationFile), startupFlags, runtimeInfoFlags, pDirectory,
234 dwDirectory, dwDirectoryLength, pVersion, cchBuffer, dwlength);
235
236 if (!dwDirectoryLength) dwDirectoryLength = &length_dummy;
237
238 if (!dwlength) dwlength = &length_dummy;
239
240 ret = get_runtime_info(pExe, pwszVersion, pConfigurationFile, startupFlags, runtimeInfoFlags, TRUE, &info);
241
242 if (SUCCEEDED(ret))
243 {
244 *dwlength = cchBuffer;
245 ret = ICLRRuntimeInfo_GetVersionString(info, pVersion, dwlength);
246
247 if (SUCCEEDED(ret))
248 {
249 *dwDirectoryLength = dwDirectory;
250 ret = ICLRRuntimeInfo_GetRuntimeDirectory(info, pDirectory, dwDirectoryLength);
251 }
252
253 ICLRRuntimeInfo_Release(info);
254 }
255
256 return ret;
257 }
258
259 HRESULT WINAPI GetRequestedRuntimeVersion(LPWSTR pExe, LPWSTR pVersion, DWORD cchBuffer, DWORD *dwlength)
260 {
261 TRACE("(%s, %p, %d, %p)\n", debugstr_w(pExe), debugstr_w(pExe), cchBuffer, dwlength);
262
263 if(!dwlength)
264 return E_POINTER;
265
266 return GetRequestedRuntimeInfo(pExe, NULL, NULL, 0, 0, NULL, 0, NULL, pVersion, cchBuffer, dwlength);
267 }
268
269 HRESULT WINAPI GetRealProcAddress(LPCSTR procname, void **ppv)
270 {
271 FIXME("(%s, %p)\n", debugstr_a(procname), ppv);
272 return CLR_E_SHIM_RUNTIMEEXPORT;
273 }
274
275 HRESULT WINAPI GetFileVersion(LPCWSTR szFilename, LPWSTR szBuffer, DWORD cchBuffer, DWORD *dwLength)
276 {
277 TRACE("(%s, %p, %d, %p)\n", debugstr_w(szFilename), szBuffer, cchBuffer, dwLength);
278
279 if (!szFilename || !dwLength)
280 return E_POINTER;
281
282 *dwLength = cchBuffer;
283 return CLRMetaHost_GetVersionFromFile(0, szFilename, szBuffer, dwLength);
284 }
285
286 HRESULT WINAPI LoadLibraryShim( LPCWSTR szDllName, LPCWSTR szVersion, LPVOID pvReserved, HMODULE * phModDll)
287 {
288 HRESULT ret=S_OK;
289 WCHAR dll_filename[MAX_PATH];
290 WCHAR version[MAX_PATH];
291 static const WCHAR default_version[] = {'v','1','.','1','.','4','3','2','2',0};
292 static const WCHAR slash[] = {'\\',0};
293 DWORD dummy;
294
295 TRACE("(%p %s, %p, %p, %p)\n", szDllName, debugstr_w(szDllName), szVersion, pvReserved, phModDll);
296
297 if (!szDllName || !phModDll)
298 return E_POINTER;
299
300 if (!get_install_root(dll_filename))
301 {
302 ERR("error reading registry key for installroot\n");
303 dll_filename[0] = 0;
304 }
305 else
306 {
307 if (!szVersion)
308 {
309 ret = GetCORVersion(version, MAX_PATH, &dummy);
310 if (SUCCEEDED(ret))
311 szVersion = version;
312 else
313 szVersion = default_version;
314 }
315 strcatW(dll_filename, szVersion);
316 strcatW(dll_filename, slash);
317 }
318
319 strcatW(dll_filename, szDllName);
320
321 *phModDll = LoadLibraryW(dll_filename);
322
323 return *phModDll ? S_OK : E_HANDLE;
324 }
325
326 HRESULT WINAPI LockClrVersion(FLockClrVersionCallback hostCallback, FLockClrVersionCallback *pBeginHostSetup, FLockClrVersionCallback *pEndHostSetup)
327 {
328 FIXME("(%p %p %p): stub\n", hostCallback, pBeginHostSetup, pEndHostSetup);
329 return S_OK;
330 }
331
332 HRESULT WINAPI CoInitializeCor(DWORD fFlags)
333 {
334 FIXME("(0x%08x): stub\n", fFlags);
335 return S_OK;
336 }
337
338 HRESULT WINAPI GetAssemblyMDImport(LPCWSTR szFileName, REFIID riid, IUnknown **ppIUnk)
339 {
340 FIXME("(%p %s, %s, %p): stub\n", szFileName, debugstr_w(szFileName), debugstr_guid(riid), *ppIUnk);
341 return ERROR_CALL_NOT_IMPLEMENTED;
342 }
343
344 HRESULT WINAPI GetVersionFromProcess(HANDLE hProcess, LPWSTR pVersion, DWORD cchBuffer, DWORD *dwLength)
345 {
346 FIXME("(%p, %p, %d, %p): stub\n", hProcess, pVersion, cchBuffer, dwLength);
347 return E_NOTIMPL;
348 }
349
350 HRESULT WINAPI LoadStringRCEx(LCID culture, UINT resId, LPWSTR pBuffer, int iBufLen, int bQuiet, int* pBufLen)
351 {
352 HRESULT res = S_OK;
353 if ((iBufLen <= 0) || !pBuffer)
354 return E_INVALIDARG;
355 pBuffer[0] = 0;
356 if (resId) {
357 FIXME("(%d, %x, %p, %d, %d, %p): semi-stub\n", culture, resId, pBuffer, iBufLen, bQuiet, pBufLen);
358 res = E_NOTIMPL;
359 }
360 else
361 res = E_FAIL;
362 if (pBufLen)
363 *pBufLen = lstrlenW(pBuffer);
364 return res;
365 }
366
367 HRESULT WINAPI LoadStringRC(UINT resId, LPWSTR pBuffer, int iBufLen, int bQuiet)
368 {
369 return LoadStringRCEx(-1, resId, pBuffer, iBufLen, bQuiet, NULL);
370 }
371
372 HRESULT WINAPI CorBindToRuntimeEx(LPWSTR szVersion, LPWSTR szBuildFlavor, DWORD nflags, REFCLSID rslsid,
373 REFIID riid, LPVOID *ppv)
374 {
375 HRESULT ret;
376 ICLRRuntimeInfo *info;
377
378 TRACE("%s %s %d %s %s %p\n", debugstr_w(szVersion), debugstr_w(szBuildFlavor), nflags, debugstr_guid( rslsid ),
379 debugstr_guid( riid ), ppv);
380
381 *ppv = NULL;
382
383 ret = get_runtime_info(NULL, szVersion, NULL, nflags, RUNTIME_INFO_UPGRADE_VERSION, TRUE, &info);
384
385 if (SUCCEEDED(ret))
386 {
387 ret = ICLRRuntimeInfo_GetInterface(info, rslsid, riid, ppv);
388
389 ICLRRuntimeInfo_Release(info);
390 }
391
392 return ret;
393 }
394
395 HRESULT WINAPI CorBindToCurrentRuntime(LPCWSTR filename, REFCLSID rclsid, REFIID riid, LPVOID *ppv)
396 {
397 FIXME("(%s, %s, %s, %p): stub\n", debugstr_w(filename), debugstr_guid(rclsid), debugstr_guid(riid), ppv);
398 return E_NOTIMPL;
399 }
400
401 STDAPI ClrCreateManagedInstance(LPCWSTR pTypeName, REFIID riid, void **ppObject)
402 {
403 HRESULT ret;
404 ICLRRuntimeInfo *info;
405 RuntimeHost *host;
406 MonoObject *obj;
407 IUnknown *unk;
408
409 TRACE("(%s,%s,%p)\n", debugstr_w(pTypeName), debugstr_guid(riid), ppObject);
410
411 /* FIXME: How to determine which runtime version to use? */
412 ret = get_runtime_info(NULL, NULL, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, TRUE, &info);
413
414 if (SUCCEEDED(ret))
415 {
416 ret = ICLRRuntimeInfo_GetRuntimeHost(info, &host);
417
418 ICLRRuntimeInfo_Release(info);
419 }
420
421 if (SUCCEEDED(ret))
422 ret = RuntimeHost_CreateManagedInstance(host, pTypeName, NULL, &obj);
423
424 if (SUCCEEDED(ret))
425 ret = RuntimeHost_GetIUnknownForObject(host, obj, &unk);
426
427 if (SUCCEEDED(ret))
428 {
429 ret = IUnknown_QueryInterface(unk, riid, ppObject);
430 IUnknown_Release(unk);
431 }
432
433 return ret;
434 }
435
436 BOOL WINAPI StrongNameSignatureVerification(LPCWSTR filename, DWORD inFlags, DWORD* pOutFlags)
437 {
438 FIXME("(%s, 0x%X, %p): stub\n", debugstr_w(filename), inFlags, pOutFlags);
439 return FALSE;
440 }
441
442 BOOL WINAPI StrongNameSignatureVerificationEx(LPCWSTR filename, BOOL forceVerification, BOOL* pVerified)
443 {
444 FIXME("(%s, %u, %p): stub\n", debugstr_w(filename), forceVerification, pVerified);
445 return FALSE;
446 }
447
448 HRESULT WINAPI CreateConfigStream(LPCWSTR filename, IStream **stream)
449 {
450 FIXME("(%s, %p): stub\n", debugstr_w(filename), stream);
451 return E_NOTIMPL;
452 }
453
454 HRESULT WINAPI CreateDebuggingInterfaceFromVersion(int nDebugVersion, LPCWSTR version, IUnknown **ppv)
455 {
456 const WCHAR v2_0[] = {'v','2','.','0','.','5','0','7','2','7',0};
457 HRESULT hr = E_FAIL;
458 ICLRRuntimeInfo *runtimeinfo;
459
460 if(nDebugVersion < 1 || nDebugVersion > 4)
461 return E_INVALIDARG;
462
463 TRACE("(%d %s, %p): stub\n", nDebugVersion, debugstr_w(version), ppv);
464
465 if(!ppv)
466 return E_INVALIDARG;
467
468 *ppv = NULL;
469
470 if(strcmpW(version, v2_0) != 0)
471 {
472 FIXME("Currently .NET Version '%s' not support.\n", debugstr_w(version));
473 return E_INVALIDARG;
474 }
475
476 if(nDebugVersion != 3)
477 return E_INVALIDARG;
478
479 hr = CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)&runtimeinfo);
480 if(hr == S_OK)
481 {
482 hr = ICLRRuntimeInfo_GetInterface(runtimeinfo, &CLSID_CLRDebuggingLegacy, &IID_ICorDebug, (void**)ppv);
483
484 ICLRRuntimeInfo_Release(runtimeinfo);
485 }
486
487 if(!*ppv)
488 return E_FAIL;
489
490 return hr;
491 }
492
493 HRESULT WINAPI CLRCreateInstance(REFCLSID clsid, REFIID riid, LPVOID *ppInterface)
494 {
495 TRACE("(%s,%s,%p)\n", debugstr_guid(clsid), debugstr_guid(riid), ppInterface);
496
497 if (IsEqualGUID(clsid, &CLSID_CLRMetaHost))
498 return CLRMetaHost_CreateInstance(riid, ppInterface);
499
500 FIXME("not implemented for class %s\n", debugstr_guid(clsid));
501
502 return CLASS_E_CLASSNOTAVAILABLE;
503 }
504
505 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
506 {
507 FIXME("(%s, %s, %p): stub\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
508 if(!ppv)
509 return E_INVALIDARG;
510
511 return E_NOTIMPL;
512 }
513
514 HRESULT WINAPI DllRegisterServer(void)
515 {
516 return __wine_register_resources( MSCOREE_hInstance );
517 }
518
519 HRESULT WINAPI DllUnregisterServer(void)
520 {
521 return __wine_unregister_resources( MSCOREE_hInstance );
522 }
523
524 HRESULT WINAPI DllCanUnloadNow(VOID)
525 {
526 return S_FALSE;
527 }
528
529 INT WINAPI ND_RU1( const void *ptr, INT offset )
530 {
531 return *((const BYTE *)ptr + offset);
532 }
533
534 INT WINAPI ND_RI2( const void *ptr, INT offset )
535 {
536 return *(const SHORT *)((const BYTE *)ptr + offset);
537 }
538
539 INT WINAPI ND_RI4( const void *ptr, INT offset )
540 {
541 return *(const INT *)((const BYTE *)ptr + offset);
542 }
543
544 INT64 WINAPI ND_RI8( const void *ptr, INT offset )
545 {
546 return *(const INT64 *)((const BYTE *)ptr + offset);
547 }
548
549 void WINAPI ND_WU1( void *ptr, INT offset, BYTE val )
550 {
551 *((BYTE *)ptr + offset) = val;
552 }
553
554 void WINAPI ND_WI2( void *ptr, INT offset, SHORT val )
555 {
556 *(SHORT *)((BYTE *)ptr + offset) = val;
557 }
558
559 void WINAPI ND_WI4( void *ptr, INT offset, INT val )
560 {
561 *(INT *)((BYTE *)ptr + offset) = val;
562 }
563
564 void WINAPI ND_WI8( void *ptr, INT offset, INT64 val )
565 {
566 *(INT64 *)((BYTE *)ptr + offset) = val;
567 }
568
569 void WINAPI ND_CopyObjDst( const void *src, void *dst, INT offset, INT size )
570 {
571 memcpy( (BYTE *)dst + offset, src, size );
572 }
573
574 void WINAPI ND_CopyObjSrc( const void *src, INT offset, void *dst, INT size )
575 {
576 memcpy( dst, (const BYTE *)src + offset, size );
577 }