wined3d: Figure out the index data pointer inside drawPrimitive().
[wine/multimedia.git] / dlls / mscoree / mscoree_main.c
blob1e06909b45e14036de401defb5072e0f876c91b1
1 /*
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
22 #include <stdarg.h>
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"
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"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL( mscoree );
53 static HINSTANCE MSCOREE_hInstance;
55 typedef HRESULT (*fnCreateInstance)(REFIID riid, LPVOID *ppObj);
57 char *WtoA(LPCWSTR wstr)
59 int length;
60 char *result;
62 length = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
64 result = HeapAlloc(GetProcessHeap(), 0, length);
66 if (result)
67 WideCharToMultiByte(CP_UTF8, 0, wstr, -1, result, length, NULL, NULL);
69 return result;
72 static BOOL get_install_root(LPWSTR install_dir)
74 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};
75 const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
77 DWORD len;
78 HKEY key;
80 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
81 return FALSE;
83 len = MAX_PATH;
84 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
86 RegCloseKey(key);
87 return FALSE;
89 RegCloseKey(key);
91 return TRUE;
94 typedef struct mscorecf
96 IClassFactory IClassFactory_iface;
97 LONG ref;
99 fnCreateInstance pfnCreateInstance;
101 CLSID clsid;
102 } mscorecf;
104 static inline mscorecf *impl_from_IClassFactory( IClassFactory *iface )
106 return CONTAINING_RECORD(iface, mscorecf, IClassFactory_iface);
109 static HRESULT WINAPI mscorecf_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppobj )
111 TRACE("%s %p\n", debugstr_guid(riid), ppobj);
113 if (IsEqualGUID(riid, &IID_IUnknown) ||
114 IsEqualGUID(riid, &IID_IClassFactory))
116 IClassFactory_AddRef( iface );
117 *ppobj = iface;
118 return S_OK;
121 ERR("interface %s not implemented\n", debugstr_guid(riid));
122 return E_NOINTERFACE;
125 static ULONG WINAPI mscorecf_AddRef(IClassFactory *iface )
127 mscorecf *This = impl_from_IClassFactory(iface);
128 ULONG ref = InterlockedIncrement(&This->ref);
130 TRACE("%p ref=%u\n", This, ref);
132 return ref;
135 static ULONG WINAPI mscorecf_Release(IClassFactory *iface )
137 mscorecf *This = impl_from_IClassFactory(iface);
138 ULONG ref = InterlockedDecrement(&This->ref);
140 TRACE("%p ref=%u\n", This, ref);
142 if (ref == 0)
144 HeapFree(GetProcessHeap(), 0, This);
147 return ref;
150 static HRESULT WINAPI mscorecf_CreateInstance(IClassFactory *iface,LPUNKNOWN pOuter,
151 REFIID riid, LPVOID *ppobj )
153 mscorecf *This = impl_from_IClassFactory( iface );
154 HRESULT hr;
155 IUnknown *punk;
157 TRACE("%p %s %p\n", pOuter, debugstr_guid(riid), ppobj );
159 *ppobj = NULL;
161 if (pOuter)
162 return CLASS_E_NOAGGREGATION;
164 hr = This->pfnCreateInstance( &This->clsid, (LPVOID*) &punk );
165 if (SUCCEEDED(hr))
167 hr = IUnknown_QueryInterface( punk, riid, ppobj );
169 IUnknown_Release( punk );
171 else
173 WARN("Cannot create an instance object. 0x%08x\n", hr);
175 return hr;
178 static HRESULT WINAPI mscorecf_LockServer(IClassFactory *iface, BOOL dolock)
180 FIXME("(%p)->(%d),stub!\n",iface,dolock);
181 return S_OK;
184 static const struct IClassFactoryVtbl mscorecf_vtbl =
186 mscorecf_QueryInterface,
187 mscorecf_AddRef,
188 mscorecf_Release,
189 mscorecf_CreateInstance,
190 mscorecf_LockServer
193 HRESULT WINAPI CorBindToRuntimeHost(LPCWSTR pwszVersion, LPCWSTR pwszBuildFlavor,
194 LPCWSTR pwszHostConfigFile, VOID *pReserved,
195 DWORD startupFlags, REFCLSID rclsid,
196 REFIID riid, LPVOID *ppv)
198 HRESULT ret;
199 ICLRRuntimeInfo *info;
201 TRACE("(%s, %s, %s, %p, %d, %s, %s, %p)\n", debugstr_w(pwszVersion),
202 debugstr_w(pwszBuildFlavor), debugstr_w(pwszHostConfigFile), pReserved,
203 startupFlags, debugstr_guid(rclsid), debugstr_guid(riid), ppv);
205 *ppv = NULL;
207 ret = get_runtime_info(NULL, pwszVersion, pwszHostConfigFile, startupFlags, 0, TRUE, &info);
209 if (SUCCEEDED(ret))
211 ret = ICLRRuntimeInfo_GetInterface(info, rclsid, riid, ppv);
213 ICLRRuntimeInfo_Release(info);
216 return ret;
219 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
221 TRACE("(%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
223 MSCOREE_hInstance = hinstDLL;
225 switch (fdwReason)
227 case DLL_WINE_PREATTACH:
228 return FALSE; /* prefer native version */
229 case DLL_PROCESS_ATTACH:
230 DisableThreadLibraryCalls(hinstDLL);
231 break;
232 case DLL_PROCESS_DETACH:
233 expect_no_runtimes();
234 break;
236 return TRUE;
239 BOOL WINAPI _CorDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
241 FIXME("(%p, %d, %p): stub\n", hinstDLL, fdwReason, lpvReserved);
243 switch (fdwReason)
245 case DLL_PROCESS_ATTACH:
246 DisableThreadLibraryCalls(hinstDLL);
247 break;
248 case DLL_PROCESS_DETACH:
249 break;
251 return TRUE;
254 __int32 WINAPI _CorExeMain2(PBYTE ptrMemory, DWORD cntMemory, LPWSTR imageName, LPWSTR loaderName, LPWSTR cmdLine)
256 TRACE("(%p, %u, %s, %s, %s)\n", ptrMemory, cntMemory, debugstr_w(imageName), debugstr_w(loaderName), debugstr_w(cmdLine));
257 FIXME("Directly running .NET applications not supported.\n");
258 return -1;
261 void WINAPI CorExitProcess(int exitCode)
263 TRACE("(%x)\n", exitCode);
264 unload_all_runtimes();
265 ExitProcess(exitCode);
268 VOID WINAPI _CorImageUnloading(PVOID imageBase)
270 TRACE("(%p): stub\n", imageBase);
273 HRESULT WINAPI _CorValidateImage(PVOID* imageBase, LPCWSTR imageName)
275 TRACE("(%p, %s): stub\n", imageBase, debugstr_w(imageName));
276 return E_FAIL;
279 HRESULT WINAPI GetCORSystemDirectory(LPWSTR pbuffer, DWORD cchBuffer, DWORD *dwLength)
281 ICLRRuntimeInfo *info;
282 HRESULT ret;
284 TRACE("(%p, %d, %p)!\n", pbuffer, cchBuffer, dwLength);
286 if (!dwLength || !pbuffer)
287 return E_POINTER;
289 ret = get_runtime_info(NULL, NULL, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, TRUE, &info);
291 if (SUCCEEDED(ret))
293 *dwLength = cchBuffer;
294 ret = ICLRRuntimeInfo_GetRuntimeDirectory(info, pbuffer, dwLength);
296 ICLRRuntimeInfo_Release(info);
299 return ret;
302 HRESULT WINAPI GetCORVersion(LPWSTR pbuffer, DWORD cchBuffer, DWORD *dwLength)
304 ICLRRuntimeInfo *info;
305 HRESULT ret;
307 TRACE("(%p, %d, %p)!\n", pbuffer, cchBuffer, dwLength);
309 if (!dwLength || !pbuffer)
310 return E_POINTER;
312 ret = get_runtime_info(NULL, NULL, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, TRUE, &info);
314 if (SUCCEEDED(ret))
316 *dwLength = cchBuffer;
317 ret = ICLRRuntimeInfo_GetVersionString(info, pbuffer, dwLength);
319 ICLRRuntimeInfo_Release(info);
322 return ret;
325 HRESULT WINAPI GetRequestedRuntimeInfo(LPCWSTR pExe, LPCWSTR pwszVersion, LPCWSTR pConfigurationFile,
326 DWORD startupFlags, DWORD runtimeInfoFlags, LPWSTR pDirectory, DWORD dwDirectory, DWORD *dwDirectoryLength,
327 LPWSTR pVersion, DWORD cchBuffer, DWORD *dwlength)
329 HRESULT ret;
330 ICLRRuntimeInfo *info;
331 DWORD length_dummy;
333 TRACE("(%s, %s, %s, 0x%08x, 0x%08x, %p, 0x%08x, %p, %p, 0x%08x, %p)\n", debugstr_w(pExe),
334 debugstr_w(pwszVersion), debugstr_w(pConfigurationFile), startupFlags, runtimeInfoFlags, pDirectory,
335 dwDirectory, dwDirectoryLength, pVersion, cchBuffer, dwlength);
337 if (!dwDirectoryLength) dwDirectoryLength = &length_dummy;
339 if (!dwlength) dwlength = &length_dummy;
341 ret = get_runtime_info(pExe, pwszVersion, pConfigurationFile, startupFlags, runtimeInfoFlags, TRUE, &info);
343 if (SUCCEEDED(ret))
345 *dwlength = cchBuffer;
346 ret = ICLRRuntimeInfo_GetVersionString(info, pVersion, dwlength);
348 if (SUCCEEDED(ret))
350 if(pwszVersion)
351 pVersion[0] = pwszVersion[0];
353 *dwDirectoryLength = dwDirectory;
354 ret = ICLRRuntimeInfo_GetRuntimeDirectory(info, pDirectory, dwDirectoryLength);
357 ICLRRuntimeInfo_Release(info);
360 return ret;
363 HRESULT WINAPI GetRequestedRuntimeVersion(LPWSTR pExe, LPWSTR pVersion, DWORD cchBuffer, DWORD *dwlength)
365 TRACE("(%s, %p, %d, %p)\n", debugstr_w(pExe), pVersion, cchBuffer, dwlength);
367 if(!dwlength)
368 return E_POINTER;
370 return GetRequestedRuntimeInfo(pExe, NULL, NULL, 0, 0, NULL, 0, NULL, pVersion, cchBuffer, dwlength);
373 HRESULT WINAPI GetRealProcAddress(LPCSTR procname, void **ppv)
375 FIXME("(%s, %p)\n", debugstr_a(procname), ppv);
376 return CLR_E_SHIM_RUNTIMEEXPORT;
379 HRESULT WINAPI GetFileVersion(LPCWSTR szFilename, LPWSTR szBuffer, DWORD cchBuffer, DWORD *dwLength)
381 TRACE("(%s, %p, %d, %p)\n", debugstr_w(szFilename), szBuffer, cchBuffer, dwLength);
383 if (!szFilename || !dwLength)
384 return E_POINTER;
386 *dwLength = cchBuffer;
387 return CLRMetaHost_GetVersionFromFile(0, szFilename, szBuffer, dwLength);
390 HRESULT WINAPI LoadLibraryShim( LPCWSTR szDllName, LPCWSTR szVersion, LPVOID pvReserved, HMODULE * phModDll)
392 HRESULT ret=S_OK;
393 WCHAR dll_filename[MAX_PATH];
394 WCHAR version[MAX_PATH];
395 static const WCHAR default_version[] = {'v','1','.','1','.','4','3','2','2',0};
396 static const WCHAR slash[] = {'\\',0};
397 DWORD dummy;
399 TRACE("(%p %s, %p, %p, %p)\n", szDllName, debugstr_w(szDllName), szVersion, pvReserved, phModDll);
401 if (!szDllName || !phModDll)
402 return E_POINTER;
404 if (!get_install_root(dll_filename))
406 ERR("error reading registry key for installroot\n");
407 dll_filename[0] = 0;
409 else
411 if (!szVersion)
413 ret = GetCORVersion(version, MAX_PATH, &dummy);
414 if (SUCCEEDED(ret))
415 szVersion = version;
416 else
417 szVersion = default_version;
419 strcatW(dll_filename, szVersion);
420 strcatW(dll_filename, slash);
423 strcatW(dll_filename, szDllName);
425 *phModDll = LoadLibraryW(dll_filename);
427 return *phModDll ? S_OK : E_HANDLE;
430 HRESULT WINAPI LockClrVersion(FLockClrVersionCallback hostCallback, FLockClrVersionCallback *pBeginHostSetup, FLockClrVersionCallback *pEndHostSetup)
432 FIXME("(%p %p %p): stub\n", hostCallback, pBeginHostSetup, pEndHostSetup);
433 return S_OK;
436 HRESULT WINAPI CoInitializeCor(DWORD fFlags)
438 FIXME("(0x%08x): stub\n", fFlags);
439 return S_OK;
442 HRESULT WINAPI GetAssemblyMDImport(LPCWSTR szFileName, REFIID riid, IUnknown **ppIUnk)
444 FIXME("(%p %s, %s, %p): stub\n", szFileName, debugstr_w(szFileName), debugstr_guid(riid), *ppIUnk);
445 return ERROR_CALL_NOT_IMPLEMENTED;
448 HRESULT WINAPI GetVersionFromProcess(HANDLE hProcess, LPWSTR pVersion, DWORD cchBuffer, DWORD *dwLength)
450 FIXME("(%p, %p, %d, %p): stub\n", hProcess, pVersion, cchBuffer, dwLength);
451 return E_NOTIMPL;
454 HRESULT WINAPI LoadStringRCEx(LCID culture, UINT resId, LPWSTR pBuffer, int iBufLen, int bQuiet, int* pBufLen)
456 HRESULT res = S_OK;
457 if ((iBufLen <= 0) || !pBuffer)
458 return E_INVALIDARG;
459 pBuffer[0] = 0;
460 if (resId) {
461 FIXME("(%d, %x, %p, %d, %d, %p): semi-stub\n", culture, resId, pBuffer, iBufLen, bQuiet, pBufLen);
462 res = E_NOTIMPL;
464 else
465 res = E_FAIL;
466 if (pBufLen)
467 *pBufLen = lstrlenW(pBuffer);
468 return res;
471 HRESULT WINAPI LoadStringRC(UINT resId, LPWSTR pBuffer, int iBufLen, int bQuiet)
473 return LoadStringRCEx(-1, resId, pBuffer, iBufLen, bQuiet, NULL);
476 HRESULT WINAPI CorBindToRuntimeEx(LPWSTR szVersion, LPWSTR szBuildFlavor, DWORD nflags, REFCLSID rslsid,
477 REFIID riid, LPVOID *ppv)
479 HRESULT ret;
480 ICLRRuntimeInfo *info;
482 TRACE("%s %s %d %s %s %p\n", debugstr_w(szVersion), debugstr_w(szBuildFlavor), nflags, debugstr_guid( rslsid ),
483 debugstr_guid( riid ), ppv);
485 *ppv = NULL;
487 ret = get_runtime_info(NULL, szVersion, NULL, nflags, RUNTIME_INFO_UPGRADE_VERSION, TRUE, &info);
489 if (SUCCEEDED(ret))
491 ret = ICLRRuntimeInfo_GetInterface(info, rslsid, riid, ppv);
493 ICLRRuntimeInfo_Release(info);
496 return ret;
499 HRESULT WINAPI CorBindToCurrentRuntime(LPCWSTR filename, REFCLSID rclsid, REFIID riid, LPVOID *ppv)
501 FIXME("(%s, %s, %s, %p): stub\n", debugstr_w(filename), debugstr_guid(rclsid), debugstr_guid(riid), ppv);
502 return E_NOTIMPL;
505 STDAPI ClrCreateManagedInstance(LPCWSTR pTypeName, REFIID riid, void **ppObject)
507 HRESULT ret;
508 ICLRRuntimeInfo *info;
509 RuntimeHost *host;
510 MonoObject *obj;
511 IUnknown *unk;
513 TRACE("(%s,%s,%p)\n", debugstr_w(pTypeName), debugstr_guid(riid), ppObject);
515 /* FIXME: How to determine which runtime version to use? */
516 ret = get_runtime_info(NULL, NULL, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, TRUE, &info);
518 if (SUCCEEDED(ret))
520 ret = ICLRRuntimeInfo_GetRuntimeHost(info, &host);
522 ICLRRuntimeInfo_Release(info);
525 if (SUCCEEDED(ret))
526 ret = RuntimeHost_CreateManagedInstance(host, pTypeName, NULL, &obj);
528 if (SUCCEEDED(ret))
529 ret = RuntimeHost_GetIUnknownForObject(host, obj, &unk);
531 if (SUCCEEDED(ret))
533 ret = IUnknown_QueryInterface(unk, riid, ppObject);
534 IUnknown_Release(unk);
537 return ret;
540 BOOL WINAPI StrongNameSignatureVerification(LPCWSTR filename, DWORD inFlags, DWORD* pOutFlags)
542 FIXME("(%s, 0x%X, %p): stub\n", debugstr_w(filename), inFlags, pOutFlags);
543 return FALSE;
546 BOOL WINAPI StrongNameSignatureVerificationEx(LPCWSTR filename, BOOL forceVerification, BOOL* pVerified)
548 FIXME("(%s, %u, %p): stub\n", debugstr_w(filename), forceVerification, pVerified);
549 return FALSE;
552 HRESULT WINAPI CreateConfigStream(LPCWSTR filename, IStream **stream)
554 FIXME("(%s, %p): stub\n", debugstr_w(filename), stream);
555 return E_NOTIMPL;
558 HRESULT WINAPI CreateDebuggingInterfaceFromVersion(int nDebugVersion, LPCWSTR version, IUnknown **ppv)
560 const WCHAR v2_0[] = {'v','2','.','0','.','5','0','7','2','7',0};
561 HRESULT hr = E_FAIL;
562 ICLRRuntimeInfo *runtimeinfo;
564 if(nDebugVersion < 1 || nDebugVersion > 4)
565 return E_INVALIDARG;
567 TRACE("(%d %s, %p): stub\n", nDebugVersion, debugstr_w(version), ppv);
569 if(!ppv)
570 return E_INVALIDARG;
572 *ppv = NULL;
574 if(strcmpW(version, v2_0) != 0)
576 FIXME("Currently .NET Version '%s' not support.\n", debugstr_w(version));
577 return E_INVALIDARG;
580 if(nDebugVersion != 3)
581 return E_INVALIDARG;
583 hr = CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)&runtimeinfo);
584 if(hr == S_OK)
586 hr = ICLRRuntimeInfo_GetInterface(runtimeinfo, &CLSID_CLRDebuggingLegacy, &IID_ICorDebug, (void**)ppv);
588 ICLRRuntimeInfo_Release(runtimeinfo);
591 if(!*ppv)
592 return E_FAIL;
594 return hr;
597 HRESULT WINAPI CLRCreateInstance(REFCLSID clsid, REFIID riid, LPVOID *ppInterface)
599 TRACE("(%s,%s,%p)\n", debugstr_guid(clsid), debugstr_guid(riid), ppInterface);
601 if (IsEqualGUID(clsid, &CLSID_CLRMetaHost))
602 return CLRMetaHost_CreateInstance(riid, ppInterface);
604 FIXME("not implemented for class %s\n", debugstr_guid(clsid));
606 return CLASS_E_CLASSNOTAVAILABLE;
609 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
611 mscorecf *This;
612 HRESULT hr;
614 TRACE("(%s, %s, %p): stub\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
616 if(!ppv)
617 return E_INVALIDARG;
619 This = HeapAlloc(GetProcessHeap(), 0, sizeof(mscorecf));
621 This->IClassFactory_iface.lpVtbl = &mscorecf_vtbl;
622 This->pfnCreateInstance = &create_monodata;
623 This->ref = 1;
624 This->clsid = *rclsid;
626 hr = IClassFactory_QueryInterface( &This->IClassFactory_iface, riid, ppv );
627 IClassFactory_Release(&This->IClassFactory_iface);
629 return hr;
632 HRESULT WINAPI DllRegisterServer(void)
634 return __wine_register_resources( MSCOREE_hInstance );
637 HRESULT WINAPI DllUnregisterServer(void)
639 return __wine_unregister_resources( MSCOREE_hInstance );
642 HRESULT WINAPI DllCanUnloadNow(VOID)
644 return S_FALSE;
647 void WINAPI CoEEShutDownCOM(void)
649 FIXME("stub.\n");
652 INT WINAPI ND_RU1( const void *ptr, INT offset )
654 return *((const BYTE *)ptr + offset);
657 INT WINAPI ND_RI2( const void *ptr, INT offset )
659 return *(const SHORT *)((const BYTE *)ptr + offset);
662 INT WINAPI ND_RI4( const void *ptr, INT offset )
664 return *(const INT *)((const BYTE *)ptr + offset);
667 INT64 WINAPI ND_RI8( const void *ptr, INT offset )
669 return *(const INT64 *)((const BYTE *)ptr + offset);
672 void WINAPI ND_WU1( void *ptr, INT offset, BYTE val )
674 *((BYTE *)ptr + offset) = val;
677 void WINAPI ND_WI2( void *ptr, INT offset, SHORT val )
679 *(SHORT *)((BYTE *)ptr + offset) = val;
682 void WINAPI ND_WI4( void *ptr, INT offset, INT val )
684 *(INT *)((BYTE *)ptr + offset) = val;
687 void WINAPI ND_WI8( void *ptr, INT offset, INT64 val )
689 *(INT64 *)((BYTE *)ptr + offset) = val;
692 void WINAPI ND_CopyObjDst( const void *src, void *dst, INT offset, INT size )
694 memcpy( (BYTE *)dst + offset, src, size );
697 void WINAPI ND_CopyObjSrc( const void *src, INT offset, void *dst, INT size )
699 memcpy( dst, (const BYTE *)src + offset, size );