imm32: Implement ImmConfigureIME using the loaded IME.
[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 "initguid.h"
31 #include "cor.h"
32 #include "mscoree.h"
33 #include "mscoree_private.h"
34
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL( mscoree );
38
39 static LPWSTR get_mono_exe(void)
40 {
41     static const WCHAR mono_exe[] = {'b','i','n','\\','m','o','n','o','.','e','x','e',' ',0};
42     static const WCHAR mono_key[] = {'S','o','f','t','w','a','r','e','\\','N','o','v','e','l','l','\\','M','o','n','o',0};
43     static const WCHAR defaul_clr[] = {'D','e','f','a','u','l','t','C','L','R',0};
44     static const WCHAR install_root[] = {'S','d','k','I','n','s','t','a','l','l','R','o','o','t',0};
45     static const WCHAR slash[] = {'\\',0};
46
47     WCHAR version[64], version_key[MAX_PATH], root[MAX_PATH], *ret;
48     DWORD len, size;
49     HKEY key;
50
51     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, mono_key, 0, KEY_READ, &key))
52         return NULL;
53
54     len = sizeof(version);
55     if (RegQueryValueExW(key, defaul_clr, 0, NULL, (LPBYTE)version, &len))
56     {
57         RegCloseKey(key);
58         return NULL;
59     }
60     RegCloseKey(key);
61
62     lstrcpyW(version_key, mono_key);
63     lstrcatW(version_key, slash);
64     lstrcatW(version_key, version);
65
66     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, version_key, 0, KEY_READ, &key))
67         return NULL;
68
69     len = sizeof(root);
70     if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)root, &len))
71     {
72         RegCloseKey(key);
73         return NULL;
74     }
75     RegCloseKey(key);
76
77     size = len + sizeof(slash) + sizeof(mono_exe);
78     if (!(ret = HeapAlloc(GetProcessHeap(), 0, size))) return NULL;
79
80     lstrcpyW(ret, root);
81     lstrcatW(ret, slash);
82     lstrcatW(ret, mono_exe);
83
84     return ret;
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     WCHAR *mono_exe;
93
94     FIXME("(%s, %s, %s, %p, %d, %p, %p, %p): semi-stub!\n", debugstr_w(pwszVersion),
95           debugstr_w(pwszBuildFlavor), debugstr_w(pwszHostConfigFile), pReserved,
96           startupFlags, rclsid, riid, ppv);
97
98     if (!(mono_exe = get_mono_exe()))
99     {
100         MESSAGE("wine: Install the Windows version of Mono to run .NET executables\n");
101         return E_FAIL;
102     }
103
104     HeapFree(GetProcessHeap(), 0, mono_exe);
105
106     return S_OK;
107 }
108
109 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
110 {
111     TRACE("(%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
112
113     switch (fdwReason)
114     {
115     case DLL_WINE_PREATTACH:
116         return FALSE;  /* prefer native version */
117     case DLL_PROCESS_ATTACH:
118         DisableThreadLibraryCalls(hinstDLL);
119         break;
120     case DLL_PROCESS_DETACH:
121         break;
122     }
123     return TRUE;
124 }
125
126 BOOL WINAPI _CorDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
127 {
128     FIXME("(%p, %d, %p): stub\n", hinstDLL, fdwReason, lpvReserved);
129
130     switch (fdwReason)
131     {
132     case DLL_PROCESS_ATTACH:
133         DisableThreadLibraryCalls(hinstDLL);
134         break;
135     case DLL_PROCESS_DETACH:
136         break;
137     }
138     return TRUE;
139 }
140
141 __int32 WINAPI _CorExeMain(void)
142 {
143     STARTUPINFOW si;
144     PROCESS_INFORMATION pi;
145     WCHAR *mono_exe, *cmd_line;
146     DWORD size, exit_code;
147
148     if (!(mono_exe = get_mono_exe()))
149     {
150         MESSAGE("install the Windows version of Mono to run .NET executables\n");
151         return -1;
152     }
153
154     size = (lstrlenW(mono_exe) + lstrlenW(GetCommandLineW()) + 1) * sizeof(WCHAR);
155     if (!(cmd_line = HeapAlloc(GetProcessHeap(), 0, size)))
156     {
157         HeapFree(GetProcessHeap(), 0, mono_exe);
158         return -1;
159     }
160
161     lstrcpyW(cmd_line, mono_exe);
162     HeapFree(GetProcessHeap(), 0, mono_exe);
163     lstrcatW(cmd_line, GetCommandLineW());
164
165     TRACE("new command line: %s\n", debugstr_w(cmd_line));
166
167     memset(&si, 0, sizeof(si));
168     si.cb = sizeof(si);
169     if (!CreateProcessW(NULL, cmd_line, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
170     {
171         HeapFree(GetProcessHeap(), 0, cmd_line);
172         return -1;
173     }
174     HeapFree(GetProcessHeap(), 0, cmd_line);
175
176     /* wait for the process to exit */
177     WaitForSingleObject(pi.hProcess, INFINITE);
178     GetExitCodeProcess(pi.hProcess, &exit_code);
179
180     CloseHandle(pi.hThread);
181     CloseHandle(pi.hProcess);
182
183     return (int)exit_code;
184 }
185
186 __int32 WINAPI _CorExeMain2(PBYTE ptrMemory, DWORD cntMemory, LPWSTR imageName, LPWSTR loaderName, LPWSTR cmdLine)
187 {
188     TRACE("(%p, %u, %s, %s, %s)\n", ptrMemory, cntMemory, debugstr_w(imageName), debugstr_w(loaderName), debugstr_w(cmdLine));
189     FIXME("Directly running .NET applications not supported.\n");
190     return -1;
191 }
192
193 void WINAPI CorExitProcess(int exitCode)
194 {
195     FIXME("(%x) stub\n", exitCode);
196     ExitProcess(exitCode);
197 }
198
199 VOID WINAPI _CorImageUnloading(PVOID imageBase)
200 {
201     TRACE("(%p): stub\n", imageBase);
202 }
203
204 HRESULT WINAPI _CorValidateImage(PVOID* imageBase, LPCWSTR imageName)
205 {
206     TRACE("(%p, %s): stub\n", imageBase, debugstr_w(imageName));
207     return E_FAIL;
208 }
209
210 HRESULT WINAPI GetCORSystemDirectory(LPWSTR pbuffer, DWORD cchBuffer, DWORD *dwLength)
211 {
212     FIXME("(%p, %d, %p): stub!\n", pbuffer, cchBuffer, dwLength);
213
214     if (!dwLength)
215         return E_POINTER;
216
217     *dwLength = 0;
218
219     return S_OK;
220 }
221
222 HRESULT WINAPI GetCORVersion(LPWSTR pbuffer, DWORD cchBuffer, DWORD *dwLength)
223 {
224     static const WCHAR version[] = {'v','1','.','1','.','4','3','2','2',0};
225
226     FIXME("(%p, %d, %p): semi-stub!\n", pbuffer, cchBuffer, dwLength);
227
228     if (!dwLength)
229         return E_POINTER;
230
231     *dwLength = lstrlenW(version);
232
233     if (cchBuffer < *dwLength)
234         return ERROR_INSUFFICIENT_BUFFER;
235
236     if (pbuffer)
237         lstrcpyW(pbuffer, version);
238
239     return S_OK;
240 }
241
242 HRESULT WINAPI GetRequestedRuntimeInfo(LPCWSTR pExe, LPCWSTR pwszVersion, LPCWSTR pConfigurationFile,
243     DWORD startupFlags, DWORD runtimeInfoFlags, LPWSTR pDirectory, DWORD dwDirectory, DWORD *dwDirectoryLength,
244     LPWSTR pVersion, DWORD cchBuffer, DWORD *dwlength)
245 {
246     FIXME("(%s, %s, %s, 0x%08x, 0x%08x, %p, 0x%08x, %p, %p, 0x%08x, %p) stub\n", debugstr_w(pExe),
247           debugstr_w(pwszVersion), debugstr_w(pConfigurationFile), startupFlags, runtimeInfoFlags, pDirectory,
248           dwDirectory, dwDirectoryLength, pVersion, cchBuffer, dwlength);
249     return GetCORVersion(pVersion, cchBuffer, dwlength);
250 }
251
252 HRESULT WINAPI LoadLibraryShim( LPCWSTR szDllName, LPCWSTR szVersion, LPVOID pvReserved, HMODULE * phModDll)
253 {
254     FIXME("(%p %s, %p, %p, %p): semi-stub\n", szDllName, debugstr_w(szDllName), szVersion, pvReserved, phModDll);
255
256     if (phModDll) *phModDll = LoadLibraryW(szDllName);
257     return S_OK;
258 }
259
260 HRESULT WINAPI CoInitializeCor(DWORD fFlags)
261 {
262     FIXME("(0x%08x): stub\n", fFlags);
263     return S_OK;
264 }
265
266 HRESULT WINAPI GetAssemblyMDImport(LPCWSTR szFileName, REFIID riid, IUnknown **ppIUnk)
267 {
268     FIXME("(%p %s, %p, %p): stub\n", szFileName, debugstr_w(szFileName), riid, *ppIUnk);
269     return ERROR_CALL_NOT_IMPLEMENTED;
270 }
271
272 HRESULT WINAPI GetVersionFromProcess(HANDLE hProcess, LPWSTR pVersion, DWORD cchBuffer, DWORD *dwLength)
273 {
274     FIXME("(%p, %p, %d, %p): stub\n", hProcess, pVersion, cchBuffer, dwLength);
275     return E_NOTIMPL;
276 }
277
278 HRESULT WINAPI LoadStringRCEx(LCID culture, UINT resId, LPWSTR pBuffer, int iBufLen, int bQuiet, int* pBufLen)
279 {
280     HRESULT res = S_OK;
281     if ((iBufLen <= 0) || !pBuffer)
282         return E_INVALIDARG;
283     pBuffer[0] = 0;
284     if (resId) {
285         FIXME("(%d, %x, %p, %d, %d, %p): semi-stub\n", culture, resId, pBuffer, iBufLen, bQuiet, pBufLen);
286         res = E_NOTIMPL;
287     }
288     else
289         res = E_FAIL;
290     if (pBufLen)
291         *pBufLen = lstrlenW(pBuffer);
292     return res;
293 }
294
295 HRESULT WINAPI LoadStringRC(UINT resId, LPWSTR pBuffer, int iBufLen, int bQuiet)
296 {
297     return LoadStringRCEx(-1, resId, pBuffer, iBufLen, bQuiet, NULL);
298 }
299
300 HRESULT WINAPI CorBindToRuntimeEx(LPWSTR szVersion, LPWSTR szBuildFlavor, DWORD nflags, REFCLSID rslsid,
301                                   REFIID riid, LPVOID *ppv)
302 {
303     FIXME("%s %s %d %s %s %p\n", debugstr_w(szVersion), debugstr_w(szBuildFlavor), nflags, debugstr_guid( rslsid ),
304           debugstr_guid( riid ), ppv);
305
306     if(IsEqualGUID( riid, &IID_ICorRuntimeHost ))
307     {
308         *ppv = create_corruntimehost();
309         return S_OK;
310     }
311     *ppv = NULL;
312     return E_NOTIMPL;
313 }
314
315 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
316 {
317     FIXME("(%p, %p, %p): stub\n", rclsid, riid, ppv);
318     if(!ppv)
319         return E_INVALIDARG;
320
321     return E_NOTIMPL;
322 }
323
324 HRESULT WINAPI DllCanUnloadNow(VOID)
325 {
326     FIXME("stub\n");
327     return S_OK;
328 }
329
330 INT WINAPI ND_RU1( const void *ptr, INT offset )
331 {
332     return *((const BYTE *)ptr + offset);
333 }
334
335 INT WINAPI ND_RI2( const void *ptr, INT offset )
336 {
337     return *(const SHORT *)((const BYTE *)ptr + offset);
338 }
339
340 INT WINAPI ND_RI4( const void *ptr, INT offset )
341 {
342     return *(const INT *)((const BYTE *)ptr + offset);
343 }
344
345 INT64 WINAPI ND_RI8( const void *ptr, INT offset )
346 {
347     return *(const INT64 *)((const BYTE *)ptr + offset);
348 }
349
350 void WINAPI ND_WU1( void *ptr, INT offset, BYTE val )
351 {
352     *((BYTE *)ptr + offset) = val;
353 }
354
355 void WINAPI ND_WI2( void *ptr, INT offset, SHORT val )
356 {
357     *(SHORT *)((BYTE *)ptr + offset) = val;
358 }
359
360 void WINAPI ND_WI4( void *ptr, INT offset, INT val )
361 {
362     *(INT *)((BYTE *)ptr + offset) = val;
363 }
364
365 void WINAPI ND_WI8( void *ptr, INT offset, INT64 val )
366 {
367     *(INT64 *)((BYTE *)ptr + offset) = val;
368 }
369
370 void WINAPI ND_CopyObjDst( const void *src, void *dst, INT offset, INT size )
371 {
372     memcpy( (BYTE *)dst + offset, src, size );
373 }
374
375 void WINAPI ND_CopyObjSrc( const void *src, INT offset, void *dst, INT size )
376 {
377     memcpy( dst, (const BYTE *)src + offset, size );
378 }