dmloader: Check return of CoCreateInstance (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 #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 "corerror.h"
39 #include "cor.h"
40 #include "mscoree.h"
41 #include "corhdr.h"
42 #include "cordebug.h"
43 #include "metahost.h"
44 #include "fusion.h"
45 #include "wine/list.h"
46 #include "mscoree_private.h"
47 #include "rpcproxy.h"
48
49 #include "wine/debug.h"
50
51 WINE_DEFAULT_DEBUG_CHANNEL( mscoree );
52
53 static HINSTANCE MSCOREE_hInstance;
54
55 typedef HRESULT (*fnCreateInstance)(REFIID riid, LPVOID *ppObj);
56
57 char *WtoA(LPCWSTR wstr)
58 {
59     int length;
60     char *result;
61
62     length = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
63
64     result = HeapAlloc(GetProcessHeap(), 0, length);
65
66     if (result)
67         WideCharToMultiByte(CP_UTF8, 0, wstr, -1, result, length, NULL, NULL);
68
69     return result;
70 }
71
72 static BOOL get_install_root(LPWSTR install_dir)
73 {
74     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};
75     const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
76
77     DWORD len;
78     HKEY key;
79
80     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
81         return FALSE;
82
83     len = MAX_PATH;
84     if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
85     {
86         RegCloseKey(key);
87         return FALSE;
88     }
89     RegCloseKey(key);
90
91     return TRUE;
92 }
93
94 typedef struct mscorecf
95 {
96     IClassFactory    IClassFactory_iface;
97     LONG ref;
98
99     fnCreateInstance pfnCreateInstance;
100
101     CLSID clsid;
102 } mscorecf;
103
104 static inline mscorecf *impl_from_IClassFactory( IClassFactory *iface )
105 {
106     return CONTAINING_RECORD(iface, mscorecf, IClassFactory_iface);
107 }
108
109 static HRESULT WINAPI mscorecf_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppobj )
110 {
111     TRACE("%s %p\n", debugstr_guid(riid), ppobj);
112
113     if (IsEqualGUID(riid, &IID_IUnknown) ||
114         IsEqualGUID(riid, &IID_IClassFactory))
115     {
116         IClassFactory_AddRef( iface );
117         *ppobj = iface;
118         return S_OK;
119     }
120
121     ERR("interface %s not implemented\n", debugstr_guid(riid));
122     return E_NOINTERFACE;
123 }
124
125 static ULONG WINAPI mscorecf_AddRef(IClassFactory *iface )
126 {
127     mscorecf *This = impl_from_IClassFactory(iface);
128     ULONG ref = InterlockedIncrement(&This->ref);
129
130     TRACE("%p ref=%u\n", This, ref);
131
132     return ref;
133 }
134
135 static ULONG WINAPI mscorecf_Release(IClassFactory *iface )
136 {
137     mscorecf *This = impl_from_IClassFactory(iface);
138     ULONG ref = InterlockedDecrement(&This->ref);
139
140     TRACE("%p ref=%u\n", This, ref);
141
142     if (ref == 0)
143     {
144         HeapFree(GetProcessHeap(), 0, This);
145     }
146
147     return ref;
148 }
149
150 static HRESULT WINAPI mscorecf_CreateInstance(IClassFactory *iface,LPUNKNOWN pOuter,
151                             REFIID riid, LPVOID *ppobj )
152 {
153     mscorecf *This = impl_from_IClassFactory( iface );
154     HRESULT hr;
155     IUnknown *punk;
156
157     TRACE("%p %s %p\n", pOuter, debugstr_guid(riid), ppobj );
158
159     *ppobj = NULL;
160
161     if (pOuter)
162         return CLASS_E_NOAGGREGATION;
163
164     hr = This->pfnCreateInstance( &This->clsid, (LPVOID*) &punk );
165     if (SUCCEEDED(hr))
166     {
167         hr = IUnknown_QueryInterface( punk, riid, ppobj );
168
169         IUnknown_Release( punk );
170     }
171     else
172     {
173         WARN("Cannot create an instance object. 0x%08x\n", hr);
174     }
175     return hr;
176 }
177
178 static HRESULT WINAPI mscorecf_LockServer(IClassFactory *iface, BOOL dolock)
179 {
180     FIXME("(%p)->(%d),stub!\n",iface,dolock);
181     return S_OK;
182 }
183
184 static const struct IClassFactoryVtbl mscorecf_vtbl =
185 {
186     mscorecf_QueryInterface,
187     mscorecf_AddRef,
188     mscorecf_Release,
189     mscorecf_CreateInstance,
190     mscorecf_LockServer
191 };
192
193 HRESULT WINAPI CorBindToRuntimeHost(LPCWSTR pwszVersion, LPCWSTR pwszBuildFlavor,
194                                     LPCWSTR pwszHostConfigFile, VOID *pReserved,
195                                     DWORD startupFlags, REFCLSID rclsid,
196                                     REFIID riid, LPVOID *ppv)
197 {
198     HRESULT ret;
199     ICLRRuntimeInfo *info;
200
201     TRACE("(%s, %s, %s, %p, %d, %s, %s, %p)\n", debugstr_w(pwszVersion),
202           debugstr_w(pwszBuildFlavor), debugstr_w(pwszHostConfigFile), pReserved,
203           startupFlags, debugstr_guid(rclsid), debugstr_guid(riid), ppv);
204
205     *ppv = NULL;
206
207     ret = get_runtime_info(NULL, pwszVersion, pwszHostConfigFile, startupFlags, 0, TRUE, &info);
208
209     if (SUCCEEDED(ret))
210     {
211         ret = ICLRRuntimeInfo_GetInterface(info, rclsid, riid, ppv);
212
213         ICLRRuntimeInfo_Release(info);
214     }
215
216     return ret;
217 }
218
219 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
220 {
221     TRACE("(%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
222
223     MSCOREE_hInstance = hinstDLL;
224
225     switch (fdwReason)
226     {
227     case DLL_WINE_PREATTACH:
228         return FALSE;  /* prefer native version */
229     case DLL_PROCESS_ATTACH:
230         runtimehost_init();
231         DisableThreadLibraryCalls(hinstDLL);
232         break;
233     case DLL_PROCESS_DETACH:
234         expect_no_runtimes();
235         if (lpvReserved) break; /* process is terminating */
236         runtimehost_uninit();
237         break;
238     }
239     return TRUE;
240 }
241
242 __int32 WINAPI _CorExeMain2(PBYTE ptrMemory, DWORD cntMemory, LPWSTR imageName, LPWSTR loaderName, LPWSTR cmdLine)
243 {
244     TRACE("(%p, %u, %s, %s, %s)\n", ptrMemory, cntMemory, debugstr_w(imageName), debugstr_w(loaderName), debugstr_w(cmdLine));
245     FIXME("Directly running .NET applications not supported.\n");
246     return -1;
247 }
248
249 void WINAPI CorExitProcess(int exitCode)
250 {
251     TRACE("(%x)\n", exitCode);
252     unload_all_runtimes();
253     ExitProcess(exitCode);
254 }
255
256 VOID WINAPI _CorImageUnloading(PVOID imageBase)
257 {
258     TRACE("(%p): stub\n", imageBase);
259 }
260
261 HRESULT WINAPI _CorValidateImage(PVOID* imageBase, LPCWSTR imageName)
262 {
263     TRACE("(%p, %s): stub\n", imageBase, debugstr_w(imageName));
264     return E_FAIL;
265 }
266
267 HRESULT WINAPI GetCORSystemDirectory(LPWSTR pbuffer, DWORD cchBuffer, DWORD *dwLength)
268 {
269     ICLRRuntimeInfo *info;
270     HRESULT ret;
271
272     TRACE("(%p, %d, %p)!\n", pbuffer, cchBuffer, dwLength);
273
274     if (!dwLength || !pbuffer)
275         return E_POINTER;
276
277     ret = get_runtime_info(NULL, NULL, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, TRUE, &info);
278
279     if (SUCCEEDED(ret))
280     {
281         *dwLength = cchBuffer;
282         ret = ICLRRuntimeInfo_GetRuntimeDirectory(info, pbuffer, dwLength);
283
284         ICLRRuntimeInfo_Release(info);
285     }
286
287     return ret;
288 }
289
290 HRESULT WINAPI GetCORVersion(LPWSTR pbuffer, DWORD cchBuffer, DWORD *dwLength)
291 {
292     ICLRRuntimeInfo *info;
293     HRESULT ret;
294
295     TRACE("(%p, %d, %p)!\n", pbuffer, cchBuffer, dwLength);
296
297     if (!dwLength || !pbuffer)
298         return E_POINTER;
299
300     ret = get_runtime_info(NULL, NULL, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, TRUE, &info);
301
302     if (SUCCEEDED(ret))
303     {
304         *dwLength = cchBuffer;
305         ret = ICLRRuntimeInfo_GetVersionString(info, pbuffer, dwLength);
306
307         ICLRRuntimeInfo_Release(info);
308     }
309
310     return ret;
311 }
312
313 HRESULT WINAPI GetRequestedRuntimeInfo(LPCWSTR pExe, LPCWSTR pwszVersion, LPCWSTR pConfigurationFile,
314     DWORD startupFlags, DWORD runtimeInfoFlags, LPWSTR pDirectory, DWORD dwDirectory, DWORD *dwDirectoryLength,
315     LPWSTR pVersion, DWORD cchBuffer, DWORD *dwlength)
316 {
317     HRESULT ret;
318     ICLRRuntimeInfo *info;
319     DWORD length_dummy;
320
321     TRACE("(%s, %s, %s, 0x%08x, 0x%08x, %p, 0x%08x, %p, %p, 0x%08x, %p)\n", debugstr_w(pExe),
322           debugstr_w(pwszVersion), debugstr_w(pConfigurationFile), startupFlags, runtimeInfoFlags, pDirectory,
323           dwDirectory, dwDirectoryLength, pVersion, cchBuffer, dwlength);
324
325     if (!dwDirectoryLength) dwDirectoryLength = &length_dummy;
326
327     if (!dwlength) dwlength = &length_dummy;
328
329     ret = get_runtime_info(pExe, pwszVersion, pConfigurationFile, startupFlags, runtimeInfoFlags, TRUE, &info);
330
331     if (SUCCEEDED(ret))
332     {
333         *dwlength = cchBuffer;
334         ret = ICLRRuntimeInfo_GetVersionString(info, pVersion, dwlength);
335
336         if (SUCCEEDED(ret))
337         {
338             if(pwszVersion)
339                 pVersion[0] = pwszVersion[0];
340
341             *dwDirectoryLength = dwDirectory;
342             ret = ICLRRuntimeInfo_GetRuntimeDirectory(info, pDirectory, dwDirectoryLength);
343         }
344
345         ICLRRuntimeInfo_Release(info);
346     }
347
348     return ret;
349 }
350
351 HRESULT WINAPI GetRequestedRuntimeVersion(LPWSTR pExe, LPWSTR pVersion, DWORD cchBuffer, DWORD *dwlength)
352 {
353     TRACE("(%s, %p, %d, %p)\n", debugstr_w(pExe), pVersion, cchBuffer, dwlength);
354
355     if(!dwlength)
356         return E_POINTER;
357
358     return GetRequestedRuntimeInfo(pExe, NULL, NULL, 0, 0, NULL, 0, NULL, pVersion, cchBuffer, dwlength);
359 }
360
361 HRESULT WINAPI GetRealProcAddress(LPCSTR procname, void **ppv)
362 {
363     FIXME("(%s, %p)\n", debugstr_a(procname), ppv);
364     return CLR_E_SHIM_RUNTIMEEXPORT;
365 }
366
367 HRESULT WINAPI GetFileVersion(LPCWSTR szFilename, LPWSTR szBuffer, DWORD cchBuffer, DWORD *dwLength)
368 {
369     TRACE("(%s, %p, %d, %p)\n", debugstr_w(szFilename), szBuffer, cchBuffer, dwLength);
370
371     if (!szFilename || !dwLength)
372         return E_POINTER;
373
374     *dwLength = cchBuffer;
375     return CLRMetaHost_GetVersionFromFile(0, szFilename, szBuffer, dwLength);
376 }
377
378 HRESULT WINAPI LoadLibraryShim( LPCWSTR szDllName, LPCWSTR szVersion, LPVOID pvReserved, HMODULE * phModDll)
379 {
380     HRESULT ret=S_OK;
381     WCHAR dll_filename[MAX_PATH];
382     WCHAR version[MAX_PATH];
383     static const WCHAR default_version[] = {'v','1','.','1','.','4','3','2','2',0};
384     static const WCHAR slash[] = {'\\',0};
385     DWORD dummy;
386
387     TRACE("(%p %s, %p, %p, %p)\n", szDllName, debugstr_w(szDllName), szVersion, pvReserved, phModDll);
388
389     if (!szDllName || !phModDll)
390         return E_POINTER;
391
392     if (!get_install_root(dll_filename))
393     {
394         ERR("error reading registry key for installroot\n");
395         dll_filename[0] = 0;
396     }
397     else
398     {
399         if (!szVersion)
400         {
401             ret = GetCORVersion(version, MAX_PATH, &dummy);
402             if (SUCCEEDED(ret))
403                 szVersion = version;
404             else
405                 szVersion = default_version;
406         }
407         strcatW(dll_filename, szVersion);
408         strcatW(dll_filename, slash);
409     }
410
411     strcatW(dll_filename, szDllName);
412
413     *phModDll = LoadLibraryW(dll_filename);
414
415     return *phModDll ? S_OK : E_HANDLE;
416 }
417
418 HRESULT WINAPI LockClrVersion(FLockClrVersionCallback hostCallback, FLockClrVersionCallback *pBeginHostSetup, FLockClrVersionCallback *pEndHostSetup)
419 {
420     FIXME("(%p %p %p): stub\n", hostCallback, pBeginHostSetup, pEndHostSetup);
421     return S_OK;
422 }
423
424 HRESULT WINAPI CoInitializeCor(DWORD fFlags)
425 {
426     FIXME("(0x%08x): stub\n", fFlags);
427     return S_OK;
428 }
429
430 HRESULT WINAPI GetAssemblyMDImport(LPCWSTR szFileName, REFIID riid, IUnknown **ppIUnk)
431 {
432     FIXME("(%p %s, %s, %p): stub\n", szFileName, debugstr_w(szFileName), debugstr_guid(riid), *ppIUnk);
433     return ERROR_CALL_NOT_IMPLEMENTED;
434 }
435
436 HRESULT WINAPI GetVersionFromProcess(HANDLE hProcess, LPWSTR pVersion, DWORD cchBuffer, DWORD *dwLength)
437 {
438     FIXME("(%p, %p, %d, %p): stub\n", hProcess, pVersion, cchBuffer, dwLength);
439     return E_NOTIMPL;
440 }
441
442 HRESULT WINAPI LoadStringRCEx(LCID culture, UINT resId, LPWSTR pBuffer, int iBufLen, int bQuiet, int* pBufLen)
443 {
444     HRESULT res = S_OK;
445     if ((iBufLen <= 0) || !pBuffer)
446         return E_INVALIDARG;
447     pBuffer[0] = 0;
448     if (resId) {
449         FIXME("(%d, %x, %p, %d, %d, %p): semi-stub\n", culture, resId, pBuffer, iBufLen, bQuiet, pBufLen);
450         res = E_NOTIMPL;
451     }
452     else
453         res = E_FAIL;
454     if (pBufLen)
455         *pBufLen = lstrlenW(pBuffer);
456     return res;
457 }
458
459 HRESULT WINAPI LoadStringRC(UINT resId, LPWSTR pBuffer, int iBufLen, int bQuiet)
460 {
461     return LoadStringRCEx(-1, resId, pBuffer, iBufLen, bQuiet, NULL);
462 }
463
464 HRESULT WINAPI CorBindToRuntimeEx(LPWSTR szVersion, LPWSTR szBuildFlavor, DWORD nflags, REFCLSID rslsid,
465                                   REFIID riid, LPVOID *ppv)
466 {
467     HRESULT ret;
468     ICLRRuntimeInfo *info;
469
470     TRACE("%s %s %d %s %s %p\n", debugstr_w(szVersion), debugstr_w(szBuildFlavor), nflags, debugstr_guid( rslsid ),
471           debugstr_guid( riid ), ppv);
472
473     *ppv = NULL;
474
475     ret = get_runtime_info(NULL, szVersion, NULL, nflags, RUNTIME_INFO_UPGRADE_VERSION, TRUE, &info);
476
477     if (SUCCEEDED(ret))
478     {
479         ret = ICLRRuntimeInfo_GetInterface(info, rslsid, riid, ppv);
480
481         ICLRRuntimeInfo_Release(info);
482     }
483
484     return ret;
485 }
486
487 HRESULT WINAPI CorBindToCurrentRuntime(LPCWSTR filename, REFCLSID rclsid, REFIID riid, LPVOID *ppv)
488 {
489     FIXME("(%s, %s, %s, %p): stub\n", debugstr_w(filename), debugstr_guid(rclsid), debugstr_guid(riid), ppv);
490     return E_NOTIMPL;
491 }
492
493 STDAPI ClrCreateManagedInstance(LPCWSTR pTypeName, REFIID riid, void **ppObject)
494 {
495     HRESULT ret;
496     ICLRRuntimeInfo *info;
497     RuntimeHost *host;
498     MonoObject *obj;
499     IUnknown *unk;
500
501     TRACE("(%s,%s,%p)\n", debugstr_w(pTypeName), debugstr_guid(riid), ppObject);
502
503     /* FIXME: How to determine which runtime version to use? */
504     ret = get_runtime_info(NULL, NULL, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, TRUE, &info);
505
506     if (SUCCEEDED(ret))
507     {
508         ret = ICLRRuntimeInfo_GetRuntimeHost(info, &host);
509
510         ICLRRuntimeInfo_Release(info);
511     }
512
513     if (SUCCEEDED(ret))
514         ret = RuntimeHost_CreateManagedInstance(host, pTypeName, NULL, &obj);
515
516     if (SUCCEEDED(ret))
517         ret = RuntimeHost_GetIUnknownForObject(host, obj, &unk);
518
519     if (SUCCEEDED(ret))
520     {
521         ret = IUnknown_QueryInterface(unk, riid, ppObject);
522         IUnknown_Release(unk);
523     }
524
525     return ret;
526 }
527
528 BOOL WINAPI StrongNameSignatureVerification(LPCWSTR filename, DWORD inFlags, DWORD* pOutFlags)
529 {
530     FIXME("(%s, 0x%X, %p): stub\n", debugstr_w(filename), inFlags, pOutFlags);
531     return FALSE;
532 }
533
534 BOOL WINAPI StrongNameSignatureVerificationEx(LPCWSTR filename, BOOL forceVerification, BOOL* pVerified)
535 {
536     FIXME("(%s, %u, %p): stub\n", debugstr_w(filename), forceVerification, pVerified);
537     return FALSE;
538 }
539
540 HRESULT WINAPI CreateConfigStream(LPCWSTR filename, IStream **stream)
541 {
542     FIXME("(%s, %p): stub\n", debugstr_w(filename), stream);
543     return E_NOTIMPL;
544 }
545
546 HRESULT WINAPI CreateDebuggingInterfaceFromVersion(int nDebugVersion, LPCWSTR version, IUnknown **ppv)
547 {
548     const WCHAR v2_0[] = {'v','2','.','0','.','5','0','7','2','7',0};
549     HRESULT hr = E_FAIL;
550     ICLRRuntimeInfo *runtimeinfo;
551
552     if(nDebugVersion < 1 || nDebugVersion > 4)
553         return E_INVALIDARG;
554
555     TRACE("(%d %s, %p): stub\n", nDebugVersion, debugstr_w(version), ppv);
556
557     if(!ppv)
558         return E_INVALIDARG;
559
560     *ppv = NULL;
561
562     if(strcmpW(version, v2_0) != 0)
563     {
564         FIXME("Currently .NET Version '%s' not support.\n", debugstr_w(version));
565         return E_INVALIDARG;
566     }
567
568     if(nDebugVersion != 3)
569         return E_INVALIDARG;
570
571     hr = CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)&runtimeinfo);
572     if(hr == S_OK)
573     {
574         hr = ICLRRuntimeInfo_GetInterface(runtimeinfo, &CLSID_CLRDebuggingLegacy, &IID_ICorDebug, (void**)ppv);
575
576         ICLRRuntimeInfo_Release(runtimeinfo);
577     }
578
579     if(!*ppv)
580         return E_FAIL;
581
582     return hr;
583 }
584
585 HRESULT WINAPI CLRCreateInstance(REFCLSID clsid, REFIID riid, LPVOID *ppInterface)
586 {
587     TRACE("(%s,%s,%p)\n", debugstr_guid(clsid), debugstr_guid(riid), ppInterface);
588
589     if (IsEqualGUID(clsid, &CLSID_CLRMetaHost))
590         return CLRMetaHost_CreateInstance(riid, ppInterface);
591
592     FIXME("not implemented for class %s\n", debugstr_guid(clsid));
593
594     return CLASS_E_CLASSNOTAVAILABLE;
595 }
596
597 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
598 {
599     mscorecf *This;
600     HRESULT hr;
601
602     TRACE("(%s, %s, %p): stub\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
603
604     if(!ppv)
605         return E_INVALIDARG;
606
607     This = HeapAlloc(GetProcessHeap(), 0, sizeof(mscorecf));
608
609     This->IClassFactory_iface.lpVtbl = &mscorecf_vtbl;
610     This->pfnCreateInstance = create_monodata;
611     This->ref = 1;
612     This->clsid = *rclsid;
613
614     hr = IClassFactory_QueryInterface( &This->IClassFactory_iface, riid, ppv );
615     IClassFactory_Release(&This->IClassFactory_iface);
616
617     return hr;
618 }
619
620 static void parse_msi_version_string(const char *version, int *parts)
621 {
622     const char *minor_start, *build_start;
623
624     parts[0] = atoi(version);
625
626     parts[1] = parts[2] = 0;
627
628     minor_start = strchr(version, '.');
629     if (minor_start)
630     {
631         minor_start++;
632         parts[1] = atoi(minor_start);
633
634         build_start = strchr(minor_start, '.');
635         if (build_start)
636             parts[2] = atoi(build_start+1);
637     }
638 }
639
640 static BOOL install_wine_mono(void)
641 {
642     BOOL is_wow64=0;
643     HMODULE hmsi;
644     UINT (WINAPI *pMsiGetProductInfoA)(LPCSTR,LPCSTR,LPSTR,DWORD*);
645     char versionstringbuf[15];
646     UINT res;
647     DWORD buffer_size;
648     PROCESS_INFORMATION pi;
649     STARTUPINFOW si;
650     WCHAR app[MAX_PATH];
651     WCHAR *args;
652     LONG len;
653     BOOL ret;
654
655     static const char* mono_version = "0.0.8";
656     static const char* mono_product_code = "{E45D8920-A758-4088-B6C6-31DBB276992E}";
657
658     static const WCHAR controlW[] = {'\\','c','o','n','t','r','o','l','.','e','x','e',0};
659     static const WCHAR argsW[] =
660         {' ','a','p','p','w','i','z','.','c','p','l',' ','i','n','s','t','a','l','l','_','m','o','n','o',0};
661
662     IsWow64Process(GetCurrentProcess(), &is_wow64);
663
664     if (is_wow64)
665     {
666         TRACE("not installing mono in wow64 process\n");
667         return TRUE;
668     }
669
670     hmsi = LoadLibraryA("msi");
671
672     if (!hmsi)
673     {
674         ERR("couldn't load msi.dll\n");
675         return FALSE;
676     }
677
678     pMsiGetProductInfoA = (void*)GetProcAddress(hmsi, "MsiGetProductInfoA");
679
680     buffer_size = sizeof(versionstringbuf);
681
682     res = pMsiGetProductInfoA(mono_product_code, "VersionString", versionstringbuf, &buffer_size);
683
684     FreeLibrary(hmsi);
685
686     if (res == ERROR_SUCCESS)
687     {
688         int current_version[3], wanted_version[3], i;
689
690         TRACE("found installed version %s\n", versionstringbuf);
691
692         parse_msi_version_string(versionstringbuf, current_version);
693         parse_msi_version_string(mono_version, wanted_version);
694
695         for (i=0; i<3; i++)
696         {
697             if (current_version[i] < wanted_version[i])
698                 break;
699             else if (current_version[i] > wanted_version[i])
700             {
701                 TRACE("installed version is newer than %s, quitting\n", mono_version);
702                 return TRUE;
703             }
704         }
705
706         if (i == 3)
707         {
708             TRACE("version %s is already installed, quitting\n", mono_version);
709             return TRUE;
710         }
711     }
712
713     len = GetSystemDirectoryW(app, MAX_PATH-sizeof(controlW)/sizeof(WCHAR));
714     memcpy(app+len, controlW, sizeof(controlW));
715
716     args = HeapAlloc(GetProcessHeap(), 0, (len*sizeof(WCHAR) + sizeof(controlW) + sizeof(argsW)));
717     if(!args)
718         return FALSE;
719
720     memcpy(args, app, len*sizeof(WCHAR) + sizeof(controlW));
721     memcpy(args + len + sizeof(controlW)/sizeof(WCHAR)-1, argsW, sizeof(argsW));
722
723     TRACE("starting %s\n", debugstr_w(args));
724
725     memset(&si, 0, sizeof(si));
726     si.cb = sizeof(si);
727     ret = CreateProcessW(app, args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
728     HeapFree(GetProcessHeap(), 0, args);
729     if (ret) {
730         CloseHandle(pi.hThread);
731         WaitForSingleObject(pi.hProcess, INFINITE);
732         CloseHandle(pi.hProcess);
733     }
734
735     return ret;
736 }
737
738 HRESULT WINAPI DllRegisterServer(void)
739 {
740     install_wine_mono();
741
742     return __wine_register_resources( MSCOREE_hInstance );
743 }
744
745 HRESULT WINAPI DllUnregisterServer(void)
746 {
747     return __wine_unregister_resources( MSCOREE_hInstance );
748 }
749
750 HRESULT WINAPI DllCanUnloadNow(VOID)
751 {
752     return S_FALSE;
753 }
754
755 void WINAPI CoEEShutDownCOM(void)
756 {
757     FIXME("stub.\n");
758 }
759
760 INT WINAPI ND_RU1( const void *ptr, INT offset )
761 {
762     return *((const BYTE *)ptr + offset);
763 }
764
765 INT WINAPI ND_RI2( const void *ptr, INT offset )
766 {
767     return *(const SHORT *)((const BYTE *)ptr + offset);
768 }
769
770 INT WINAPI ND_RI4( const void *ptr, INT offset )
771 {
772     return *(const INT *)((const BYTE *)ptr + offset);
773 }
774
775 INT64 WINAPI ND_RI8( const void *ptr, INT offset )
776 {
777     return *(const INT64 *)((const BYTE *)ptr + offset);
778 }
779
780 void WINAPI ND_WU1( void *ptr, INT offset, BYTE val )
781 {
782     *((BYTE *)ptr + offset) = val;
783 }
784
785 void WINAPI ND_WI2( void *ptr, INT offset, SHORT val )
786 {
787     *(SHORT *)((BYTE *)ptr + offset) = val;
788 }
789
790 void WINAPI ND_WI4( void *ptr, INT offset, INT val )
791 {
792     *(INT *)((BYTE *)ptr + offset) = val;
793 }
794
795 void WINAPI ND_WI8( void *ptr, INT offset, INT64 val )
796 {
797     *(INT64 *)((BYTE *)ptr + offset) = val;
798 }
799
800 void WINAPI ND_CopyObjDst( const void *src, void *dst, INT offset, INT size )
801 {
802     memcpy( (BYTE *)dst + offset, src, size );
803 }
804
805 void WINAPI ND_CopyObjSrc( const void *src, INT offset, void *dst, INT size )
806 {
807     memcpy( dst, (const BYTE *)src + offset, size );
808 }