3 * Copyright 2008 Alistair Leslie-Hughes
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
38 #include "wine/list.h"
39 #include "mscoree_private.h"
41 #include "wine/debug.h"
42 #include "wine/unicode.h"
43 #include "wine/list.h"
45 WINE_DEFAULT_DEBUG_CHANNEL( mscoree
);
49 DEFINE_GUID(IID__AppDomain
, 0x05f696dc,0x2b29,0x3663,0xad,0x8b,0xc4,0x38,0x9c,0xf2,0xa7,0x13);
57 static HANDLE dll_fixup_heap
; /* using a separate heap so we can have execute permission */
59 static struct list dll_fixups
;
66 void *thunk_code
; /* pointer into dll_fixup_heap */
69 void *tokens
; /* pointer into process heap */
72 static HRESULT
RuntimeHost_AddDomain(RuntimeHost
*This
, MonoDomain
**result
)
74 struct DomainEntry
*entry
;
78 EnterCriticalSection(&This
->lock
);
80 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(*entry
));
87 mscorlib_path
= WtoA(This
->version
->mscorlib_path
);
90 HeapFree(GetProcessHeap(), 0, entry
);
95 entry
->domain
= This
->mono
->mono_jit_init(mscorlib_path
);
97 HeapFree(GetProcessHeap(), 0, mscorlib_path
);
101 HeapFree(GetProcessHeap(), 0, entry
);
106 This
->mono
->is_started
= TRUE
;
108 list_add_tail(&This
->domains
, &entry
->entry
);
110 *result
= entry
->domain
;
113 LeaveCriticalSection(&This
->lock
);
118 static HRESULT
RuntimeHost_GetDefaultDomain(RuntimeHost
*This
, MonoDomain
**result
)
122 EnterCriticalSection(&This
->lock
);
124 if (This
->default_domain
) goto end
;
126 res
= RuntimeHost_AddDomain(This
, &This
->default_domain
);
129 *result
= This
->default_domain
;
131 LeaveCriticalSection(&This
->lock
);
136 static void RuntimeHost_DeleteDomain(RuntimeHost
*This
, MonoDomain
*domain
)
138 struct DomainEntry
*entry
;
140 EnterCriticalSection(&This
->lock
);
142 LIST_FOR_EACH_ENTRY(entry
, &This
->domains
, struct DomainEntry
, entry
)
144 if (entry
->domain
== domain
)
146 list_remove(&entry
->entry
);
147 if (This
->default_domain
== domain
)
148 This
->default_domain
= NULL
;
149 HeapFree(GetProcessHeap(), 0, entry
);
154 LeaveCriticalSection(&This
->lock
);
157 static HRESULT
RuntimeHost_GetIUnknownForDomain(RuntimeHost
*This
, MonoDomain
*domain
, IUnknown
**punk
)
161 MonoAssembly
*assembly
;
165 MonoObject
*appdomain_object
;
168 This
->mono
->mono_thread_attach(domain
);
170 assembly
= This
->mono
->mono_domain_assembly_open(domain
, "mscorlib");
173 ERR("Cannot load mscorlib\n");
177 image
= This
->mono
->mono_assembly_get_image(assembly
);
180 ERR("Couldn't get assembly image\n");
184 klass
= This
->mono
->mono_class_from_name(image
, "System", "AppDomain");
187 ERR("Couldn't get class from image\n");
191 method
= This
->mono
->mono_class_get_method_from_name(klass
, "get_CurrentDomain", 0);
194 ERR("Couldn't get method from class\n");
199 appdomain_object
= This
->mono
->mono_runtime_invoke(method
, NULL
, args
, NULL
);
200 if (!appdomain_object
)
202 ERR("Couldn't get result pointer\n");
206 hr
= RuntimeHost_GetIUnknownForObject(This
, appdomain_object
, &unk
);
210 hr
= IUnknown_QueryInterface(unk
, &IID__AppDomain
, (void**)punk
);
212 IUnknown_Release(unk
);
218 static inline RuntimeHost
*impl_from_ICLRRuntimeHost( ICLRRuntimeHost
*iface
)
220 return CONTAINING_RECORD(iface
, RuntimeHost
, ICLRRuntimeHost_iface
);
223 static inline RuntimeHost
*impl_from_ICorRuntimeHost( ICorRuntimeHost
*iface
)
225 return CONTAINING_RECORD(iface
, RuntimeHost
, ICorRuntimeHost_iface
);
228 /*** IUnknown methods ***/
229 static HRESULT WINAPI
corruntimehost_QueryInterface(ICorRuntimeHost
* iface
,
233 RuntimeHost
*This
= impl_from_ICorRuntimeHost( iface
);
234 TRACE("%p %s %p\n", This
, debugstr_guid(riid
), ppvObject
);
236 if ( IsEqualGUID( riid
, &IID_ICorRuntimeHost
) ||
237 IsEqualGUID( riid
, &IID_IUnknown
) )
243 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
244 return E_NOINTERFACE
;
247 ICorRuntimeHost_AddRef( iface
);
252 static ULONG WINAPI
corruntimehost_AddRef(ICorRuntimeHost
* iface
)
254 RuntimeHost
*This
= impl_from_ICorRuntimeHost( iface
);
256 return InterlockedIncrement( &This
->ref
);
259 static ULONG WINAPI
corruntimehost_Release(ICorRuntimeHost
* iface
)
261 RuntimeHost
*This
= impl_from_ICorRuntimeHost( iface
);
264 ref
= InterlockedDecrement( &This
->ref
);
269 /*** ICorRuntimeHost methods ***/
270 static HRESULT WINAPI
corruntimehost_CreateLogicalThreadState(
271 ICorRuntimeHost
* iface
)
273 FIXME("stub %p\n", iface
);
277 static HRESULT WINAPI
corruntimehost_DeleteLogicalThreadState(
278 ICorRuntimeHost
* iface
)
280 FIXME("stub %p\n", iface
);
284 static HRESULT WINAPI
corruntimehost_SwitchInLogicalThreadState(
285 ICorRuntimeHost
* iface
,
288 FIXME("stub %p\n", iface
);
292 static HRESULT WINAPI
corruntimehost_SwitchOutLogicalThreadState(
293 ICorRuntimeHost
* iface
,
296 FIXME("stub %p\n", iface
);
300 static HRESULT WINAPI
corruntimehost_LocksHeldByLogicalThread(
301 ICorRuntimeHost
* iface
,
304 FIXME("stub %p\n", iface
);
308 static HRESULT WINAPI
corruntimehost_MapFile(
309 ICorRuntimeHost
* iface
,
313 FIXME("stub %p\n", iface
);
317 static HRESULT WINAPI
corruntimehost_GetConfiguration(
318 ICorRuntimeHost
* iface
,
319 ICorConfiguration
**pConfiguration
)
321 FIXME("stub %p\n", iface
);
325 static HRESULT WINAPI
corruntimehost_Start(
326 ICorRuntimeHost
* iface
)
328 FIXME("stub %p\n", iface
);
332 static HRESULT WINAPI
corruntimehost_Stop(
333 ICorRuntimeHost
* iface
)
335 FIXME("stub %p\n", iface
);
339 static HRESULT WINAPI
corruntimehost_CreateDomain(
340 ICorRuntimeHost
* iface
,
341 LPCWSTR friendlyName
,
342 IUnknown
*identityArray
,
343 IUnknown
**appDomain
)
345 FIXME("stub %p\n", iface
);
349 static HRESULT WINAPI
corruntimehost_GetDefaultDomain(
350 ICorRuntimeHost
* iface
,
351 IUnknown
**pAppDomain
)
353 RuntimeHost
*This
= impl_from_ICorRuntimeHost( iface
);
357 TRACE("(%p)\n", iface
);
359 hr
= RuntimeHost_GetDefaultDomain(This
, &domain
);
363 hr
= RuntimeHost_GetIUnknownForDomain(This
, domain
, pAppDomain
);
369 static HRESULT WINAPI
corruntimehost_EnumDomains(
370 ICorRuntimeHost
* iface
,
373 FIXME("stub %p\n", iface
);
377 static HRESULT WINAPI
corruntimehost_NextDomain(
378 ICorRuntimeHost
* iface
,
380 IUnknown
**appDomain
)
382 FIXME("stub %p\n", iface
);
386 static HRESULT WINAPI
corruntimehost_CloseEnum(
387 ICorRuntimeHost
* iface
,
390 FIXME("stub %p\n", iface
);
394 static HRESULT WINAPI
corruntimehost_CreateDomainEx(
395 ICorRuntimeHost
* iface
,
396 LPCWSTR friendlyName
,
399 IUnknown
**appDomain
)
401 FIXME("stub %p\n", iface
);
405 static HRESULT WINAPI
corruntimehost_CreateDomainSetup(
406 ICorRuntimeHost
* iface
,
407 IUnknown
**appDomainSetup
)
409 FIXME("stub %p\n", iface
);
413 static HRESULT WINAPI
corruntimehost_CreateEvidence(
414 ICorRuntimeHost
* iface
,
417 FIXME("stub %p\n", iface
);
421 static HRESULT WINAPI
corruntimehost_UnloadDomain(
422 ICorRuntimeHost
* iface
,
425 FIXME("stub %p\n", iface
);
429 static HRESULT WINAPI
corruntimehost_CurrentDomain(
430 ICorRuntimeHost
* iface
,
431 IUnknown
**appDomain
)
433 FIXME("stub %p\n", iface
);
437 static const struct ICorRuntimeHostVtbl corruntimehost_vtbl
=
439 corruntimehost_QueryInterface
,
440 corruntimehost_AddRef
,
441 corruntimehost_Release
,
442 corruntimehost_CreateLogicalThreadState
,
443 corruntimehost_DeleteLogicalThreadState
,
444 corruntimehost_SwitchInLogicalThreadState
,
445 corruntimehost_SwitchOutLogicalThreadState
,
446 corruntimehost_LocksHeldByLogicalThread
,
447 corruntimehost_MapFile
,
448 corruntimehost_GetConfiguration
,
449 corruntimehost_Start
,
451 corruntimehost_CreateDomain
,
452 corruntimehost_GetDefaultDomain
,
453 corruntimehost_EnumDomains
,
454 corruntimehost_NextDomain
,
455 corruntimehost_CloseEnum
,
456 corruntimehost_CreateDomainEx
,
457 corruntimehost_CreateDomainSetup
,
458 corruntimehost_CreateEvidence
,
459 corruntimehost_UnloadDomain
,
460 corruntimehost_CurrentDomain
463 static HRESULT WINAPI
CLRRuntimeHost_QueryInterface(ICLRRuntimeHost
* iface
,
467 RuntimeHost
*This
= impl_from_ICLRRuntimeHost( iface
);
468 TRACE("%p %s %p\n", This
, debugstr_guid(riid
), ppvObject
);
470 if ( IsEqualGUID( riid
, &IID_ICLRRuntimeHost
) ||
471 IsEqualGUID( riid
, &IID_IUnknown
) )
477 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
478 return E_NOINTERFACE
;
481 ICLRRuntimeHost_AddRef( iface
);
486 static ULONG WINAPI
CLRRuntimeHost_AddRef(ICLRRuntimeHost
* iface
)
488 RuntimeHost
*This
= impl_from_ICLRRuntimeHost( iface
);
489 return ICorRuntimeHost_AddRef(&This
->ICorRuntimeHost_iface
);
492 static ULONG WINAPI
CLRRuntimeHost_Release(ICLRRuntimeHost
* iface
)
494 RuntimeHost
*This
= impl_from_ICLRRuntimeHost( iface
);
495 return ICorRuntimeHost_Release(&This
->ICorRuntimeHost_iface
);
498 static HRESULT WINAPI
CLRRuntimeHost_Start(ICLRRuntimeHost
* iface
)
500 FIXME("(%p)\n", iface
);
504 static HRESULT WINAPI
CLRRuntimeHost_Stop(ICLRRuntimeHost
* iface
)
506 FIXME("(%p)\n", iface
);
510 static HRESULT WINAPI
CLRRuntimeHost_SetHostControl(ICLRRuntimeHost
* iface
,
511 IHostControl
*pHostControl
)
513 FIXME("(%p,%p)\n", iface
, pHostControl
);
517 static HRESULT WINAPI
CLRRuntimeHost_GetCLRControl(ICLRRuntimeHost
* iface
,
518 ICLRControl
**pCLRControl
)
520 FIXME("(%p,%p)\n", iface
, pCLRControl
);
524 static HRESULT WINAPI
CLRRuntimeHost_UnloadAppDomain(ICLRRuntimeHost
* iface
,
525 DWORD dwAppDomainId
, BOOL fWaitUntilDone
)
527 FIXME("(%p,%u,%i)\n", iface
, dwAppDomainId
, fWaitUntilDone
);
531 static HRESULT WINAPI
CLRRuntimeHost_ExecuteInAppDomain(ICLRRuntimeHost
* iface
,
532 DWORD dwAppDomainId
, FExecuteInAppDomainCallback pCallback
, void *cookie
)
534 FIXME("(%p,%u,%p,%p)\n", iface
, dwAppDomainId
, pCallback
, cookie
);
538 static HRESULT WINAPI
CLRRuntimeHost_GetCurrentAppDomainId(ICLRRuntimeHost
* iface
,
539 DWORD
*pdwAppDomainId
)
541 FIXME("(%p,%p)\n", iface
, pdwAppDomainId
);
545 static HRESULT WINAPI
CLRRuntimeHost_ExecuteApplication(ICLRRuntimeHost
* iface
,
546 LPCWSTR pwzAppFullName
, DWORD dwManifestPaths
, LPCWSTR
*ppwzManifestPaths
,
547 DWORD dwActivationData
, LPCWSTR
*ppwzActivationData
, int *pReturnValue
)
549 FIXME("(%p,%s,%u,%u)\n", iface
, debugstr_w(pwzAppFullName
), dwManifestPaths
, dwActivationData
);
553 static HRESULT WINAPI
CLRRuntimeHost_ExecuteInDefaultAppDomain(ICLRRuntimeHost
* iface
,
554 LPCWSTR pwzAssemblyPath
, LPCWSTR pwzTypeName
, LPCWSTR pwzMethodName
,
555 LPCWSTR pwzArgument
, DWORD
*pReturnValue
)
557 RuntimeHost
*This
= impl_from_ICLRRuntimeHost( iface
);
560 MonoAssembly
*assembly
;
567 char *filenameA
= NULL
, *classA
= NULL
, *methodA
= NULL
;
568 char *argsA
= NULL
, *ns
;
570 TRACE("(%p,%s,%s,%s,%s)\n", iface
, debugstr_w(pwzAssemblyPath
),
571 debugstr_w(pwzTypeName
), debugstr_w(pwzMethodName
), debugstr_w(pwzArgument
));
573 hr
= RuntimeHost_GetDefaultDomain(This
, &domain
);
576 ERR("Couldn't get Default Domain\n");
582 This
->mono
->mono_thread_attach(domain
);
584 filenameA
= WtoA(pwzAssemblyPath
);
585 assembly
= This
->mono
->mono_domain_assembly_open(domain
, filenameA
);
588 ERR("Cannot open assembly %s\n", filenameA
);
592 image
= This
->mono
->mono_assembly_get_image(assembly
);
595 ERR("Couldn't get assembly image\n");
599 classA
= WtoA(pwzTypeName
);
600 ns
= strrchr(classA
, '.');
602 klass
= This
->mono
->mono_class_from_name(image
, classA
, ns
+1);
605 ERR("Couldn't get class from image\n");
609 methodA
= WtoA(pwzMethodName
);
610 method
= This
->mono
->mono_class_get_method_from_name(klass
, methodA
, 1);
613 ERR("Couldn't get method from class\n");
617 /* The .NET function we are calling has the following declaration
618 * public static int functionName(String param)
620 argsA
= WtoA(pwzArgument
);
621 str
= This
->mono
->mono_string_new(domain
, argsA
);
624 result
= This
->mono
->mono_runtime_invoke(method
, NULL
, args
, NULL
);
626 ERR("Couldn't get result pointer\n");
629 *pReturnValue
= *(DWORD
*)This
->mono
->mono_object_unbox(result
);
634 HeapFree(GetProcessHeap(), 0, filenameA
);
635 HeapFree(GetProcessHeap(), 0, classA
);
636 HeapFree(GetProcessHeap(), 0, argsA
);
637 HeapFree(GetProcessHeap(), 0, methodA
);
642 static const struct ICLRRuntimeHostVtbl CLRHostVtbl
=
644 CLRRuntimeHost_QueryInterface
,
645 CLRRuntimeHost_AddRef
,
646 CLRRuntimeHost_Release
,
647 CLRRuntimeHost_Start
,
649 CLRRuntimeHost_SetHostControl
,
650 CLRRuntimeHost_GetCLRControl
,
651 CLRRuntimeHost_UnloadAppDomain
,
652 CLRRuntimeHost_ExecuteInAppDomain
,
653 CLRRuntimeHost_GetCurrentAppDomainId
,
654 CLRRuntimeHost_ExecuteApplication
,
655 CLRRuntimeHost_ExecuteInDefaultAppDomain
658 /* Create an instance of a type given its name, by calling its constructor with
659 * no arguments. Note that result MUST be in the stack, or the garbage
660 * collector may free it prematurely. */
661 HRESULT
RuntimeHost_CreateManagedInstance(RuntimeHost
*This
, LPCWSTR name
,
662 MonoDomain
*domain
, MonoObject
**result
)
671 hr
= RuntimeHost_GetDefaultDomain(This
, &domain
);
682 This
->mono
->mono_thread_attach(domain
);
684 type
= This
->mono
->mono_reflection_type_from_name(nameA
, NULL
);
687 ERR("Cannot find type %s\n", debugstr_w(name
));
694 klass
= This
->mono
->mono_class_from_mono_type(type
);
697 ERR("Cannot convert type %s to a class\n", debugstr_w(name
));
704 obj
= This
->mono
->mono_object_new(domain
, klass
);
707 ERR("Cannot allocate object of type %s\n", debugstr_w(name
));
714 /* FIXME: Detect exceptions from the constructor? */
715 This
->mono
->mono_runtime_object_init(obj
);
719 HeapFree(GetProcessHeap(), 0, nameA
);
724 /* Get an IUnknown pointer for a Mono object.
726 * This is just a "light" wrapper around
727 * System.Runtime.InteropServices.Marshal:GetIUnknownForObject
729 * NOTE: The IUnknown* is created with a reference to the object.
730 * Until they have a reference, objects must be in the stack to prevent the
731 * garbage collector from freeing them.
733 * mono_thread_attach must have already been called for this thread. */
734 HRESULT
RuntimeHost_GetIUnknownForObject(RuntimeHost
*This
, MonoObject
*obj
,
738 MonoAssembly
*assembly
;
745 domain
= This
->mono
->mono_object_get_domain(obj
);
747 assembly
= This
->mono
->mono_domain_assembly_open(domain
, "mscorlib");
750 ERR("Cannot load mscorlib\n");
754 image
= This
->mono
->mono_assembly_get_image(assembly
);
757 ERR("Couldn't get assembly image\n");
761 klass
= This
->mono
->mono_class_from_name(image
, "System.Runtime.InteropServices", "Marshal");
764 ERR("Couldn't get class from image\n");
768 method
= This
->mono
->mono_class_get_method_from_name(klass
, "GetIUnknownForObject", 1);
771 ERR("Couldn't get method from class\n");
777 result
= This
->mono
->mono_runtime_invoke(method
, NULL
, args
, NULL
);
780 ERR("Couldn't get result pointer\n");
784 *ppUnk
= *(IUnknown
**)This
->mono
->mono_object_unbox(result
);
787 ERR("GetIUnknownForObject returned 0\n");
794 static void get_utf8_args(int *argc
, char ***argv
)
800 argvw
= CommandLineToArgvW(GetCommandLineW(), argc
);
802 for (i
=0; i
<*argc
; i
++)
804 size
+= sizeof(char*);
805 size
+= WideCharToMultiByte(CP_UTF8
, 0, argvw
[i
], -1, NULL
, 0, NULL
, NULL
);
807 size
+= sizeof(char*);
809 *argv
= HeapAlloc(GetProcessHeap(), 0, size
);
810 current_arg
= (char*)(*argv
+ *argc
+ 1);
812 for (i
=0; i
<*argc
; i
++)
814 (*argv
)[i
] = current_arg
;
815 current_arg
+= WideCharToMultiByte(CP_UTF8
, 0, argvw
[i
], -1, current_arg
, size
, NULL
, NULL
);
818 (*argv
)[*argc
] = NULL
;
820 HeapFree(GetProcessHeap(), 0, argvw
);
825 # define CAN_FIXUP_VTABLE 1
827 #include "pshpack1.h"
829 struct vtable_fixup_thunk
833 /* mov fixup,(%esp) */
835 struct dll_fixup
*fixup
;
836 /* mov function,%eax */
838 void (CDECL
*function
)(struct dll_fixup
*);
843 /* jmp *vtable_entry */
848 static const struct vtable_fixup_thunk thunk_template
= {
862 #else /* !defined(__i386__) */
864 # define CAN_FIXUP_VTABLE 0
866 struct vtable_fixup_thunk
868 struct dll_fixup
*fixup
;
869 void (CDECL
*function
)(struct dll_fixup
*fixup
);
873 static const struct vtable_fixup_thunk thunk_template
= {0};
877 static void CDECL
ReallyFixupVTable(struct dll_fixup
*fixup
)
880 WCHAR filename
[MAX_PATH
];
881 ICLRRuntimeInfo
*info
=NULL
;
884 MonoImage
*image
=NULL
;
885 MonoAssembly
*assembly
=NULL
;
886 MonoImageOpenStatus status
=0;
889 if (fixup
->done
) return;
891 /* It's possible we'll have two threads doing this at once. This is
892 * considered preferable to the potential deadlock if we use a mutex. */
894 GetModuleFileNameW(fixup
->dll
, filename
, MAX_PATH
);
896 TRACE("%p,%p,%s\n", fixup
, fixup
->dll
, debugstr_w(filename
));
898 filenameA
= WtoA(filename
);
903 hr
= get_runtime_info(filename
, NULL
, NULL
, 0, 0, FALSE
, &info
);
906 hr
= ICLRRuntimeInfo_GetRuntimeHost(info
, &host
);
909 hr
= RuntimeHost_GetDefaultDomain(host
, &domain
);
913 host
->mono
->mono_thread_attach(domain
);
915 image
= host
->mono
->mono_image_open_from_module_handle(fixup
->dll
,
916 filenameA
, 1, &status
);
920 assembly
= host
->mono
->mono_assembly_load_from(image
, filenameA
, &status
);
926 /* Mono needs an image that belongs to an assembly. */
927 image
= host
->mono
->mono_assembly_get_image(assembly
);
929 if (fixup
->fixup
->type
& COR_VTABLE_32BIT
)
931 DWORD
*vtable
= fixup
->vtable
;
932 DWORD
*tokens
= fixup
->tokens
;
933 for (i
=0; i
<fixup
->fixup
->count
; i
++)
935 TRACE("%x\n", tokens
[i
]);
936 vtable
[i
] = PtrToUint(host
->mono
->mono_marshal_get_vtfixup_ftnptr(
937 image
, tokens
[i
], fixup
->fixup
->type
));
945 ICLRRuntimeHost_Release(info
);
947 HeapFree(GetProcessHeap(), 0, filenameA
);
951 ERR("unable to fixup vtable, hr=%x, status=%d\n", hr
, status
);
952 /* If we returned now, we'd get an infinite loop. */
957 static void FixupVTableEntry(HMODULE hmodule
, VTableFixup
*vtable_fixup
)
959 /* We can't actually generate code for the functions without loading mono,
960 * and loading mono inside DllMain is a terrible idea. So we make thunks
961 * that call ReallyFixupVTable, which will load the runtime and fill in the
962 * vtable, then do an indirect jump using the (now filled in) vtable. Note
963 * that we have to keep the thunks around forever, as one of them may get
964 * called while we're filling in the table, and we can never be sure all
965 * threads are clear. */
966 struct dll_fixup
*fixup
;
968 fixup
= HeapAlloc(GetProcessHeap(), 0, sizeof(*fixup
));
970 fixup
->dll
= hmodule
;
971 fixup
->thunk_code
= HeapAlloc(dll_fixup_heap
, 0, sizeof(struct vtable_fixup_thunk
) * vtable_fixup
->count
);
972 fixup
->fixup
= vtable_fixup
;
973 fixup
->vtable
= (BYTE
*)hmodule
+ vtable_fixup
->rva
;
976 if (vtable_fixup
->type
& COR_VTABLE_32BIT
)
978 DWORD
*vtable
= fixup
->vtable
;
981 struct vtable_fixup_thunk
*thunks
= fixup
->thunk_code
;
983 if (sizeof(void*) > 4)
984 ERR("32-bit fixup in 64-bit mode; broken image?\n");
986 tokens
= fixup
->tokens
= HeapAlloc(GetProcessHeap(), 0, sizeof(*tokens
) * vtable_fixup
->count
);
987 memcpy(tokens
, vtable
, sizeof(*tokens
) * vtable_fixup
->count
);
988 for (i
=0; i
<vtable_fixup
->count
; i
++)
990 memcpy(&thunks
[i
], &thunk_template
, sizeof(thunk_template
));
991 thunks
[i
].fixup
= fixup
;
992 thunks
[i
].function
= ReallyFixupVTable
;
993 thunks
[i
].vtable_entry
= &vtable
[i
];
994 vtable
[i
] = PtrToUint(&thunks
[i
]);
999 ERR("unsupported vtable fixup flags %x\n", vtable_fixup
->type
);
1000 HeapFree(dll_fixup_heap
, 0, fixup
->thunk_code
);
1001 HeapFree(GetProcessHeap(), 0, fixup
);
1005 list_add_tail(&dll_fixups
, &fixup
->entry
);
1008 static void FixupVTable(HMODULE hmodule
)
1012 VTableFixup
*vtable_fixups
;
1013 ULONG vtable_fixup_count
, i
;
1015 hr
= assembly_from_hmodule(&assembly
, hmodule
);
1018 hr
= assembly_get_vtable_fixups(assembly
, &vtable_fixups
, &vtable_fixup_count
);
1019 if (CAN_FIXUP_VTABLE
)
1020 for (i
=0; i
<vtable_fixup_count
; i
++)
1021 FixupVTableEntry(hmodule
, &vtable_fixups
[i
]);
1022 else if (vtable_fixup_count
)
1023 FIXME("cannot fixup vtable; expect a crash\n");
1025 assembly_release(assembly
);
1028 ERR("failed to read CLR headers, hr=%x\n", hr
);
1031 __int32 WINAPI
_CorExeMain(void)
1038 MonoImageOpenStatus status
;
1039 MonoAssembly
*assembly
=NULL
;
1040 WCHAR filename
[MAX_PATH
];
1042 ICLRRuntimeInfo
*info
;
1047 get_utf8_args(&argc
, &argv
);
1049 GetModuleFileNameW(NULL
, filename
, MAX_PATH
);
1051 TRACE("%s", debugstr_w(filename
));
1052 for (i
=0; i
<argc
; i
++)
1053 TRACE(" %s", debugstr_a(argv
[i
]));
1056 filenameA
= WtoA(filename
);
1060 FixupVTable(GetModuleHandleW(NULL
));
1062 hr
= get_runtime_info(filename
, NULL
, NULL
, 0, 0, FALSE
, &info
);
1066 hr
= ICLRRuntimeInfo_GetRuntimeHost(info
, &host
);
1069 hr
= RuntimeHost_GetDefaultDomain(host
, &domain
);
1073 image
= host
->mono
->mono_image_open_from_module_handle(GetModuleHandleW(NULL
),
1074 filenameA
, 1, &status
);
1077 assembly
= host
->mono
->mono_assembly_load_from(image
, filenameA
, &status
);
1081 exit_code
= host
->mono
->mono_jit_exec(domain
, assembly
, argc
, argv
);
1085 ERR("couldn't load %s, status=%d\n", debugstr_w(filename
), status
);
1089 RuntimeHost_DeleteDomain(host
, domain
);
1094 ICLRRuntimeInfo_Release(info
);
1099 HeapFree(GetProcessHeap(), 0, argv
);
1101 unload_all_runtimes();
1106 BOOL WINAPI
_CorDllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
1108 TRACE("(%p, %d, %p)\n", hinstDLL
, fdwReason
, lpvReserved
);
1112 case DLL_PROCESS_ATTACH
:
1113 DisableThreadLibraryCalls(hinstDLL
);
1114 FixupVTable(hinstDLL
);
1116 case DLL_PROCESS_DETACH
:
1117 /* FIXME: clean up the vtables */
1123 /* called from DLL_PROCESS_ATTACH */
1124 void runtimehost_init(void)
1126 dll_fixup_heap
= HeapCreate(HEAP_CREATE_ENABLE_EXECUTE
, 0, 0);
1127 list_init(&dll_fixups
);
1130 /* called from DLL_PROCESS_DETACH */
1131 void runtimehost_uninit(void)
1133 struct dll_fixup
*fixup
, *fixup2
;
1135 HeapDestroy(dll_fixup_heap
);
1136 LIST_FOR_EACH_ENTRY_SAFE(fixup
, fixup2
, &dll_fixups
, struct dll_fixup
, entry
)
1138 HeapFree(GetProcessHeap(), 0, fixup
->tokens
);
1139 HeapFree(GetProcessHeap(), 0, fixup
);
1143 HRESULT
RuntimeHost_Construct(const CLRRuntimeInfo
*runtime_version
,
1144 loaded_mono
*loaded_mono
, RuntimeHost
** result
)
1148 This
= HeapAlloc( GetProcessHeap(), 0, sizeof *This
);
1150 return E_OUTOFMEMORY
;
1152 This
->ICorRuntimeHost_iface
.lpVtbl
= &corruntimehost_vtbl
;
1153 This
->ICLRRuntimeHost_iface
.lpVtbl
= &CLRHostVtbl
;
1156 This
->version
= runtime_version
;
1157 This
->mono
= loaded_mono
;
1158 list_init(&This
->domains
);
1159 This
->default_domain
= NULL
;
1160 InitializeCriticalSection(&This
->lock
);
1161 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": RuntimeHost.lock");
1168 HRESULT
RuntimeHost_GetInterface(RuntimeHost
*This
, REFCLSID clsid
, REFIID riid
, void **ppv
)
1173 if (IsEqualGUID(clsid
, &CLSID_CorRuntimeHost
))
1175 unk
= (IUnknown
*)&This
->ICorRuntimeHost_iface
;
1176 IUnknown_AddRef(unk
);
1178 else if (IsEqualGUID(clsid
, &CLSID_CLRRuntimeHost
))
1180 unk
= (IUnknown
*)&This
->ICLRRuntimeHost_iface
;
1181 IUnknown_AddRef(unk
);
1183 else if (IsEqualGUID(clsid
, &CLSID_CorMetaDataDispenser
) ||
1184 IsEqualGUID(clsid
, &CLSID_CorMetaDataDispenserRuntime
))
1186 hr
= MetaDataDispenser_CreateInstance(&unk
);
1190 else if (IsEqualGUID(clsid
, &CLSID_CLRDebuggingLegacy
))
1192 hr
= CorDebug_Create(&This
->ICLRRuntimeHost_iface
, &unk
);
1201 hr
= IUnknown_QueryInterface(unk
, riid
, ppv
);
1203 IUnknown_Release(unk
);
1208 FIXME("not implemented for class %s\n", debugstr_guid(clsid
));
1210 return CLASS_E_CLASSNOTAVAILABLE
;
1213 HRESULT
RuntimeHost_Destroy(RuntimeHost
*This
)
1215 struct DomainEntry
*cursor
, *cursor2
;
1217 This
->lock
.DebugInfo
->Spare
[0] = 0;
1218 DeleteCriticalSection(&This
->lock
);
1220 LIST_FOR_EACH_ENTRY_SAFE(cursor
, cursor2
, &This
->domains
, struct DomainEntry
, entry
)
1222 list_remove(&cursor
->entry
);
1223 HeapFree(GetProcessHeap(), 0, cursor
);
1226 HeapFree( GetProcessHeap(), 0, This
);
1230 #define CHARS_IN_GUID 39
1231 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
1233 HRESULT
create_monodata(REFIID riid
, LPVOID
*ppObj
)
1235 static const WCHAR wszCodebase
[] = {'C','o','d','e','B','a','s','e',0};
1236 static const WCHAR wszClass
[] = {'C','l','a','s','s',0};
1237 static const WCHAR wszFileSlash
[] = {'f','i','l','e',':','/','/','/',0};
1238 static const WCHAR wszCLSIDSlash
[] = {'C','L','S','I','D','\\',0};
1239 static const WCHAR wszInprocServer32
[] = {'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2',0};
1240 WCHAR path
[CHARS_IN_GUID
+ ARRAYSIZE(wszCLSIDSlash
) + ARRAYSIZE(wszInprocServer32
) - 1];
1242 MonoAssembly
*assembly
;
1243 ICLRRuntimeInfo
*info
;
1249 WCHAR codebase
[MAX_PATH
+ 8];
1250 WCHAR classname
[350];
1251 WCHAR filename
[MAX_PATH
];
1253 DWORD dwBufLen
= 350;
1255 lstrcpyW(path
, wszCLSIDSlash
);
1256 StringFromGUID2(riid
, path
+ lstrlenW(wszCLSIDSlash
), CHARS_IN_GUID
);
1257 lstrcatW(path
, wszInprocServer32
);
1259 TRACE("Registry key: %s\n", debugstr_w(path
));
1261 res
= RegOpenKeyExW(HKEY_CLASSES_ROOT
, path
, 0, KEY_READ
, &key
);
1262 if (res
== ERROR_FILE_NOT_FOUND
)
1263 return CLASS_E_CLASSNOTAVAILABLE
;
1265 res
= RegGetValueW( key
, NULL
, wszClass
, RRF_RT_REG_SZ
, NULL
, classname
, &dwBufLen
);
1266 if(res
!= ERROR_SUCCESS
)
1268 WARN("Class value cannot be found.\n");
1269 hr
= CLASS_E_CLASSNOTAVAILABLE
;
1273 TRACE("classname (%s)\n", debugstr_w(classname
));
1275 dwBufLen
= MAX_PATH
+ 8;
1276 res
= RegGetValueW( key
, NULL
, wszCodebase
, RRF_RT_REG_SZ
, NULL
, codebase
, &dwBufLen
);
1277 if(res
!= ERROR_SUCCESS
)
1279 WARN("CodeBase value cannot be found.\n");
1280 hr
= CLASS_E_CLASSNOTAVAILABLE
;
1284 /* Strip file:/// */
1285 if(strncmpW(codebase
, wszFileSlash
, strlenW(wszFileSlash
)) == 0)
1286 offset
= strlenW(wszFileSlash
);
1288 strcpyW(filename
, codebase
+ offset
);
1290 TRACE("codebase (%s)\n", debugstr_w(filename
));
1295 hr
= get_runtime_info(filename
, NULL
, NULL
, 0, 0, FALSE
, &info
);
1298 hr
= ICLRRuntimeInfo_GetRuntimeHost(info
, &host
);
1301 hr
= RuntimeHost_GetDefaultDomain(host
, &domain
);
1308 IUnknown
*unk
= NULL
;
1309 char *filenameA
, *ns
;
1312 hr
= CLASS_E_CLASSNOTAVAILABLE
;
1314 host
->mono
->mono_thread_attach(domain
);
1316 filenameA
= WtoA(filename
);
1317 assembly
= host
->mono
->mono_domain_assembly_open(domain
, filenameA
);
1318 HeapFree(GetProcessHeap(), 0, filenameA
);
1321 ERR("Cannot open assembly %s\n", filenameA
);
1325 image
= host
->mono
->mono_assembly_get_image(assembly
);
1328 ERR("Couldn't get assembly image\n");
1332 classA
= WtoA(classname
);
1333 ns
= strrchr(classA
, '.');
1336 klass
= host
->mono
->mono_class_from_name(image
, classA
, ns
+1);
1337 HeapFree(GetProcessHeap(), 0, classA
);
1340 ERR("Couldn't get class from image\n");
1345 * Use the default constructor for the .NET class.
1347 result
= host
->mono
->mono_object_new(domain
, klass
);
1348 host
->mono
->mono_runtime_object_init(result
);
1350 hr
= RuntimeHost_GetIUnknownForObject(host
, result
, &unk
);
1353 hr
= IUnknown_QueryInterface(unk
, &IID_IUnknown
, ppObj
);
1355 IUnknown_Release(unk
);
1358 hr
= CLASS_E_CLASSNOTAVAILABLE
;
1361 hr
= CLASS_E_CLASSNOTAVAILABLE
;
1364 hr
= CLASS_E_CLASSNOTAVAILABLE
;
1368 ICLRRuntimeInfo_Release(info
);