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