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