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"
44 WINE_DEFAULT_DEBUG_CHANNEL( mscoree
);
48 DEFINE_GUID(IID__AppDomain
, 0x05f696dc,0x2b29,0x3663,0xad,0x8b,0xc4,0x38,0x9c,0xf2,0xa7,0x13);
56 static HANDLE dll_fixup_heap
; /* using a separate heap so we can have execute permission */
58 static struct list dll_fixups
;
65 void *thunk_code
; /* pointer into dll_fixup_heap */
68 void *tokens
; /* pointer into process heap */
71 static HRESULT
RuntimeHost_AddDomain(RuntimeHost
*This
, MonoDomain
**result
)
73 struct DomainEntry
*entry
;
77 EnterCriticalSection(&This
->lock
);
79 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(*entry
));
86 mscorlib_path
= WtoA(This
->version
->mscorlib_path
);
89 HeapFree(GetProcessHeap(), 0, entry
);
94 entry
->domain
= This
->mono
->mono_jit_init(mscorlib_path
);
96 HeapFree(GetProcessHeap(), 0, mscorlib_path
);
100 HeapFree(GetProcessHeap(), 0, entry
);
105 This
->mono
->is_started
= TRUE
;
107 list_add_tail(&This
->domains
, &entry
->entry
);
109 *result
= entry
->domain
;
112 LeaveCriticalSection(&This
->lock
);
117 static HRESULT
RuntimeHost_GetDefaultDomain(RuntimeHost
*This
, MonoDomain
**result
)
121 EnterCriticalSection(&This
->lock
);
123 if (This
->default_domain
) goto end
;
125 res
= RuntimeHost_AddDomain(This
, &This
->default_domain
);
128 *result
= This
->default_domain
;
130 LeaveCriticalSection(&This
->lock
);
135 static void RuntimeHost_DeleteDomain(RuntimeHost
*This
, MonoDomain
*domain
)
137 struct DomainEntry
*entry
;
139 EnterCriticalSection(&This
->lock
);
141 LIST_FOR_EACH_ENTRY(entry
, &This
->domains
, struct DomainEntry
, entry
)
143 if (entry
->domain
== domain
)
145 list_remove(&entry
->entry
);
146 if (This
->default_domain
== domain
)
147 This
->default_domain
= NULL
;
148 HeapFree(GetProcessHeap(), 0, entry
);
153 LeaveCriticalSection(&This
->lock
);
156 static HRESULT
RuntimeHost_GetIUnknownForDomain(RuntimeHost
*This
, MonoDomain
*domain
, IUnknown
**punk
)
160 MonoAssembly
*assembly
;
164 MonoObject
*appdomain_object
;
167 This
->mono
->mono_thread_attach(domain
);
169 assembly
= This
->mono
->mono_domain_assembly_open(domain
, "mscorlib");
172 ERR("Cannot load mscorlib\n");
176 image
= This
->mono
->mono_assembly_get_image(assembly
);
179 ERR("Couldn't get assembly image\n");
183 klass
= This
->mono
->mono_class_from_name(image
, "System", "AppDomain");
186 ERR("Couldn't get class from image\n");
190 method
= This
->mono
->mono_class_get_method_from_name(klass
, "get_CurrentDomain", 0);
193 ERR("Couldn't get method from class\n");
198 appdomain_object
= This
->mono
->mono_runtime_invoke(method
, NULL
, args
, NULL
);
199 if (!appdomain_object
)
201 ERR("Couldn't get result pointer\n");
205 hr
= RuntimeHost_GetIUnknownForObject(This
, appdomain_object
, &unk
);
209 hr
= IUnknown_QueryInterface(unk
, &IID__AppDomain
, (void**)punk
);
211 IUnknown_Release(unk
);
217 static inline RuntimeHost
*impl_from_ICLRRuntimeHost( ICLRRuntimeHost
*iface
)
219 return CONTAINING_RECORD(iface
, RuntimeHost
, ICLRRuntimeHost_iface
);
222 static inline RuntimeHost
*impl_from_ICorRuntimeHost( ICorRuntimeHost
*iface
)
224 return CONTAINING_RECORD(iface
, RuntimeHost
, ICorRuntimeHost_iface
);
227 /*** IUnknown methods ***/
228 static HRESULT WINAPI
corruntimehost_QueryInterface(ICorRuntimeHost
* iface
,
232 RuntimeHost
*This
= impl_from_ICorRuntimeHost( iface
);
233 TRACE("%p %s %p\n", This
, debugstr_guid(riid
), ppvObject
);
235 if ( IsEqualGUID( riid
, &IID_ICorRuntimeHost
) ||
236 IsEqualGUID( riid
, &IID_IUnknown
) )
242 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
243 return E_NOINTERFACE
;
246 ICorRuntimeHost_AddRef( iface
);
251 static ULONG WINAPI
corruntimehost_AddRef(ICorRuntimeHost
* iface
)
253 RuntimeHost
*This
= impl_from_ICorRuntimeHost( iface
);
255 return InterlockedIncrement( &This
->ref
);
258 static ULONG WINAPI
corruntimehost_Release(ICorRuntimeHost
* iface
)
260 RuntimeHost
*This
= impl_from_ICorRuntimeHost( iface
);
263 ref
= InterlockedDecrement( &This
->ref
);
268 /*** ICorRuntimeHost methods ***/
269 static HRESULT WINAPI
corruntimehost_CreateLogicalThreadState(
270 ICorRuntimeHost
* iface
)
272 FIXME("stub %p\n", iface
);
276 static HRESULT WINAPI
corruntimehost_DeleteLogicalThreadState(
277 ICorRuntimeHost
* iface
)
279 FIXME("stub %p\n", iface
);
283 static HRESULT WINAPI
corruntimehost_SwitchInLogicalThreadState(
284 ICorRuntimeHost
* iface
,
287 FIXME("stub %p\n", iface
);
291 static HRESULT WINAPI
corruntimehost_SwitchOutLogicalThreadState(
292 ICorRuntimeHost
* iface
,
295 FIXME("stub %p\n", iface
);
299 static HRESULT WINAPI
corruntimehost_LocksHeldByLogicalThread(
300 ICorRuntimeHost
* iface
,
303 FIXME("stub %p\n", iface
);
307 static HRESULT WINAPI
corruntimehost_MapFile(
308 ICorRuntimeHost
* iface
,
312 FIXME("stub %p\n", iface
);
316 static HRESULT WINAPI
corruntimehost_GetConfiguration(
317 ICorRuntimeHost
* iface
,
318 ICorConfiguration
**pConfiguration
)
320 FIXME("stub %p\n", iface
);
324 static HRESULT WINAPI
corruntimehost_Start(
325 ICorRuntimeHost
* iface
)
327 FIXME("stub %p\n", iface
);
331 static HRESULT WINAPI
corruntimehost_Stop(
332 ICorRuntimeHost
* iface
)
334 FIXME("stub %p\n", iface
);
338 static HRESULT WINAPI
corruntimehost_CreateDomain(
339 ICorRuntimeHost
* iface
,
340 LPCWSTR friendlyName
,
341 IUnknown
*identityArray
,
342 IUnknown
**appDomain
)
344 FIXME("stub %p\n", iface
);
348 static HRESULT WINAPI
corruntimehost_GetDefaultDomain(
349 ICorRuntimeHost
* iface
,
350 IUnknown
**pAppDomain
)
352 RuntimeHost
*This
= impl_from_ICorRuntimeHost( iface
);
356 TRACE("(%p)\n", iface
);
358 hr
= RuntimeHost_GetDefaultDomain(This
, &domain
);
362 hr
= RuntimeHost_GetIUnknownForDomain(This
, domain
, pAppDomain
);
368 static HRESULT WINAPI
corruntimehost_EnumDomains(
369 ICorRuntimeHost
* iface
,
372 FIXME("stub %p\n", iface
);
376 static HRESULT WINAPI
corruntimehost_NextDomain(
377 ICorRuntimeHost
* iface
,
379 IUnknown
**appDomain
)
381 FIXME("stub %p\n", iface
);
385 static HRESULT WINAPI
corruntimehost_CloseEnum(
386 ICorRuntimeHost
* iface
,
389 FIXME("stub %p\n", iface
);
393 static HRESULT WINAPI
corruntimehost_CreateDomainEx(
394 ICorRuntimeHost
* iface
,
395 LPCWSTR friendlyName
,
398 IUnknown
**appDomain
)
400 FIXME("stub %p\n", iface
);
404 static HRESULT WINAPI
corruntimehost_CreateDomainSetup(
405 ICorRuntimeHost
* iface
,
406 IUnknown
**appDomainSetup
)
408 FIXME("stub %p\n", iface
);
412 static HRESULT WINAPI
corruntimehost_CreateEvidence(
413 ICorRuntimeHost
* iface
,
416 FIXME("stub %p\n", iface
);
420 static HRESULT WINAPI
corruntimehost_UnloadDomain(
421 ICorRuntimeHost
* iface
,
424 FIXME("stub %p\n", iface
);
428 static HRESULT WINAPI
corruntimehost_CurrentDomain(
429 ICorRuntimeHost
* iface
,
430 IUnknown
**appDomain
)
432 FIXME("stub %p\n", iface
);
436 static const struct ICorRuntimeHostVtbl corruntimehost_vtbl
=
438 corruntimehost_QueryInterface
,
439 corruntimehost_AddRef
,
440 corruntimehost_Release
,
441 corruntimehost_CreateLogicalThreadState
,
442 corruntimehost_DeleteLogicalThreadState
,
443 corruntimehost_SwitchInLogicalThreadState
,
444 corruntimehost_SwitchOutLogicalThreadState
,
445 corruntimehost_LocksHeldByLogicalThread
,
446 corruntimehost_MapFile
,
447 corruntimehost_GetConfiguration
,
448 corruntimehost_Start
,
450 corruntimehost_CreateDomain
,
451 corruntimehost_GetDefaultDomain
,
452 corruntimehost_EnumDomains
,
453 corruntimehost_NextDomain
,
454 corruntimehost_CloseEnum
,
455 corruntimehost_CreateDomainEx
,
456 corruntimehost_CreateDomainSetup
,
457 corruntimehost_CreateEvidence
,
458 corruntimehost_UnloadDomain
,
459 corruntimehost_CurrentDomain
462 static HRESULT WINAPI
CLRRuntimeHost_QueryInterface(ICLRRuntimeHost
* iface
,
466 RuntimeHost
*This
= impl_from_ICLRRuntimeHost( iface
);
467 TRACE("%p %s %p\n", This
, debugstr_guid(riid
), ppvObject
);
469 if ( IsEqualGUID( riid
, &IID_ICLRRuntimeHost
) ||
470 IsEqualGUID( riid
, &IID_IUnknown
) )
476 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
477 return E_NOINTERFACE
;
480 ICLRRuntimeHost_AddRef( iface
);
485 static ULONG WINAPI
CLRRuntimeHost_AddRef(ICLRRuntimeHost
* iface
)
487 RuntimeHost
*This
= impl_from_ICLRRuntimeHost( iface
);
488 return ICorRuntimeHost_AddRef(&This
->ICorRuntimeHost_iface
);
491 static ULONG WINAPI
CLRRuntimeHost_Release(ICLRRuntimeHost
* iface
)
493 RuntimeHost
*This
= impl_from_ICLRRuntimeHost( iface
);
494 return ICorRuntimeHost_Release(&This
->ICorRuntimeHost_iface
);
497 static HRESULT WINAPI
CLRRuntimeHost_Start(ICLRRuntimeHost
* iface
)
499 FIXME("(%p)\n", iface
);
503 static HRESULT WINAPI
CLRRuntimeHost_Stop(ICLRRuntimeHost
* iface
)
505 FIXME("(%p)\n", iface
);
509 static HRESULT WINAPI
CLRRuntimeHost_SetHostControl(ICLRRuntimeHost
* iface
,
510 IHostControl
*pHostControl
)
512 FIXME("(%p,%p)\n", iface
, pHostControl
);
516 static HRESULT WINAPI
CLRRuntimeHost_GetCLRControl(ICLRRuntimeHost
* iface
,
517 ICLRControl
**pCLRControl
)
519 FIXME("(%p,%p)\n", iface
, pCLRControl
);
523 static HRESULT WINAPI
CLRRuntimeHost_UnloadAppDomain(ICLRRuntimeHost
* iface
,
524 DWORD dwAppDomainId
, BOOL fWaitUntilDone
)
526 FIXME("(%p,%u,%i)\n", iface
, dwAppDomainId
, fWaitUntilDone
);
530 static HRESULT WINAPI
CLRRuntimeHost_ExecuteInAppDomain(ICLRRuntimeHost
* iface
,
531 DWORD dwAppDomainId
, FExecuteInAppDomainCallback pCallback
, void *cookie
)
533 FIXME("(%p,%u,%p,%p)\n", iface
, dwAppDomainId
, pCallback
, cookie
);
537 static HRESULT WINAPI
CLRRuntimeHost_GetCurrentAppDomainId(ICLRRuntimeHost
* iface
,
538 DWORD
*pdwAppDomainId
)
540 FIXME("(%p,%p)\n", iface
, pdwAppDomainId
);
544 static HRESULT WINAPI
CLRRuntimeHost_ExecuteApplication(ICLRRuntimeHost
* iface
,
545 LPCWSTR pwzAppFullName
, DWORD dwManifestPaths
, LPCWSTR
*ppwzManifestPaths
,
546 DWORD dwActivationData
, LPCWSTR
*ppwzActivationData
, int *pReturnValue
)
548 FIXME("(%p,%s,%u,%u)\n", iface
, debugstr_w(pwzAppFullName
), dwManifestPaths
, dwActivationData
);
552 static HRESULT WINAPI
CLRRuntimeHost_ExecuteInDefaultAppDomain(ICLRRuntimeHost
* iface
,
553 LPCWSTR pwzAssemblyPath
, LPCWSTR pwzTypeName
, LPCWSTR pwzMethodName
,
554 LPCWSTR pwzArgument
, DWORD
*pReturnValue
)
556 RuntimeHost
*This
= impl_from_ICLRRuntimeHost( iface
);
559 MonoAssembly
*assembly
;
566 char *filenameA
= NULL
, *classA
= NULL
, *methodA
= NULL
;
567 char *argsA
= NULL
, *ns
;
569 TRACE("(%p,%s,%s,%s,%s)\n", iface
, debugstr_w(pwzAssemblyPath
),
570 debugstr_w(pwzTypeName
), debugstr_w(pwzMethodName
), debugstr_w(pwzArgument
));
572 hr
= RuntimeHost_GetDefaultDomain(This
, &domain
);
575 ERR("Couldn't get Default Domain\n");
581 This
->mono
->mono_thread_attach(domain
);
583 filenameA
= WtoA(pwzAssemblyPath
);
584 assembly
= This
->mono
->mono_domain_assembly_open(domain
, filenameA
);
587 ERR("Cannot open assembly %s\n", filenameA
);
591 image
= This
->mono
->mono_assembly_get_image(assembly
);
594 ERR("Couldn't get assembly image\n");
598 classA
= WtoA(pwzTypeName
);
599 ns
= strrchr(classA
, '.');
601 klass
= This
->mono
->mono_class_from_name(image
, classA
, ns
+1);
604 ERR("Couldn't get class from image\n");
608 methodA
= WtoA(pwzMethodName
);
609 method
= This
->mono
->mono_class_get_method_from_name(klass
, methodA
, 1);
612 ERR("Couldn't get method from class\n");
616 /* The .NET function we are calling has the following declaration
617 * public static int functionName(String param)
619 argsA
= WtoA(pwzArgument
);
620 str
= This
->mono
->mono_string_new(domain
, argsA
);
623 result
= This
->mono
->mono_runtime_invoke(method
, NULL
, args
, NULL
);
625 ERR("Couldn't get result pointer\n");
628 *pReturnValue
= *(DWORD
*)This
->mono
->mono_object_unbox(result
);
633 HeapFree(GetProcessHeap(), 0, filenameA
);
634 HeapFree(GetProcessHeap(), 0, classA
);
635 HeapFree(GetProcessHeap(), 0, argsA
);
636 HeapFree(GetProcessHeap(), 0, methodA
);
641 static const struct ICLRRuntimeHostVtbl CLRHostVtbl
=
643 CLRRuntimeHost_QueryInterface
,
644 CLRRuntimeHost_AddRef
,
645 CLRRuntimeHost_Release
,
646 CLRRuntimeHost_Start
,
648 CLRRuntimeHost_SetHostControl
,
649 CLRRuntimeHost_GetCLRControl
,
650 CLRRuntimeHost_UnloadAppDomain
,
651 CLRRuntimeHost_ExecuteInAppDomain
,
652 CLRRuntimeHost_GetCurrentAppDomainId
,
653 CLRRuntimeHost_ExecuteApplication
,
654 CLRRuntimeHost_ExecuteInDefaultAppDomain
657 /* Create an instance of a type given its name, by calling its constructor with
658 * no arguments. Note that result MUST be in the stack, or the garbage
659 * collector may free it prematurely. */
660 HRESULT
RuntimeHost_CreateManagedInstance(RuntimeHost
*This
, LPCWSTR name
,
661 MonoDomain
*domain
, MonoObject
**result
)
670 hr
= RuntimeHost_GetDefaultDomain(This
, &domain
);
681 This
->mono
->mono_thread_attach(domain
);
683 type
= This
->mono
->mono_reflection_type_from_name(nameA
, NULL
);
686 ERR("Cannot find type %s\n", debugstr_w(name
));
693 klass
= This
->mono
->mono_class_from_mono_type(type
);
696 ERR("Cannot convert type %s to a class\n", debugstr_w(name
));
703 obj
= This
->mono
->mono_object_new(domain
, klass
);
706 ERR("Cannot allocate object of type %s\n", debugstr_w(name
));
713 /* FIXME: Detect exceptions from the constructor? */
714 This
->mono
->mono_runtime_object_init(obj
);
718 HeapFree(GetProcessHeap(), 0, nameA
);
723 /* Get an IUnknown pointer for a Mono object.
725 * This is just a "light" wrapper around
726 * System.Runtime.InteropServices.Marshal:GetIUnknownForObject
728 * NOTE: The IUnknown* is created with a reference to the object.
729 * Until they have a reference, objects must be in the stack to prevent the
730 * garbage collector from freeing them.
732 * mono_thread_attach must have already been called for this thread. */
733 HRESULT
RuntimeHost_GetIUnknownForObject(RuntimeHost
*This
, MonoObject
*obj
,
737 MonoAssembly
*assembly
;
744 domain
= This
->mono
->mono_object_get_domain(obj
);
746 assembly
= This
->mono
->mono_domain_assembly_open(domain
, "mscorlib");
749 ERR("Cannot load mscorlib\n");
753 image
= This
->mono
->mono_assembly_get_image(assembly
);
756 ERR("Couldn't get assembly image\n");
760 klass
= This
->mono
->mono_class_from_name(image
, "System.Runtime.InteropServices", "Marshal");
763 ERR("Couldn't get class from image\n");
767 method
= This
->mono
->mono_class_get_method_from_name(klass
, "GetIUnknownForObject", 1);
770 ERR("Couldn't get method from class\n");
776 result
= This
->mono
->mono_runtime_invoke(method
, NULL
, args
, NULL
);
779 ERR("Couldn't get result pointer\n");
783 *ppUnk
= *(IUnknown
**)This
->mono
->mono_object_unbox(result
);
786 ERR("GetIUnknownForObject returned 0\n");
793 static void get_utf8_args(int *argc
, char ***argv
)
799 argvw
= CommandLineToArgvW(GetCommandLineW(), argc
);
801 for (i
=0; i
<*argc
; i
++)
803 size
+= sizeof(char*);
804 size
+= WideCharToMultiByte(CP_UTF8
, 0, argvw
[i
], -1, NULL
, 0, NULL
, NULL
);
806 size
+= sizeof(char*);
808 *argv
= HeapAlloc(GetProcessHeap(), 0, size
);
809 current_arg
= (char*)(*argv
+ *argc
+ 1);
811 for (i
=0; i
<*argc
; i
++)
813 (*argv
)[i
] = current_arg
;
814 current_arg
+= WideCharToMultiByte(CP_UTF8
, 0, argvw
[i
], -1, current_arg
, size
, NULL
, NULL
);
817 (*argv
)[*argc
] = NULL
;
819 HeapFree(GetProcessHeap(), 0, argvw
);
824 # define CAN_FIXUP_VTABLE 1
826 #include "pshpack1.h"
828 struct vtable_fixup_thunk
832 /* mov fixup,(%esp) */
834 struct dll_fixup
*fixup
;
835 /* mov function,%eax */
837 void (CDECL
*function
)(struct dll_fixup
*);
842 /* jmp *vtable_entry */
847 static const struct vtable_fixup_thunk thunk_template
= {
861 #else /* !defined(__i386__) */
863 # define CAN_FIXUP_VTABLE 0
865 struct vtable_fixup_thunk
867 struct dll_fixup
*fixup
;
868 void (CDECL
*function
)(struct dll_fixup
*fixup
);
872 static const struct vtable_fixup_thunk thunk_template
= {0};
876 static void CDECL
ReallyFixupVTable(struct dll_fixup
*fixup
)
879 WCHAR filename
[MAX_PATH
];
880 ICLRRuntimeInfo
*info
=NULL
;
883 MonoImage
*image
=NULL
;
884 MonoAssembly
*assembly
=NULL
;
885 MonoImageOpenStatus status
=0;
888 if (fixup
->done
) return;
890 /* It's possible we'll have two threads doing this at once. This is
891 * considered preferable to the potential deadlock if we use a mutex. */
893 GetModuleFileNameW(fixup
->dll
, filename
, MAX_PATH
);
895 TRACE("%p,%p,%s\n", fixup
, fixup
->dll
, debugstr_w(filename
));
897 filenameA
= WtoA(filename
);
902 hr
= get_runtime_info(filename
, NULL
, NULL
, 0, 0, FALSE
, &info
);
905 hr
= ICLRRuntimeInfo_GetRuntimeHost(info
, &host
);
908 hr
= RuntimeHost_GetDefaultDomain(host
, &domain
);
912 host
->mono
->mono_thread_attach(domain
);
914 image
= host
->mono
->mono_image_open_from_module_handle(fixup
->dll
,
915 filenameA
, 1, &status
);
919 assembly
= host
->mono
->mono_assembly_load_from(image
, filenameA
, &status
);
925 /* Mono needs an image that belongs to an assembly. */
926 image
= host
->mono
->mono_assembly_get_image(assembly
);
928 if (fixup
->fixup
->type
& COR_VTABLE_32BIT
)
930 DWORD
*vtable
= fixup
->vtable
;
931 DWORD
*tokens
= fixup
->tokens
;
932 for (i
=0; i
<fixup
->fixup
->count
; i
++)
934 TRACE("%x\n", tokens
[i
]);
935 vtable
[i
] = PtrToUint(host
->mono
->mono_marshal_get_vtfixup_ftnptr(
936 image
, tokens
[i
], fixup
->fixup
->type
));
944 ICLRRuntimeHost_Release(info
);
946 HeapFree(GetProcessHeap(), 0, filenameA
);
950 ERR("unable to fixup vtable, hr=%x, status=%d\n", hr
, status
);
951 /* If we returned now, we'd get an infinite loop. */
956 static void FixupVTableEntry(HMODULE hmodule
, VTableFixup
*vtable_fixup
)
958 /* We can't actually generate code for the functions without loading mono,
959 * and loading mono inside DllMain is a terrible idea. So we make thunks
960 * that call ReallyFixupVTable, which will load the runtime and fill in the
961 * vtable, then do an indirect jump using the (now filled in) vtable. Note
962 * that we have to keep the thunks around forever, as one of them may get
963 * called while we're filling in the table, and we can never be sure all
964 * threads are clear. */
965 struct dll_fixup
*fixup
;
967 fixup
= HeapAlloc(GetProcessHeap(), 0, sizeof(*fixup
));
969 fixup
->dll
= hmodule
;
970 fixup
->thunk_code
= HeapAlloc(dll_fixup_heap
, 0, sizeof(struct vtable_fixup_thunk
) * vtable_fixup
->count
);
971 fixup
->fixup
= vtable_fixup
;
972 fixup
->vtable
= (BYTE
*)hmodule
+ vtable_fixup
->rva
;
975 if (vtable_fixup
->type
& COR_VTABLE_32BIT
)
977 DWORD
*vtable
= fixup
->vtable
;
980 struct vtable_fixup_thunk
*thunks
= fixup
->thunk_code
;
982 if (sizeof(void*) > 4)
983 ERR("32-bit fixup in 64-bit mode; broken image?\n");
985 tokens
= fixup
->tokens
= HeapAlloc(GetProcessHeap(), 0, sizeof(*tokens
) * vtable_fixup
->count
);
986 memcpy(tokens
, vtable
, sizeof(*tokens
) * vtable_fixup
->count
);
987 for (i
=0; i
<vtable_fixup
->count
; i
++)
989 memcpy(&thunks
[i
], &thunk_template
, sizeof(thunk_template
));
990 thunks
[i
].fixup
= fixup
;
991 thunks
[i
].function
= ReallyFixupVTable
;
992 thunks
[i
].vtable_entry
= &vtable
[i
];
993 vtable
[i
] = PtrToUint(&thunks
[i
]);
998 ERR("unsupported vtable fixup flags %x\n", vtable_fixup
->type
);
999 HeapFree(dll_fixup_heap
, 0, fixup
->thunk_code
);
1000 HeapFree(GetProcessHeap(), 0, fixup
);
1004 list_add_tail(&dll_fixups
, &fixup
->entry
);
1007 static void FixupVTable(HMODULE hmodule
)
1011 VTableFixup
*vtable_fixups
;
1012 ULONG vtable_fixup_count
, i
;
1014 hr
= assembly_from_hmodule(&assembly
, hmodule
);
1017 hr
= assembly_get_vtable_fixups(assembly
, &vtable_fixups
, &vtable_fixup_count
);
1018 if (CAN_FIXUP_VTABLE
)
1019 for (i
=0; i
<vtable_fixup_count
; i
++)
1020 FixupVTableEntry(hmodule
, &vtable_fixups
[i
]);
1021 else if (vtable_fixup_count
)
1022 FIXME("cannot fixup vtable; expect a crash\n");
1024 assembly_release(assembly
);
1027 ERR("failed to read CLR headers, hr=%x\n", hr
);
1030 __int32 WINAPI
_CorExeMain(void)
1037 MonoImageOpenStatus status
;
1038 MonoAssembly
*assembly
=NULL
;
1039 WCHAR filename
[MAX_PATH
];
1041 ICLRRuntimeInfo
*info
;
1046 get_utf8_args(&argc
, &argv
);
1048 GetModuleFileNameW(NULL
, filename
, MAX_PATH
);
1050 TRACE("%s", debugstr_w(filename
));
1051 for (i
=0; i
<argc
; i
++)
1052 TRACE(" %s", debugstr_a(argv
[i
]));
1055 filenameA
= WtoA(filename
);
1059 FixupVTable(GetModuleHandleW(NULL
));
1061 hr
= get_runtime_info(filename
, NULL
, NULL
, 0, 0, FALSE
, &info
);
1065 hr
= ICLRRuntimeInfo_GetRuntimeHost(info
, &host
);
1068 hr
= RuntimeHost_GetDefaultDomain(host
, &domain
);
1072 image
= host
->mono
->mono_image_open_from_module_handle(GetModuleHandleW(NULL
),
1073 filenameA
, 1, &status
);
1076 assembly
= host
->mono
->mono_assembly_load_from(image
, filenameA
, &status
);
1080 exit_code
= host
->mono
->mono_jit_exec(domain
, assembly
, argc
, argv
);
1084 ERR("couldn't load %s, status=%d\n", debugstr_w(filename
), status
);
1088 RuntimeHost_DeleteDomain(host
, domain
);
1093 ICLRRuntimeInfo_Release(info
);
1098 HeapFree(GetProcessHeap(), 0, argv
);
1100 unload_all_runtimes();
1105 BOOL WINAPI
_CorDllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
1107 TRACE("(%p, %d, %p)\n", hinstDLL
, fdwReason
, lpvReserved
);
1111 case DLL_PROCESS_ATTACH
:
1112 DisableThreadLibraryCalls(hinstDLL
);
1113 FixupVTable(hinstDLL
);
1115 case DLL_PROCESS_DETACH
:
1116 /* FIXME: clean up the vtables */
1122 /* called from DLL_PROCESS_ATTACH */
1123 void runtimehost_init(void)
1125 dll_fixup_heap
= HeapCreate(HEAP_CREATE_ENABLE_EXECUTE
, 0, 0);
1126 list_init(&dll_fixups
);
1129 /* called from DLL_PROCESS_DETACH */
1130 void runtimehost_uninit(void)
1132 struct dll_fixup
*fixup
, *fixup2
;
1134 HeapDestroy(dll_fixup_heap
);
1135 LIST_FOR_EACH_ENTRY_SAFE(fixup
, fixup2
, &dll_fixups
, struct dll_fixup
, entry
)
1137 HeapFree(GetProcessHeap(), 0, fixup
->tokens
);
1138 HeapFree(GetProcessHeap(), 0, fixup
);
1142 HRESULT
RuntimeHost_Construct(const CLRRuntimeInfo
*runtime_version
,
1143 loaded_mono
*loaded_mono
, RuntimeHost
** result
)
1147 This
= HeapAlloc( GetProcessHeap(), 0, sizeof *This
);
1149 return E_OUTOFMEMORY
;
1151 This
->ICorRuntimeHost_iface
.lpVtbl
= &corruntimehost_vtbl
;
1152 This
->ICLRRuntimeHost_iface
.lpVtbl
= &CLRHostVtbl
;
1155 This
->version
= runtime_version
;
1156 This
->mono
= loaded_mono
;
1157 list_init(&This
->domains
);
1158 This
->default_domain
= NULL
;
1159 InitializeCriticalSection(&This
->lock
);
1160 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": RuntimeHost.lock");
1167 HRESULT
RuntimeHost_GetInterface(RuntimeHost
*This
, REFCLSID clsid
, REFIID riid
, void **ppv
)
1172 if (IsEqualGUID(clsid
, &CLSID_CorRuntimeHost
))
1174 unk
= (IUnknown
*)&This
->ICorRuntimeHost_iface
;
1175 IUnknown_AddRef(unk
);
1177 else if (IsEqualGUID(clsid
, &CLSID_CLRRuntimeHost
))
1179 unk
= (IUnknown
*)&This
->ICLRRuntimeHost_iface
;
1180 IUnknown_AddRef(unk
);
1182 else if (IsEqualGUID(clsid
, &CLSID_CorMetaDataDispenser
) ||
1183 IsEqualGUID(clsid
, &CLSID_CorMetaDataDispenserRuntime
))
1185 hr
= MetaDataDispenser_CreateInstance(&unk
);
1189 else if (IsEqualGUID(clsid
, &CLSID_CLRDebuggingLegacy
))
1191 hr
= CorDebug_Create(&This
->ICLRRuntimeHost_iface
, &unk
);
1200 hr
= IUnknown_QueryInterface(unk
, riid
, ppv
);
1202 IUnknown_Release(unk
);
1207 FIXME("not implemented for class %s\n", debugstr_guid(clsid
));
1209 return CLASS_E_CLASSNOTAVAILABLE
;
1212 HRESULT
RuntimeHost_Destroy(RuntimeHost
*This
)
1214 struct DomainEntry
*cursor
, *cursor2
;
1216 This
->lock
.DebugInfo
->Spare
[0] = 0;
1217 DeleteCriticalSection(&This
->lock
);
1219 LIST_FOR_EACH_ENTRY_SAFE(cursor
, cursor2
, &This
->domains
, struct DomainEntry
, entry
)
1221 list_remove(&cursor
->entry
);
1222 HeapFree(GetProcessHeap(), 0, cursor
);
1225 HeapFree( GetProcessHeap(), 0, This
);
1229 #define CHARS_IN_GUID 39
1230 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
1232 HRESULT
create_monodata(REFIID riid
, LPVOID
*ppObj
)
1234 static const WCHAR wszCodebase
[] = {'C','o','d','e','B','a','s','e',0};
1235 static const WCHAR wszClass
[] = {'C','l','a','s','s',0};
1236 static const WCHAR wszFileSlash
[] = {'f','i','l','e',':','/','/','/',0};
1237 static const WCHAR wszCLSIDSlash
[] = {'C','L','S','I','D','\\',0};
1238 static const WCHAR wszInprocServer32
[] = {'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2',0};
1239 WCHAR path
[CHARS_IN_GUID
+ ARRAYSIZE(wszCLSIDSlash
) + ARRAYSIZE(wszInprocServer32
) - 1];
1241 MonoAssembly
*assembly
;
1242 ICLRRuntimeInfo
*info
= NULL
;
1248 WCHAR codebase
[MAX_PATH
+ 8];
1249 WCHAR classname
[350];
1250 WCHAR filename
[MAX_PATH
];
1252 DWORD dwBufLen
= 350;
1254 lstrcpyW(path
, wszCLSIDSlash
);
1255 StringFromGUID2(riid
, path
+ lstrlenW(wszCLSIDSlash
), CHARS_IN_GUID
);
1256 lstrcatW(path
, wszInprocServer32
);
1258 TRACE("Registry key: %s\n", debugstr_w(path
));
1260 res
= RegOpenKeyExW(HKEY_CLASSES_ROOT
, path
, 0, KEY_READ
, &key
);
1261 if (res
== ERROR_FILE_NOT_FOUND
)
1262 return CLASS_E_CLASSNOTAVAILABLE
;
1264 res
= RegGetValueW( key
, NULL
, wszClass
, RRF_RT_REG_SZ
, NULL
, classname
, &dwBufLen
);
1265 if(res
!= ERROR_SUCCESS
)
1267 WARN("Class value cannot be found.\n");
1268 hr
= CLASS_E_CLASSNOTAVAILABLE
;
1272 TRACE("classname (%s)\n", debugstr_w(classname
));
1274 dwBufLen
= MAX_PATH
+ 8;
1275 res
= RegGetValueW( key
, NULL
, wszCodebase
, RRF_RT_REG_SZ
, NULL
, codebase
, &dwBufLen
);
1276 if(res
!= ERROR_SUCCESS
)
1278 WARN("CodeBase value cannot be found.\n");
1279 hr
= CLASS_E_CLASSNOTAVAILABLE
;
1283 /* Strip file:/// */
1284 if(strncmpW(codebase
, wszFileSlash
, strlenW(wszFileSlash
)) == 0)
1285 offset
= strlenW(wszFileSlash
);
1287 strcpyW(filename
, codebase
+ offset
);
1289 TRACE("codebase (%s)\n", debugstr_w(filename
));
1294 hr
= get_runtime_info(filename
, NULL
, NULL
, 0, 0, FALSE
, &info
);
1297 hr
= ICLRRuntimeInfo_GetRuntimeHost(info
, &host
);
1300 hr
= RuntimeHost_GetDefaultDomain(host
, &domain
);
1307 IUnknown
*unk
= NULL
;
1308 char *filenameA
, *ns
;
1311 hr
= CLASS_E_CLASSNOTAVAILABLE
;
1313 host
->mono
->mono_thread_attach(domain
);
1315 filenameA
= WtoA(filename
);
1316 assembly
= host
->mono
->mono_domain_assembly_open(domain
, filenameA
);
1317 HeapFree(GetProcessHeap(), 0, filenameA
);
1320 ERR("Cannot open assembly %s\n", filenameA
);
1324 image
= host
->mono
->mono_assembly_get_image(assembly
);
1327 ERR("Couldn't get assembly image\n");
1331 classA
= WtoA(classname
);
1332 ns
= strrchr(classA
, '.');
1335 klass
= host
->mono
->mono_class_from_name(image
, classA
, ns
+1);
1336 HeapFree(GetProcessHeap(), 0, classA
);
1339 ERR("Couldn't get class from image\n");
1344 * Use the default constructor for the .NET class.
1346 result
= host
->mono
->mono_object_new(domain
, klass
);
1347 host
->mono
->mono_runtime_object_init(result
);
1349 hr
= RuntimeHost_GetIUnknownForObject(host
, result
, &unk
);
1352 hr
= IUnknown_QueryInterface(unk
, &IID_IUnknown
, ppObj
);
1354 IUnknown_Release(unk
);
1357 hr
= CLASS_E_CLASSNOTAVAILABLE
;
1360 hr
= CLASS_E_CLASSNOTAVAILABLE
;
1363 hr
= CLASS_E_CLASSNOTAVAILABLE
;
1367 ICLRRuntimeInfo_Release(info
);