wined3d: Implement a detection for the MacOS OpenGL implementation.
[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 "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "winreg.h"
28 #include "ole2.h"
29
30 #include "cor.h"
31 #include "mscoree.h"
32
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL( mscoree );
36
37 static LPWSTR get_mono_exe(void)
38 {
39     static const WCHAR mono_exe[] = {'b','i','n','\\','m','o','n','o','.','e','x','e',' ',0};
40     static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
41     static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
42     static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
43     static const WCHAR slash[] = {'\\',0};
44
45     WCHAR version[64], version_key[MAX_PATH], root[MAX_PATH], *ret;
46     DWORD len, size;
47     HKEY key;
48
49     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
50         return NULL;
51
52     len = sizeof(version);
53     if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
54     {
55         RegCloseKey(key);
56         return NULL;
57     }
58     RegCloseKey(key);
59
60     lstrcpyW(version_key, mono_key);
61     lstrcatW(version_key, slash);
62     lstrcatW(version_key, version);
63
64     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
65         return NULL;
66
67     len = sizeof(root);
68     if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)root, &len))
69     {
70         RegCloseKey(key);
71         return NULL;
72     }
73     RegCloseKey(key);
74
75     size = len + sizeof(slash) + sizeof(mono_exe);
76     if (!(ret = HeapAlloc(GetProcessHeap(), 0, size))) return NULL;
77
78     lstrcpyW(ret, root);
79     lstrcatW(ret, slash);
80     lstrcatW(ret, mono_exe);
81
82     return ret;
83 }
84
85 HRESULT WINAPI CorBindToRuntimeHost(LPCWSTR pwszVersion, LPCWSTR pwszBuildFlavor,
86                                     LPCWSTR pwszHostConfigFile, VOID *pReserved,
87                                     DWORD startupFlags, REFCLSID rclsid,
88                                     REFIID riid, LPVOID *ppv)
89 {
90     WCHAR *mono_exe;
91
92     FIXME("(%s, %s, %s, %p, %d, %p, %p, %p): semi-stub!\n", debugstr_w(pwszVersion),
93           debugstr_w(pwszBuildFlavor), debugstr_w(pwszHostConfigFile), pReserved,
94           startupFlags, rclsid, riid, ppv);
95
96     if (!(mono_exe = get_mono_exe()))
97     {
98         MESSAGE("wine: Install the Windows version of Mono to run .NET executables\n");
99         return E_FAIL;
100     }
101
102     return S_OK;
103 }
104
105 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
106 {
107     TRACE("(%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
108
109     switch (fdwReason)
110     {
111     case DLL_WINE_PREATTACH:
112         return FALSE;  /* prefer native version */
113     case DLL_PROCESS_ATTACH:
114         DisableThreadLibraryCalls(hinstDLL);
115         break;
116     case DLL_PROCESS_DETACH:
117         break;
118     }
119     return TRUE;
120 }
121
122 BOOL WINAPI _CorDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
123 {
124     FIXME("(%p, %d, %p): stub\n", hinstDLL, fdwReason, lpvReserved);
125
126     switch (fdwReason)
127     {
128     case DLL_PROCESS_ATTACH:
129         DisableThreadLibraryCalls(hinstDLL);
130         break;
131     case DLL_PROCESS_DETACH:
132         break;
133     }
134     return TRUE;
135 }
136
137 __int32 WINAPI _CorExeMain(void)
138 {
139     STARTUPINFOW si;
140     PROCESS_INFORMATION pi;
141     WCHAR *mono_exe, *cmd_line;
142     DWORD size, exit_code;
143
144     if (!(mono_exe = get_mono_exe()))
145     {
146         MESSAGE("install the Windows version of Mono to run .NET executables\n");
147         return -1;
148     }
149
150     size = (lstrlenW(mono_exe) + lstrlenW(GetCommandLineW()) + 1) * sizeof(WCHAR);
151     if (!(cmd_line = HeapAlloc(GetProcessHeap(), 0, size)))
152     {
153         HeapFree(GetProcessHeap(), 0, mono_exe);
154         return -1;
155     }
156
157     lstrcpyW(cmd_line, mono_exe);
158     HeapFree(GetProcessHeap(), 0, mono_exe);
159     lstrcatW(cmd_line, GetCommandLineW());
160
161     TRACE("new command line: %s\n", debugstr_w(cmd_line));
162
163     memset(&si, 0, sizeof(si));
164     si.cb = sizeof(si);
165     if (!CreateProcessW(NULL, cmd_line, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
166     {
167         HeapFree(GetProcessHeap(), 0, cmd_line);
168         return -1;
169     }
170     HeapFree(GetProcessHeap(), 0, cmd_line);
171
172     /* wait for the process to exit */
173     WaitForSingleObject(pi.hProcess, INFINITE);
174     GetExitCodeProcess(pi.hProcess, &exit_code);
175
176     CloseHandle(pi.hThread);
177     CloseHandle(pi.hProcess);
178
179     return (int)exit_code;
180 }
181
182 __int32 WINAPI _CorExeMain2(PBYTE ptrMemory, DWORD cntMemory, LPWSTR imageName, LPWSTR loaderName, LPWSTR cmdLine)
183 {
184     TRACE("(%p, %u, %s, %s, %s)\n", ptrMemory, cntMemory, debugstr_w(imageName), debugstr_w(loaderName), debugstr_w(cmdLine));
185     FIXME("Directly running .NET applications not supported.\n");
186     return -1;
187 }
188
189 void WINAPI CorExitProcess(int exitCode)
190 {
191     FIXME("(%x) stub\n", exitCode);
192     ExitProcess(exitCode);
193 }
194
195 VOID WINAPI _CorImageUnloading(PVOID imageBase)
196 {
197     TRACE("(%p): stub\n", imageBase);
198 }
199
200 HRESULT WINAPI _CorValidateImage(PVOID* imageBase, LPCWSTR imageName)
201 {
202     TRACE("(%p, %s): stub\n", imageBase, debugstr_w(imageName));
203     return E_FAIL;
204 }
205
206 HRESULT WINAPI GetCORSystemDirectory(LPWSTR pbuffer, DWORD cchBuffer, DWORD *dwLength)
207 {
208     FIXME("(%p, %d, %p): stub!\n", pbuffer, cchBuffer, dwLength);
209
210     if (!dwLength)
211         return E_POINTER;
212
213     *dwLength = 0;
214
215     return S_OK;
216 }
217
218 HRESULT WINAPI GetCORVersion(LPWSTR pbuffer, DWORD cchBuffer, DWORD *dwLength)
219 {
220     static const WCHAR version[] = {'v','1','.','1','.','4','3','2','2',0};
221
222     FIXME("(%p, %d, %p): semi-stub!\n", pbuffer, cchBuffer, dwLength);
223
224     if (!dwLength)
225         return E_POINTER;
226
227     *dwLength = lstrlenW(version);
228
229     if (cchBuffer < *dwLength)
230         return ERROR_INSUFFICIENT_BUFFER;
231
232     if (pbuffer)
233         lstrcpyW(pbuffer, version);
234
235     return S_OK;
236 }
237
238 HRESULT WINAPI GetRequestedRuntimeInfo(LPCWSTR pExe, LPCWSTR pwszVersion, LPCWSTR pConfigurationFile,
239     DWORD startupFlags, DWORD runtimeInfoFlags, LPWSTR pDirectory, DWORD dwDirectory, DWORD *dwDirectoryLength,
240     LPWSTR pVersion, DWORD cchBuffer, DWORD *dwlength)
241 {
242     FIXME("(%s, %s, %s, 0x%08x, 0x%08x, %p, 0x%08x, %p, %p, 0x%08x, %p) stub\n", debugstr_w(pExe),
243           debugstr_w(pwszVersion), debugstr_w(pConfigurationFile), startupFlags, runtimeInfoFlags, pDirectory,
244           dwDirectory, dwDirectoryLength, pVersion, cchBuffer, dwlength);
245     return GetCORVersion(pVersion, cchBuffer, dwlength);
246 }
247
248 HRESULT WINAPI LoadLibraryShim( LPCWSTR szDllName, LPCWSTR szVersion, LPVOID pvReserved, HMODULE * phModDll)
249 {
250     FIXME("(%p %s, %p, %p, %p): semi-stub\n", szDllName, debugstr_w(szDllName), szVersion, pvReserved, phModDll);
251
252     if (phModDll) *phModDll = LoadLibraryW(szDllName);
253     return S_OK;
254 }
255
256 HRESULT WINAPI CoInitializeCor(DWORD fFlags)
257 {
258     FIXME("(0x%08x): stub\n", fFlags);
259     return S_OK;
260 }
261
262 HRESULT WINAPI GetAssemblyMDImport(LPCWSTR szFileName, REFIID riid, IUnknown **ppIUnk)
263 {
264     FIXME("(%p %s, %p, %p): stub\n", szFileName, debugstr_w(szFileName), riid, *ppIUnk);
265     return ERROR_CALL_NOT_IMPLEMENTED;
266 }