mscoree: Implement GetFileVersion.
[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 LONG dll_refs = 0;
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         unload_all_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     FIXME("(%x) stub\n", exitCode);
158     ExitProcess(exitCode);
159 }
160
161 VOID WINAPI _CorImageUnloading(PVOID imageBase)
162 {
163     TRACE("(%p): stub\n", imageBase);
164 }
165
166 HRESULT WINAPI _CorValidateImage(PVOID* imageBase, LPCWSTR imageName)
167 {
168     TRACE("(%p, %s): stub\n", imageBase, debugstr_w(imageName));
169     return E_FAIL;
170 }
171
172 HRESULT WINAPI GetCORSystemDirectory(LPWSTR pbuffer, DWORD cchBuffer, DWORD *dwLength)
173 {
174     ICLRRuntimeInfo *info;
175     HRESULT ret;
176
177     TRACE("(%p, %d, %p)!\n", pbuffer, cchBuffer, dwLength);
178
179     if (!dwLength || !pbuffer)
180         return E_POINTER;
181
182     ret = get_runtime_info(NULL, NULL, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, TRUE, &info);
183
184     if (SUCCEEDED(ret))
185     {
186         *dwLength = cchBuffer;
187         ret = ICLRRuntimeInfo_GetRuntimeDirectory(info, pbuffer, dwLength);
188
189         ICLRRuntimeInfo_Release(info);
190     }
191
192     return ret;
193 }
194
195 HRESULT WINAPI GetCORVersion(LPWSTR pbuffer, DWORD cchBuffer, DWORD *dwLength)
196 {
197     ICLRRuntimeInfo *info;
198     HRESULT ret;
199
200     TRACE("(%p, %d, %p)!\n", pbuffer, cchBuffer, dwLength);
201
202     if (!dwLength || !pbuffer)
203         return E_POINTER;
204
205     ret = get_runtime_info(NULL, NULL, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, TRUE, &info);
206
207     if (SUCCEEDED(ret))
208     {
209         *dwLength = cchBuffer;
210         ret = ICLRRuntimeInfo_GetVersionString(info, pbuffer, dwLength);
211
212         ICLRRuntimeInfo_Release(info);
213     }
214
215     return ret;
216 }
217
218 HRESULT WINAPI GetRequestedRuntimeInfo(LPCWSTR pExe, LPCWSTR pwszVersion, LPCWSTR pConfigurationFile,
219     DWORD startupFlags, DWORD runtimeInfoFlags, LPWSTR pDirectory, DWORD dwDirectory, DWORD *dwDirectoryLength,
220     LPWSTR pVersion, DWORD cchBuffer, DWORD *dwlength)
221 {
222     HRESULT ret;
223     ICLRRuntimeInfo *info;
224     DWORD length_dummy;
225
226     TRACE("(%s, %s, %s, 0x%08x, 0x%08x, %p, 0x%08x, %p, %p, 0x%08x, %p)\n", debugstr_w(pExe),
227           debugstr_w(pwszVersion), debugstr_w(pConfigurationFile), startupFlags, runtimeInfoFlags, pDirectory,
228           dwDirectory, dwDirectoryLength, pVersion, cchBuffer, dwlength);
229
230     if (!dwDirectoryLength) dwDirectoryLength = &length_dummy;
231
232     if (!dwlength) dwlength = &length_dummy;
233
234     ret = get_runtime_info(pExe, pwszVersion, pConfigurationFile, startupFlags, runtimeInfoFlags, TRUE, &info);
235
236     if (SUCCEEDED(ret))
237     {
238         *dwlength = cchBuffer;
239         ret = ICLRRuntimeInfo_GetVersionString(info, pVersion, dwlength);
240
241         if (SUCCEEDED(ret))
242         {
243             *dwDirectoryLength = dwDirectory;
244             ret = ICLRRuntimeInfo_GetRuntimeDirectory(info, pDirectory, dwDirectoryLength);
245         }
246
247         ICLRRuntimeInfo_Release(info);
248     }
249
250     return ret;
251 }
252
253 HRESULT WINAPI GetFileVersion(LPCWSTR szFilename, LPWSTR szBuffer, DWORD cchBuffer, DWORD *dwLength)
254 {
255     TRACE("(%s, %p, %d, %p)\n", debugstr_w(szFilename), szBuffer, cchBuffer, dwLength);
256
257     if (!szFilename || !dwLength)
258         return E_POINTER;
259
260     *dwLength = cchBuffer;
261     return CLRMetaHost_GetVersionFromFile(0, szFilename, szBuffer, dwLength);
262 }
263
264 HRESULT WINAPI LoadLibraryShim( LPCWSTR szDllName, LPCWSTR szVersion, LPVOID pvReserved, HMODULE * phModDll)
265 {
266     HRESULT ret=S_OK;
267     WCHAR dll_filename[MAX_PATH];
268     WCHAR version[MAX_PATH];
269     static const WCHAR default_version[] = {'v','1','.','1','.','4','3','2','2',0};
270     static const WCHAR slash[] = {'\\',0};
271     DWORD dummy;
272
273     TRACE("(%p %s, %p, %p, %p)\n", szDllName, debugstr_w(szDllName), szVersion, pvReserved, phModDll);
274
275     if (!szDllName || !phModDll)
276         return E_POINTER;
277
278     if (!get_install_root(dll_filename))
279     {
280         ERR("error reading registry key for installroot\n");
281         dll_filename[0] = 0;
282     }
283     else
284     {
285         if (!szVersion)
286         {
287             ret = GetCORVersion(version, MAX_PATH, &dummy);
288             if (SUCCEEDED(ret))
289                 szVersion = version;
290             else
291                 szVersion = default_version;
292         }
293         strcatW(dll_filename, szVersion);
294         strcatW(dll_filename, slash);
295     }
296
297     strcatW(dll_filename, szDllName);
298
299     *phModDll = LoadLibraryW(dll_filename);
300
301     return *phModDll ? S_OK : E_HANDLE;
302 }
303
304 HRESULT WINAPI LockClrVersion(FLockClrVersionCallback hostCallback, FLockClrVersionCallback *pBeginHostSetup, FLockClrVersionCallback *pEndHostSetup)
305 {
306     FIXME("(%p %p %p): stub\n", hostCallback, pBeginHostSetup, pEndHostSetup);
307     return S_OK;
308 }
309
310 HRESULT WINAPI CoInitializeCor(DWORD fFlags)
311 {
312     FIXME("(0x%08x): stub\n", fFlags);
313     return S_OK;
314 }
315
316 HRESULT WINAPI GetAssemblyMDImport(LPCWSTR szFileName, REFIID riid, IUnknown **ppIUnk)
317 {
318     FIXME("(%p %s, %s, %p): stub\n", szFileName, debugstr_w(szFileName), debugstr_guid(riid), *ppIUnk);
319     return ERROR_CALL_NOT_IMPLEMENTED;
320 }
321
322 HRESULT WINAPI GetVersionFromProcess(HANDLE hProcess, LPWSTR pVersion, DWORD cchBuffer, DWORD *dwLength)
323 {
324     FIXME("(%p, %p, %d, %p): stub\n", hProcess, pVersion, cchBuffer, dwLength);
325     return E_NOTIMPL;
326 }
327
328 HRESULT WINAPI LoadStringRCEx(LCID culture, UINT resId, LPWSTR pBuffer, int iBufLen, int bQuiet, int* pBufLen)
329 {
330     HRESULT res = S_OK;
331     if ((iBufLen <= 0) || !pBuffer)
332         return E_INVALIDARG;
333     pBuffer[0] = 0;
334     if (resId) {
335         FIXME("(%d, %x, %p, %d, %d, %p): semi-stub\n", culture, resId, pBuffer, iBufLen, bQuiet, pBufLen);
336         res = E_NOTIMPL;
337     }
338     else
339         res = E_FAIL;
340     if (pBufLen)
341         *pBufLen = lstrlenW(pBuffer);
342     return res;
343 }
344
345 HRESULT WINAPI LoadStringRC(UINT resId, LPWSTR pBuffer, int iBufLen, int bQuiet)
346 {
347     return LoadStringRCEx(-1, resId, pBuffer, iBufLen, bQuiet, NULL);
348 }
349
350 HRESULT WINAPI CorBindToRuntimeEx(LPWSTR szVersion, LPWSTR szBuildFlavor, DWORD nflags, REFCLSID rslsid,
351                                   REFIID riid, LPVOID *ppv)
352 {
353     HRESULT ret;
354     ICLRRuntimeInfo *info;
355
356     TRACE("%s %s %d %s %s %p\n", debugstr_w(szVersion), debugstr_w(szBuildFlavor), nflags, debugstr_guid( rslsid ),
357           debugstr_guid( riid ), ppv);
358
359     *ppv = NULL;
360
361     ret = get_runtime_info(NULL, szVersion, NULL, nflags, 0, TRUE, &info);
362
363     if (SUCCEEDED(ret))
364     {
365         ret = ICLRRuntimeInfo_GetInterface(info, rslsid, riid, ppv);
366
367         ICLRRuntimeInfo_Release(info);
368     }
369
370     return ret;
371 }
372
373 HRESULT WINAPI CorBindToCurrentRuntime(LPCWSTR filename, REFCLSID rclsid, REFIID riid, LPVOID *ppv)
374 {
375     FIXME("(%s, %s, %s, %p): stub\n", debugstr_w(filename), debugstr_guid(rclsid), debugstr_guid(riid), ppv);
376     return E_NOTIMPL;
377 }
378
379 STDAPI ClrCreateManagedInstance(LPCWSTR pTypeName, REFIID riid, void **ppObject)
380 {
381     HRESULT ret;
382     ICLRRuntimeInfo *info;
383     RuntimeHost *host;
384     MonoObject *obj;
385     IUnknown *unk;
386
387     TRACE("(%s,%s,%p)\n", debugstr_w(pTypeName), debugstr_guid(riid), ppObject);
388
389     /* FIXME: How to determine which runtime version to use? */
390     ret = get_runtime_info(NULL, NULL, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, TRUE, &info);
391
392     if (SUCCEEDED(ret))
393     {
394         ret = ICLRRuntimeInfo_GetRuntimeHost(info, &host);
395
396         ICLRRuntimeInfo_Release(info);
397     }
398
399     if (SUCCEEDED(ret))
400         ret = RuntimeHost_CreateManagedInstance(host, pTypeName, NULL, &obj);
401
402     if (SUCCEEDED(ret))
403         ret = RuntimeHost_GetIUnknownForObject(host, obj, &unk);
404
405     if (SUCCEEDED(ret))
406     {
407         ret = IUnknown_QueryInterface(unk, riid, ppObject);
408         IUnknown_Release(unk);
409     }
410
411     return ret;
412 }
413
414 BOOL WINAPI StrongNameSignatureVerification(LPCWSTR filename, DWORD inFlags, DWORD* pOutFlags)
415 {
416     FIXME("(%s, 0x%X, %p): stub\n", debugstr_w(filename), inFlags, pOutFlags);
417     return FALSE;
418 }
419
420 BOOL WINAPI StrongNameSignatureVerificationEx(LPCWSTR filename, BOOL forceVerification, BOOL* pVerified)
421 {
422     FIXME("(%s, %u, %p): stub\n", debugstr_w(filename), forceVerification, pVerified);
423     return FALSE;
424 }
425
426 HRESULT WINAPI CLRCreateInstance(REFCLSID clsid, REFIID riid, LPVOID *ppInterface)
427 {
428     TRACE("(%s,%s,%p)\n", debugstr_guid(clsid), debugstr_guid(riid), ppInterface);
429
430     if (IsEqualGUID(clsid, &CLSID_CLRMetaHost))
431         return CLRMetaHost_CreateInstance(riid, ppInterface);
432
433     FIXME("not implemented for class %s\n", debugstr_guid(clsid));
434
435     return CLASS_E_CLASSNOTAVAILABLE;
436 }
437
438 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
439 {
440     FIXME("(%s, %s, %p): stub\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
441     if(!ppv)
442         return E_INVALIDARG;
443
444     return E_NOTIMPL;
445 }
446
447 HRESULT WINAPI DllRegisterServer(void)
448 {
449     FIXME("\n");
450     return S_OK;
451 }
452
453 HRESULT WINAPI DllUnregisterServer(void)
454 {
455     FIXME("\n");
456     return S_OK;
457 }
458
459 HRESULT WINAPI DllCanUnloadNow(VOID)
460 {
461     if (dll_refs)
462         return S_FALSE;
463     else
464         return S_OK;
465 }
466
467 INT WINAPI ND_RU1( const void *ptr, INT offset )
468 {
469     return *((const BYTE *)ptr + offset);
470 }
471
472 INT WINAPI ND_RI2( const void *ptr, INT offset )
473 {
474     return *(const SHORT *)((const BYTE *)ptr + offset);
475 }
476
477 INT WINAPI ND_RI4( const void *ptr, INT offset )
478 {
479     return *(const INT *)((const BYTE *)ptr + offset);
480 }
481
482 INT64 WINAPI ND_RI8( const void *ptr, INT offset )
483 {
484     return *(const INT64 *)((const BYTE *)ptr + offset);
485 }
486
487 void WINAPI ND_WU1( void *ptr, INT offset, BYTE val )
488 {
489     *((BYTE *)ptr + offset) = val;
490 }
491
492 void WINAPI ND_WI2( void *ptr, INT offset, SHORT val )
493 {
494     *(SHORT *)((BYTE *)ptr + offset) = val;
495 }
496
497 void WINAPI ND_WI4( void *ptr, INT offset, INT val )
498 {
499     *(INT *)((BYTE *)ptr + offset) = val;
500 }
501
502 void WINAPI ND_WI8( void *ptr, INT offset, INT64 val )
503 {
504     *(INT64 *)((BYTE *)ptr + offset) = val;
505 }
506
507 void WINAPI ND_CopyObjDst( const void *src, void *dst, INT offset, INT size )
508 {
509     memcpy( (BYTE *)dst + offset, src, size );
510 }
511
512 void WINAPI ND_CopyObjSrc( const void *src, INT offset, void *dst, INT size )
513 {
514     memcpy( dst, (const BYTE *)src + offset, size );
515 }