localspl: Add unixname port extension.
[wine.git] / dlls / mscoree / mscoree_main.c
bloba6134f0226b1427fabadd1d3d9f8ff7e97af18a2
1 /*
2 * Implementation of mscoree.dll
3 * Microsoft Component Object Runtime Execution Engine
5 * Copyright 2006 Paul Chitescu
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.
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.
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
22 #include <stdarg.h>
24 #define COBJMACROS
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 "ocidl.h"
32 #include "shellapi.h"
33 #include "strongname.h"
35 #include "initguid.h"
36 #include "msxml2.h"
37 #include "corerror.h"
38 #include "cor.h"
39 #include "mscoree.h"
40 #include "corhdr.h"
41 #include "cordebug.h"
42 #include "metahost.h"
43 #include "fusion.h"
44 #include "wine/list.h"
45 #include "mscoree_private.h"
46 #include "rpcproxy.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL( mscoree );
51 WINE_DECLARE_DEBUG_CHANNEL(winediag);
53 struct print_handler_tls
55 int length;
56 char buffer[1018];
59 static DWORD print_tls_index = TLS_OUT_OF_INDEXES;
61 typedef HRESULT (*fnCreateInstance)(REFIID riid, LPVOID *ppObj);
63 char *WtoA(LPCWSTR wstr)
65 int length;
66 char *result;
68 length = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
70 result = HeapAlloc(GetProcessHeap(), 0, length);
72 if (result)
73 WideCharToMultiByte(CP_UTF8, 0, wstr, -1, result, length, NULL, NULL);
75 return result;
78 static BOOL get_install_root(LPWSTR install_dir)
80 static 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};
81 static const WCHAR install_root[] = {'I','n','s','t','a','l','l','R','o','o','t',0};
83 DWORD len;
84 HKEY key;
86 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, dotnet_key, 0, KEY_READ, &key))
87 return FALSE;
89 len = MAX_PATH * sizeof(WCHAR);
90 if (RegQueryValueExW(key, install_root, 0, NULL, (LPBYTE)install_dir, &len))
92 RegCloseKey(key);
93 return FALSE;
95 RegCloseKey(key);
97 return TRUE;
100 typedef struct mscorecf
102 IClassFactory IClassFactory_iface;
103 LONG ref;
105 fnCreateInstance pfnCreateInstance;
107 CLSID clsid;
108 } mscorecf;
110 static inline mscorecf *impl_from_IClassFactory( IClassFactory *iface )
112 return CONTAINING_RECORD(iface, mscorecf, IClassFactory_iface);
115 static HRESULT WINAPI mscorecf_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppobj )
117 TRACE("%s %p\n", debugstr_guid(riid), ppobj);
119 if (IsEqualGUID(riid, &IID_IUnknown) ||
120 IsEqualGUID(riid, &IID_IClassFactory))
122 IClassFactory_AddRef( iface );
123 *ppobj = iface;
124 return S_OK;
127 ERR("interface %s not implemented\n", debugstr_guid(riid));
128 return E_NOINTERFACE;
131 static ULONG WINAPI mscorecf_AddRef(IClassFactory *iface )
133 mscorecf *This = impl_from_IClassFactory(iface);
134 ULONG ref = InterlockedIncrement(&This->ref);
136 TRACE("%p ref=%lu\n", This, ref);
138 return ref;
141 static ULONG WINAPI mscorecf_Release(IClassFactory *iface )
143 mscorecf *This = impl_from_IClassFactory(iface);
144 ULONG ref = InterlockedDecrement(&This->ref);
146 TRACE("%p ref=%lu\n", This, ref);
148 if (ref == 0)
150 HeapFree(GetProcessHeap(), 0, This);
153 return ref;
156 static HRESULT WINAPI mscorecf_CreateInstance(IClassFactory *iface,LPUNKNOWN pOuter,
157 REFIID riid, LPVOID *ppobj )
159 mscorecf *This = impl_from_IClassFactory( iface );
160 HRESULT hr;
161 IUnknown *punk;
163 TRACE("%p %s %p\n", pOuter, debugstr_guid(riid), ppobj );
165 *ppobj = NULL;
167 if (pOuter)
168 return CLASS_E_NOAGGREGATION;
170 hr = This->pfnCreateInstance( &This->clsid, (LPVOID*) &punk );
171 if (SUCCEEDED(hr))
173 hr = IUnknown_QueryInterface( punk, riid, ppobj );
175 IUnknown_Release( punk );
177 else
179 WARN("Cannot create an instance object. 0x%08lx\n", hr);
181 return hr;
184 static HRESULT WINAPI mscorecf_LockServer(IClassFactory *iface, BOOL dolock)
186 FIXME("(%p)->(%d),stub!\n",iface,dolock);
187 return S_OK;
190 static const struct IClassFactoryVtbl mscorecf_vtbl =
192 mscorecf_QueryInterface,
193 mscorecf_AddRef,
194 mscorecf_Release,
195 mscorecf_CreateInstance,
196 mscorecf_LockServer
199 HRESULT WINAPI CorBindToRuntimeHost(LPCWSTR pwszVersion, LPCWSTR pwszBuildFlavor,
200 LPCWSTR pwszHostConfigFile, VOID *pReserved,
201 DWORD startupFlags, REFCLSID rclsid,
202 REFIID riid, LPVOID *ppv)
204 HRESULT ret;
205 ICLRRuntimeInfo *info;
207 TRACE("(%s, %s, %s, %p, %ld, %s, %s, %p)\n", debugstr_w(pwszVersion),
208 debugstr_w(pwszBuildFlavor), debugstr_w(pwszHostConfigFile), pReserved,
209 startupFlags, debugstr_guid(rclsid), debugstr_guid(riid), ppv);
211 *ppv = NULL;
213 ret = get_runtime_info(NULL, pwszVersion, pwszHostConfigFile, NULL, startupFlags, 0, TRUE, &info);
215 if (SUCCEEDED(ret))
217 ret = ICLRRuntimeInfo_GetInterface(info, rclsid, riid, ppv);
219 ICLRRuntimeInfo_Release(info);
222 return ret;
225 void CDECL mono_print_handler_fn(const char *string, INT is_stdout)
227 struct print_handler_tls *tls = TlsGetValue(print_tls_index);
229 if (!tls)
231 tls = HeapAlloc(GetProcessHeap(), 0, sizeof(*tls));
232 tls->length = 0;
233 TlsSetValue(print_tls_index, tls);
236 while (*string)
238 int remaining_buffer = sizeof(tls->buffer) - tls->length;
239 int length = strlen(string);
240 const char *newline = memchr(string, '\n', min(length, remaining_buffer));
242 if (newline)
244 length = newline - string + 1;
245 wine_dbg_printf("%.*s%.*s", tls->length, tls->buffer, length, string);
246 tls->length = 0;
247 string += length;
249 else if (length > remaining_buffer)
251 /* this would overflow Wine's debug buffer */
252 wine_dbg_printf("%.*s%.*s\n", tls->length, tls->buffer, remaining_buffer, string);
253 tls->length = 0;
254 string += remaining_buffer;
256 else
258 memcpy(tls->buffer + tls->length, string, length);
259 tls->length += length;
260 break;
265 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
267 TRACE("(%p, %ld, %p)\n", hinstDLL, fdwReason, lpvReserved);
269 switch (fdwReason)
271 case DLL_PROCESS_ATTACH:
272 runtimehost_init();
274 print_tls_index = TlsAlloc();
276 if (print_tls_index == TLS_OUT_OF_INDEXES)
277 return FALSE;
279 break;
280 case DLL_THREAD_DETACH:
281 if (print_tls_index != TLS_OUT_OF_INDEXES)
282 HeapFree(GetProcessHeap(), 0, TlsGetValue(print_tls_index));
283 break;
284 case DLL_PROCESS_DETACH:
285 expect_no_runtimes();
286 if (lpvReserved) break; /* process is terminating */
287 runtimehost_uninit();
288 if (print_tls_index != TLS_OUT_OF_INDEXES)
290 HeapFree(GetProcessHeap(), 0, TlsGetValue(print_tls_index));
291 TlsFree(print_tls_index);
293 break;
295 return TRUE;
298 __int32 WINAPI _CorExeMain2(PBYTE ptrMemory, DWORD cntMemory, LPWSTR imageName, LPWSTR loaderName, LPWSTR cmdLine)
300 TRACE("(%p, %lu, %s, %s, %s)\n", ptrMemory, cntMemory, debugstr_w(imageName), debugstr_w(loaderName), debugstr_w(cmdLine));
301 FIXME("Directly running .NET applications not supported.\n");
302 return -1;
305 void WINAPI CorExitProcess(int exitCode)
307 TRACE("(%x)\n", exitCode);
308 CLRMetaHost_ExitProcess(0, exitCode);
311 VOID WINAPI _CorImageUnloading(PVOID imageBase)
313 TRACE("(%p): stub\n", imageBase);
316 HRESULT WINAPI _CorValidateImage(PVOID* imageBase, LPCWSTR imageName)
318 TRACE("(%p, %s): stub\n", imageBase, debugstr_w(imageName));
319 return E_FAIL;
322 HRESULT WINAPI GetCORSystemDirectory(LPWSTR pbuffer, DWORD cchBuffer, DWORD *dwLength)
324 ICLRRuntimeInfo *info;
325 HRESULT ret;
327 TRACE("(%p, %ld, %p)!\n", pbuffer, cchBuffer, dwLength);
329 if (!dwLength || !pbuffer)
330 return E_POINTER;
332 ret = get_runtime_info(NULL, NULL, NULL, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, TRUE, &info);
334 if (SUCCEEDED(ret))
336 *dwLength = cchBuffer;
337 ret = ICLRRuntimeInfo_GetRuntimeDirectory(info, pbuffer, dwLength);
339 ICLRRuntimeInfo_Release(info);
342 return ret;
345 HRESULT WINAPI GetCORVersion(LPWSTR pbuffer, DWORD cchBuffer, DWORD *dwLength)
347 ICLRRuntimeInfo *info;
348 HRESULT ret;
350 TRACE("(%p, %ld, %p)!\n", pbuffer, cchBuffer, dwLength);
352 if (!dwLength || !pbuffer)
353 return E_POINTER;
355 ret = get_runtime_info(NULL, NULL, NULL, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, TRUE, &info);
357 if (SUCCEEDED(ret))
359 *dwLength = cchBuffer;
360 ret = ICLRRuntimeInfo_GetVersionString(info, pbuffer, dwLength);
362 ICLRRuntimeInfo_Release(info);
365 return ret;
368 HRESULT WINAPI CorIsLatestSvc(int *unk1, int *unk2)
370 ERR_(winediag)("If this function is called, it is likely the result of a broken .NET installation\n");
372 if (!unk1 || !unk2)
373 return E_POINTER;
375 return S_OK;
378 HRESULT WINAPI CorGetSvc(void *unk)
380 ERR_(winediag)("If this function is called, it is likely the result of a broken .NET installation\n");
382 return E_NOTIMPL;
385 HRESULT WINAPI GetRequestedRuntimeInfo(LPCWSTR pExe, LPCWSTR pwszVersion, LPCWSTR pConfigurationFile,
386 DWORD startupFlags, DWORD runtimeInfoFlags, LPWSTR pDirectory, DWORD dwDirectory, DWORD *dwDirectoryLength,
387 LPWSTR pVersion, DWORD cchBuffer, DWORD *dwlength)
389 HRESULT ret;
390 ICLRRuntimeInfo *info;
391 DWORD length_dummy;
393 TRACE("(%s, %s, %s, 0x%08lx, 0x%08lx, %p, 0x%08lx, %p, %p, 0x%08lx, %p)\n", debugstr_w(pExe),
394 debugstr_w(pwszVersion), debugstr_w(pConfigurationFile), startupFlags, runtimeInfoFlags, pDirectory,
395 dwDirectory, dwDirectoryLength, pVersion, cchBuffer, dwlength);
397 if (!dwDirectoryLength) dwDirectoryLength = &length_dummy;
399 if (!dwlength) dwlength = &length_dummy;
401 ret = get_runtime_info(pExe, pwszVersion, pConfigurationFile, NULL, startupFlags, runtimeInfoFlags, TRUE, &info);
403 if (SUCCEEDED(ret))
405 *dwlength = cchBuffer;
406 ret = ICLRRuntimeInfo_GetVersionString(info, pVersion, dwlength);
408 if (SUCCEEDED(ret))
410 if(pwszVersion)
411 pVersion[0] = pwszVersion[0];
413 *dwDirectoryLength = dwDirectory;
414 ret = ICLRRuntimeInfo_GetRuntimeDirectory(info, pDirectory, dwDirectoryLength);
417 ICLRRuntimeInfo_Release(info);
420 return ret;
423 HRESULT WINAPI GetRequestedRuntimeVersion(LPWSTR pExe, LPWSTR pVersion, DWORD cchBuffer, DWORD *dwlength)
425 TRACE("(%s, %p, %ld, %p)\n", debugstr_w(pExe), pVersion, cchBuffer, dwlength);
427 if(!dwlength)
428 return E_POINTER;
430 return GetRequestedRuntimeInfo(pExe, NULL, NULL, 0, 0, NULL, 0, NULL, pVersion, cchBuffer, dwlength);
433 HRESULT WINAPI GetRealProcAddress(LPCSTR procname, void **ppv)
435 FIXME("(%s, %p)\n", debugstr_a(procname), ppv);
436 return CLR_E_SHIM_RUNTIMEEXPORT;
439 HRESULT WINAPI GetFileVersion(LPCWSTR szFilename, LPWSTR szBuffer, DWORD cchBuffer, DWORD *dwLength)
441 TRACE("(%s, %p, %ld, %p)\n", debugstr_w(szFilename), szBuffer, cchBuffer, dwLength);
443 if (!szFilename || !dwLength)
444 return E_POINTER;
446 *dwLength = cchBuffer;
447 return CLRMetaHost_GetVersionFromFile(0, szFilename, szBuffer, dwLength);
450 HRESULT WINAPI LoadLibraryShim( LPCWSTR szDllName, LPCWSTR szVersion, LPVOID pvReserved, HMODULE * phModDll)
452 HRESULT ret=S_OK;
453 WCHAR dll_filename[MAX_PATH];
454 WCHAR version[MAX_PATH];
455 static const WCHAR default_version[] = {'v','1','.','1','.','4','3','2','2',0};
456 static const WCHAR slash[] = {'\\',0};
457 DWORD dummy;
459 TRACE("(%p %s, %p, %p, %p)\n", szDllName, debugstr_w(szDllName), szVersion, pvReserved, phModDll);
461 if (!szDllName || !phModDll)
462 return E_POINTER;
464 if (!get_install_root(dll_filename))
466 ERR("error reading registry key for installroot\n");
467 dll_filename[0] = 0;
469 else
471 if (!szVersion)
473 ret = GetCORVersion(version, MAX_PATH, &dummy);
474 if (SUCCEEDED(ret))
475 szVersion = version;
476 else
477 szVersion = default_version;
479 lstrcatW(dll_filename, szVersion);
480 lstrcatW(dll_filename, slash);
483 lstrcatW(dll_filename, szDllName);
485 *phModDll = LoadLibraryW(dll_filename);
487 return *phModDll ? S_OK : E_HANDLE;
490 HRESULT WINAPI LockClrVersion(FLockClrVersionCallback hostCallback, FLockClrVersionCallback *pBeginHostSetup, FLockClrVersionCallback *pEndHostSetup)
492 FIXME("(%p %p %p): stub\n", hostCallback, pBeginHostSetup, pEndHostSetup);
493 return S_OK;
496 HRESULT WINAPI CoInitializeCor(DWORD fFlags)
498 FIXME("(0x%08lx): stub\n", fFlags);
499 return S_OK;
502 HRESULT WINAPI GetAssemblyMDImport(LPCWSTR szFileName, REFIID riid, IUnknown **ppIUnk)
504 FIXME("(%p %s, %s, %p): stub\n", szFileName, debugstr_w(szFileName), debugstr_guid(riid), *ppIUnk);
505 return ERROR_CALL_NOT_IMPLEMENTED;
508 HRESULT WINAPI GetVersionFromProcess(HANDLE hProcess, LPWSTR pVersion, DWORD cchBuffer, DWORD *dwLength)
510 FIXME("(%p, %p, %ld, %p): stub\n", hProcess, pVersion, cchBuffer, dwLength);
511 return E_NOTIMPL;
514 HRESULT WINAPI LoadStringRCEx(LCID culture, UINT resId, LPWSTR pBuffer, int iBufLen, int bQuiet, int* pBufLen)
516 HRESULT res = S_OK;
517 if ((iBufLen <= 0) || !pBuffer)
518 return E_INVALIDARG;
519 pBuffer[0] = 0;
520 if (resId) {
521 FIXME("(%ld, %x, %p, %d, %d, %p): semi-stub\n", culture, resId, pBuffer, iBufLen, bQuiet, pBufLen);
522 res = E_NOTIMPL;
524 else
525 res = E_FAIL;
526 if (pBufLen)
527 *pBufLen = lstrlenW(pBuffer);
528 return res;
531 HRESULT WINAPI LoadStringRC(UINT resId, LPWSTR pBuffer, int iBufLen, int bQuiet)
533 return LoadStringRCEx(-1, resId, pBuffer, iBufLen, bQuiet, NULL);
536 HRESULT WINAPI CorBindToRuntimeEx(LPWSTR szVersion, LPWSTR szBuildFlavor, DWORD nflags, REFCLSID rslsid,
537 REFIID riid, LPVOID *ppv)
539 HRESULT ret;
540 ICLRRuntimeInfo *info;
542 TRACE("%s %s %ld %s %s %p\n", debugstr_w(szVersion), debugstr_w(szBuildFlavor), nflags, debugstr_guid( rslsid ),
543 debugstr_guid( riid ), ppv);
545 *ppv = NULL;
547 ret = get_runtime_info(NULL, szVersion, NULL, NULL, nflags, RUNTIME_INFO_UPGRADE_VERSION, TRUE, &info);
549 if (SUCCEEDED(ret))
551 ret = ICLRRuntimeInfo_GetInterface(info, rslsid, riid, ppv);
553 ICLRRuntimeInfo_Release(info);
556 return ret;
559 HRESULT WINAPI CorBindToCurrentRuntime(LPCWSTR filename, REFCLSID rclsid, REFIID riid, LPVOID *ppv)
561 HRESULT ret;
562 ICLRRuntimeInfo *info;
564 TRACE("(%s, %s, %s, %p)\n", debugstr_w(filename), debugstr_guid(rclsid), debugstr_guid(riid), ppv);
566 *ppv = NULL;
568 ret = get_runtime_info(NULL, NULL, filename, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, TRUE, &info);
570 if (SUCCEEDED(ret))
572 ret = ICLRRuntimeInfo_GetInterface(info, rclsid, riid, ppv);
574 ICLRRuntimeInfo_Release(info);
577 return ret;
580 STDAPI ClrCreateManagedInstance(LPCWSTR pTypeName, REFIID riid, void **ppObject)
582 HRESULT ret;
583 ICLRRuntimeInfo *info;
584 RuntimeHost *host;
585 MonoObject *obj;
586 IUnknown *unk;
588 TRACE("(%s,%s,%p)\n", debugstr_w(pTypeName), debugstr_guid(riid), ppObject);
590 /* FIXME: How to determine which runtime version to use? */
591 ret = get_runtime_info(NULL, NULL, NULL, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, TRUE, &info);
593 if (SUCCEEDED(ret))
595 ret = ICLRRuntimeInfo_GetRuntimeHost(info, &host);
597 ICLRRuntimeInfo_Release(info);
600 if (SUCCEEDED(ret))
601 ret = RuntimeHost_CreateManagedInstance(host, pTypeName, NULL, &obj);
603 if (SUCCEEDED(ret))
604 ret = RuntimeHost_GetIUnknownForObject(host, obj, &unk);
606 if (SUCCEEDED(ret))
608 ret = IUnknown_QueryInterface(unk, riid, ppObject);
609 IUnknown_Release(unk);
612 return ret;
615 BOOLEAN WINAPI StrongNameSignatureVerification(LPCWSTR filename, DWORD inFlags, DWORD *pOutFlags)
617 FIXME("(%s, 0x%lX, %p): stub\n", debugstr_w(filename), inFlags, pOutFlags);
618 return FALSE;
621 BOOLEAN WINAPI StrongNameSignatureVerificationEx(LPCWSTR filename, BOOLEAN forceVerification, BOOLEAN *pVerified)
623 FIXME("(%s, %u, %p): stub\n", debugstr_w(filename), forceVerification, pVerified);
624 *pVerified = TRUE;
625 return TRUE;
628 BOOLEAN WINAPI StrongNameTokenFromAssembly(LPCWSTR path, BYTE **token, ULONG *size)
630 FIXME("(%s, %p, %p): stub\n", debugstr_w(path), token, size);
631 return FALSE;
634 HRESULT WINAPI CreateDebuggingInterfaceFromVersion(int nDebugVersion, LPCWSTR version, IUnknown **ppv)
636 static const WCHAR v2_0[] = {'v','2','.','0','.','5','0','7','2','7',0};
637 HRESULT hr = E_FAIL;
638 ICLRRuntimeInfo *runtimeinfo;
640 if(nDebugVersion < 1 || nDebugVersion > 4)
641 return E_INVALIDARG;
643 TRACE("(%d %s, %p): stub\n", nDebugVersion, debugstr_w(version), ppv);
645 if(!ppv)
646 return E_INVALIDARG;
648 *ppv = NULL;
650 if(wcscmp(version, v2_0) != 0)
652 FIXME("Currently .NET Version '%s' not support.\n", debugstr_w(version));
653 return E_INVALIDARG;
656 if(nDebugVersion != 3)
657 return E_INVALIDARG;
659 hr = CLRMetaHost_GetRuntime(0, version, &IID_ICLRRuntimeInfo, (void**)&runtimeinfo);
660 if(hr == S_OK)
662 hr = ICLRRuntimeInfo_GetInterface(runtimeinfo, &CLSID_CLRDebuggingLegacy, &IID_ICorDebug, (void**)ppv);
664 ICLRRuntimeInfo_Release(runtimeinfo);
667 if(!*ppv)
668 return E_FAIL;
670 return hr;
673 HRESULT WINAPI CLRCreateInstance(REFCLSID clsid, REFIID riid, LPVOID *ppInterface)
675 TRACE("(%s,%s,%p)\n", debugstr_guid(clsid), debugstr_guid(riid), ppInterface);
677 if (IsEqualGUID(clsid, &CLSID_CLRMetaHost))
678 return CLRMetaHost_CreateInstance(riid, ppInterface);
679 if (IsEqualGUID(clsid, &CLSID_CLRMetaHostPolicy))
680 return CLRMetaHostPolicy_CreateInstance(riid, ppInterface);
682 FIXME("not implemented for class %s\n", debugstr_guid(clsid));
684 return CLASS_E_CLASSNOTAVAILABLE;
687 HRESULT WINAPI CreateInterface(REFCLSID clsid, REFIID riid, LPVOID *ppInterface)
689 TRACE("(%s,%s,%p)\n", debugstr_guid(clsid), debugstr_guid(riid), ppInterface);
691 return CLRCreateInstance(clsid, riid, ppInterface);
694 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
696 mscorecf *This;
697 HRESULT hr;
699 TRACE("(%s, %s, %p): stub\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
701 if(!ppv)
702 return E_INVALIDARG;
704 This = HeapAlloc(GetProcessHeap(), 0, sizeof(mscorecf));
706 This->IClassFactory_iface.lpVtbl = &mscorecf_vtbl;
707 This->pfnCreateInstance = create_monodata;
708 This->ref = 1;
709 This->clsid = *rclsid;
711 hr = IClassFactory_QueryInterface( &This->IClassFactory_iface, riid, ppv );
712 IClassFactory_Release(&This->IClassFactory_iface);
714 return hr;
717 static void parse_msi_version_string(const char *version, int *parts)
719 const char *minor_start, *build_start;
721 parts[0] = atoi(version);
723 parts[1] = parts[2] = 0;
725 minor_start = strchr(version, '.');
726 if (minor_start)
728 minor_start++;
729 parts[1] = atoi(minor_start);
731 build_start = strchr(minor_start, '.');
732 if (build_start)
733 parts[2] = atoi(build_start+1);
737 static int compare_versions(const char *a, const char *b)
739 int a_parts[3], b_parts[3], i;
741 parse_msi_version_string(a, a_parts);
742 parse_msi_version_string(b, b_parts);
744 for (i=0; i<3; i++)
745 if (a_parts[i] != b_parts[i])
746 return a_parts[i] - b_parts[i];
748 return 0;
751 static BOOL invoke_appwiz(void)
753 PROCESS_INFORMATION pi;
754 STARTUPINFOW si;
755 WCHAR app[MAX_PATH];
756 WCHAR *args;
757 LONG len;
758 BOOL ret;
760 static const WCHAR controlW[] = {'\\','c','o','n','t','r','o','l','.','e','x','e',0};
761 static const WCHAR argsW[] =
762 {' ','a','p','p','w','i','z','.','c','p','l',' ','i','n','s','t','a','l','l','_','m','o','n','o',0};
764 len = GetSystemDirectoryW(app, MAX_PATH - ARRAY_SIZE(controlW));
765 memcpy(app+len, controlW, sizeof(controlW));
767 args = HeapAlloc(GetProcessHeap(), 0, (len*sizeof(WCHAR) + sizeof(controlW) + sizeof(argsW)));
768 if(!args)
769 return FALSE;
771 memcpy(args, app, len*sizeof(WCHAR) + sizeof(controlW));
772 memcpy(args + len + ARRAY_SIZE(controlW) - 1, argsW, sizeof(argsW));
774 TRACE("starting %s\n", debugstr_w(args));
776 memset(&si, 0, sizeof(si));
777 si.cb = sizeof(si);
778 ret = CreateProcessW(app, args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
779 HeapFree(GetProcessHeap(), 0, args);
780 if (ret) {
781 CloseHandle(pi.hThread);
782 WaitForSingleObject(pi.hProcess, INFINITE);
783 CloseHandle(pi.hProcess);
786 return ret;
789 static BOOL get_support_msi(LPCWSTR mono_path, LPWSTR msi_path)
791 static const WCHAR support_msi_relative[] = {'\\','s','u','p','p','o','r','t','\\','w','i','n','e','m','o','n','o','-','s','u','p','p','o','r','t','.','m','s','i',0};
792 UINT (WINAPI *pMsiOpenPackageW)(LPCWSTR,ULONG*);
793 UINT (WINAPI *pMsiGetProductPropertyA)(ULONG,LPCSTR,LPSTR,LPDWORD);
794 UINT (WINAPI *pMsiCloseHandle)(ULONG);
795 HMODULE hmsi = NULL;
796 char versionstringbuf[15];
797 UINT res;
798 DWORD buffer_size;
799 ULONG msiproduct;
800 BOOL ret=FALSE;
802 hmsi = GetModuleHandleA("msi");
804 lstrcpyW(msi_path, mono_path);
805 lstrcatW(msi_path, support_msi_relative);
807 pMsiOpenPackageW = (void*)GetProcAddress(hmsi, "MsiOpenPackageW");
809 res = pMsiOpenPackageW(msi_path, &msiproduct);
811 if (res == ERROR_SUCCESS)
813 buffer_size = sizeof(versionstringbuf);
815 pMsiGetProductPropertyA = (void*)GetProcAddress(hmsi, "MsiGetProductPropertyA");
817 res = pMsiGetProductPropertyA(msiproduct, "ProductVersion", versionstringbuf, &buffer_size);
819 pMsiCloseHandle = (void*)GetProcAddress(hmsi, "MsiCloseHandle");
821 pMsiCloseHandle(msiproduct);
824 if (res == ERROR_SUCCESS) {
825 TRACE("found support msi version %s at %s\n", versionstringbuf, debugstr_w(msi_path));
827 if (compare_versions(WINE_MONO_VERSION, versionstringbuf) <= 0)
829 ret = TRUE;
833 return ret;
836 static BOOL install_wine_mono(void)
838 BOOL is_wow64 = FALSE;
839 HMODULE hmsi = NULL;
840 HRESULT initresult = E_FAIL;
841 UINT (WINAPI *pMsiEnumRelatedProductsA)(LPCSTR,DWORD,DWORD,LPSTR);
842 UINT (WINAPI *pMsiGetProductInfoA)(LPCSTR,LPCSTR,LPSTR,DWORD*);
843 UINT (WINAPI *pMsiInstallProductW)(LPCWSTR,LPCWSTR);
844 char versionstringbuf[15];
845 char productcodebuf[39];
846 UINT res;
847 DWORD buffer_size;
848 BOOL ret;
849 WCHAR mono_path[MAX_PATH];
850 WCHAR support_msi_path[MAX_PATH];
852 static const char* mono_upgrade_code = "{DE624609-C6B5-486A-9274-EF0B854F6BC5}";
854 IsWow64Process(GetCurrentProcess(), &is_wow64);
856 if (is_wow64)
858 TRACE("not installing mono in wow64 process\n");
859 return TRUE;
862 TRACE("searching for mono runtime\n");
864 if (!get_mono_path(mono_path, FALSE))
866 TRACE("mono runtime not found\n");
867 return invoke_appwiz();
870 TRACE("mono runtime is at %s\n", debugstr_w(mono_path));
872 hmsi = LoadLibraryA("msi");
874 if (!hmsi)
876 ERR("couldn't load msi.dll\n");
877 return FALSE;
880 pMsiEnumRelatedProductsA = (void*)GetProcAddress(hmsi, "MsiEnumRelatedProductsA");
882 res = pMsiEnumRelatedProductsA(mono_upgrade_code, 0, 0, productcodebuf);
884 if (res == ERROR_SUCCESS)
886 pMsiGetProductInfoA = (void*)GetProcAddress(hmsi, "MsiGetProductInfoA");
888 buffer_size = sizeof(versionstringbuf);
890 res = pMsiGetProductInfoA(productcodebuf, "VersionString", versionstringbuf, &buffer_size);
892 else if (res != ERROR_NO_MORE_ITEMS)
894 ERR("MsiEnumRelatedProducts failed, err=%u\n", res);
897 if (res == ERROR_SUCCESS)
899 TRACE("found installed support package %s\n", versionstringbuf);
901 if (compare_versions(WINE_MONO_VERSION, versionstringbuf) <= 0)
903 TRACE("support package is at least %s, quitting\n", WINE_MONO_VERSION);
904 ret = TRUE;
905 goto end;
909 initresult = CoInitialize(NULL);
911 ret = get_support_msi(mono_path, support_msi_path);
912 if (!ret)
914 /* Try looking outside c:\windows\mono */
915 ret = (get_mono_path(mono_path, TRUE) &&
916 get_support_msi(mono_path, support_msi_path));
919 if (ret)
921 TRACE("installing support msi\n");
923 pMsiInstallProductW = (void*)GetProcAddress(hmsi, "MsiInstallProductW");
925 res = pMsiInstallProductW(support_msi_path, NULL);
927 if (res == ERROR_SUCCESS)
929 ret = TRUE;
930 goto end;
932 else
933 ERR("MsiInstallProduct failed, err=%i\n", res);
936 ret = invoke_appwiz();
938 end:
939 if (hmsi)
940 FreeLibrary(hmsi);
941 if (SUCCEEDED(initresult))
942 CoUninitialize();
943 return ret;
946 HRESULT WINAPI DllRegisterServer(void)
948 install_wine_mono();
950 return __wine_register_resources();
953 HRESULT WINAPI DllUnregisterServer(void)
955 return __wine_unregister_resources();
958 void WINAPI CoEEShutDownCOM(void)
960 FIXME("stub.\n");
963 INT WINAPI ND_RU1( const void *ptr, INT offset )
965 return *((const BYTE *)ptr + offset);
968 INT WINAPI ND_RI2( const void *ptr, INT offset )
970 return *(const SHORT *)((const BYTE *)ptr + offset);
973 INT WINAPI ND_RI4( const void *ptr, INT offset )
975 return *(const INT *)((const BYTE *)ptr + offset);
978 INT64 WINAPI ND_RI8( const void *ptr, INT offset )
980 return *(const INT64 *)((const BYTE *)ptr + offset);
983 void WINAPI ND_WU1( void *ptr, INT offset, BYTE val )
985 *((BYTE *)ptr + offset) = val;
988 void WINAPI ND_WI2( void *ptr, INT offset, SHORT val )
990 *(SHORT *)((BYTE *)ptr + offset) = val;
993 void WINAPI ND_WI4( void *ptr, INT offset, INT val )
995 *(INT *)((BYTE *)ptr + offset) = val;
998 void WINAPI ND_WI8( void *ptr, INT offset, INT64 val )
1000 *(INT64 *)((BYTE *)ptr + offset) = val;
1003 void WINAPI ND_CopyObjDst( const void *src, void *dst, INT offset, INT size )
1005 memcpy( (BYTE *)dst + offset, src, size );
1008 void WINAPI ND_CopyObjSrc( const void *src, INT offset, void *dst, INT size )
1010 memcpy( dst, (const BYTE *)src + offset, size );