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
20 #include "mscoree_private.h"
26 DEFINE_GUID(IID__AppDomain
, 0x05f696dc,0x2b29,0x3663,0xad,0x8b,0xc4,0x38,0x9c,0xf2,0xa7,0x13);
34 static HANDLE dll_fixup_heap
; /* using a separate heap so we can have execute permission */
36 static struct list dll_fixups
;
43 void *thunk_code
; /* pointer into dll_fixup_heap */
46 void *tokens
; /* pointer into process heap */
49 static HRESULT
RuntimeHost_AddDomain(RuntimeHost
*This
, MonoDomain
**result
)
51 struct DomainEntry
*entry
;
55 EnterCriticalSection(&This
->lock
);
57 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(*entry
));
64 mscorlib_path
= WtoA(This
->version
->mscorlib_path
);
67 HeapFree(GetProcessHeap(), 0, entry
);
72 entry
->domain
= This
->mono
->mono_jit_init(mscorlib_path
);
74 HeapFree(GetProcessHeap(), 0, mscorlib_path
);
78 HeapFree(GetProcessHeap(), 0, entry
);
83 This
->mono
->is_started
= TRUE
;
85 list_add_tail(&This
->domains
, &entry
->entry
);
87 *result
= entry
->domain
;
90 LeaveCriticalSection(&This
->lock
);
95 static HRESULT
RuntimeHost_GetDefaultDomain(RuntimeHost
*This
, MonoDomain
**result
)
99 EnterCriticalSection(&This
->lock
);
101 if (This
->default_domain
) goto end
;
103 res
= RuntimeHost_AddDomain(This
, &This
->default_domain
);
106 *result
= This
->default_domain
;
108 LeaveCriticalSection(&This
->lock
);
113 static void RuntimeHost_DeleteDomain(RuntimeHost
*This
, MonoDomain
*domain
)
115 struct DomainEntry
*entry
;
117 EnterCriticalSection(&This
->lock
);
119 LIST_FOR_EACH_ENTRY(entry
, &This
->domains
, struct DomainEntry
, entry
)
121 if (entry
->domain
== domain
)
123 list_remove(&entry
->entry
);
124 if (This
->default_domain
== domain
)
125 This
->default_domain
= NULL
;
126 HeapFree(GetProcessHeap(), 0, entry
);
131 LeaveCriticalSection(&This
->lock
);
134 static HRESULT
RuntimeHost_GetIUnknownForDomain(RuntimeHost
*This
, MonoDomain
*domain
, IUnknown
**punk
)
138 MonoAssembly
*assembly
;
142 MonoObject
*appdomain_object
;
145 This
->mono
->mono_thread_attach(domain
);
147 assembly
= This
->mono
->mono_domain_assembly_open(domain
, "mscorlib");
150 ERR("Cannot load mscorlib\n");
154 image
= This
->mono
->mono_assembly_get_image(assembly
);
157 ERR("Couldn't get assembly image\n");
161 klass
= This
->mono
->mono_class_from_name(image
, "System", "AppDomain");
164 ERR("Couldn't get class from image\n");
168 method
= This
->mono
->mono_class_get_method_from_name(klass
, "get_CurrentDomain", 0);
171 ERR("Couldn't get method from class\n");
176 appdomain_object
= This
->mono
->mono_runtime_invoke(method
, NULL
, args
, NULL
);
177 if (!appdomain_object
)
179 ERR("Couldn't get result pointer\n");
183 hr
= RuntimeHost_GetIUnknownForObject(This
, appdomain_object
, &unk
);
187 hr
= IUnknown_QueryInterface(unk
, &IID__AppDomain
, (void**)punk
);
189 IUnknown_Release(unk
);
195 static inline RuntimeHost
*impl_from_ICLRRuntimeHost( ICLRRuntimeHost
*iface
)
197 return CONTAINING_RECORD(iface
, RuntimeHost
, ICLRRuntimeHost_iface
);
200 static inline RuntimeHost
*impl_from_ICorRuntimeHost( ICorRuntimeHost
*iface
)
202 return CONTAINING_RECORD(iface
, RuntimeHost
, ICorRuntimeHost_iface
);
205 /*** IUnknown methods ***/
206 static HRESULT WINAPI
corruntimehost_QueryInterface(ICorRuntimeHost
* iface
,
210 RuntimeHost
*This
= impl_from_ICorRuntimeHost( iface
);
211 TRACE("%p %s %p\n", This
, debugstr_guid(riid
), ppvObject
);
213 if ( IsEqualGUID( riid
, &IID_ICorRuntimeHost
) ||
214 IsEqualGUID( riid
, &IID_IUnknown
) )
220 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
221 return E_NOINTERFACE
;
224 ICorRuntimeHost_AddRef( iface
);
229 static ULONG WINAPI
corruntimehost_AddRef(ICorRuntimeHost
* iface
)
231 RuntimeHost
*This
= impl_from_ICorRuntimeHost( iface
);
233 return InterlockedIncrement( &This
->ref
);
236 static ULONG WINAPI
corruntimehost_Release(ICorRuntimeHost
* iface
)
238 RuntimeHost
*This
= impl_from_ICorRuntimeHost( iface
);
241 ref
= InterlockedDecrement( &This
->ref
);
246 /*** ICorRuntimeHost methods ***/
247 static HRESULT WINAPI
corruntimehost_CreateLogicalThreadState(
248 ICorRuntimeHost
* iface
)
250 FIXME("stub %p\n", iface
);
254 static HRESULT WINAPI
corruntimehost_DeleteLogicalThreadState(
255 ICorRuntimeHost
* iface
)
257 FIXME("stub %p\n", iface
);
261 static HRESULT WINAPI
corruntimehost_SwitchInLogicalThreadState(
262 ICorRuntimeHost
* iface
,
265 FIXME("stub %p\n", iface
);
269 static HRESULT WINAPI
corruntimehost_SwitchOutLogicalThreadState(
270 ICorRuntimeHost
* iface
,
273 FIXME("stub %p\n", iface
);
277 static HRESULT WINAPI
corruntimehost_LocksHeldByLogicalThread(
278 ICorRuntimeHost
* iface
,
281 FIXME("stub %p\n", iface
);
285 static HRESULT WINAPI
corruntimehost_MapFile(
286 ICorRuntimeHost
* iface
,
290 FIXME("stub %p\n", iface
);
294 static HRESULT WINAPI
corruntimehost_GetConfiguration(
295 ICorRuntimeHost
* iface
,
296 ICorConfiguration
**pConfiguration
)
298 FIXME("stub %p\n", iface
);
302 static HRESULT WINAPI
corruntimehost_Start(
303 ICorRuntimeHost
* iface
)
305 FIXME("stub %p\n", iface
);
309 static HRESULT WINAPI
corruntimehost_Stop(
310 ICorRuntimeHost
* iface
)
312 FIXME("stub %p\n", iface
);
316 static HRESULT WINAPI
corruntimehost_CreateDomain(
317 ICorRuntimeHost
* iface
,
318 LPCWSTR friendlyName
,
319 IUnknown
*identityArray
,
320 IUnknown
**appDomain
)
322 FIXME("stub %p\n", iface
);
326 static HRESULT WINAPI
corruntimehost_GetDefaultDomain(
327 ICorRuntimeHost
* iface
,
328 IUnknown
**pAppDomain
)
330 RuntimeHost
*This
= impl_from_ICorRuntimeHost( iface
);
334 TRACE("(%p)\n", iface
);
336 hr
= RuntimeHost_GetDefaultDomain(This
, &domain
);
340 hr
= RuntimeHost_GetIUnknownForDomain(This
, domain
, pAppDomain
);
346 static HRESULT WINAPI
corruntimehost_EnumDomains(
347 ICorRuntimeHost
* iface
,
350 FIXME("stub %p\n", iface
);
354 static HRESULT WINAPI
corruntimehost_NextDomain(
355 ICorRuntimeHost
* iface
,
357 IUnknown
**appDomain
)
359 FIXME("stub %p\n", iface
);
363 static HRESULT WINAPI
corruntimehost_CloseEnum(
364 ICorRuntimeHost
* iface
,
367 FIXME("stub %p\n", iface
);
371 static HRESULT WINAPI
corruntimehost_CreateDomainEx(
372 ICorRuntimeHost
* iface
,
373 LPCWSTR friendlyName
,
376 IUnknown
**appDomain
)
378 FIXME("stub %p\n", iface
);
382 static HRESULT WINAPI
corruntimehost_CreateDomainSetup(
383 ICorRuntimeHost
* iface
,
384 IUnknown
**appDomainSetup
)
386 FIXME("stub %p\n", iface
);
390 static HRESULT WINAPI
corruntimehost_CreateEvidence(
391 ICorRuntimeHost
* iface
,
394 FIXME("stub %p\n", iface
);
398 static HRESULT WINAPI
corruntimehost_UnloadDomain(
399 ICorRuntimeHost
* iface
,
402 FIXME("stub %p\n", iface
);
406 static HRESULT WINAPI
corruntimehost_CurrentDomain(
407 ICorRuntimeHost
* iface
,
408 IUnknown
**appDomain
)
410 FIXME("stub %p\n", iface
);
414 static const struct ICorRuntimeHostVtbl corruntimehost_vtbl
=
416 corruntimehost_QueryInterface
,
417 corruntimehost_AddRef
,
418 corruntimehost_Release
,
419 corruntimehost_CreateLogicalThreadState
,
420 corruntimehost_DeleteLogicalThreadState
,
421 corruntimehost_SwitchInLogicalThreadState
,
422 corruntimehost_SwitchOutLogicalThreadState
,
423 corruntimehost_LocksHeldByLogicalThread
,
424 corruntimehost_MapFile
,
425 corruntimehost_GetConfiguration
,
426 corruntimehost_Start
,
428 corruntimehost_CreateDomain
,
429 corruntimehost_GetDefaultDomain
,
430 corruntimehost_EnumDomains
,
431 corruntimehost_NextDomain
,
432 corruntimehost_CloseEnum
,
433 corruntimehost_CreateDomainEx
,
434 corruntimehost_CreateDomainSetup
,
435 corruntimehost_CreateEvidence
,
436 corruntimehost_UnloadDomain
,
437 corruntimehost_CurrentDomain
440 static HRESULT WINAPI
CLRRuntimeHost_QueryInterface(ICLRRuntimeHost
* iface
,
444 RuntimeHost
*This
= impl_from_ICLRRuntimeHost( iface
);
445 TRACE("%p %s %p\n", This
, debugstr_guid(riid
), ppvObject
);
447 if ( IsEqualGUID( riid
, &IID_ICLRRuntimeHost
) ||
448 IsEqualGUID( riid
, &IID_IUnknown
) )
454 FIXME("Unsupported interface %s\n", debugstr_guid(riid
));
455 return E_NOINTERFACE
;
458 ICLRRuntimeHost_AddRef( iface
);
463 static ULONG WINAPI
CLRRuntimeHost_AddRef(ICLRRuntimeHost
* iface
)
465 RuntimeHost
*This
= impl_from_ICLRRuntimeHost( iface
);
466 return ICorRuntimeHost_AddRef(&This
->ICorRuntimeHost_iface
);
469 static ULONG WINAPI
CLRRuntimeHost_Release(ICLRRuntimeHost
* iface
)
471 RuntimeHost
*This
= impl_from_ICLRRuntimeHost( iface
);
472 return ICorRuntimeHost_Release(&This
->ICorRuntimeHost_iface
);
475 static HRESULT WINAPI
CLRRuntimeHost_Start(ICLRRuntimeHost
* iface
)
477 FIXME("(%p)\n", iface
);
481 static HRESULT WINAPI
CLRRuntimeHost_Stop(ICLRRuntimeHost
* iface
)
483 FIXME("(%p)\n", iface
);
487 static HRESULT WINAPI
CLRRuntimeHost_SetHostControl(ICLRRuntimeHost
* iface
,
488 IHostControl
*pHostControl
)
490 FIXME("(%p,%p)\n", iface
, pHostControl
);
494 static HRESULT WINAPI
CLRRuntimeHost_GetCLRControl(ICLRRuntimeHost
* iface
,
495 ICLRControl
**pCLRControl
)
497 FIXME("(%p,%p)\n", iface
, pCLRControl
);
501 static HRESULT WINAPI
CLRRuntimeHost_UnloadAppDomain(ICLRRuntimeHost
* iface
,
502 DWORD dwAppDomainId
, BOOL fWaitUntilDone
)
504 FIXME("(%p,%u,%i)\n", iface
, dwAppDomainId
, fWaitUntilDone
);
508 static HRESULT WINAPI
CLRRuntimeHost_ExecuteInAppDomain(ICLRRuntimeHost
* iface
,
509 DWORD dwAppDomainId
, FExecuteInAppDomainCallback pCallback
, void *cookie
)
511 FIXME("(%p,%u,%p,%p)\n", iface
, dwAppDomainId
, pCallback
, cookie
);
515 static HRESULT WINAPI
CLRRuntimeHost_GetCurrentAppDomainId(ICLRRuntimeHost
* iface
,
516 DWORD
*pdwAppDomainId
)
518 FIXME("(%p,%p)\n", iface
, pdwAppDomainId
);
522 static HRESULT WINAPI
CLRRuntimeHost_ExecuteApplication(ICLRRuntimeHost
* iface
,
523 LPCWSTR pwzAppFullName
, DWORD dwManifestPaths
, LPCWSTR
*ppwzManifestPaths
,
524 DWORD dwActivationData
, LPCWSTR
*ppwzActivationData
, int *pReturnValue
)
526 FIXME("(%p,%s,%u,%u)\n", iface
, debugstr_w(pwzAppFullName
), dwManifestPaths
, dwActivationData
);
530 static HRESULT WINAPI
CLRRuntimeHost_ExecuteInDefaultAppDomain(ICLRRuntimeHost
* iface
,
531 LPCWSTR pwzAssemblyPath
, LPCWSTR pwzTypeName
, LPCWSTR pwzMethodName
,
532 LPCWSTR pwzArgument
, DWORD
*pReturnValue
)
534 RuntimeHost
*This
= impl_from_ICLRRuntimeHost( iface
);
537 MonoAssembly
*assembly
;
544 char *filenameA
= NULL
, *classA
= NULL
, *methodA
= NULL
;
545 char *argsA
= NULL
, *ns
;
547 TRACE("(%p,%s,%s,%s,%s)\n", iface
, debugstr_w(pwzAssemblyPath
),
548 debugstr_w(pwzTypeName
), debugstr_w(pwzMethodName
), debugstr_w(pwzArgument
));
550 hr
= RuntimeHost_GetDefaultDomain(This
, &domain
);
553 ERR("Couldn't get Default Domain\n");
559 This
->mono
->mono_thread_attach(domain
);
561 filenameA
= WtoA(pwzAssemblyPath
);
562 assembly
= This
->mono
->mono_domain_assembly_open(domain
, filenameA
);
565 ERR("Cannot open assembly %s\n", filenameA
);
569 image
= This
->mono
->mono_assembly_get_image(assembly
);
572 ERR("Couldn't get assembly image\n");
576 classA
= WtoA(pwzTypeName
);
577 ns
= strrchr(classA
, '.');
579 klass
= This
->mono
->mono_class_from_name(image
, classA
, ns
+1);
582 ERR("Couldn't get class from image\n");
586 methodA
= WtoA(pwzMethodName
);
587 method
= This
->mono
->mono_class_get_method_from_name(klass
, methodA
, 1);
590 ERR("Couldn't get method from class\n");
594 /* The .NET function we are calling has the following declaration
595 * public static int functionName(String param)
597 argsA
= WtoA(pwzArgument
);
598 str
= This
->mono
->mono_string_new(domain
, argsA
);
601 result
= This
->mono
->mono_runtime_invoke(method
, NULL
, args
, NULL
);
603 ERR("Couldn't get result pointer\n");
606 *pReturnValue
= *(DWORD
*)This
->mono
->mono_object_unbox(result
);
611 HeapFree(GetProcessHeap(), 0, filenameA
);
612 HeapFree(GetProcessHeap(), 0, classA
);
613 HeapFree(GetProcessHeap(), 0, argsA
);
614 HeapFree(GetProcessHeap(), 0, methodA
);
619 static const struct ICLRRuntimeHostVtbl CLRHostVtbl
=
621 CLRRuntimeHost_QueryInterface
,
622 CLRRuntimeHost_AddRef
,
623 CLRRuntimeHost_Release
,
624 CLRRuntimeHost_Start
,
626 CLRRuntimeHost_SetHostControl
,
627 CLRRuntimeHost_GetCLRControl
,
628 CLRRuntimeHost_UnloadAppDomain
,
629 CLRRuntimeHost_ExecuteInAppDomain
,
630 CLRRuntimeHost_GetCurrentAppDomainId
,
631 CLRRuntimeHost_ExecuteApplication
,
632 CLRRuntimeHost_ExecuteInDefaultAppDomain
635 /* Create an instance of a type given its name, by calling its constructor with
636 * no arguments. Note that result MUST be in the stack, or the garbage
637 * collector may free it prematurely. */
638 HRESULT
RuntimeHost_CreateManagedInstance(RuntimeHost
*This
, LPCWSTR name
,
639 MonoDomain
*domain
, MonoObject
**result
)
648 hr
= RuntimeHost_GetDefaultDomain(This
, &domain
);
659 This
->mono
->mono_thread_attach(domain
);
661 type
= This
->mono
->mono_reflection_type_from_name(nameA
, NULL
);
664 ERR("Cannot find type %s\n", debugstr_w(name
));
671 klass
= This
->mono
->mono_class_from_mono_type(type
);
674 ERR("Cannot convert type %s to a class\n", debugstr_w(name
));
681 obj
= This
->mono
->mono_object_new(domain
, klass
);
684 ERR("Cannot allocate object of type %s\n", debugstr_w(name
));
691 /* FIXME: Detect exceptions from the constructor? */
692 This
->mono
->mono_runtime_object_init(obj
);
696 HeapFree(GetProcessHeap(), 0, nameA
);
701 /* Get an IUnknown pointer for a Mono object.
703 * This is just a "light" wrapper around
704 * System.Runtime.InteropServices.Marshal:GetIUnknownForObject
706 * NOTE: The IUnknown* is created with a reference to the object.
707 * Until they have a reference, objects must be in the stack to prevent the
708 * garbage collector from freeing them.
710 * mono_thread_attach must have already been called for this thread. */
711 HRESULT
RuntimeHost_GetIUnknownForObject(RuntimeHost
*This
, MonoObject
*obj
,
715 MonoAssembly
*assembly
;
722 domain
= This
->mono
->mono_object_get_domain(obj
);
724 assembly
= This
->mono
->mono_domain_assembly_open(domain
, "mscorlib");
727 ERR("Cannot load mscorlib\n");
731 image
= This
->mono
->mono_assembly_get_image(assembly
);
734 ERR("Couldn't get assembly image\n");
738 klass
= This
->mono
->mono_class_from_name(image
, "System.Runtime.InteropServices", "Marshal");
741 ERR("Couldn't get class from image\n");
745 method
= This
->mono
->mono_class_get_method_from_name(klass
, "GetIUnknownForObject", 1);
748 ERR("Couldn't get method from class\n");
754 result
= This
->mono
->mono_runtime_invoke(method
, NULL
, args
, NULL
);
757 ERR("Couldn't get result pointer\n");
761 *ppUnk
= *(IUnknown
**)This
->mono
->mono_object_unbox(result
);
764 ERR("GetIUnknownForObject returned 0\n");
771 static void get_utf8_args(int *argc
, char ***argv
)
777 argvw
= CommandLineToArgvW(GetCommandLineW(), argc
);
779 for (i
=0; i
<*argc
; i
++)
781 size
+= sizeof(char*);
782 size
+= WideCharToMultiByte(CP_UTF8
, 0, argvw
[i
], -1, NULL
, 0, NULL
, NULL
);
784 size
+= sizeof(char*);
786 *argv
= HeapAlloc(GetProcessHeap(), 0, size
);
787 current_arg
= (char*)(*argv
+ *argc
+ 1);
789 for (i
=0; i
<*argc
; i
++)
791 (*argv
)[i
] = current_arg
;
792 current_arg
+= WideCharToMultiByte(CP_UTF8
, 0, argvw
[i
], -1, current_arg
, size
, NULL
, NULL
);
795 (*argv
)[*argc
] = NULL
;
797 HeapFree(GetProcessHeap(), 0, argvw
);
802 # define CAN_FIXUP_VTABLE 1
804 #include "pshpack1.h"
806 struct vtable_fixup_thunk
810 /* mov fixup,(%esp) */
812 struct dll_fixup
*fixup
;
813 /* mov function,%eax */
815 void (CDECL
*function
)(struct dll_fixup
*);
820 /* jmp *vtable_entry */
825 static const struct vtable_fixup_thunk thunk_template
= {
839 #else /* !defined(__i386__) */
841 # define CAN_FIXUP_VTABLE 0
843 struct vtable_fixup_thunk
845 struct dll_fixup
*fixup
;
846 void (CDECL
*function
)(struct dll_fixup
*fixup
);
850 static const struct vtable_fixup_thunk thunk_template
= {0};
854 static void CDECL
ReallyFixupVTable(struct dll_fixup
*fixup
)
857 WCHAR filename
[MAX_PATH
];
858 ICLRRuntimeInfo
*info
=NULL
;
861 MonoImage
*image
=NULL
;
862 MonoAssembly
*assembly
=NULL
;
863 MonoImageOpenStatus status
=0;
866 if (fixup
->done
) return;
868 /* It's possible we'll have two threads doing this at once. This is
869 * considered preferable to the potential deadlock if we use a mutex. */
871 GetModuleFileNameW(fixup
->dll
, filename
, MAX_PATH
);
873 TRACE("%p,%p,%s\n", fixup
, fixup
->dll
, debugstr_w(filename
));
875 filenameA
= WtoA(filename
);
880 hr
= get_runtime_info(filename
, NULL
, NULL
, 0, 0, FALSE
, &info
);
883 hr
= ICLRRuntimeInfo_GetRuntimeHost(info
, &host
);
886 hr
= RuntimeHost_GetDefaultDomain(host
, &domain
);
890 host
->mono
->mono_thread_attach(domain
);
892 image
= host
->mono
->mono_image_open_from_module_handle(fixup
->dll
,
893 filenameA
, 1, &status
);
897 assembly
= host
->mono
->mono_assembly_load_from(image
, filenameA
, &status
);
903 /* Mono needs an image that belongs to an assembly. */
904 image
= host
->mono
->mono_assembly_get_image(assembly
);
906 if (fixup
->fixup
->type
& COR_VTABLE_32BIT
)
908 DWORD
*vtable
= fixup
->vtable
;
909 DWORD
*tokens
= fixup
->tokens
;
910 for (i
=0; i
<fixup
->fixup
->count
; i
++)
912 TRACE("%x\n", tokens
[i
]);
913 vtable
[i
] = PtrToUint(host
->mono
->mono_marshal_get_vtfixup_ftnptr(
914 image
, tokens
[i
], fixup
->fixup
->type
));
922 ICLRRuntimeInfo_Release(info
);
924 HeapFree(GetProcessHeap(), 0, filenameA
);
928 ERR("unable to fixup vtable, hr=%x, status=%d\n", hr
, status
);
929 /* If we returned now, we'd get an infinite loop. */
934 static void FixupVTableEntry(HMODULE hmodule
, VTableFixup
*vtable_fixup
)
936 /* We can't actually generate code for the functions without loading mono,
937 * and loading mono inside DllMain is a terrible idea. So we make thunks
938 * that call ReallyFixupVTable, which will load the runtime and fill in the
939 * vtable, then do an indirect jump using the (now filled in) vtable. Note
940 * that we have to keep the thunks around forever, as one of them may get
941 * called while we're filling in the table, and we can never be sure all
942 * threads are clear. */
943 struct dll_fixup
*fixup
;
945 fixup
= HeapAlloc(GetProcessHeap(), 0, sizeof(*fixup
));
947 fixup
->dll
= hmodule
;
948 fixup
->thunk_code
= HeapAlloc(dll_fixup_heap
, 0, sizeof(struct vtable_fixup_thunk
) * vtable_fixup
->count
);
949 fixup
->fixup
= vtable_fixup
;
950 fixup
->vtable
= (BYTE
*)hmodule
+ vtable_fixup
->rva
;
953 if (vtable_fixup
->type
& COR_VTABLE_32BIT
)
955 DWORD
*vtable
= fixup
->vtable
;
958 struct vtable_fixup_thunk
*thunks
= fixup
->thunk_code
;
960 if (sizeof(void*) > 4)
961 ERR("32-bit fixup in 64-bit mode; broken image?\n");
963 tokens
= fixup
->tokens
= HeapAlloc(GetProcessHeap(), 0, sizeof(*tokens
) * vtable_fixup
->count
);
964 memcpy(tokens
, vtable
, sizeof(*tokens
) * vtable_fixup
->count
);
965 for (i
=0; i
<vtable_fixup
->count
; i
++)
967 memcpy(&thunks
[i
], &thunk_template
, sizeof(thunk_template
));
968 thunks
[i
].fixup
= fixup
;
969 thunks
[i
].function
= ReallyFixupVTable
;
970 thunks
[i
].vtable_entry
= &vtable
[i
];
971 vtable
[i
] = PtrToUint(&thunks
[i
]);
976 ERR("unsupported vtable fixup flags %x\n", vtable_fixup
->type
);
977 HeapFree(dll_fixup_heap
, 0, fixup
->thunk_code
);
978 HeapFree(GetProcessHeap(), 0, fixup
);
982 list_add_tail(&dll_fixups
, &fixup
->entry
);
985 static void FixupVTable(HMODULE hmodule
)
989 VTableFixup
*vtable_fixups
;
990 ULONG vtable_fixup_count
, i
;
992 hr
= assembly_from_hmodule(&assembly
, hmodule
);
995 hr
= assembly_get_vtable_fixups(assembly
, &vtable_fixups
, &vtable_fixup_count
);
996 if (CAN_FIXUP_VTABLE
)
997 for (i
=0; i
<vtable_fixup_count
; i
++)
998 FixupVTableEntry(hmodule
, &vtable_fixups
[i
]);
999 else if (vtable_fixup_count
)
1000 FIXME("cannot fixup vtable; expect a crash\n");
1002 assembly_release(assembly
);
1005 ERR("failed to read CLR headers, hr=%x\n", hr
);
1008 __int32 WINAPI
_CorExeMain(void)
1015 MonoImageOpenStatus status
;
1016 MonoAssembly
*assembly
=NULL
;
1017 WCHAR filename
[MAX_PATH
];
1019 ICLRRuntimeInfo
*info
;
1024 get_utf8_args(&argc
, &argv
);
1026 GetModuleFileNameW(NULL
, filename
, MAX_PATH
);
1028 TRACE("%s", debugstr_w(filename
));
1029 for (i
=0; i
<argc
; i
++)
1030 TRACE(" %s", debugstr_a(argv
[i
]));
1033 filenameA
= WtoA(filename
);
1037 FixupVTable(GetModuleHandleW(NULL
));
1039 hr
= get_runtime_info(filename
, NULL
, NULL
, 0, 0, FALSE
, &info
);
1043 hr
= ICLRRuntimeInfo_GetRuntimeHost(info
, &host
);
1046 hr
= RuntimeHost_GetDefaultDomain(host
, &domain
);
1050 image
= host
->mono
->mono_image_open_from_module_handle(GetModuleHandleW(NULL
),
1051 filenameA
, 1, &status
);
1054 assembly
= host
->mono
->mono_assembly_load_from(image
, filenameA
, &status
);
1058 exit_code
= host
->mono
->mono_jit_exec(domain
, assembly
, argc
, argv
);
1062 ERR("couldn't load %s, status=%d\n", debugstr_w(filename
), status
);
1066 RuntimeHost_DeleteDomain(host
, domain
);
1071 ICLRRuntimeInfo_Release(info
);
1076 HeapFree(GetProcessHeap(), 0, argv
);
1078 unload_all_runtimes();
1083 BOOL WINAPI
_CorDllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
1085 TRACE("(%p, %d, %p)\n", hinstDLL
, fdwReason
, lpvReserved
);
1089 case DLL_PROCESS_ATTACH
:
1090 DisableThreadLibraryCalls(hinstDLL
);
1091 FixupVTable(hinstDLL
);
1093 case DLL_PROCESS_DETACH
:
1094 /* FIXME: clean up the vtables */
1100 /* called from DLL_PROCESS_ATTACH */
1101 void runtimehost_init(void)
1103 dll_fixup_heap
= HeapCreate(HEAP_CREATE_ENABLE_EXECUTE
, 0, 0);
1104 list_init(&dll_fixups
);
1107 /* called from DLL_PROCESS_DETACH */
1108 void runtimehost_uninit(void)
1110 struct dll_fixup
*fixup
, *fixup2
;
1112 HeapDestroy(dll_fixup_heap
);
1113 LIST_FOR_EACH_ENTRY_SAFE(fixup
, fixup2
, &dll_fixups
, struct dll_fixup
, entry
)
1115 HeapFree(GetProcessHeap(), 0, fixup
->tokens
);
1116 HeapFree(GetProcessHeap(), 0, fixup
);
1120 HRESULT
RuntimeHost_Construct(const CLRRuntimeInfo
*runtime_version
,
1121 loaded_mono
*loaded_mono
, RuntimeHost
** result
)
1125 This
= HeapAlloc( GetProcessHeap(), 0, sizeof *This
);
1127 return E_OUTOFMEMORY
;
1129 This
->ICorRuntimeHost_iface
.lpVtbl
= &corruntimehost_vtbl
;
1130 This
->ICLRRuntimeHost_iface
.lpVtbl
= &CLRHostVtbl
;
1133 This
->version
= runtime_version
;
1134 This
->mono
= loaded_mono
;
1135 list_init(&This
->domains
);
1136 This
->default_domain
= NULL
;
1137 InitializeCriticalSection(&This
->lock
);
1138 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": RuntimeHost.lock");
1145 HRESULT
RuntimeHost_GetInterface(RuntimeHost
*This
, REFCLSID clsid
, REFIID riid
, void **ppv
)
1150 if (IsEqualGUID(clsid
, &CLSID_CorRuntimeHost
))
1152 unk
= (IUnknown
*)&This
->ICorRuntimeHost_iface
;
1153 IUnknown_AddRef(unk
);
1155 else if (IsEqualGUID(clsid
, &CLSID_CLRRuntimeHost
))
1157 unk
= (IUnknown
*)&This
->ICLRRuntimeHost_iface
;
1158 IUnknown_AddRef(unk
);
1160 else if (IsEqualGUID(clsid
, &CLSID_CorMetaDataDispenser
) ||
1161 IsEqualGUID(clsid
, &CLSID_CorMetaDataDispenserRuntime
))
1163 hr
= MetaDataDispenser_CreateInstance(&unk
);
1167 else if (IsEqualGUID(clsid
, &CLSID_CLRDebuggingLegacy
))
1169 hr
= CorDebug_Create(&This
->ICLRRuntimeHost_iface
, &unk
);
1178 hr
= IUnknown_QueryInterface(unk
, riid
, ppv
);
1180 IUnknown_Release(unk
);
1185 FIXME("not implemented for class %s\n", debugstr_guid(clsid
));
1187 return CLASS_E_CLASSNOTAVAILABLE
;
1190 HRESULT
RuntimeHost_Destroy(RuntimeHost
*This
)
1192 struct DomainEntry
*cursor
, *cursor2
;
1194 This
->lock
.DebugInfo
->Spare
[0] = 0;
1195 DeleteCriticalSection(&This
->lock
);
1197 LIST_FOR_EACH_ENTRY_SAFE(cursor
, cursor2
, &This
->domains
, struct DomainEntry
, entry
)
1199 list_remove(&cursor
->entry
);
1200 HeapFree(GetProcessHeap(), 0, cursor
);
1203 HeapFree( GetProcessHeap(), 0, This
);
1207 #define CHARS_IN_GUID 39
1209 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
1211 HRESULT
create_monodata(REFIID riid
, LPVOID
*ppObj
)
1213 static const WCHAR wszCodebase
[] = {'C','o','d','e','B','a','s','e',0};
1214 static const WCHAR wszClass
[] = {'C','l','a','s','s',0};
1215 static const WCHAR wszFileSlash
[] = {'f','i','l','e',':','/','/','/',0};
1216 static const WCHAR wszCLSIDSlash
[] = {'C','L','S','I','D','\\',0};
1217 static const WCHAR wszInprocServer32
[] = {'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2',0};
1218 WCHAR path
[CHARS_IN_GUID
+ ARRAYSIZE(wszCLSIDSlash
) + ARRAYSIZE(wszInprocServer32
) - 1];
1220 MonoAssembly
*assembly
;
1221 ICLRRuntimeInfo
*info
= NULL
;
1227 WCHAR codebase
[MAX_PATH
+ 8];
1228 WCHAR classname
[350];
1229 WCHAR filename
[MAX_PATH
];
1231 DWORD dwBufLen
= 350;
1233 lstrcpyW(path
, wszCLSIDSlash
);
1234 StringFromGUID2(riid
, path
+ lstrlenW(wszCLSIDSlash
), CHARS_IN_GUID
);
1235 lstrcatW(path
, wszInprocServer32
);
1237 TRACE("Registry key: %s\n", debugstr_w(path
));
1239 res
= RegOpenKeyExW(HKEY_CLASSES_ROOT
, path
, 0, KEY_READ
, &key
);
1240 if (res
== ERROR_FILE_NOT_FOUND
)
1241 return CLASS_E_CLASSNOTAVAILABLE
;
1243 res
= RegGetValueW( key
, NULL
, wszClass
, RRF_RT_REG_SZ
, NULL
, classname
, &dwBufLen
);
1244 if(res
!= ERROR_SUCCESS
)
1246 WARN("Class value cannot be found.\n");
1247 hr
= CLASS_E_CLASSNOTAVAILABLE
;
1251 TRACE("classname (%s)\n", debugstr_w(classname
));
1253 dwBufLen
= MAX_PATH
+ 8;
1254 res
= RegGetValueW( key
, NULL
, wszCodebase
, RRF_RT_REG_SZ
, NULL
, codebase
, &dwBufLen
);
1255 if(res
!= ERROR_SUCCESS
)
1257 WARN("CodeBase value cannot be found.\n");
1258 hr
= CLASS_E_CLASSNOTAVAILABLE
;
1262 /* Strip file:/// */
1263 if(strncmpW(codebase
, wszFileSlash
, strlenW(wszFileSlash
)) == 0)
1264 offset
= strlenW(wszFileSlash
);
1266 strcpyW(filename
, codebase
+ offset
);
1268 TRACE("codebase (%s)\n", debugstr_w(filename
));
1273 hr
= get_runtime_info(filename
, NULL
, NULL
, 0, 0, FALSE
, &info
);
1276 hr
= ICLRRuntimeInfo_GetRuntimeHost(info
, &host
);
1279 hr
= RuntimeHost_GetDefaultDomain(host
, &domain
);
1286 IUnknown
*unk
= NULL
;
1287 char *filenameA
, *ns
;
1290 hr
= CLASS_E_CLASSNOTAVAILABLE
;
1292 host
->mono
->mono_thread_attach(domain
);
1294 filenameA
= WtoA(filename
);
1295 assembly
= host
->mono
->mono_domain_assembly_open(domain
, filenameA
);
1296 HeapFree(GetProcessHeap(), 0, filenameA
);
1299 ERR("Cannot open assembly %s\n", filenameA
);
1303 image
= host
->mono
->mono_assembly_get_image(assembly
);
1306 ERR("Couldn't get assembly image\n");
1310 classA
= WtoA(classname
);
1311 ns
= strrchr(classA
, '.');
1314 klass
= host
->mono
->mono_class_from_name(image
, classA
, ns
+1);
1315 HeapFree(GetProcessHeap(), 0, classA
);
1318 ERR("Couldn't get class from image\n");
1323 * Use the default constructor for the .NET class.
1325 result
= host
->mono
->mono_object_new(domain
, klass
);
1326 host
->mono
->mono_runtime_object_init(result
);
1328 hr
= RuntimeHost_GetIUnknownForObject(host
, result
, &unk
);
1331 hr
= IUnknown_QueryInterface(unk
, &IID_IUnknown
, ppObj
);
1333 IUnknown_Release(unk
);
1336 hr
= CLASS_E_CLASSNOTAVAILABLE
;
1339 hr
= CLASS_E_CLASSNOTAVAILABLE
;
1342 hr
= CLASS_E_CLASSNOTAVAILABLE
;
1346 ICLRRuntimeInfo_Release(info
);