mscoree: Add stub CreateDebuggingInterfaceFromVersion.
[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 GetRealProcAddress(LPCSTR procname, void **ppv)
253 {
254     FIXME("(%s, %p)\n", debugstr_a(procname), ppv);
255     return CLR_E_SHIM_RUNTIMEEXPORT;
256 }
257
258 HRESULT WINAPI GetFileVersion(LPCWSTR szFilename, LPWSTR szBuffer, DWORD cchBuffer, DWORD *dwLength)
259 {
260     TRACE("(%s, %p, %d, %p)\n", debugstr_w(szFilename), szBuffer, cchBuffer, dwLength);
261
262     if (!szFilename || !dwLength)
263         return E_POINTER;
264
265     *dwLength = cchBuffer;
266     return CLRMetaHost_GetVersionFromFile(0, szFilename, szBuffer, dwLength);
267 }
268
269 HRESULT WINAPI LoadLibraryShim( LPCWSTR szDllName, LPCWSTR szVersion, LPVOID pvReserved, HMODULE * phModDll)
270 {
271     HRESULT ret=S_OK;
272     WCHAR dll_filename[MAX_PATH];
273     WCHAR version[MAX_PATH];
274     static const WCHAR default_version[] = {'v','1','.','1','.','4','3','2','2',0};
275     static const WCHAR slash[] = {'\\',0};
276     DWORD dummy;
277
278     TRACE("(%p %s, %p, %p, %p)\n", szDllName, debugstr_w(szDllName), szVersion, pvReserved, phModDll);
279
280     if (!szDllName || !phModDll)
281         return E_POINTER;
282
283     if (!get_install_root(dll_filename))
284     {
285         ERR("error reading registry key for installroot\n");
286         dll_filename[0] = 0;
287     }
288     else
289     {
290         if (!szVersion)
291         {
292             ret = GetCORVersion(version, MAX_PATH, &dummy);
293             if (SUCCEEDED(ret))
294                 szVersion = version;
295             else
296                 szVersion = default_version;
297         }
298         strcatW(dll_filename, szVersion);
299         strcatW(dll_filename, slash);
300     }
301
302     strcatW(dll_filename, szDllName);
303
304     *phModDll = LoadLibraryW(dll_filename);
305
306     return *phModDll ? S_OK : E_HANDLE;
307 }
308
309 HRESULT WINAPI LockClrVersion(FLockClrVersionCallback hostCallback, FLockClrVersionCallback *pBeginHostSetup, FLockClrVersionCallback *pEndHostSetup)
310 {
311     FIXME("(%p %p %p): stub\n", hostCallback, pBeginHostSetup, pEndHostSetup);
312     return S_OK;
313 }
314
315 HRESULT WINAPI CoInitializeCor(DWORD fFlags)
316 {
317     FIXME("(0x%08x): stub\n", fFlags);
318     return S_OK;
319 }
320
321 HRESULT WINAPI GetAssemblyMDImport(LPCWSTR szFileName, REFIID riid, IUnknown **ppIUnk)
322 {
323     FIXME("(%p %s, %s, %p): stub\n", szFileName, debugstr_w(szFileName), debugstr_guid(riid), *ppIUnk);
324     return ERROR_CALL_NOT_IMPLEMENTED;
325 }
326
327 HRESULT WINAPI GetVersionFromProcess(HANDLE hProcess, LPWSTR pVersion, DWORD cchBuffer, DWORD *dwLength)
328 {
329     FIXME("(%p, %p, %d, %p): stub\n", hProcess, pVersion, cchBuffer, dwLength);
330     return E_NOTIMPL;
331 }
332
333 HRESULT WINAPI LoadStringRCEx(LCID culture, UINT resId, LPWSTR pBuffer, int iBufLen, int bQuiet, int* pBufLen)
334 {
335     HRESULT res = S_OK;
336     if ((iBufLen <= 0) || !pBuffer)
337         return E_INVALIDARG;
338     pBuffer[0] = 0;
339     if (resId) {
340         FIXME("(%d, %x, %p, %d, %d, %p): semi-stub\n", culture, resId, pBuffer, iBufLen, bQuiet, pBufLen);
341         res = E_NOTIMPL;
342     }
343     else
344         res = E_FAIL;
345     if (pBufLen)
346         *pBufLen = lstrlenW(pBuffer);
347     return res;
348 }
349
350 HRESULT WINAPI LoadStringRC(UINT resId, LPWSTR pBuffer, int iBufLen, int bQuiet)
351 {
352     return LoadStringRCEx(-1, resId, pBuffer, iBufLen, bQuiet, NULL);
353 }
354
355 HRESULT WINAPI CorBindToRuntimeEx(LPWSTR szVersion, LPWSTR szBuildFlavor, DWORD nflags, REFCLSID rslsid,
356                                   REFIID riid, LPVOID *ppv)
357 {
358     HRESULT ret;
359     ICLRRuntimeInfo *info;
360
361     TRACE("%s %s %d %s %s %p\n", debugstr_w(szVersion), debugstr_w(szBuildFlavor), nflags, debugstr_guid( rslsid ),
362           debugstr_guid( riid ), ppv);
363
364     *ppv = NULL;
365
366     ret = get_runtime_info(NULL, szVersion, NULL, nflags, 0, TRUE, &info);
367
368     if (SUCCEEDED(ret))
369     {
370         ret = ICLRRuntimeInfo_GetInterface(info, rslsid, riid, ppv);
371
372         ICLRRuntimeInfo_Release(info);
373     }
374
375     return ret;
376 }
377
378 HRESULT WINAPI CorBindToCurrentRuntime(LPCWSTR filename, REFCLSID rclsid, REFIID riid, LPVOID *ppv)
379 {
380     FIXME("(%s, %s, %s, %p): stub\n", debugstr_w(filename), debugstr_guid(rclsid), debugstr_guid(riid), ppv);
381     return E_NOTIMPL;
382 }
383
384 STDAPI ClrCreateManagedInstance(LPCWSTR pTypeName, REFIID riid, void **ppObject)
385 {
386     HRESULT ret;
387     ICLRRuntimeInfo *info;
388     RuntimeHost *host;
389     MonoObject *obj;
390     IUnknown *unk;
391
392     TRACE("(%s,%s,%p)\n", debugstr_w(pTypeName), debugstr_guid(riid), ppObject);
393
394     /* FIXME: How to determine which runtime version to use? */
395     ret = get_runtime_info(NULL, NULL, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, TRUE, &info);
396
397     if (SUCCEEDED(ret))
398     {
399         ret = ICLRRuntimeInfo_GetRuntimeHost(info, &host);
400
401         ICLRRuntimeInfo_Release(info);
402     }
403
404     if (SUCCEEDED(ret))
405         ret = RuntimeHost_CreateManagedInstance(host, pTypeName, NULL, &obj);
406
407     if (SUCCEEDED(ret))
408         ret = RuntimeHost_GetIUnknownForObject(host, obj, &unk);
409
410     if (SUCCEEDED(ret))
411     {
412         ret = IUnknown_QueryInterface(unk, riid, ppObject);
413         IUnknown_Release(unk);
414     }
415
416     return ret;
417 }
418
419 BOOL WINAPI StrongNameSignatureVerification(LPCWSTR filename, DWORD inFlags, DWORD* pOutFlags)
420 {
421     FIXME("(%s, 0x%X, %p): stub\n", debugstr_w(filename), inFlags, pOutFlags);
422     return FALSE;
423 }
424
425 BOOL WINAPI StrongNameSignatureVerificationEx(LPCWSTR filename, BOOL forceVerification, BOOL* pVerified)
426 {
427     FIXME("(%s, %u, %p): stub\n", debugstr_w(filename), forceVerification, pVerified);
428     return FALSE;
429 }
430
431 HRESULT WINAPI CreateConfigStream(LPCWSTR filename, IStream **stream)
432 {
433     FIXME("(%s, %p): stub\n", debugstr_w(filename), stream);
434     return E_NOTIMPL;
435 }
436
437 HRESULT WINAPI CreateDebuggingInterfaceFromVersion(int nDebugVersion, LPCWSTR version, IUnknown **ppIUnk)
438 {
439     FIXME("(%d %s, %p): stub\n", nDebugVersion, debugstr_w(version), ppIUnk);
440     return E_NOTIMPL;
441 }
442
443 HRESULT WINAPI CLRCreateInstance(REFCLSID clsid, REFIID riid, LPVOID *ppInterface)
444 {
445     TRACE("(%s,%s,%p)\n", debugstr_guid(clsid), debugstr_guid(riid), ppInterface);
446
447     if (IsEqualGUID(clsid, &CLSID_CLRMetaHost))
448         return CLRMetaHost_CreateInstance(riid, ppInterface);
449
450     FIXME("not implemented for class %s\n", debugstr_guid(clsid));
451
452     return CLASS_E_CLASSNOTAVAILABLE;
453 }
454
455 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
456 {
457     FIXME("(%s, %s, %p): stub\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
458     if(!ppv)
459         return E_INVALIDARG;
460
461     return E_NOTIMPL;
462 }
463
464 HRESULT WINAPI DllRegisterServer(void)
465 {
466     FIXME("\n");
467     return S_OK;
468 }
469
470 HRESULT WINAPI DllUnregisterServer(void)
471 {
472     FIXME("\n");
473     return S_OK;
474 }
475
476 HRESULT WINAPI DllCanUnloadNow(VOID)
477 {
478     return S_FALSE;
479 }
480
481 INT WINAPI ND_RU1( const void *ptr, INT offset )
482 {
483     return *((const BYTE *)ptr + offset);
484 }
485
486 INT WINAPI ND_RI2( const void *ptr, INT offset )
487 {
488     return *(const SHORT *)((const BYTE *)ptr + offset);
489 }
490
491 INT WINAPI ND_RI4( const void *ptr, INT offset )
492 {
493     return *(const INT *)((const BYTE *)ptr + offset);
494 }
495
496 INT64 WINAPI ND_RI8( const void *ptr, INT offset )
497 {
498     return *(const INT64 *)((const BYTE *)ptr + offset);
499 }
500
501 void WINAPI ND_WU1( void *ptr, INT offset, BYTE val )
502 {
503     *((BYTE *)ptr + offset) = val;
504 }
505
506 void WINAPI ND_WI2( void *ptr, INT offset, SHORT val )
507 {
508     *(SHORT *)((BYTE *)ptr + offset) = val;
509 }
510
511 void WINAPI ND_WI4( void *ptr, INT offset, INT val )
512 {
513     *(INT *)((BYTE *)ptr + offset) = val;
514 }
515
516 void WINAPI ND_WI8( void *ptr, INT offset, INT64 val )
517 {
518     *(INT64 *)((BYTE *)ptr + offset) = val;
519 }
520
521 void WINAPI ND_CopyObjDst( const void *src, void *dst, INT offset, INT size )
522 {
523     memcpy( (BYTE *)dst + offset, src, size );
524 }
525
526 void WINAPI ND_CopyObjSrc( const void *src, INT offset, void *dst, INT size )
527 {
528     memcpy( dst, (const BYTE *)src + offset, size );
529 }