shell32: Add ICommDlgBrowser3 stub to the ExplorerBrowser control.
[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 #include "wine/unicode.h"
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winnls.h"
29 #include "winreg.h"
30 #include "ole2.h"
31 #include "shellapi.h"
32
33 #include "initguid.h"
34 #include "cor.h"
35 #include "mscoree.h"
36 #include "mscoree_private.h"
37
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL( mscoree );
41
42 static BOOL get_mono_path(LPWSTR path)
43 {
44     static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
45     static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
46     static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
47     static const WCHAR slash[] = {'\\',0};
48
49     WCHAR version[64], version_key[MAX_PATH];
50     DWORD len;
51     HKEY key;
52
53     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
54         return FALSE;
55
56     len = sizeof(version);
57     if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
58     {
59         RegCloseKey(key);
60         return FALSE;
61     }
62     RegCloseKey(key);
63
64     lstrcpyW(version_key, mono_key);
65     lstrcatW(version_key, slash);
66     lstrcatW(version_key, version);
67
68     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
69         return FALSE;
70
71     len = sizeof(WCHAR) * MAX_PATH;
72     if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)path, &len))
73     {
74         RegCloseKey(key);
75         return FALSE;
76     }
77     RegCloseKey(key);
78
79     return TRUE;
80 }
81
82 static BOOL get_install_root(LPWSTR install_dir)
83 {
84     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};
85     const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
86
87     DWORD len;
88     HKEY key;
89
90     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
91         return FALSE;
92
93     len = MAX_PATH;
94     if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
95     {
96         RegCloseKey(key);
97         return FALSE;
98     }
99     RegCloseKey(key);
100
101     return TRUE;
102 }
103
104 static CRITICAL_SECTION mono_lib_cs;
105 static CRITICAL_SECTION_DEBUG mono_lib_cs_debug =
106 {
107     0, 0, &mono_lib_cs,
108     { &mono_lib_cs_debug.ProcessLocksList,
109       &mono_lib_cs_debug.ProcessLocksList },
110       0, 0, { (DWORD_PTR)(__FILE__ ": mono_lib_cs") }
111 };
112 static CRITICAL_SECTION mono_lib_cs = { &mono_lib_cs_debug, -1, 0, 0, 0, 0 };
113
114 HMODULE mono_handle;
115
116 void (*mono_config_parse)(const char *filename);
117 MonoAssembly* (*mono_domain_assembly_open) (MonoDomain *domain, const char *name);
118 void (*mono_jit_cleanup)(MonoDomain *domain);
119 int (*mono_jit_exec)(MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[]);
120 MonoDomain* (*mono_jit_init)(const char *file);
121 int (*mono_jit_set_trace_options)(const char* options);
122 void (*mono_set_dirs)(const char *assembly_dir, const char *config_dir);
123
124 static void set_environment(LPCWSTR bin_path)
125 {
126     WCHAR path_env[MAX_PATH];
127     int len;
128
129     static const WCHAR pathW[] = {'P','A','T','H',0};
130
131     /* We have to modify PATH as Mono loads other DLLs from this directory. */
132     GetEnvironmentVariableW(pathW, path_env, sizeof(path_env)/sizeof(WCHAR));
133     len = strlenW(path_env);
134     path_env[len++] = ';';
135     strcpyW(path_env+len, bin_path);
136     SetEnvironmentVariableW(pathW, path_env);
137 }
138
139 static HMODULE load_mono(void)
140 {
141     static const WCHAR mono_dll[] = {'\\','b','i','n','\\','m','o','n','o','.','d','l','l',0};
142     static const WCHAR libmono_dll[] = {'\\','b','i','n','\\','l','i','b','m','o','n','o','.','d','l','l',0};
143     static const WCHAR bin[] = {'\\','b','i','n',0};
144     static const WCHAR lib[] = {'\\','l','i','b',0};
145     static const WCHAR etc[] = {'\\','e','t','c',0};
146     HMODULE result;
147     WCHAR mono_path[MAX_PATH], mono_dll_path[MAX_PATH+16], mono_bin_path[MAX_PATH+4];
148     WCHAR mono_lib_path[MAX_PATH+4], mono_etc_path[MAX_PATH+4];
149     char mono_lib_path_a[MAX_PATH], mono_etc_path_a[MAX_PATH];
150
151     EnterCriticalSection(&mono_lib_cs);
152
153     if (!mono_handle)
154     {
155         if (!get_mono_path(mono_path)) goto end;
156
157         strcpyW(mono_bin_path, mono_path);
158         strcatW(mono_bin_path, bin);
159         set_environment(mono_bin_path);
160
161         strcpyW(mono_lib_path, mono_path);
162         strcatW(mono_lib_path, lib);
163         WideCharToMultiByte(CP_UTF8, 0, mono_lib_path, -1, mono_lib_path_a, MAX_PATH, NULL, NULL);
164
165         strcpyW(mono_etc_path, mono_path);
166         strcatW(mono_etc_path, etc);
167         WideCharToMultiByte(CP_UTF8, 0, mono_etc_path, -1, mono_etc_path_a, MAX_PATH, NULL, NULL);
168
169         strcpyW(mono_dll_path, mono_path);
170         strcatW(mono_dll_path, mono_dll);
171         mono_handle = LoadLibraryW(mono_dll_path);
172
173         if (!mono_handle)
174         {
175             strcpyW(mono_dll_path, mono_path);
176             strcatW(mono_dll_path, libmono_dll);
177             mono_handle = LoadLibraryW(mono_dll_path);
178         }
179
180         if (!mono_handle) goto end;
181
182 #define LOAD_MONO_FUNCTION(x) do { \
183     x = (void*)GetProcAddress(mono_handle, #x); \
184     if (!x) { \
185         mono_handle = NULL; \
186         goto end; \
187     } \
188 } while (0);
189
190         LOAD_MONO_FUNCTION(mono_config_parse);
191         LOAD_MONO_FUNCTION(mono_domain_assembly_open);
192         LOAD_MONO_FUNCTION(mono_jit_cleanup);
193         LOAD_MONO_FUNCTION(mono_jit_exec);
194         LOAD_MONO_FUNCTION(mono_jit_init);
195         LOAD_MONO_FUNCTION(mono_jit_set_trace_options);
196         LOAD_MONO_FUNCTION(mono_set_dirs);
197
198 #undef LOAD_MONO_FUNCTION
199
200         mono_set_dirs(mono_lib_path_a, mono_etc_path_a);
201
202         mono_config_parse(NULL);
203     }
204
205 end:
206     result = mono_handle;
207
208     LeaveCriticalSection(&mono_lib_cs);
209
210     if (!result)
211         MESSAGE("wine: Install the Windows version of Mono to run .NET executables\n");
212
213     return result;
214 }
215
216 HRESULT WINAPI CorBindToRuntimeHost(LPCWSTR pwszVersion, LPCWSTR pwszBuildFlavor,
217                                     LPCWSTR pwszHostConfigFile, VOID *pReserved,
218                                     DWORD startupFlags, REFCLSID rclsid,
219                                     REFIID riid, LPVOID *ppv)
220 {
221     FIXME("(%s, %s, %s, %p, %d, %s, %s, %p): semi-stub!\n", debugstr_w(pwszVersion),
222           debugstr_w(pwszBuildFlavor), debugstr_w(pwszHostConfigFile), pReserved,
223           startupFlags, debugstr_guid(rclsid), debugstr_guid(riid), ppv);
224
225     if (!get_mono_path(NULL))
226     {
227         MESSAGE("wine: Install the Windows version of Mono to run .NET executables\n");
228         return E_FAIL;
229     }
230
231     return S_OK;
232 }
233
234 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
235 {
236     TRACE("(%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
237
238     switch (fdwReason)
239     {
240     case DLL_WINE_PREATTACH:
241         return FALSE;  /* prefer native version */
242     case DLL_PROCESS_ATTACH:
243         DisableThreadLibraryCalls(hinstDLL);
244         break;
245     case DLL_PROCESS_DETACH:
246         break;
247     }
248     return TRUE;
249 }
250
251 BOOL WINAPI _CorDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
252 {
253     FIXME("(%p, %d, %p): stub\n", hinstDLL, fdwReason, lpvReserved);
254
255     switch (fdwReason)
256     {
257     case DLL_PROCESS_ATTACH:
258         DisableThreadLibraryCalls(hinstDLL);
259         break;
260     case DLL_PROCESS_DETACH:
261         break;
262     }
263     return TRUE;
264 }
265
266 static void get_utf8_args(int *argc, char ***argv)
267 {
268     WCHAR **argvw;
269     int size=0, i;
270     char *current_arg;
271
272     argvw = CommandLineToArgvW(GetCommandLineW(), argc);
273
274     for (i=0; i<*argc; i++)
275     {
276         size += sizeof(char*);
277         size += WideCharToMultiByte(CP_UTF8, 0, argvw[i], -1, NULL, 0, NULL, NULL);
278     }
279     size += sizeof(char*);
280
281     *argv = HeapAlloc(GetProcessHeap(), 0, size);
282     current_arg = (char*)(*argv + *argc + 1);
283
284     for (i=0; i<*argc; i++)
285     {
286         (*argv)[i] = current_arg;
287         current_arg += WideCharToMultiByte(CP_UTF8, 0, argvw[i], -1, current_arg, size, NULL, NULL);
288     }
289
290     (*argv)[*argc] = NULL;
291
292     HeapFree(GetProcessHeap(), 0, argvw);
293 }
294
295 __int32 WINAPI _CorExeMain(void)
296 {
297     int exit_code;
298     int trace_size;
299     char trace_setting[256];
300     int argc;
301     char **argv;
302     MonoDomain *domain;
303     MonoAssembly *assembly;
304     char filename[MAX_PATH];
305
306     if (!load_mono())
307     {
308         return -1;
309     }
310
311     get_utf8_args(&argc, &argv);
312
313     trace_size = GetEnvironmentVariableA("WINE_MONO_TRACE", trace_setting, sizeof(trace_setting));
314
315     if (trace_size)
316     {
317         mono_jit_set_trace_options(trace_setting);
318     }
319
320     GetModuleFileNameA(NULL, filename, MAX_PATH);
321
322     domain = mono_jit_init(filename);
323
324     assembly = mono_domain_assembly_open(domain, filename);
325
326     exit_code = mono_jit_exec(domain, assembly, argc, argv);
327
328     mono_jit_cleanup(domain);
329
330     HeapFree(GetProcessHeap(), 0, argv);
331
332     return exit_code;
333 }
334
335 __int32 WINAPI _CorExeMain2(PBYTE ptrMemory, DWORD cntMemory, LPWSTR imageName, LPWSTR loaderName, LPWSTR cmdLine)
336 {
337     TRACE("(%p, %u, %s, %s, %s)\n", ptrMemory, cntMemory, debugstr_w(imageName), debugstr_w(loaderName), debugstr_w(cmdLine));
338     FIXME("Directly running .NET applications not supported.\n");
339     return -1;
340 }
341
342 void WINAPI CorExitProcess(int exitCode)
343 {
344     FIXME("(%x) stub\n", exitCode);
345     ExitProcess(exitCode);
346 }
347
348 VOID WINAPI _CorImageUnloading(PVOID imageBase)
349 {
350     TRACE("(%p): stub\n", imageBase);
351 }
352
353 HRESULT WINAPI _CorValidateImage(PVOID* imageBase, LPCWSTR imageName)
354 {
355     TRACE("(%p, %s): stub\n", imageBase, debugstr_w(imageName));
356     return E_FAIL;
357 }
358
359 HRESULT WINAPI GetCORSystemDirectory(LPWSTR pbuffer, DWORD cchBuffer, DWORD *dwLength)
360 {
361     static const WCHAR slash[] = {'\\',0};
362     WCHAR system_dir[MAX_PATH];
363     WCHAR version[MAX_PATH];
364
365     FIXME("(%p, %d, %p): semi-stub!\n", pbuffer, cchBuffer, dwLength);
366
367     if (!dwLength)
368         return E_POINTER;
369
370     if (!pbuffer)
371         return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
372
373     if (!get_install_root(system_dir))
374     {
375         ERR("error reading registry key for installroot, returning empty path\n");
376         *dwLength = 0;
377     }
378     else
379     {
380         GetCORVersion(version, MAX_PATH, dwLength);
381         lstrcatW(system_dir, version);
382         lstrcatW(system_dir, slash);
383         *dwLength = lstrlenW(system_dir) + 1;
384
385         if (cchBuffer < *dwLength)
386             return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
387
388         lstrcpyW(pbuffer, system_dir);
389     }
390
391     return S_OK;
392 }
393
394 HRESULT WINAPI GetCORVersion(LPWSTR pbuffer, DWORD cchBuffer, DWORD *dwLength)
395 {
396     static const WCHAR version[] = {'v','2','.','0','.','5','0','7','2','7',0};
397
398     FIXME("(%p, %d, %p): semi-stub!\n", pbuffer, cchBuffer, dwLength);
399
400     if (!dwLength || !pbuffer)
401         return E_POINTER;
402
403     *dwLength = lstrlenW(version) + 1;
404
405     if (cchBuffer < *dwLength)
406         return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
407
408     lstrcpyW(pbuffer, version);
409
410     return S_OK;
411 }
412
413 HRESULT WINAPI GetRequestedRuntimeInfo(LPCWSTR pExe, LPCWSTR pwszVersion, LPCWSTR pConfigurationFile,
414     DWORD startupFlags, DWORD runtimeInfoFlags, LPWSTR pDirectory, DWORD dwDirectory, DWORD *dwDirectoryLength,
415     LPWSTR pVersion, DWORD cchBuffer, DWORD *dwlength)
416 {
417     FIXME("(%s, %s, %s, 0x%08x, 0x%08x, %p, 0x%08x, %p, %p, 0x%08x, %p) stub\n", debugstr_w(pExe),
418           debugstr_w(pwszVersion), debugstr_w(pConfigurationFile), startupFlags, runtimeInfoFlags, pDirectory,
419           dwDirectory, dwDirectoryLength, pVersion, cchBuffer, dwlength);
420     return GetCORVersion(pVersion, cchBuffer, dwlength);
421 }
422
423 HRESULT WINAPI LoadLibraryShim( LPCWSTR szDllName, LPCWSTR szVersion, LPVOID pvReserved, HMODULE * phModDll)
424 {
425     FIXME("(%p %s, %p, %p, %p): semi-stub\n", szDllName, debugstr_w(szDllName), szVersion, pvReserved, phModDll);
426
427     if (phModDll) *phModDll = LoadLibraryW(szDllName);
428     return S_OK;
429 }
430
431 HRESULT WINAPI LockClrVersion(FLockClrVersionCallback hostCallback, FLockClrVersionCallback *pBeginHostSetup, FLockClrVersionCallback *pEndHostSetup)
432 {
433     FIXME("(%p %p %p): stub\n", hostCallback, pBeginHostSetup, pEndHostSetup);
434     return S_OK;
435 }
436
437 HRESULT WINAPI CoInitializeCor(DWORD fFlags)
438 {
439     FIXME("(0x%08x): stub\n", fFlags);
440     return S_OK;
441 }
442
443 HRESULT WINAPI GetAssemblyMDImport(LPCWSTR szFileName, REFIID riid, IUnknown **ppIUnk)
444 {
445     FIXME("(%p %s, %s, %p): stub\n", szFileName, debugstr_w(szFileName), debugstr_guid(riid), *ppIUnk);
446     return ERROR_CALL_NOT_IMPLEMENTED;
447 }
448
449 HRESULT WINAPI GetVersionFromProcess(HANDLE hProcess, LPWSTR pVersion, DWORD cchBuffer, DWORD *dwLength)
450 {
451     FIXME("(%p, %p, %d, %p): stub\n", hProcess, pVersion, cchBuffer, dwLength);
452     return E_NOTIMPL;
453 }
454
455 HRESULT WINAPI LoadStringRCEx(LCID culture, UINT resId, LPWSTR pBuffer, int iBufLen, int bQuiet, int* pBufLen)
456 {
457     HRESULT res = S_OK;
458     if ((iBufLen <= 0) || !pBuffer)
459         return E_INVALIDARG;
460     pBuffer[0] = 0;
461     if (resId) {
462         FIXME("(%d, %x, %p, %d, %d, %p): semi-stub\n", culture, resId, pBuffer, iBufLen, bQuiet, pBufLen);
463         res = E_NOTIMPL;
464     }
465     else
466         res = E_FAIL;
467     if (pBufLen)
468         *pBufLen = lstrlenW(pBuffer);
469     return res;
470 }
471
472 HRESULT WINAPI LoadStringRC(UINT resId, LPWSTR pBuffer, int iBufLen, int bQuiet)
473 {
474     return LoadStringRCEx(-1, resId, pBuffer, iBufLen, bQuiet, NULL);
475 }
476
477 HRESULT WINAPI CorBindToRuntimeEx(LPWSTR szVersion, LPWSTR szBuildFlavor, DWORD nflags, REFCLSID rslsid,
478                                   REFIID riid, LPVOID *ppv)
479 {
480     FIXME("%s %s %d %s %s %p\n", debugstr_w(szVersion), debugstr_w(szBuildFlavor), nflags, debugstr_guid( rslsid ),
481           debugstr_guid( riid ), ppv);
482
483     if(IsEqualGUID( riid, &IID_ICorRuntimeHost ))
484     {
485         *ppv = create_corruntimehost();
486         return S_OK;
487     }
488     *ppv = NULL;
489     return E_NOTIMPL;
490 }
491
492 HRESULT WINAPI CorBindToCurrentRuntime(LPCWSTR filename, REFCLSID rclsid, REFIID riid, LPVOID *ppv)
493 {
494     FIXME("(%s, %s, %s, %p): stub\n", debugstr_w(filename), debugstr_guid(rclsid), debugstr_guid(riid), ppv);
495     return E_NOTIMPL;
496 }
497
498 STDAPI ClrCreateManagedInstance(LPCWSTR pTypeName, REFIID riid, void **ppObject)
499 {
500     FIXME("(%s,%s,%p)\n", debugstr_w(pTypeName), debugstr_guid(riid), ppObject);
501     return E_NOTIMPL;
502 }
503
504 BOOL WINAPI StrongNameSignatureVerification(LPCWSTR filename, DWORD inFlags, DWORD* pOutFlags)
505 {
506     FIXME("(%s, 0x%X, %p): stub\n", debugstr_w(filename), inFlags, pOutFlags);
507     return FALSE;
508 }
509
510 BOOL WINAPI StrongNameSignatureVerificationEx(LPCWSTR filename, BOOL forceVerification, BOOL* pVerified)
511 {
512     FIXME("(%s, %u, %p): stub\n", debugstr_w(filename), forceVerification, pVerified);
513     return FALSE;
514 }
515
516 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
517 {
518     FIXME("(%s, %s, %p): stub\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
519     if(!ppv)
520         return E_INVALIDARG;
521
522     return E_NOTIMPL;
523 }
524
525 HRESULT WINAPI DllRegisterServer(void)
526 {
527     FIXME("\n");
528     return S_OK;
529 }
530
531 HRESULT WINAPI DllUnregisterServer(void)
532 {
533     FIXME("\n");
534     return S_OK;
535 }
536
537 HRESULT WINAPI DllCanUnloadNow(VOID)
538 {
539     return S_OK;
540 }
541
542 INT WINAPI ND_RU1( const void *ptr, INT offset )
543 {
544     return *((const BYTE *)ptr + offset);
545 }
546
547 INT WINAPI ND_RI2( const void *ptr, INT offset )
548 {
549     return *(const SHORT *)((const BYTE *)ptr + offset);
550 }
551
552 INT WINAPI ND_RI4( const void *ptr, INT offset )
553 {
554     return *(const INT *)((const BYTE *)ptr + offset);
555 }
556
557 INT64 WINAPI ND_RI8( const void *ptr, INT offset )
558 {
559     return *(const INT64 *)((const BYTE *)ptr + offset);
560 }
561
562 void WINAPI ND_WU1( void *ptr, INT offset, BYTE val )
563 {
564     *((BYTE *)ptr + offset) = val;
565 }
566
567 void WINAPI ND_WI2( void *ptr, INT offset, SHORT val )
568 {
569     *(SHORT *)((BYTE *)ptr + offset) = val;
570 }
571
572 void WINAPI ND_WI4( void *ptr, INT offset, INT val )
573 {
574     *(INT *)((BYTE *)ptr + offset) = val;
575 }
576
577 void WINAPI ND_WI8( void *ptr, INT offset, INT64 val )
578 {
579     *(INT64 *)((BYTE *)ptr + offset) = val;
580 }
581
582 void WINAPI ND_CopyObjDst( const void *src, void *dst, INT offset, INT size )
583 {
584     memcpy( (BYTE *)dst + offset, src, size );
585 }
586
587 void WINAPI ND_CopyObjSrc( const void *src, INT offset, void *dst, INT size )
588 {
589     memcpy( dst, (const BYTE *)src + offset, size );
590 }