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