2 * cominterop.c: COM Interop Support
5 * (C) 2002 Ximian, Inc. http://www.ximian.com
17 #include "metadata/cominterop.h"
18 #include "metadata/marshal.h"
19 #include "metadata/method-builder.h"
20 #include "metadata/tabledefs.h"
21 #include "metadata/exception.h"
22 #include "metadata/appdomain.h"
23 #include "mono/metadata/debug-helpers.h"
24 #include "mono/metadata/threadpool.h"
25 #include "mono/metadata/threads.h"
26 #include "mono/metadata/monitor.h"
27 #include "mono/metadata/metadata-internals.h"
28 #include "mono/metadata/domain-internals.h"
29 #include "mono/metadata/gc-internal.h"
30 #include "mono/metadata/threads-types.h"
31 #include "mono/metadata/string-icalls.h"
32 #include "mono/metadata/attrdefs.h"
33 #include "mono/metadata/gc-internal.h"
34 #include "mono/utils/mono-counters.h"
40 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
44 MONO_MARSHAL_NONE, /* No marshalling needed */
45 MONO_MARSHAL_COPY, /* Can be copied by value to the new domain */
46 MONO_MARSHAL_COPY_OUT, /* out parameter that needs to be copied back to the original instance */
47 MONO_MARSHAL_SERIALIZE /* Value needs to be serialized into the new domain */
48 } MonoXDomainMarshalType;
55 static MonoCOMProvider com_provider = MONO_COM_DEFAULT;
58 #include "mono/cil/opcode.def"
63 /* This mutex protects the various cominterop related caches in MonoImage */
64 #define mono_cominterop_lock() EnterCriticalSection (&cominterop_mutex)
65 #define mono_cominterop_unlock() LeaveCriticalSection (&cominterop_mutex)
66 static CRITICAL_SECTION cominterop_mutex;
68 /* STDCALL on windows, CDECL everywhere else to work with XPCOM and MainWin COM */
70 #define STDCALL __stdcall
75 /* Upon creation of a CCW, only allocate a weak handle and set the
76 * reference count to 0. If the unmanaged client code decides to addref and
77 * hold onto the CCW, I then allocate a strong handle. Once the reference count
78 * goes back to 0, convert back to a weak handle.
83 GHashTable* vtable_hash;
85 gpointer free_marshaler;
89 /* This type is the actual pointer passed to unmanaged code
90 * to represent a COM interface.
98 static int STDCALL cominterop_ccw_addref (MonoCCWInterface* ccwe);
100 static int STDCALL cominterop_ccw_release (MonoCCWInterface* ccwe);
102 static int STDCALL cominterop_ccw_queryinterface (MonoCCWInterface* ccwe, guint8* riid, gpointer* ppv);
105 static int STDCALL cominterop_ccw_get_type_info_count (MonoCCWInterface* ccwe, guint32 *pctinfo);
107 static int STDCALL cominterop_ccw_get_type_info (MonoCCWInterface* ccwe, guint32 iTInfo, guint32 lcid, gpointer *ppTInfo);
109 static int STDCALL cominterop_ccw_get_ids_of_names (MonoCCWInterface* ccwe, gpointer riid,
110 gunichar2** rgszNames, guint32 cNames,
111 guint32 lcid, gint32 *rgDispId);
113 static int STDCALL cominterop_ccw_invoke (MonoCCWInterface* ccwe, guint32 dispIdMember,
114 gpointer riid, guint32 lcid,
115 guint16 wFlags, gpointer pDispParams,
116 gpointer pVarResult, gpointer pExcepInfo,
120 cominterop_get_managed_wrapper_adjusted (MonoMethod *method);
123 cominterop_get_ccw (MonoObject* object, MonoClass* itf);
126 cominterop_get_ccw_object (MonoCCWInterface* ccw_entry, gboolean verify);
128 /* SAFEARRAY marshalling */
130 mono_marshal_safearray_begin (gpointer safearray, MonoArray **result, gpointer *indices, gpointer empty, gpointer parameter, gboolean allocateNewArray);
133 mono_marshal_safearray_get_value (gpointer safearray, gpointer indices);
136 mono_marshal_safearray_next (gpointer safearray, gpointer indices);
139 mono_marshal_safearray_end (gpointer safearray, gpointer indices);
142 mono_marshal_safearray_create (MonoArray *input, gpointer *newsafearray, gpointer *indices, gpointer empty);
145 mono_marshal_safearray_set_value (gpointer safearray, gpointer indices, gpointer value);
148 mono_marshal_safearray_free_indices (gpointer indices);
151 * cominterop_method_signature:
154 * Returns: the corresponding unmanaged method signature for a managed COM
157 static MonoMethodSignature*
158 cominterop_method_signature (MonoMethod* method)
160 MonoMethodSignature *res;
161 MonoImage *image = method->klass->image;
162 MonoMethodSignature *sig = mono_method_signature (method);
163 gboolean preserve_sig = method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG;
166 int param_count = sig->param_count + 1; // convert this arg into IntPtr arg
168 if (!preserve_sig &&!MONO_TYPE_IS_VOID (sig->ret))
171 res = mono_metadata_signature_alloc (image, param_count);
172 sigsize = MONO_SIZEOF_METHOD_SIGNATURE + sig->param_count * sizeof (MonoType *);
173 memcpy (res, sig, sigsize);
175 // now move args forward one
176 for (i = sig->param_count-1; i >= 0; i--)
177 res->params[i+1] = sig->params[i];
179 // first arg is interface pointer
180 res->params[0] = &mono_defaults.int_class->byval_arg;
186 // last arg is return type
187 if (!MONO_TYPE_IS_VOID (sig->ret)) {
188 res->params[param_count-1] = mono_metadata_type_dup (image, sig->ret);
189 res->params[param_count-1]->byref = 1;
190 res->params[param_count-1]->attrs = PARAM_ATTRIBUTE_OUT;
193 // return type is always int32 (HRESULT)
194 res->ret = &mono_defaults.int32_class->byval_arg;
198 res->pinvoke = FALSE;
204 res->param_count = param_count;
206 // STDCALL on windows, CDECL everywhere else to work with XPCOM and MainWin COM
208 res->call_convention = MONO_CALL_STDCALL;
210 res->call_convention = MONO_CALL_C;
217 * cominterop_get_function_pointer:
218 * @itf: a pointer to the COM interface
219 * @slot: the vtable slot of the method pointer to return
221 * Returns: the unmanaged vtable function pointer from the interface
224 cominterop_get_function_pointer (gpointer itf, int slot)
227 func = *((*(gpointer**)itf)+slot);
232 * cominterop_object_is_com_object:
233 * @obj: a pointer to the object
235 * Returns: a value indicating if the object is a
236 * Runtime Callable Wrapper (RCW) for a COM object
239 cominterop_object_is_rcw (MonoObject *obj)
241 MonoClass *klass = NULL;
242 MonoRealProxy* real_proxy = NULL;
245 klass = mono_object_class (obj);
246 if (klass != mono_defaults.transparent_proxy_class)
249 real_proxy = ((MonoTransparentProxy*)obj)->rp;
253 klass = mono_object_class (real_proxy);
254 return (klass && klass == mono_defaults.com_interop_proxy_class);
258 cominterop_get_com_slot_begin (MonoClass* klass)
260 static MonoClass *interface_type_attribute = NULL;
261 MonoCustomAttrInfo *cinfo = NULL;
262 MonoInterfaceTypeAttribute* itf_attr = NULL;
264 if (!interface_type_attribute)
265 interface_type_attribute = mono_class_from_name (mono_defaults.corlib, "System.Runtime.InteropServices", "InterfaceTypeAttribute");
266 cinfo = mono_custom_attrs_from_class (klass);
268 itf_attr = (MonoInterfaceTypeAttribute*)mono_custom_attrs_get_attr (cinfo, interface_type_attribute);
270 mono_custom_attrs_free (cinfo);
273 if (itf_attr && itf_attr->intType == 1)
274 return 3; /* 3 methods in IUnknown*/
276 return 7; /* 7 methods in IDispatch*/
280 * cominterop_get_method_interface:
281 * @method: method being called
283 * Returns: the MonoClass* representing the interface on which
284 * the method is defined.
287 cominterop_get_method_interface (MonoMethod* method)
290 MonoClass *ic = method->klass;
292 /* if method is on a class, we need to look up interface method exists on */
293 if (!MONO_CLASS_IS_INTERFACE(method->klass)) {
294 GPtrArray *ifaces = mono_class_get_implemented_interfaces (method->klass, &error);
295 g_assert (mono_error_ok (&error));
298 mono_class_setup_vtable (method->klass);
299 for (i = 0; i < ifaces->len; ++i) {
301 gboolean found = FALSE;
302 ic = g_ptr_array_index (ifaces, i);
303 offset = mono_class_interface_offset (method->klass, ic);
304 for (j = 0; j < ic->method.count; ++j) {
305 if (method->klass->vtable [j + offset] == method) {
314 g_ptr_array_free (ifaces, TRUE);
320 g_assert (MONO_CLASS_IS_INTERFACE (ic));
326 * cominterop_get_com_slot_for_method:
329 * Returns: the method's slot in the COM interface vtable
332 cominterop_get_com_slot_for_method (MonoMethod* method)
334 guint32 slot = method->slot;
335 MonoClass *ic = method->klass;
337 /* if method is on a class, we need to look up interface method exists on */
338 if (!MONO_CLASS_IS_INTERFACE(ic)) {
341 ic = cominterop_get_method_interface (method);
342 offset = mono_class_interface_offset (method->klass, ic);
343 g_assert(offset >= 0);
344 for(i = 0; i < ic->method.count; ++i) {
345 if (method->klass->vtable [i + offset] == method)
347 slot = ic->methods[i]->slot;
354 g_assert (MONO_CLASS_IS_INTERFACE (ic));
356 return slot + cominterop_get_com_slot_begin (ic);
361 cominterop_mono_string_to_guid (MonoString* string, guint8 *guid);
364 cominterop_class_guid (MonoClass* klass, guint8* guid)
366 static MonoClass *GuidAttribute = NULL;
367 MonoCustomAttrInfo *cinfo;
369 /* Handle the GuidAttribute */
371 GuidAttribute = mono_class_from_name (mono_defaults.corlib, "System.Runtime.InteropServices", "GuidAttribute");
373 cinfo = mono_custom_attrs_from_class (klass);
375 MonoReflectionGuidAttribute *attr = (MonoReflectionGuidAttribute*)mono_custom_attrs_get_attr (cinfo, GuidAttribute);
380 mono_custom_attrs_free (cinfo);
382 cominterop_mono_string_to_guid (attr->guid, guid);
389 cominterop_com_visible (MonoClass* klass)
391 static MonoClass *ComVisibleAttribute = NULL;
393 MonoCustomAttrInfo *cinfo;
395 MonoBoolean visible = 1;
397 /* Handle the ComVisibleAttribute */
398 if (!ComVisibleAttribute)
399 ComVisibleAttribute = mono_class_from_name (mono_defaults.corlib, "System.Runtime.InteropServices", "ComVisibleAttribute");
401 cinfo = mono_custom_attrs_from_class (klass);
403 MonoReflectionComVisibleAttribute *attr = (MonoReflectionComVisibleAttribute*)mono_custom_attrs_get_attr (cinfo, ComVisibleAttribute);
406 visible = attr->visible;
408 mono_custom_attrs_free (cinfo);
413 ifaces = mono_class_get_implemented_interfaces (klass, &error);
414 g_assert (mono_error_ok (&error));
417 for (i = 0; i < ifaces->len; ++i) {
418 MonoClass *ic = NULL;
419 ic = g_ptr_array_index (ifaces, i);
420 if (MONO_CLASS_IS_IMPORT (ic))
424 g_ptr_array_free (ifaces, TRUE);
430 static void cominterop_raise_hr_exception (int hr)
432 static MonoMethod* throw_exception_for_hr = NULL;
434 void* params[1] = {&hr};
435 if (!throw_exception_for_hr)
436 throw_exception_for_hr = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetExceptionForHR", 1);
437 ex = (MonoException*)mono_runtime_invoke (throw_exception_for_hr, NULL, params, NULL);
438 mono_raise_exception (ex);
442 * cominterop_get_interface:
443 * @obj: managed wrapper object containing COM object
444 * @ic: interface type to retrieve for COM object
446 * Returns: the COM interface requested
449 cominterop_get_interface (MonoComObject* obj, MonoClass* ic, gboolean throw_exception)
454 g_assert (MONO_CLASS_IS_INTERFACE (ic));
456 mono_cominterop_lock ();
458 itf = g_hash_table_lookup (obj->itf_hash, GUINT_TO_POINTER ((guint)ic->interface_id));
459 mono_cominterop_unlock ();
463 int found = cominterop_class_guid (ic, iid);
466 hr = ves_icall_System_Runtime_InteropServices_Marshal_QueryInterfaceInternal (obj->iunknown, iid, &itf);
467 if (hr < 0 && throw_exception) {
468 cominterop_raise_hr_exception (hr);
471 if (hr >= 0 && itf) {
472 mono_cominterop_lock ();
474 obj->itf_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
475 g_hash_table_insert (obj->itf_hash, GUINT_TO_POINTER ((guint)ic->interface_id), itf);
476 mono_cominterop_unlock ();
487 cominterop_get_hresult_for_exception (MonoException* exc)
493 static MonoReflectionType *
494 cominterop_type_from_handle (MonoType *handle)
496 MonoDomain *domain = mono_domain_get ();
497 MonoClass *klass = mono_class_from_mono_type (handle);
501 mono_class_init (klass);
502 return mono_type_get_object (domain, handle);
506 register_icall (gpointer func, const char *name, const char *sigstr, gboolean save)
508 MonoMethodSignature *sig = mono_create_icall_signature (sigstr);
510 mono_register_jit_icall (func, name, sig, save);
514 mono_cominterop_init (void)
516 char* com_provider_env = NULL;
518 InitializeCriticalSection (&cominterop_mutex);
520 com_provider_env = getenv ("MONO_COM");
521 if (com_provider_env && !strcmp(com_provider_env, "MS"))
522 com_provider = MONO_COM_MS;
524 register_icall (cominterop_get_method_interface, "cominterop_get_method_interface", "ptr ptr", FALSE);
525 register_icall (cominterop_get_function_pointer, "cominterop_get_function_pointer", "ptr ptr int32", FALSE);
526 register_icall (cominterop_object_is_rcw, "cominterop_object_is_rcw", "int32 object", FALSE);
527 register_icall (cominterop_get_ccw, "cominterop_get_ccw", "ptr object ptr", FALSE);
528 register_icall (cominterop_get_ccw_object, "cominterop_get_ccw_object", "object ptr int32", FALSE);
529 register_icall (cominterop_get_hresult_for_exception, "cominterop_get_hresult_for_exception", "int32 object", FALSE);
530 register_icall (cominterop_get_interface, "cominterop_get_interface", "ptr object ptr int32", FALSE);
532 register_icall (mono_string_to_bstr, "mono_string_to_bstr", "ptr obj", FALSE);
533 register_icall (mono_string_from_bstr, "mono_string_from_bstr", "obj ptr", FALSE);
534 register_icall (mono_free_bstr, "mono_free_bstr", "void ptr", FALSE);
535 register_icall (cominterop_type_from_handle, "cominterop_type_from_handle", "object ptr", FALSE);
537 /* SAFEARRAY marshalling */
538 register_icall (mono_marshal_safearray_begin, "mono_marshal_safearray_begin", "int32 ptr ptr ptr ptr ptr int32", FALSE);
539 register_icall (mono_marshal_safearray_get_value, "mono_marshal_safearray_get_value", "ptr ptr ptr", FALSE);
540 register_icall (mono_marshal_safearray_next, "mono_marshal_safearray_next", "int32 ptr ptr", FALSE);
541 register_icall (mono_marshal_safearray_end, "mono_marshal_safearray_end", "void ptr ptr", FALSE);
542 register_icall (mono_marshal_safearray_create, "mono_marshal_safearray_create", "int32 object ptr ptr ptr", FALSE);
543 register_icall (mono_marshal_safearray_set_value, "mono_marshal_safearray_set_value", "void ptr ptr ptr", FALSE);
544 register_icall (mono_marshal_safearray_free_indices, "mono_marshal_safearray_free_indices", "void ptr", FALSE);
548 mono_cominterop_cleanup (void)
550 DeleteCriticalSection (&cominterop_mutex);
554 mono_mb_emit_cominterop_call (MonoMethodBuilder *mb, MonoMethodSignature *sig, MonoMethod* method)
556 // get function pointer from 1st arg, the COM interface pointer
557 mono_mb_emit_ldarg (mb, 0);
558 mono_mb_emit_icon (mb, cominterop_get_com_slot_for_method (method));
559 mono_mb_emit_icall (mb, cominterop_get_function_pointer);
561 mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
562 mono_mb_emit_byte (mb, CEE_MONO_SAVE_LMF);
563 mono_mb_emit_calli (mb, sig);
564 mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
565 mono_mb_emit_byte (mb, CEE_MONO_RESTORE_LMF);
569 mono_cominterop_emit_ptr_to_object_conv (MonoMethodBuilder *mb, MonoType *type, MonoMarshalConv conv, MonoMarshalSpec *mspec)
572 case MONO_MARSHAL_CONV_OBJECT_INTERFACE:
573 case MONO_MARSHAL_CONV_OBJECT_IUNKNOWN:
574 case MONO_MARSHAL_CONV_OBJECT_IDISPATCH: {
575 static MonoClass* com_interop_proxy_class = NULL;
576 static MonoMethod* com_interop_proxy_get_proxy = NULL;
577 static MonoMethod* get_transparent_proxy = NULL;
579 guint32 pos_null = 0, pos_ccw = 0, pos_end = 0;
580 MonoClass *klass = NULL;
582 /* COM types are initialized lazily */
583 mono_init_com_types ();
585 klass = mono_class_from_mono_type (type);
587 mono_mb_emit_ldloc (mb, 1);
588 mono_mb_emit_byte (mb, CEE_LDNULL);
589 mono_mb_emit_byte (mb, CEE_STIND_REF);
591 mono_mb_emit_ldloc (mb, 0);
592 mono_mb_emit_byte (mb, CEE_LDIND_I);
593 pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
595 /* load dst to store later */
596 mono_mb_emit_ldloc (mb, 1);
598 mono_mb_emit_ldloc (mb, 0);
599 mono_mb_emit_byte (mb, CEE_LDIND_I);
600 mono_mb_emit_icon (mb, TRUE);
601 mono_mb_emit_icall (mb, cominterop_get_ccw_object);
602 pos_ccw = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);
604 if (!com_interop_proxy_class)
605 com_interop_proxy_class = mono_class_from_name (mono_defaults.corlib, "Mono.Interop", "ComInteropProxy");
606 if (!com_interop_proxy_get_proxy)
607 com_interop_proxy_get_proxy = mono_class_get_method_from_name_flags (com_interop_proxy_class, "GetProxy", 2, METHOD_ATTRIBUTE_PRIVATE);
608 if (!get_transparent_proxy)
609 get_transparent_proxy = mono_class_get_method_from_name (mono_defaults.real_proxy_class, "GetTransparentProxy", 0);
611 real_proxy = mono_mb_add_local (mb, &com_interop_proxy_class->byval_arg);
613 mono_mb_emit_ldloc (mb, 0);
614 mono_mb_emit_byte (mb, CEE_LDIND_I);
615 mono_mb_emit_ptr (mb, &mono_defaults.com_object_class->byval_arg);
616 mono_mb_emit_icall (mb, cominterop_type_from_handle);
617 mono_mb_emit_managed_call (mb, com_interop_proxy_get_proxy, NULL);
618 mono_mb_emit_managed_call (mb, get_transparent_proxy, NULL);
619 if (conv == MONO_MARSHAL_CONV_OBJECT_INTERFACE) {
621 mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
623 mono_mb_emit_byte (mb, CEE_STIND_REF);
624 pos_end = mono_mb_emit_short_branch (mb, CEE_BR_S);
626 /* is already managed object */
627 mono_mb_patch_short_branch (mb, pos_ccw);
628 mono_mb_emit_ldloc (mb, 0);
629 mono_mb_emit_byte (mb, CEE_LDIND_I);
630 mono_mb_emit_icon (mb, TRUE);
631 mono_mb_emit_icall (mb, cominterop_get_ccw_object);
633 if (conv == MONO_MARSHAL_CONV_OBJECT_INTERFACE) {
635 mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
637 mono_mb_emit_byte (mb, CEE_STIND_REF);
639 mono_mb_patch_short_branch (mb, pos_end);
641 mono_mb_patch_short_branch (mb, pos_null);
645 g_assert_not_reached ();
650 mono_cominterop_emit_object_to_ptr_conv (MonoMethodBuilder *mb, MonoType *type, MonoMarshalConv conv, MonoMarshalSpec *mspec)
653 case MONO_MARSHAL_CONV_OBJECT_INTERFACE:
654 case MONO_MARSHAL_CONV_OBJECT_IDISPATCH:
655 case MONO_MARSHAL_CONV_OBJECT_IUNKNOWN: {
656 guint32 pos_null = 0, pos_rcw = 0, pos_end = 0;
658 /* COM types are initialized lazily */
659 mono_init_com_types ();
662 mono_mb_emit_ldloc (mb, 1);
663 mono_mb_emit_icon (mb, 0);
664 mono_mb_emit_byte (mb, CEE_CONV_U);
665 mono_mb_emit_byte (mb, CEE_STIND_I);
667 mono_mb_emit_ldloc (mb, 0);
668 mono_mb_emit_byte (mb, CEE_LDIND_REF);
670 // if null just break, dst was already inited to 0
671 pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
673 mono_mb_emit_ldloc (mb, 0);
674 mono_mb_emit_byte (mb, CEE_LDIND_REF);
675 mono_mb_emit_icall (mb, cominterop_object_is_rcw);
676 pos_rcw = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
678 // load dst to store later
679 mono_mb_emit_ldloc (mb, 1);
682 mono_mb_emit_ldloc (mb, 0);
683 mono_mb_emit_byte (mb, CEE_LDIND_REF);
684 mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoTransparentProxy, rp));
685 mono_mb_emit_byte (mb, CEE_LDIND_REF);
687 /* load the RCW from the ComInteropProxy*/
688 mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoComInteropProxy, com_object));
689 mono_mb_emit_byte (mb, CEE_LDIND_REF);
691 if (conv == MONO_MARSHAL_CONV_OBJECT_INTERFACE) {
692 mono_mb_emit_ptr (mb, mono_type_get_class (type));
693 mono_mb_emit_icon (mb, TRUE);
694 mono_mb_emit_icall (mb, cominterop_get_interface);
697 else if (conv == MONO_MARSHAL_CONV_OBJECT_IUNKNOWN) {
698 static MonoProperty* iunknown = NULL;
701 iunknown = mono_class_get_property_from_name (mono_defaults.com_object_class, "IUnknown");
702 mono_mb_emit_managed_call (mb, iunknown->get, NULL);
704 else if (conv == MONO_MARSHAL_CONV_OBJECT_IDISPATCH) {
705 static MonoProperty* idispatch = NULL;
708 idispatch = mono_class_get_property_from_name (mono_defaults.com_object_class, "IDispatch");
709 mono_mb_emit_managed_call (mb, idispatch->get, NULL);
712 g_assert_not_reached ();
714 mono_mb_emit_byte (mb, CEE_STIND_I);
715 pos_end = mono_mb_emit_short_branch (mb, CEE_BR_S);
718 mono_mb_patch_short_branch (mb, pos_rcw);
719 /* load dst to store later */
720 mono_mb_emit_ldloc (mb, 1);
722 mono_mb_emit_ldloc (mb, 0);
723 mono_mb_emit_byte (mb, CEE_LDIND_REF);
725 if (conv == MONO_MARSHAL_CONV_OBJECT_INTERFACE)
726 mono_mb_emit_ptr (mb, mono_type_get_class (type));
727 else if (conv == MONO_MARSHAL_CONV_OBJECT_IUNKNOWN)
728 mono_mb_emit_ptr (mb, mono_defaults.iunknown_class);
729 else if (conv == MONO_MARSHAL_CONV_OBJECT_IDISPATCH)
730 mono_mb_emit_ptr (mb, mono_defaults.idispatch_class);
732 g_assert_not_reached ();
733 mono_mb_emit_icall (mb, cominterop_get_ccw);
734 mono_mb_emit_byte (mb, CEE_STIND_I);
736 mono_mb_patch_short_branch (mb, pos_end);
737 mono_mb_patch_short_branch (mb, pos_null);
741 g_assert_not_reached ();
746 * cominterop_get_native_wrapper_adjusted:
747 * @method: managed COM Interop method
749 * Returns: the generated method to call with signature matching
750 * the unmanaged COM Method signature
753 cominterop_get_native_wrapper_adjusted (MonoMethod *method)
756 MonoMethodBuilder *mb_native;
757 MonoMarshalSpec **mspecs;
758 MonoMethodSignature *sig, *sig_native;
759 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *) method;
762 sig = mono_method_signature (method);
764 // create unmanaged wrapper
765 mb_native = mono_mb_new (method->klass, method->name, MONO_WRAPPER_MANAGED_TO_NATIVE);
766 sig_native = cominterop_method_signature (method);
768 mspecs = g_new (MonoMarshalSpec*, sig_native->param_count+1);
769 memset (mspecs, 0, sizeof(MonoMarshalSpec*)*(sig_native->param_count+1));
771 mono_method_get_marshal_info (method, mspecs);
773 // move managed args up one
774 for (i = sig->param_count; i >= 1; i--)
775 mspecs[i+1] = mspecs[i];
777 // first arg is IntPtr for interface
780 if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG)) {
781 // move return spec to last param
782 if (!MONO_TYPE_IS_VOID (sig->ret))
783 mspecs[sig_native->param_count] = mspecs[0];
788 for (i = 1; i < sig_native->param_count; i++) {
789 int mspec_index = i + 1;
790 if (mspecs[mspec_index] == NULL) {
791 // default object to VARIANT
792 if (sig_native->params[i]->type == MONO_TYPE_OBJECT) {
793 mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
794 mspecs[mspec_index]->native = MONO_NATIVE_STRUCT;
796 else if (sig_native->params[i]->type == MONO_TYPE_STRING) {
797 mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
798 mspecs[mspec_index]->native = MONO_NATIVE_BSTR;
800 else if (sig_native->params[i]->type == MONO_TYPE_CLASS) {
801 mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
802 mspecs[mspec_index]->native = MONO_NATIVE_INTERFACE;
804 else if (sig_native->params[i]->type == MONO_TYPE_BOOLEAN) {
805 mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
806 mspecs[mspec_index]->native = MONO_NATIVE_VARIANTBOOL;
811 if (method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG) {
812 // move return spec to last param
813 if (!MONO_TYPE_IS_VOID (sig->ret) && mspecs[0] == NULL) {
814 // default object to VARIANT
815 if (sig->ret->type == MONO_TYPE_OBJECT) {
816 mspecs[0] = g_new0 (MonoMarshalSpec, 1);
817 mspecs[0]->native = MONO_NATIVE_STRUCT;
819 else if (sig->ret->type == MONO_TYPE_STRING) {
820 mspecs[0] = g_new0 (MonoMarshalSpec, 1);
821 mspecs[0]->native = MONO_NATIVE_BSTR;
823 else if (sig->ret->type == MONO_TYPE_CLASS) {
824 mspecs[0] = g_new0 (MonoMarshalSpec, 1);
825 mspecs[0]->native = MONO_NATIVE_INTERFACE;
827 else if (sig->ret->type == MONO_TYPE_BOOLEAN) {
828 mspecs[0] = g_new0 (MonoMarshalSpec, 1);
829 mspecs[0]->native = MONO_NATIVE_VARIANTBOOL;
834 mono_marshal_emit_native_wrapper (method->klass->image, mb_native, sig_native, piinfo, mspecs, piinfo->addr, FALSE, TRUE, FALSE);
836 res = mono_mb_create_method (mb_native, sig_native, sig_native->param_count + 16);
838 mono_mb_free (mb_native);
840 for (i = sig_native->param_count; i >= 0; i--)
842 mono_metadata_free_marshal_spec (mspecs [i]);
849 * mono_cominterop_get_native_wrapper:
850 * @method: managed method
852 * Returns: the generated method to call
855 mono_cominterop_get_native_wrapper (MonoMethod *method)
859 MonoMethodBuilder *mb;
860 MonoMethodSignature *sig, *csig;
864 cache = mono_marshal_get_cache (&method->klass->image->cominterop_wrapper_cache, mono_aligned_addr_hash, NULL);
865 if ((res = mono_marshal_find_in_cache (cache, method)))
868 mono_init_com_types ();
870 if (!method->klass->vtable)
871 mono_class_setup_vtable (method->klass);
873 if (!method->klass->methods)
874 mono_class_setup_methods (method->klass);
875 g_assert (!method->klass->exception_type); /*FIXME do proper error handling*/
877 sig = mono_method_signature (method);
878 mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_COMINTEROP);
880 /* if method klass is import, that means method
881 * is really a com call. let interop system emit it.
883 if (MONO_CLASS_IS_IMPORT(method->klass)) {
884 /* FIXME: we have to call actual class .ctor
885 * instead of just __ComObject .ctor.
887 if (!strcmp(method->name, ".ctor")) {
888 static MonoMethod *ctor = NULL;
891 ctor = mono_class_get_method_from_name (mono_defaults.com_object_class, ".ctor", 0);
892 mono_mb_emit_ldarg (mb, 0);
893 mono_mb_emit_managed_call (mb, ctor, NULL);
894 mono_mb_emit_byte (mb, CEE_RET);
897 static MonoMethod * ThrowExceptionForHR = NULL;
898 MonoMethod *adjusted_method;
902 gboolean preserve_sig = method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG;
904 // add local variables
905 ptr_this = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
906 if (!MONO_TYPE_IS_VOID (sig->ret))
907 retval = mono_mb_add_local (mb, sig->ret);
909 // get the type for the interface the method is defined on
910 // and then get the underlying COM interface for that type
911 mono_mb_emit_ldarg (mb, 0);
912 mono_mb_emit_ptr (mb, method);
913 mono_mb_emit_icall (mb, cominterop_get_method_interface);
914 mono_mb_emit_icon (mb, TRUE);
915 mono_mb_emit_icall (mb, cominterop_get_interface);
916 mono_mb_emit_stloc (mb, ptr_this);
918 // arg 1 is unmanaged this pointer
919 mono_mb_emit_ldloc (mb, ptr_this);
922 for (i = 1; i <= sig->param_count; i++)
923 mono_mb_emit_ldarg (mb, i);
925 // push managed return value as byref last argument
926 if (!MONO_TYPE_IS_VOID (sig->ret) && !preserve_sig)
927 mono_mb_emit_ldloc_addr (mb, retval);
929 adjusted_method = cominterop_get_native_wrapper_adjusted (method);
930 mono_mb_emit_managed_call (mb, adjusted_method, NULL);
933 if (!ThrowExceptionForHR)
934 ThrowExceptionForHR = mono_class_get_method_from_name (mono_defaults.marshal_class, "ThrowExceptionForHR", 1);
935 mono_mb_emit_managed_call (mb, ThrowExceptionForHR, NULL);
937 // load return value managed is expecting
938 if (!MONO_TYPE_IS_VOID (sig->ret))
939 mono_mb_emit_ldloc (mb, retval);
942 mono_mb_emit_byte (mb, CEE_RET);
947 /* Does this case ever get hit? */
949 char *msg = g_strdup ("non imported interfaces on \
950 imported classes is not yet implemented.");
951 mono_mb_emit_exception (mb, "NotSupportedException", msg);
953 csig = mono_metadata_signature_dup_full (method->klass->image, sig);
955 res = mono_mb_create_and_cache (cache, method,
956 mb, csig, csig->param_count + 16);
962 * mono_cominterop_get_invoke:
963 * @method: managed method
965 * Returns: the generated method that calls the underlying __ComObject
966 * rather than the proxy object.
969 mono_cominterop_get_invoke (MonoMethod *method)
971 MonoMethodSignature *sig;
972 MonoMethodBuilder *mb;
975 GHashTable* cache = mono_marshal_get_cache (&method->klass->image->cominterop_invoke_cache, mono_aligned_addr_hash, NULL);
979 if ((res = mono_marshal_find_in_cache (cache, method)))
982 sig = mono_signature_no_pinvoke (method);
984 /* we cant remote methods without this pointer */
988 mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_COMINTEROP_INVOKE);
990 /* get real proxy object, which is a ComInteropProxy in this case*/
991 temp_obj = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
992 mono_mb_emit_ldarg (mb, 0);
993 mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoTransparentProxy, rp));
994 mono_mb_emit_byte (mb, CEE_LDIND_REF);
996 /* load the RCW from the ComInteropProxy*/
997 mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoComInteropProxy, com_object));
998 mono_mb_emit_byte (mb, CEE_LDIND_REF);
1000 /* load args and make the call on the RCW */
1001 for (i = 1; i <= sig->param_count; i++)
1002 mono_mb_emit_ldarg (mb, i);
1004 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
1005 MonoMethod * native_wrapper = mono_cominterop_get_native_wrapper(method);
1006 mono_mb_emit_managed_call (mb, native_wrapper, NULL);
1009 if (method->flags & METHOD_ATTRIBUTE_VIRTUAL)
1010 mono_mb_emit_op (mb, CEE_CALLVIRT, method);
1012 mono_mb_emit_op (mb, CEE_CALL, method);
1015 if (!strcmp(method->name, ".ctor")) {
1016 static MonoClass *com_interop_proxy_class = NULL;
1017 static MonoMethod *cache_proxy = NULL;
1019 if (!com_interop_proxy_class)
1020 com_interop_proxy_class = mono_class_from_name (mono_defaults.corlib, "Mono.Interop", "ComInteropProxy");
1022 cache_proxy = mono_class_get_method_from_name (com_interop_proxy_class, "CacheProxy", 0);
1024 mono_mb_emit_ldarg (mb, 0);
1025 mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoTransparentProxy, rp));
1026 mono_mb_emit_byte (mb, CEE_LDIND_REF);
1027 mono_mb_emit_managed_call (mb, cache_proxy, NULL);
1030 mono_marshal_emit_thread_interrupt_checkpoint (mb);
1032 mono_mb_emit_byte (mb, CEE_RET);
1034 res = mono_mb_create_and_cache (cache, method, mb, sig, sig->param_count + 16);
1040 /* Maps a managed object to its unmanaged representation
1041 * i.e. it's COM Callable Wrapper (CCW).
1045 static GHashTable* ccw_hash = NULL;
1047 /* Maps a CCW interface to it's containing CCW.
1048 * Note that a CCW support many interfaces.
1050 * Value: MonoCCWInterface*
1052 static GHashTable* ccw_interface_hash = NULL;
1054 /* Maps the IUnknown value of a RCW to
1055 * it's MonoComInteropProxy*.
1059 static GHashTable* rcw_hash = NULL;
1062 mono_cominterop_emit_marshal_com_interface (EmitMarshalContext *m, int argnum,
1064 MonoMarshalSpec *spec,
1065 int conv_arg, MonoType **conv_arg_type,
1066 MarshalAction action)
1068 MonoMethodBuilder *mb = m->mb;
1069 MonoClass *klass = t->data.klass;
1070 static MonoMethod* get_object_for_iunknown = NULL;
1071 static MonoMethod* get_iunknown_for_object_internal = NULL;
1072 static MonoMethod* get_com_interface_for_object_internal = NULL;
1073 static MonoMethod* get_idispatch_for_object_internal = NULL;
1074 static MonoMethod* marshal_release = NULL;
1075 static MonoMethod* AddRef = NULL;
1076 if (!get_object_for_iunknown)
1077 get_object_for_iunknown = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetObjectForIUnknown", 1);
1078 if (!get_iunknown_for_object_internal)
1079 get_iunknown_for_object_internal = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetIUnknownForObjectInternal", 1);
1080 if (!get_idispatch_for_object_internal)
1081 get_idispatch_for_object_internal = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetIDispatchForObjectInternal", 1);
1082 if (!get_com_interface_for_object_internal)
1083 get_com_interface_for_object_internal = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetComInterfaceForObjectInternal", 2);
1084 if (!marshal_release)
1085 marshal_release = mono_class_get_method_from_name (mono_defaults.marshal_class, "Release", 1);
1087 /* COM types are initialized lazily */
1088 mono_init_com_types ();
1091 case MARSHAL_ACTION_CONV_IN: {
1092 guint32 pos_null = 0;
1094 *conv_arg_type = &mono_defaults.int_class->byval_arg;
1095 conv_arg = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
1097 mono_mb_emit_ptr (mb, NULL);
1098 mono_mb_emit_stloc (mb, conv_arg);
1100 /* we dont need any conversions for out parameters */
1101 if (t->byref && t->attrs & PARAM_ATTRIBUTE_OUT)
1104 mono_mb_emit_ldarg (mb, argnum);
1106 mono_mb_emit_byte (mb, CEE_LDIND_REF);
1107 /* if null just break, conv arg was already inited to 0 */
1108 pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
1110 mono_mb_emit_ldarg (mb, argnum);
1112 mono_mb_emit_byte (mb, CEE_LDIND_REF);
1114 if (klass && klass != mono_defaults.object_class) {
1115 mono_mb_emit_ptr (mb, t);
1116 mono_mb_emit_icall (mb, cominterop_type_from_handle);
1117 mono_mb_emit_managed_call (mb, get_com_interface_for_object_internal, NULL);
1119 else if (spec->native == MONO_NATIVE_IUNKNOWN)
1120 mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
1121 else if (spec->native == MONO_NATIVE_IDISPATCH)
1122 mono_mb_emit_managed_call (mb, get_idispatch_for_object_internal, NULL);
1123 else if (!klass && spec->native == MONO_NATIVE_INTERFACE)
1124 mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
1126 g_assert_not_reached ();
1127 mono_mb_emit_stloc (mb, conv_arg);
1128 mono_mb_patch_short_branch (mb, pos_null);
1132 case MARSHAL_ACTION_CONV_OUT: {
1133 if (t->byref && (t->attrs & PARAM_ATTRIBUTE_OUT)) {
1135 guint32 pos_null = 0, pos_ccw = 0, pos_end = 0;
1136 ccw_obj = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
1138 mono_mb_emit_ldarg (mb, argnum);
1139 mono_mb_emit_byte (mb, CEE_LDNULL);
1140 mono_mb_emit_byte (mb, CEE_STIND_REF);
1142 mono_mb_emit_ldloc (mb, conv_arg);
1143 pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
1145 mono_mb_emit_ldloc (mb, conv_arg);
1146 mono_mb_emit_icon (mb, TRUE);
1147 mono_mb_emit_icall (mb, cominterop_get_ccw_object);
1148 mono_mb_emit_stloc (mb, ccw_obj);
1149 mono_mb_emit_ldloc (mb, ccw_obj);
1150 pos_ccw = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);
1152 mono_mb_emit_ldarg (mb, argnum);
1153 mono_mb_emit_ldloc (mb, conv_arg);
1154 mono_mb_emit_managed_call (mb, get_object_for_iunknown, NULL);
1156 if (klass && klass != mono_defaults.object_class)
1157 mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
1158 mono_mb_emit_byte (mb, CEE_STIND_REF);
1160 pos_end = mono_mb_emit_short_branch (mb, CEE_BR_S);
1162 /* is already managed object */
1163 mono_mb_patch_short_branch (mb, pos_ccw);
1164 mono_mb_emit_ldarg (mb, argnum);
1165 mono_mb_emit_ldloc (mb, ccw_obj);
1167 if (klass && klass != mono_defaults.object_class)
1168 mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
1169 mono_mb_emit_byte (mb, CEE_STIND_REF);
1171 mono_mb_patch_short_branch (mb, pos_end);
1173 /* need to call Release to follow COM rules of ownership */
1174 mono_mb_emit_ldloc (mb, conv_arg);
1175 mono_mb_emit_managed_call (mb, marshal_release, NULL);
1176 mono_mb_emit_byte (mb, CEE_POP);
1179 mono_mb_patch_short_branch (mb, pos_null);
1183 case MARSHAL_ACTION_PUSH:
1185 mono_mb_emit_ldloc_addr (mb, conv_arg);
1187 mono_mb_emit_ldloc (mb, conv_arg);
1190 case MARSHAL_ACTION_CONV_RESULT: {
1191 int ccw_obj, ret_ptr;
1192 guint32 pos_null = 0, pos_ccw = 0, pos_end = 0;
1193 ccw_obj = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
1194 ret_ptr = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
1196 /* store return value */
1197 mono_mb_emit_stloc (mb, ret_ptr);
1199 mono_mb_emit_ldloc (mb, ret_ptr);
1200 pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
1202 mono_mb_emit_ldloc (mb, ret_ptr);
1203 mono_mb_emit_icon (mb, TRUE);
1204 mono_mb_emit_icall (mb, cominterop_get_ccw_object);
1205 mono_mb_emit_stloc (mb, ccw_obj);
1206 mono_mb_emit_ldloc (mb, ccw_obj);
1207 pos_ccw = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);
1209 mono_mb_emit_ldloc (mb, ret_ptr);
1210 mono_mb_emit_managed_call (mb, get_object_for_iunknown, NULL);
1212 if (klass && klass != mono_defaults.object_class)
1213 mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
1214 mono_mb_emit_stloc (mb, 3);
1216 pos_end = mono_mb_emit_short_branch (mb, CEE_BR_S);
1218 /* is already managed object */
1219 mono_mb_patch_short_branch (mb, pos_ccw);
1220 mono_mb_emit_ldloc (mb, ccw_obj);
1222 if (klass && klass != mono_defaults.object_class)
1223 mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
1224 mono_mb_emit_stloc (mb, 3);
1226 mono_mb_patch_short_branch (mb, pos_end);
1228 /* need to call Release to follow COM rules of ownership */
1229 mono_mb_emit_ldloc (mb, ret_ptr);
1230 mono_mb_emit_managed_call (mb, marshal_release, NULL);
1231 mono_mb_emit_byte (mb, CEE_POP);
1234 mono_mb_patch_short_branch (mb, pos_null);
1238 case MARSHAL_ACTION_MANAGED_CONV_IN: {
1240 guint32 pos_null = 0, pos_ccw = 0, pos_end = 0;
1241 ccw_obj = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
1243 klass = mono_class_from_mono_type (t);
1244 conv_arg = mono_mb_add_local (mb, &klass->byval_arg);
1245 *conv_arg_type = &mono_defaults.int_class->byval_arg;
1247 mono_mb_emit_byte (mb, CEE_LDNULL);
1248 mono_mb_emit_stloc (mb, conv_arg);
1249 if (t->attrs & PARAM_ATTRIBUTE_OUT)
1252 mono_mb_emit_ldarg (mb, argnum);
1254 mono_mb_emit_byte (mb, CEE_LDIND_REF);
1255 pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
1257 mono_mb_emit_ldarg (mb, argnum);
1259 mono_mb_emit_byte (mb, CEE_LDIND_REF);
1260 mono_mb_emit_icon (mb, TRUE);
1261 mono_mb_emit_icall (mb, cominterop_get_ccw_object);
1262 mono_mb_emit_stloc (mb, ccw_obj);
1263 mono_mb_emit_ldloc (mb, ccw_obj);
1264 pos_ccw = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);
1267 mono_mb_emit_ldarg (mb, argnum);
1269 mono_mb_emit_byte (mb, CEE_LDIND_REF);
1270 mono_mb_emit_managed_call (mb, get_object_for_iunknown, NULL);
1272 if (klass && klass != mono_defaults.object_class)
1273 mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
1274 mono_mb_emit_stloc (mb, conv_arg);
1275 pos_end = mono_mb_emit_short_branch (mb, CEE_BR_S);
1277 /* is already managed object */
1278 mono_mb_patch_short_branch (mb, pos_ccw);
1279 mono_mb_emit_ldloc (mb, ccw_obj);
1280 if (klass && klass != mono_defaults.object_class)
1281 mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
1282 mono_mb_emit_stloc (mb, conv_arg);
1284 mono_mb_patch_short_branch (mb, pos_end);
1286 mono_mb_patch_short_branch (mb, pos_null);
1290 case MARSHAL_ACTION_MANAGED_CONV_OUT: {
1291 if (t->byref && t->attrs & PARAM_ATTRIBUTE_OUT) {
1292 guint32 pos_null = 0;
1295 AddRef = mono_class_get_method_from_name (mono_defaults.marshal_class, "AddRef", 1);
1297 mono_mb_emit_ldarg (mb, argnum);
1298 mono_mb_emit_byte (mb, CEE_LDC_I4_0);
1299 mono_mb_emit_byte (mb, CEE_STIND_I);
1301 mono_mb_emit_ldloc (mb, conv_arg);
1302 pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
1304 /* to store later */
1305 mono_mb_emit_ldarg (mb, argnum);
1306 mono_mb_emit_ldloc (mb, conv_arg);
1307 if (klass && klass != mono_defaults.object_class) {
1308 mono_mb_emit_ptr (mb, t);
1309 mono_mb_emit_icall (mb, cominterop_type_from_handle);
1310 mono_mb_emit_managed_call (mb, get_com_interface_for_object_internal, NULL);
1312 else if (spec->native == MONO_NATIVE_IUNKNOWN)
1313 mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
1314 else if (spec->native == MONO_NATIVE_IDISPATCH)
1315 mono_mb_emit_managed_call (mb, get_idispatch_for_object_internal, NULL);
1316 else if (!klass && spec->native == MONO_NATIVE_INTERFACE)
1317 mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
1319 g_assert_not_reached ();
1320 mono_mb_emit_byte (mb, CEE_STIND_I);
1322 mono_mb_emit_ldarg (mb, argnum);
1323 mono_mb_emit_byte (mb, CEE_LDIND_I);
1324 mono_mb_emit_managed_call (mb, AddRef, NULL);
1325 mono_mb_emit_byte (mb, CEE_POP);
1327 mono_mb_patch_short_branch (mb, pos_null);
1332 case MARSHAL_ACTION_MANAGED_CONV_RESULT: {
1333 guint32 pos_null = 0;
1335 ccw_obj = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
1338 AddRef = mono_class_get_method_from_name (mono_defaults.marshal_class, "AddRef", 1);
1340 /* store return value */
1341 mono_mb_emit_stloc (mb, ccw_obj);
1343 mono_mb_emit_ldloc (mb, ccw_obj);
1345 /* if null just break, conv arg was already inited to 0 */
1346 pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
1348 /* to store later */
1349 mono_mb_emit_ldloc (mb, ccw_obj);
1350 if (klass && klass != mono_defaults.object_class) {
1351 mono_mb_emit_ptr (mb, t);
1352 mono_mb_emit_icall (mb, cominterop_type_from_handle);
1353 mono_mb_emit_managed_call (mb, get_com_interface_for_object_internal, NULL);
1355 else if (spec->native == MONO_NATIVE_IUNKNOWN)
1356 mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
1357 else if (spec->native == MONO_NATIVE_IDISPATCH)
1358 mono_mb_emit_managed_call (mb, get_idispatch_for_object_internal, NULL);
1359 else if (!klass && spec->native == MONO_NATIVE_INTERFACE)
1360 mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
1362 g_assert_not_reached ();
1363 mono_mb_emit_stloc (mb, 3);
1364 mono_mb_emit_ldloc (mb, 3);
1366 mono_mb_emit_managed_call (mb, AddRef, NULL);
1367 mono_mb_emit_byte (mb, CEE_POP);
1369 mono_mb_patch_short_branch (mb, pos_null);
1374 g_assert_not_reached ();
1382 int (STDCALL *QueryInterface)(gpointer pUnk, gpointer riid, gpointer* ppv);
1383 int (STDCALL *AddRef)(gpointer pUnk);
1384 int (STDCALL *Release)(gpointer pUnk);
1387 #define MONO_S_OK 0x00000000L
1388 #define MONO_E_NOINTERFACE 0x80004002L
1389 #define MONO_E_NOTIMPL 0x80004001L
1392 ves_icall_System_Runtime_InteropServices_Marshal_AddRefInternal (gpointer pUnk)
1395 return (*(MonoIUnknown**)pUnk)->AddRef(pUnk);
1399 ves_icall_System_Runtime_InteropServices_Marshal_QueryInterfaceInternal (gpointer pUnk, gpointer riid, gpointer* ppv)
1402 return (*(MonoIUnknown**)pUnk)->QueryInterface(pUnk, riid, ppv);
1406 ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (gpointer pUnk)
1409 return (*(MonoIUnknown**)pUnk)->Release(pUnk);
1412 static gboolean cominterop_can_support_dispatch (MonoClass* klass)
1414 if (!(klass->flags & TYPE_ATTRIBUTE_PUBLIC) )
1417 if (!cominterop_com_visible (klass))
1424 cominterop_get_idispatch_for_object (MonoObject* object)
1429 if (cominterop_object_is_rcw (object)) {
1430 return cominterop_get_interface (((MonoComInteropProxy*)((MonoTransparentProxy*)object)->rp)->com_object,
1431 mono_defaults.idispatch_class, TRUE);
1434 MonoClass* klass = mono_object_class (object);
1435 if (!cominterop_can_support_dispatch (klass) )
1436 cominterop_raise_hr_exception (MONO_E_NOINTERFACE);
1437 return cominterop_get_ccw (object, mono_defaults.idispatch_class);
1442 ves_icall_System_Runtime_InteropServices_Marshal_GetIUnknownForObjectInternal (MonoObject* object)
1448 mono_init_com_types ();
1450 if (cominterop_object_is_rcw (object)) {
1451 MonoClass *klass = NULL;
1452 MonoRealProxy* real_proxy = NULL;
1455 klass = mono_object_class (object);
1456 if (klass != mono_defaults.transparent_proxy_class) {
1457 g_assert_not_reached ();
1461 real_proxy = ((MonoTransparentProxy*)object)->rp;
1463 g_assert_not_reached ();
1467 klass = mono_object_class (real_proxy);
1468 if (klass != mono_defaults.com_interop_proxy_class) {
1469 g_assert_not_reached ();
1473 if (!((MonoComInteropProxy*)real_proxy)->com_object) {
1474 g_assert_not_reached ();
1478 return ((MonoComInteropProxy*)real_proxy)->com_object->iunknown;
1481 return cominterop_get_ccw (object, mono_defaults.iunknown_class);
1484 g_assert_not_reached ();
1489 ves_icall_System_Runtime_InteropServices_Marshal_GetObjectForCCW (void* pUnk)
1492 MonoObject* object = NULL;
1497 /* see if it is a CCW */
1498 object = cominterop_get_ccw_object ((MonoCCWInterface*)pUnk, TRUE);
1502 g_assert_not_reached ();
1507 ves_icall_System_Runtime_InteropServices_Marshal_GetIDispatchForObjectInternal (MonoObject* object)
1510 mono_init_com_types ();
1512 return cominterop_get_idispatch_for_object (object);
1514 g_assert_not_reached ();
1519 ves_icall_System_Runtime_InteropServices_Marshal_GetCCW (MonoObject* object, MonoReflectionType* type)
1522 MonoClass* klass = NULL;
1525 g_assert (type->type);
1526 klass = mono_type_get_class (type->type);
1528 if (!mono_class_init (klass))
1529 mono_raise_exception (mono_class_get_exception_for_failure (klass));
1531 itf = cominterop_get_ccw (object, klass);
1535 g_assert_not_reached ();
1541 ves_icall_System_Runtime_InteropServices_Marshal_IsComObject (MonoObject* object)
1544 return (MonoBoolean)cominterop_object_is_rcw (object);
1546 g_assert_not_reached ();
1551 ves_icall_System_Runtime_InteropServices_Marshal_ReleaseComObjectInternal (MonoObject* object)
1554 MonoComInteropProxy* proxy = NULL;
1555 gint32 ref_count = 0;
1558 g_assert (cominterop_object_is_rcw (object));
1560 proxy = (MonoComInteropProxy*)((MonoTransparentProxy*)object)->rp;
1563 if (proxy->ref_count == 0)
1566 ref_count = InterlockedDecrement (&proxy->ref_count);
1568 g_assert (ref_count >= 0);
1571 ves_icall_System_ComObject_ReleaseInterfaces (proxy->com_object);
1575 g_assert_not_reached ();
1580 ves_icall_System_Runtime_InteropServices_Marshal_GetComSlotForMethodInfoInternal (MonoReflectionMethod *m)
1582 MONO_ARCH_SAVE_REGS;
1585 return cominterop_get_com_slot_for_method (m->method);
1587 g_assert_not_reached ();
1591 /* Only used for COM RCWs */
1593 ves_icall_System_ComObject_CreateRCW (MonoReflectionType *type)
1599 MONO_ARCH_SAVE_REGS;
1601 domain = mono_object_domain (type);
1602 klass = mono_class_from_mono_type (type->type);
1604 /* call mono_object_new_alloc_specific instead of mono_object_new
1605 * because we want to actually create object. mono_object_new checks
1606 * to see if type is import and creates transparent proxy. this method
1607 * is called by the corresponding real proxy to create the real RCW.
1608 * Constructor does not need to be called. Will be called later.
1610 obj = mono_object_new_alloc_specific (mono_class_vtable_full (domain, klass, TRUE));
1615 cominterop_rcw_interface_finalizer (gpointer key, gpointer value, gpointer user_data)
1617 ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (value);
1622 ves_icall_System_ComObject_ReleaseInterfaces (MonoComObject* obj)
1625 if (obj->itf_hash) {
1626 guint32 gchandle = 0;
1627 mono_cominterop_lock ();
1628 gchandle = GPOINTER_TO_UINT (g_hash_table_lookup (rcw_hash, obj->iunknown));
1630 mono_gchandle_free (gchandle);
1631 g_hash_table_remove (rcw_hash, obj->iunknown);
1634 g_hash_table_foreach_remove (obj->itf_hash, cominterop_rcw_interface_finalizer, NULL);
1635 g_hash_table_destroy (obj->itf_hash);
1636 ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (obj->iunknown);
1637 obj->itf_hash = obj->iunknown = NULL;
1638 mono_cominterop_unlock ();
1643 cominterop_rcw_finalizer (gpointer key, gpointer value, gpointer user_data)
1645 guint32 gchandle = 0;
1647 gchandle = GPOINTER_TO_UINT (value);
1649 MonoComInteropProxy* proxy = (MonoComInteropProxy*)mono_gchandle_get_target (gchandle);
1652 if (proxy->com_object->itf_hash) {
1653 g_hash_table_foreach_remove (proxy->com_object->itf_hash, cominterop_rcw_interface_finalizer, NULL);
1654 g_hash_table_destroy (proxy->com_object->itf_hash);
1656 if (proxy->com_object->iunknown)
1657 ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (proxy->com_object->iunknown);
1658 proxy->com_object->itf_hash = proxy->com_object->iunknown = NULL;
1661 mono_gchandle_free (gchandle);
1668 cominterop_release_all_rcws (void)
1673 mono_cominterop_lock ();
1675 g_hash_table_foreach_remove (rcw_hash, cominterop_rcw_finalizer, NULL);
1676 g_hash_table_destroy (rcw_hash);
1679 mono_cominterop_unlock ();
1683 ves_icall_System_ComObject_GetInterfaceInternal (MonoComObject* obj, MonoReflectionType* type, MonoBoolean throw_exception)
1686 MonoClass *class = mono_type_get_class (type->type);
1687 if (!mono_class_init (class))
1688 mono_raise_exception (mono_class_get_exception_for_failure (class));
1690 return cominterop_get_interface (obj, class, (gboolean)throw_exception);
1692 g_assert_not_reached ();
1697 ves_icall_Mono_Interop_ComInteropProxy_AddProxy (gpointer pUnk, MonoComInteropProxy* proxy)
1700 guint32 gchandle = 0;
1702 mono_cominterop_lock ();
1703 rcw_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
1704 mono_cominterop_unlock ();
1707 gchandle = mono_gchandle_new_weakref ((MonoObject*)proxy, FALSE);
1709 mono_cominterop_lock ();
1710 g_hash_table_insert (rcw_hash, pUnk, GUINT_TO_POINTER (gchandle));
1711 mono_cominterop_unlock ();
1713 g_assert_not_reached ();
1717 MonoComInteropProxy*
1718 ves_icall_Mono_Interop_ComInteropProxy_FindProxy (gpointer pUnk)
1721 MonoComInteropProxy* proxy = NULL;
1722 guint32 gchandle = 0;
1724 mono_cominterop_lock ();
1726 gchandle = GPOINTER_TO_UINT (g_hash_table_lookup (rcw_hash, pUnk));
1727 mono_cominterop_unlock ();
1729 proxy = (MonoComInteropProxy*)mono_gchandle_get_target (gchandle);
1730 /* proxy is null means we need to free up old RCW */
1732 mono_gchandle_free (gchandle);
1733 g_hash_table_remove (rcw_hash, pUnk);
1738 g_assert_not_reached ();
1743 * cominterop_get_ccw_object:
1744 * @ccw_entry: a pointer to the CCWEntry
1745 * @verify: verify ccw_entry is in fact a ccw
1747 * Returns: the corresponding object for the CCW
1750 cominterop_get_ccw_object (MonoCCWInterface* ccw_entry, gboolean verify)
1752 MonoCCW *ccw = NULL;
1754 /* no CCW's exist yet */
1755 if (!ccw_interface_hash)
1759 ccw = g_hash_table_lookup (ccw_interface_hash, ccw_entry);
1762 ccw = ccw_entry->ccw;
1766 return mono_gchandle_get_target (ccw->gc_handle);
1772 cominterop_setup_marshal_context (EmitMarshalContext *m, MonoMethod *method)
1774 MonoMethodSignature *sig, *csig;
1775 sig = mono_method_signature (method);
1776 /* we copy the signature, so that we can modify it */
1777 /* FIXME: which to use? */
1778 csig = mono_metadata_signature_dup_full (method->klass->image, sig);
1779 /* csig = mono_metadata_signature_dup (sig); */
1781 /* STDCALL on windows, CDECL everywhere else to work with XPCOM and MainWin COM */
1783 csig->call_convention = MONO_CALL_STDCALL;
1785 csig->call_convention = MONO_CALL_C;
1790 m->image = method->klass->image;
1798 * cominterop_get_ccw:
1799 * @object: a pointer to the object
1800 * @itf: interface type needed
1802 * Returns: a value indicating if the object is a
1803 * Runtime Callable Wrapper (RCW) for a COM object
1806 cominterop_get_ccw (MonoObject* object, MonoClass* itf)
1809 MonoCCW *ccw = NULL;
1810 MonoCCWInterface* ccw_entry = NULL;
1811 gpointer *vtable = NULL;
1812 static gpointer iunknown[3] = {NULL, NULL, NULL};
1813 static gpointer idispatch[4] = {NULL, NULL, NULL, NULL};
1814 MonoClass* iface = NULL;
1815 MonoClass* klass = NULL;
1816 EmitMarshalContext m;
1818 int method_count = 0;
1819 GList *ccw_list, *ccw_list_item;
1820 MonoCustomAttrInfo *cinfo = NULL;
1825 klass = mono_object_get_class (object);
1827 mono_cominterop_lock ();
1829 ccw_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
1830 if (!ccw_interface_hash)
1831 ccw_interface_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
1833 ccw_list = g_hash_table_lookup (ccw_hash, GINT_TO_POINTER (mono_object_hash (object)));
1834 mono_cominterop_unlock ();
1836 ccw_list_item = ccw_list;
1837 while (ccw_list_item) {
1838 MonoCCW* ccw_iter = ccw_list_item->data;
1839 if (mono_gchandle_get_target (ccw_iter->gc_handle) == object) {
1843 ccw_list_item = g_list_next(ccw_list_item);
1846 if (!iunknown [0]) {
1847 iunknown [0] = cominterop_ccw_queryinterface;
1848 iunknown [1] = cominterop_ccw_addref;
1849 iunknown [2] = cominterop_ccw_release;
1852 if (!idispatch [0]) {
1853 idispatch [0] = cominterop_ccw_get_type_info_count;
1854 idispatch [1] = cominterop_ccw_get_type_info;
1855 idispatch [2] = cominterop_ccw_get_ids_of_names;
1856 idispatch [3] = cominterop_ccw_invoke;
1860 ccw = g_new0 (MonoCCW, 1);
1862 ccw->free_marshaler = 0;
1864 ccw->vtable_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
1866 /* just alloc a weak handle until we are addref'd*/
1867 ccw->gc_handle = mono_gchandle_new_weakref (object, FALSE);
1870 ccw_list = g_list_alloc ();
1871 ccw_list->data = ccw;
1874 ccw_list = g_list_append (ccw_list, ccw);
1875 mono_cominterop_lock ();
1876 g_hash_table_insert (ccw_hash, GINT_TO_POINTER (mono_object_hash (object)), ccw_list);
1877 mono_cominterop_unlock ();
1878 /* register for finalization to clean up ccw */
1879 mono_object_register_finalizer (object);
1882 cinfo = mono_custom_attrs_from_class (itf);
1884 static MonoClass* coclass_attribute = NULL;
1885 if (!coclass_attribute)
1886 coclass_attribute = mono_class_from_name (mono_defaults.corlib, "System.Runtime.InteropServices", "CoClassAttribute");
1887 if (mono_custom_attrs_has_attr (cinfo, coclass_attribute)) {
1888 g_assert(itf->interface_count && itf->interfaces[0]);
1889 itf = itf->interfaces[0];
1892 mono_custom_attrs_free (cinfo);
1896 if (iface == mono_defaults.iunknown_class) {
1899 else if (iface == mono_defaults.idispatch_class) {
1903 method_count += iface->method.count;
1904 start_slot = cominterop_get_com_slot_begin (iface);
1908 ccw_entry = g_hash_table_lookup (ccw->vtable_hash, itf);
1911 int vtable_index = method_count-1+start_slot;
1912 vtable = mono_image_alloc0 (klass->image, sizeof (gpointer)*(method_count+start_slot));
1913 memcpy (vtable, iunknown, sizeof (iunknown));
1914 if (start_slot == 7)
1915 memcpy (vtable+3, idispatch, sizeof (idispatch));
1918 for (i = iface->method.count-1; i >= 0;i--) {
1919 int param_index = 0;
1920 MonoMethodBuilder *mb;
1921 MonoMarshalSpec ** mspecs;
1922 MonoMethod *wrapper_method, *adjust_method;
1923 MonoMethod *method = iface->methods [i];
1924 MonoMethodSignature* sig_adjusted;
1925 MonoMethodSignature* sig = mono_method_signature (method);
1926 gboolean preserve_sig = method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG;
1929 mb = mono_mb_new (iface, method->name, MONO_WRAPPER_NATIVE_TO_MANAGED);
1930 adjust_method = cominterop_get_managed_wrapper_adjusted (method);
1931 sig_adjusted = mono_method_signature (adjust_method);
1933 mspecs = g_new (MonoMarshalSpec*, sig_adjusted->param_count + 1);
1934 mono_method_get_marshal_info (method, mspecs);
1937 /* move managed args up one */
1938 for (param_index = sig->param_count; param_index >= 1; param_index--) {
1939 int mspec_index = param_index+1;
1940 mspecs [mspec_index] = mspecs [param_index];
1942 if (mspecs[mspec_index] == NULL) {
1943 if (sig_adjusted->params[param_index]->type == MONO_TYPE_OBJECT) {
1944 mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
1945 mspecs[mspec_index]->native = MONO_NATIVE_STRUCT;
1947 else if (sig_adjusted->params[param_index]->type == MONO_TYPE_STRING) {
1948 mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
1949 mspecs[mspec_index]->native = MONO_NATIVE_BSTR;
1951 else if (sig_adjusted->params[param_index]->type == MONO_TYPE_CLASS) {
1952 mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
1953 mspecs[mspec_index]->native = MONO_NATIVE_INTERFACE;
1955 else if (sig_adjusted->params[param_index]->type == MONO_TYPE_BOOLEAN) {
1956 mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
1957 mspecs[mspec_index]->native = MONO_NATIVE_VARIANTBOOL;
1960 /* increase SizeParamIndex since we've added a param */
1961 if (sig_adjusted->params[param_index]->type == MONO_TYPE_ARRAY ||
1962 sig_adjusted->params[param_index]->type == MONO_TYPE_SZARRAY)
1963 if (mspecs[mspec_index]->data.array_data.param_num != -1)
1964 mspecs[mspec_index]->data.array_data.param_num++;
1968 /* first arg is IntPtr for interface */
1971 /* move return spec to last param */
1972 if (!preserve_sig && !MONO_TYPE_IS_VOID (sig->ret)) {
1973 if (mspecs [0] == NULL) {
1974 if (sig_adjusted->params[sig_adjusted->param_count-1]->type == MONO_TYPE_OBJECT) {
1975 mspecs[0] = g_new0 (MonoMarshalSpec, 1);
1976 mspecs[0]->native = MONO_NATIVE_STRUCT;
1978 else if (sig_adjusted->params[sig_adjusted->param_count-1]->type == MONO_TYPE_STRING) {
1979 mspecs[0] = g_new0 (MonoMarshalSpec, 1);
1980 mspecs[0]->native = MONO_NATIVE_BSTR;
1982 else if (sig_adjusted->params[sig_adjusted->param_count-1]->type == MONO_TYPE_CLASS) {
1983 mspecs[0] = g_new0 (MonoMarshalSpec, 1);
1984 mspecs[0]->native = MONO_NATIVE_INTERFACE;
1986 else if (sig_adjusted->params[sig_adjusted->param_count-1]->type == MONO_TYPE_BOOLEAN) {
1987 mspecs[0] = g_new0 (MonoMarshalSpec, 1);
1988 mspecs[0]->native = MONO_NATIVE_VARIANTBOOL;
1992 mspecs [sig_adjusted->param_count] = mspecs [0];
1996 /* skip visiblity since we call internal methods */
1997 mb->skip_visibility = TRUE;
1999 cominterop_setup_marshal_context (&m, adjust_method);
2001 mono_marshal_emit_managed_wrapper (mb, sig_adjusted, mspecs, &m, adjust_method, 0);
2002 mono_loader_lock ();
2003 mono_cominterop_lock ();
2004 wrapper_method = mono_mb_create_method (mb, m.csig, m.csig->param_count + 16);
2005 mono_cominterop_unlock ();
2006 mono_loader_unlock ();
2008 vtable [vtable_index--] = mono_compile_method (wrapper_method);
2011 for (param_index = sig_adjusted->param_count; param_index >= 0; param_index--)
2012 if (mspecs [param_index])
2013 mono_metadata_free_marshal_spec (mspecs [param_index]);
2017 ccw_entry = g_new0 (MonoCCWInterface, 1);
2018 ccw_entry->ccw = ccw;
2019 ccw_entry->vtable = vtable;
2020 g_hash_table_insert (ccw->vtable_hash, itf, ccw_entry);
2021 g_hash_table_insert (ccw_interface_hash, ccw_entry, ccw);
2028 mono_marshal_free_ccw_entry (gpointer key, gpointer value, gpointer user_data)
2030 g_hash_table_remove (ccw_interface_hash, value);
2037 * mono_marshal_free_ccw:
2038 * @object: the mono object
2040 * Returns: whether the object had a CCW
2043 mono_marshal_free_ccw (MonoObject* object)
2045 GList *ccw_list, *ccw_list_orig, *ccw_list_item;
2046 /* no ccw's were created */
2047 if (!ccw_hash || g_hash_table_size (ccw_hash) == 0)
2050 /* need to cache orig list address to remove from hash_table if empty */
2051 mono_cominterop_lock ();
2052 ccw_list = ccw_list_orig = g_hash_table_lookup (ccw_hash, GINT_TO_POINTER (mono_object_hash (object)));
2053 mono_cominterop_unlock ();
2058 ccw_list_item = ccw_list;
2059 while (ccw_list_item) {
2060 MonoCCW* ccw_iter = ccw_list_item->data;
2061 MonoObject* handle_target = mono_gchandle_get_target (ccw_iter->gc_handle);
2063 /* Looks like the GC NULLs the weakref handle target before running the
2064 * finalizer. So if we get a NULL target, destroy the CCW as well. */
2065 if (!handle_target || handle_target == object) {
2066 /* remove all interfaces */
2067 g_hash_table_foreach_remove (ccw_iter->vtable_hash, mono_marshal_free_ccw_entry, NULL);
2068 g_hash_table_destroy (ccw_iter->vtable_hash);
2070 /* get next before we delete */
2071 ccw_list_item = g_list_next(ccw_list_item);
2073 /* remove ccw from list */
2074 ccw_list = g_list_remove (ccw_list, ccw_iter);
2078 ccw_list_item = g_list_next(ccw_list_item);
2081 /* if list is empty remove original address from hash */
2082 if (g_list_length (ccw_list) == 0)
2083 g_hash_table_remove (ccw_hash, GINT_TO_POINTER (mono_object_hash (object)));
2090 * cominterop_get_managed_wrapper_adjusted:
2091 * @method: managed COM Interop method
2093 * Returns: the generated method to call with signature matching
2094 * the unmanaged COM Method signature
2097 cominterop_get_managed_wrapper_adjusted (MonoMethod *method)
2099 static MonoMethod *get_hr_for_exception = NULL;
2100 MonoMethod *res = NULL;
2101 MonoMethodBuilder *mb;
2102 MonoMarshalSpec **mspecs;
2103 MonoMethodSignature *sig, *sig_native;
2104 MonoExceptionClause *main_clause = NULL;
2108 gboolean preserve_sig = method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG;
2110 if (!get_hr_for_exception)
2111 get_hr_for_exception = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetHRForException", -1);
2113 sig = mono_method_signature (method);
2115 /* create unmanaged wrapper */
2116 mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_COMINTEROP);
2118 sig_native = cominterop_method_signature (method);
2120 mspecs = g_new0 (MonoMarshalSpec*, sig_native->param_count+1);
2122 mono_method_get_marshal_info (method, mspecs);
2124 /* move managed args up one */
2125 for (i = sig->param_count; i >= 1; i--)
2126 mspecs [i+1] = mspecs [i];
2128 /* first arg is IntPtr for interface */
2131 /* move return spec to last param */
2132 if (!preserve_sig && !MONO_TYPE_IS_VOID (sig->ret))
2133 mspecs [sig_native->param_count] = mspecs [0];
2137 if (!preserve_sig) {
2138 hr = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
2140 else if (!MONO_TYPE_IS_VOID (sig->ret))
2141 hr = mono_mb_add_local (mb, sig->ret);
2144 main_clause = g_new0 (MonoExceptionClause, 1);
2145 main_clause->try_offset = mono_mb_get_label (mb);
2147 /* load last param to store result if not preserve_sig and not void */
2148 if (!preserve_sig && !MONO_TYPE_IS_VOID (sig->ret))
2149 mono_mb_emit_ldarg (mb, sig_native->param_count-1);
2151 /* the CCW -> object conversion */
2152 mono_mb_emit_ldarg (mb, 0);
2153 mono_mb_emit_icon (mb, FALSE);
2154 mono_mb_emit_icall (mb, cominterop_get_ccw_object);
2156 for (i = 0; i < sig->param_count; i++)
2157 mono_mb_emit_ldarg (mb, i+1);
2159 mono_mb_emit_managed_call (mb, method, NULL);
2161 if (!MONO_TYPE_IS_VOID (sig->ret)) {
2162 if (!preserve_sig) {
2163 MonoClass *rclass = mono_class_from_mono_type (sig->ret);
2164 if (rclass->valuetype) {
2165 mono_mb_emit_op (mb, CEE_STOBJ, rclass);
2167 mono_mb_emit_byte (mb, mono_type_to_stind (sig->ret));
2170 mono_mb_emit_stloc (mb, hr);
2173 pos_leave = mono_mb_emit_branch (mb, CEE_LEAVE);
2175 /* Main exception catch */
2176 main_clause->flags = MONO_EXCEPTION_CLAUSE_NONE;
2177 main_clause->try_len = mono_mb_get_pos (mb) - main_clause->try_offset;
2178 main_clause->data.catch_class = mono_defaults.object_class;
2181 main_clause->handler_offset = mono_mb_get_label (mb);
2183 if (!preserve_sig || (sig->ret && !sig->ret->byref && (sig->ret->type == MONO_TYPE_U4 || sig->ret->type == MONO_TYPE_I4))) {
2184 mono_mb_emit_managed_call (mb, get_hr_for_exception, NULL);
2185 mono_mb_emit_stloc (mb, hr);
2188 mono_mb_emit_byte (mb, CEE_POP);
2191 mono_mb_emit_branch (mb, CEE_LEAVE);
2192 main_clause->handler_len = mono_mb_get_pos (mb) - main_clause->handler_offset;
2195 mono_mb_set_clauses (mb, 1, main_clause);
2197 mono_mb_patch_branch (mb, pos_leave);
2199 if (!preserve_sig || !MONO_TYPE_IS_VOID (sig->ret))
2200 mono_mb_emit_ldloc (mb, hr);
2202 mono_mb_emit_byte (mb, CEE_RET);
2204 mono_loader_lock ();
2205 mono_cominterop_lock ();
2206 res = mono_mb_create_method (mb, sig_native, sig_native->param_count + 16);
2207 mono_cominterop_unlock ();
2208 mono_loader_unlock ();
2212 for (i = sig_native->param_count; i >= 0; i--)
2214 mono_metadata_free_marshal_spec (mspecs [i]);
2221 * cominterop_mono_string_to_guid:
2223 * Converts the standard string representation of a GUID
2224 * to a 16 byte Microsoft GUID.
2227 cominterop_mono_string_to_guid (MonoString* string, guint8 *guid) {
2228 gunichar2 * chars = mono_string_chars (string);
2230 static guint8 indexes[16] = {7, 5, 3, 1, 12, 10, 17, 15, 20, 22, 25, 27, 29, 31, 33, 35};
2232 for (i = 0; i < sizeof(indexes); i++)
2233 guid [i] = g_unichar_xdigit_value (chars [indexes [i]]) + (g_unichar_xdigit_value (chars [indexes [i] - 1]) << 4);
2237 cominterop_class_guid_equal (guint8* guid, MonoClass* klass)
2239 guint8 klass_guid [16];
2240 if (cominterop_class_guid (klass, klass_guid))
2241 return !memcmp (guid, klass_guid, sizeof (klass_guid));
2246 cominterop_ccw_addref (MonoCCWInterface* ccwe)
2248 gint32 ref_count = 0;
2249 MonoCCW* ccw = ccwe->ccw;
2251 g_assert (ccw->gc_handle);
2252 g_assert (ccw->ref_count >= 0);
2253 ref_count = InterlockedIncrement ((gint32*)&ccw->ref_count);
2254 if (ref_count == 1) {
2255 guint32 oldhandle = ccw->gc_handle;
2256 g_assert (oldhandle);
2257 /* since we now have a ref count, alloc a strong handle*/
2258 ccw->gc_handle = mono_gchandle_new (mono_gchandle_get_target (oldhandle), FALSE);
2259 mono_gchandle_free (oldhandle);
2265 cominterop_ccw_release (MonoCCWInterface* ccwe)
2267 gint32 ref_count = 0;
2268 MonoCCW* ccw = ccwe->ccw;
2270 g_assert (ccw->ref_count > 0);
2271 ref_count = InterlockedDecrement ((gint32*)&ccw->ref_count);
2272 if (ref_count == 0) {
2273 /* allow gc of object */
2274 guint32 oldhandle = ccw->gc_handle;
2275 g_assert (oldhandle);
2277 if (ccw->free_marshaler)
2278 ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (ccw->free_marshaler);
2280 ccw->gc_handle = mono_gchandle_new_weakref (mono_gchandle_get_target (oldhandle), FALSE);
2281 mono_gchandle_free (oldhandle);
2287 static const IID MONO_IID_IMarshal = {0x3, 0x0, 0x0, {0xC0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x46}};
2291 /* All ccw objects are free threaded */
2293 cominterop_ccw_getfreethreadedmarshaler (MonoCCW* ccw, MonoObject* object, gpointer* ppv)
2296 if (!ccw->free_marshaler) {
2299 tunk = cominterop_get_ccw (object, mono_defaults.iunknown_class);
2300 /* remember to addref on QI */
2301 cominterop_ccw_addref (tunk);
2302 ret = CoCreateFreeThreadedMarshaler (tunk, (LPUNKNOWN*)&ccw->free_marshaler);
2303 cominterop_ccw_release(tunk);
2306 if (!ccw->free_marshaler)
2307 return MONO_E_NOINTERFACE;
2309 return ves_icall_System_Runtime_InteropServices_Marshal_QueryInterfaceInternal (ccw->free_marshaler, (IID*)&MONO_IID_IMarshal, ppv);
2311 return MONO_E_NOINTERFACE;
2317 cominterop_ccw_queryinterface (MonoCCWInterface* ccwe, guint8* riid, gpointer* ppv)
2321 MonoClass *itf = NULL;
2323 MonoCCW* ccw = ccwe->ccw;
2324 MonoClass* klass = NULL;
2325 MonoClass* klass_iter = NULL;
2326 MonoObject* object = mono_gchandle_get_target (ccw->gc_handle);
2329 klass = mono_object_class (object);
2334 if (!mono_domain_get ())
2335 mono_thread_attach (mono_get_root_domain ());
2337 /* handle IUnknown special */
2338 if (cominterop_class_guid_equal (riid, mono_defaults.iunknown_class)) {
2339 *ppv = cominterop_get_ccw (object, mono_defaults.iunknown_class);
2340 /* remember to addref on QI */
2341 cominterop_ccw_addref (*ppv);
2345 /* handle IDispatch special */
2346 if (cominterop_class_guid_equal (riid, mono_defaults.idispatch_class)) {
2347 if (!cominterop_can_support_dispatch (klass))
2348 return MONO_E_NOINTERFACE;
2350 *ppv = cominterop_get_ccw (object, mono_defaults.idispatch_class);
2351 /* remember to addref on QI */
2352 cominterop_ccw_addref (*ppv);
2357 /* handle IMarshal special */
2358 if (0 == memcmp (riid, &MONO_IID_IMarshal, sizeof (IID))) {
2359 return cominterop_ccw_getfreethreadedmarshaler (ccw, object, ppv);
2363 while (klass_iter && klass_iter != mono_defaults.object_class) {
2364 ifaces = mono_class_get_implemented_interfaces (klass_iter, &error);
2365 g_assert (mono_error_ok (&error));
2367 for (i = 0; i < ifaces->len; ++i) {
2368 MonoClass *ic = NULL;
2369 ic = g_ptr_array_index (ifaces, i);
2370 if (cominterop_class_guid_equal (riid, ic)) {
2375 g_ptr_array_free (ifaces, TRUE);
2381 klass_iter = klass_iter->parent;
2384 *ppv = cominterop_get_ccw (object, itf);
2385 /* remember to addref on QI */
2386 cominterop_ccw_addref (*ppv);
2390 return MONO_E_NOINTERFACE;
2394 cominterop_ccw_get_type_info_count (MonoCCWInterface* ccwe, guint32 *pctinfo)
2396 return MONO_E_NOTIMPL;
2400 cominterop_ccw_get_type_info (MonoCCWInterface* ccwe, guint32 iTInfo, guint32 lcid, gpointer *ppTInfo)
2402 return MONO_E_NOTIMPL;
2406 cominterop_ccw_get_ids_of_names (MonoCCWInterface* ccwe, gpointer riid,
2407 gunichar2** rgszNames, guint32 cNames,
2408 guint32 lcid, gint32 *rgDispId)
2410 return MONO_E_NOTIMPL;
2414 cominterop_ccw_invoke (MonoCCWInterface* ccwe, guint32 dispIdMember,
2415 gpointer riid, guint32 lcid,
2416 guint16 wFlags, gpointer pDispParams,
2417 gpointer pVarResult, gpointer pExcepInfo,
2420 return MONO_E_NOTIMPL;
2423 typedef gpointer (STDCALL *SysAllocStringLenFunc)(gunichar* str, guint32 len);
2424 typedef guint32 (STDCALL *SysStringLenFunc)(gpointer bstr);
2425 typedef void (STDCALL *SysFreeStringFunc)(gunichar* str);
2427 static SysAllocStringLenFunc sys_alloc_string_len_ms = NULL;
2428 static SysStringLenFunc sys_string_len_ms = NULL;
2429 static SysFreeStringFunc sys_free_string_ms = NULL;
2433 typedef struct tagSAFEARRAYBOUND {
2436 }SAFEARRAYBOUND,*LPSAFEARRAYBOUND;
2437 #define VT_VARIANT 12
2441 typedef guint32 (STDCALL *SafeArrayGetDimFunc)(gpointer psa);
2442 typedef int (STDCALL *SafeArrayGetLBoundFunc)(gpointer psa, guint32 nDim, glong* plLbound);
2443 typedef int (STDCALL *SafeArrayGetUBoundFunc)(gpointer psa, guint32 nDim, glong* plUbound);
2444 typedef int (STDCALL *SafeArrayPtrOfIndexFunc)(gpointer psa, glong* rgIndices, gpointer* ppvData);
2445 typedef int (STDCALL *SafeArrayDestroyFunc)(gpointer psa);
2446 typedef int (STDCALL *SafeArrayPutElementFunc)(gpointer psa, glong* rgIndices, gpointer* ppvData);
2447 typedef gpointer (STDCALL *SafeArrayCreateFunc)(int vt, guint32 cDims, SAFEARRAYBOUND* rgsabound);
2449 static SafeArrayGetDimFunc safe_array_get_dim_ms = NULL;
2450 static SafeArrayGetLBoundFunc safe_array_get_lbound_ms = NULL;
2451 static SafeArrayGetUBoundFunc safe_array_get_ubound_ms = NULL;
2452 static SafeArrayPtrOfIndexFunc safe_array_ptr_of_index_ms = NULL;
2453 static SafeArrayDestroyFunc safe_array_destroy_ms = NULL;
2454 static SafeArrayPutElementFunc safe_array_put_element_ms = NULL;
2455 static SafeArrayCreateFunc safe_array_create_ms = NULL;
2458 init_com_provider_ms (void)
2460 static gboolean initialized = FALSE;
2462 MonoDl *module = NULL;
2463 const char* scope = "liboleaut32.so";
2468 module = mono_dl_open(scope, MONO_DL_LAZY, &error_msg);
2470 g_warning ("Error loading COM support library '%s': %s", scope, error_msg);
2471 g_assert_not_reached ();
2474 error_msg = mono_dl_symbol (module, "SysAllocStringLen", (gpointer*)&sys_alloc_string_len_ms);
2476 g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SysAllocStringLen", scope, error_msg);
2477 g_assert_not_reached ();
2481 error_msg = mono_dl_symbol (module, "SysStringLen", (gpointer*)&sys_string_len_ms);
2483 g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SysStringLen", scope, error_msg);
2484 g_assert_not_reached ();
2488 error_msg = mono_dl_symbol (module, "SysFreeString", (gpointer*)&sys_free_string_ms);
2490 g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SysFreeString", scope, error_msg);
2491 g_assert_not_reached ();
2495 error_msg = mono_dl_symbol (module, "SafeArrayGetDim", (gpointer*)&safe_array_get_dim_ms);
2497 g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SafeArrayGetDim", scope, error_msg);
2498 g_assert_not_reached ();
2502 error_msg = mono_dl_symbol (module, "SafeArrayGetLBound", (gpointer*)&safe_array_get_lbound_ms);
2504 g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SafeArrayGetLBound", scope, error_msg);
2505 g_assert_not_reached ();
2509 error_msg = mono_dl_symbol (module, "SafeArrayGetUBound", (gpointer*)&safe_array_get_ubound_ms);
2511 g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SafeArrayGetUBound", scope, error_msg);
2512 g_assert_not_reached ();
2516 error_msg = mono_dl_symbol (module, "SafeArrayPtrOfIndex", (gpointer*)&safe_array_ptr_of_index_ms);
2518 g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SafeArrayPtrOfIndex", scope, error_msg);
2519 g_assert_not_reached ();
2523 error_msg = mono_dl_symbol (module, "SafeArrayDestroy", (gpointer*)&safe_array_destroy_ms);
2525 g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SafeArrayDestroy", scope, error_msg);
2526 g_assert_not_reached ();
2530 error_msg = mono_dl_symbol (module, "SafeArrayPutElement", (gpointer*)&safe_array_put_element_ms);
2532 g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SafeArrayPutElement", scope, error_msg);
2533 g_assert_not_reached ();
2537 error_msg = mono_dl_symbol (module, "SafeArrayCreate", (gpointer*)&safe_array_create_ms);
2539 g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SafeArrayCreate", scope, error_msg);
2540 g_assert_not_reached ();
2549 mono_string_to_bstr (MonoString *string_obj)
2554 return SysAllocStringLen (mono_string_chars (string_obj), mono_string_length (string_obj));
2556 if (com_provider == MONO_COM_DEFAULT) {
2557 int slen = mono_string_length (string_obj);
2558 /* allocate len + 1 utf16 characters plus 4 byte integer for length*/
2559 char *ret = g_malloc ((slen + 1) * sizeof(gunichar2) + sizeof(guint32));
2562 memcpy (ret + sizeof(guint32), mono_string_chars (string_obj), slen * sizeof(gunichar2));
2563 * ((guint32 *) ret) = slen * sizeof(gunichar2);
2564 ret [4 + slen * sizeof(gunichar2)] = 0;
2565 ret [5 + slen * sizeof(gunichar2)] = 0;
2568 } else if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
2569 gpointer ret = NULL;
2570 gunichar* str = NULL;
2572 len = mono_string_length (string_obj);
2573 str = g_utf16_to_ucs4 (mono_string_chars (string_obj), len,
2575 ret = sys_alloc_string_len_ms (str, len);
2579 g_assert_not_reached ();
2585 mono_string_from_bstr (gpointer bstr)
2590 return mono_string_new_utf16 (mono_domain_get (), bstr, SysStringLen (bstr));
2592 if (com_provider == MONO_COM_DEFAULT) {
2593 return mono_string_new_utf16 (mono_domain_get (), bstr, *((guint32 *)bstr - 1) / sizeof(gunichar2));
2594 } else if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
2595 MonoString* str = NULL;
2597 gunichar2* utf16 = NULL;
2599 utf16 = g_ucs4_to_utf16 (bstr, sys_string_len_ms (bstr), NULL, &written, NULL);
2600 str = mono_string_new_utf16 (mono_domain_get (), utf16, written);
2604 g_assert_not_reached ();
2611 mono_free_bstr (gpointer bstr)
2616 SysFreeString ((BSTR)bstr);
2618 if (com_provider == MONO_COM_DEFAULT) {
2619 g_free (((char *)bstr) - 4);
2620 } else if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
2621 sys_free_string_ms (bstr);
2623 g_assert_not_reached ();
2630 /* SAFEARRAY marshalling */
2632 mono_cominterop_emit_marshal_safearray (EmitMarshalContext *m, int argnum, MonoType *t,
2633 MonoMarshalSpec *spec,
2634 int conv_arg, MonoType **conv_arg_type,
2635 MarshalAction action)
2637 MonoMethodBuilder *mb = m->mb;
2639 mono_init_com_types ();
2643 case MARSHAL_ACTION_CONV_IN: {
2645 if (t->attrs & PARAM_ATTRIBUTE_IN) {
2647 /* Generates IL code for the following algorithm:
2649 SafeArray safearray; // safearray_var
2650 IntPtr indices; // indices_var
2651 int empty; // empty_var
2652 if (mono_marshal_safearray_create (array, out safearray, out indices, out empty)) {
2654 int index=0; // index_var
2656 variant elem = Marshal.GetNativeVariantForObject (array.GetValueImpl(index));
2657 mono_marshal_safearray_set_value (safearray, indices, elem);
2660 while (mono_marshal_safearray_next (safearray, indices));
2662 mono_marshal_safearray_free_indices (indices);
2666 int safearray_var, indices_var, empty_var, elem_var, index_var;
2667 guint32 label1 = 0, label2 = 0, label3 = 0;
2668 static MonoMethod *get_native_variant_for_object = NULL;
2669 static MonoMethod *get_value_impl = NULL;
2670 static MonoMethod *variant_clear = NULL;
2672 conv_arg = safearray_var = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
2673 indices_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
2674 empty_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
2677 mono_mb_emit_ldarg (mb, argnum);
2678 mono_mb_emit_byte (mb, CEE_LDIND_REF);
2680 mono_mb_emit_ldarg (mb, argnum);
2682 mono_mb_emit_ldloc_addr (mb, safearray_var);
2683 mono_mb_emit_ldloc_addr (mb, indices_var);
2684 mono_mb_emit_ldloc_addr (mb, empty_var);
2685 mono_mb_emit_icall (mb, mono_marshal_safearray_create);
2687 label1 = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
2689 mono_mb_emit_ldloc (mb, empty_var);
2691 label2 = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);
2693 index_var = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
2694 mono_mb_emit_byte (mb, CEE_LDC_I4_0);
2695 mono_mb_emit_stloc (mb, index_var);
2697 label3 = mono_mb_get_label (mb);
2699 if (!get_value_impl)
2700 get_value_impl = mono_class_get_method_from_name (mono_defaults.array_class, "GetValueImpl", 1);
2701 g_assert (get_value_impl);
2704 mono_mb_emit_ldarg (mb, argnum);
2705 mono_mb_emit_byte (mb, CEE_LDIND_REF);
2707 mono_mb_emit_ldarg (mb, argnum);
2709 mono_mb_emit_ldloc (mb, index_var);
2711 mono_mb_emit_managed_call (mb, get_value_impl, NULL);
2713 if (!get_native_variant_for_object)
2714 get_native_variant_for_object = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetNativeVariantForObject", 2);
2715 g_assert (get_native_variant_for_object);
2717 elem_var = mono_mb_add_local (mb, &mono_defaults.variant_class->byval_arg);
2718 mono_mb_emit_ldloc_addr (mb, elem_var);
2720 mono_mb_emit_managed_call (mb, get_native_variant_for_object, NULL);
2722 mono_mb_emit_ldloc (mb, safearray_var);
2723 mono_mb_emit_ldloc (mb, indices_var);
2724 mono_mb_emit_ldloc_addr (mb, elem_var);
2725 mono_mb_emit_icall (mb, mono_marshal_safearray_set_value);
2728 variant_clear = mono_class_get_method_from_name (mono_defaults.variant_class, "Clear", 0);
2730 mono_mb_emit_ldloc_addr (mb, elem_var);
2731 mono_mb_emit_managed_call (mb, variant_clear, NULL);
2733 mono_mb_emit_add_to_local (mb, index_var, 1);
2735 mono_mb_emit_ldloc (mb, safearray_var);
2736 mono_mb_emit_ldloc (mb, indices_var);
2737 mono_mb_emit_icall (mb, mono_marshal_safearray_next);
2738 mono_mb_emit_branch_label (mb, CEE_BRTRUE, label3);
2740 mono_mb_patch_short_branch (mb, label2);
2742 mono_mb_emit_ldloc (mb, indices_var);
2743 mono_mb_emit_icall (mb, mono_marshal_safearray_free_indices);
2745 mono_mb_patch_short_branch (mb, label1);
2750 case MARSHAL_ACTION_PUSH:
2752 mono_mb_emit_ldloc_addr (mb, conv_arg);
2754 mono_mb_emit_ldloc (mb, conv_arg);
2757 case MARSHAL_ACTION_CONV_OUT: {
2759 if (t->attrs & PARAM_ATTRIBUTE_OUT) {
2760 /* Generates IL code for the following algorithm:
2762 Array result; // result_var
2763 IntPtr indices; // indices_var
2764 int empty; // empty_var
2765 bool byValue = !t->byref && (t->attrs & PARAM_ATTRIBUTE_IN);
2766 if (mono_marshal_safearray_begin(safearray, out result, out indices, out empty, parameter, byValue)) {
2768 int index=0; // index_var
2770 if (!byValue || (index < parameter.Length)) {
2771 object elem = Variant.GetObjectForNativeVariant(mono_marshal_safearray_get_value(safearray, indices));
2772 result.SetValueImpl(elem, index);
2776 while (mono_marshal_safearray_next(safearray, indices));
2778 mono_marshal_safearray_end(safearray, indices);
2784 int result_var, indices_var, empty_var, elem_var, index_var;
2785 guint32 label1 = 0, label2 = 0, label3 = 0, label4 = 0;
2786 static MonoMethod *get_object_for_native_variant = NULL;
2787 static MonoMethod *set_value_impl = NULL;
2788 gboolean byValue = !t->byref && (t->attrs & PARAM_ATTRIBUTE_IN);
2790 result_var = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
2791 indices_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
2792 empty_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
2794 mono_mb_emit_ldloc (mb, conv_arg);
2795 mono_mb_emit_ldloc_addr (mb, result_var);
2796 mono_mb_emit_ldloc_addr (mb, indices_var);
2797 mono_mb_emit_ldloc_addr (mb, empty_var);
2798 mono_mb_emit_ldarg (mb, argnum);
2800 mono_mb_emit_byte (mb, CEE_LDC_I4_0);
2802 mono_mb_emit_byte (mb, CEE_LDC_I4_1);
2803 mono_mb_emit_icall (mb, mono_marshal_safearray_begin);
2805 label1 = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
2807 mono_mb_emit_ldloc (mb, empty_var);
2809 label2 = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);
2811 index_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
2812 mono_mb_emit_byte (mb, CEE_LDC_I4_0);
2813 mono_mb_emit_stloc (mb, index_var);
2815 label3 = mono_mb_get_label (mb);
2818 mono_mb_emit_ldloc (mb, index_var);
2819 mono_mb_emit_ldarg (mb, argnum);
2820 mono_mb_emit_byte (mb, CEE_LDLEN);
2821 label4 = mono_mb_emit_branch (mb, CEE_BGE);
2824 mono_mb_emit_ldloc (mb, conv_arg);
2825 mono_mb_emit_ldloc (mb, indices_var);
2826 mono_mb_emit_icall (mb, mono_marshal_safearray_get_value);
2828 if (!get_object_for_native_variant)
2829 get_object_for_native_variant = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetObjectForNativeVariant", 1);
2830 g_assert (get_object_for_native_variant);
2832 if (!set_value_impl)
2833 set_value_impl = mono_class_get_method_from_name (mono_defaults.array_class, "SetValueImpl", 2);
2834 g_assert (set_value_impl);
2836 elem_var = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
2838 mono_mb_emit_managed_call (mb, get_object_for_native_variant, NULL);
2839 mono_mb_emit_stloc (mb, elem_var);
2841 mono_mb_emit_ldloc (mb, result_var);
2842 mono_mb_emit_ldloc (mb, elem_var);
2843 mono_mb_emit_ldloc (mb, index_var);
2844 mono_mb_emit_managed_call (mb, set_value_impl, NULL);
2847 mono_mb_patch_short_branch (mb, label4);
2849 mono_mb_emit_add_to_local (mb, index_var, 1);
2851 mono_mb_emit_ldloc (mb, conv_arg);
2852 mono_mb_emit_ldloc (mb, indices_var);
2853 mono_mb_emit_icall (mb, mono_marshal_safearray_next);
2854 mono_mb_emit_branch_label (mb, CEE_BRTRUE, label3);
2856 mono_mb_patch_short_branch (mb, label2);
2858 mono_mb_emit_ldloc (mb, conv_arg);
2859 mono_mb_emit_ldloc (mb, indices_var);
2860 mono_mb_emit_icall (mb, mono_marshal_safearray_end);
2862 mono_mb_patch_short_branch (mb, label1);
2865 mono_mb_emit_ldarg (mb, argnum);
2866 mono_mb_emit_ldloc (mb, result_var);
2867 mono_mb_emit_byte (mb, CEE_STIND_REF);
2874 g_assert_not_reached ();
2881 guint32 mono_marshal_safearray_get_dim (gpointer safearray)
2885 result = SafeArrayGetDim (safearray);
2887 if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
2888 result = safe_array_get_dim_ms (safearray);
2890 g_assert_not_reached ();
2897 int mono_marshal_safe_array_get_lbound (gpointer psa, guint nDim, glong* plLbound)
2899 int result=MONO_S_OK;
2901 result = SafeArrayGetLBound (psa, nDim, plLbound);
2903 if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
2904 result = safe_array_get_lbound_ms (psa, nDim, plLbound);
2906 g_assert_not_reached ();
2913 int mono_marshal_safe_array_get_ubound (gpointer psa, guint nDim, glong* plUbound)
2915 int result=MONO_S_OK;
2917 result = SafeArrayGetUBound (psa, nDim, plUbound);
2919 if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
2920 result = safe_array_get_ubound_ms (psa, nDim, plUbound);
2922 g_assert_not_reached ();
2929 mono_marshal_safearray_begin (gpointer safearray, MonoArray **result, gpointer *indices, gpointer empty, gpointer parameter, gboolean allocateNewArray)
2936 gboolean bounded = FALSE;
2939 // If not on windows, check that the MS provider is used as it is
2940 // required for SAFEARRAY support.
2941 // If SAFEARRAYs are not supported, returning FALSE from this
2942 // function will prevent the other mono_marshal_safearray_xxx functions
2943 // from being called.
2944 if ((com_provider != MONO_COM_MS) || !init_com_provider_ms ()) {
2949 (*(int*)empty) = TRUE;
2951 if (safearray != NULL) {
2953 dim = mono_marshal_safearray_get_dim (safearray);
2957 *indices = g_malloc (dim * sizeof(int));
2959 sizes = alloca (dim * sizeof(uintptr_t));
2960 bounds = alloca (dim * sizeof(intptr_t));
2962 for (i=0; i<dim; ++i) {
2963 glong lbound, ubound;
2967 hr = mono_marshal_safe_array_get_lbound (safearray, i+1, &lbound);
2969 cominterop_raise_hr_exception (hr);
2973 hr = mono_marshal_safe_array_get_ubound (safearray, i+1, &ubound);
2975 cominterop_raise_hr_exception (hr);
2977 cursize = ubound-lbound+1;
2978 sizes [i] = cursize;
2979 bounds [i] = lbound;
2981 ((int*)*indices) [i] = lbound;
2984 (*(int*)empty) = FALSE;
2987 if (allocateNewArray) {
2988 aklass = mono_bounded_array_class_get (mono_defaults.object_class, dim, bounded);
2989 *result = mono_array_new_full (mono_domain_get (), aklass, sizes, bounds);
2991 *result = parameter;
2999 gpointer mono_marshal_safearray_get_value (gpointer safearray, gpointer indices)
3003 int hr = SafeArrayPtrOfIndex (safearray, indices, &result);
3005 cominterop_raise_hr_exception (hr);
3008 if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
3009 int hr = safe_array_ptr_of_index_ms (safearray, indices, &result);
3011 cominterop_raise_hr_exception (hr);
3014 g_assert_not_reached ();
3021 gboolean mono_marshal_safearray_next (gpointer safearray, gpointer indices)
3024 int dim = mono_marshal_safearray_get_dim (safearray);
3026 int *pIndices = (int*) indices;
3029 for (i=dim-1; i>=0; --i)
3031 glong lbound, ubound;
3033 hr = mono_marshal_safe_array_get_ubound (safearray, i+1, &ubound);
3035 cominterop_raise_hr_exception (hr);
3038 if (++pIndices[i] <= ubound) {
3042 hr = mono_marshal_safe_array_get_lbound (safearray, i+1, &lbound);
3044 cominterop_raise_hr_exception (hr);
3047 pIndices[i] = lbound;
3056 void mono_marshal_safearray_end (gpointer safearray, gpointer indices)
3060 SafeArrayDestroy (safearray);
3062 if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
3063 safe_array_destroy_ms (safearray);
3065 g_assert_not_reached ();
3071 mono_marshal_safearray_create (MonoArray *input, gpointer *newsafearray, gpointer *indices, gpointer empty)
3074 SAFEARRAYBOUND *bounds;
3076 int max_array_length;
3079 // If not on windows, check that the MS provider is used as it is
3080 // required for SAFEARRAY support.
3081 // If SAFEARRAYs are not supported, returning FALSE from this
3082 // function will prevent the other mono_marshal_safearray_xxx functions
3083 // from being called.
3084 if ((com_provider != MONO_COM_MS) || !init_com_provider_ms ()) {
3089 max_array_length = mono_array_length (input);
3090 dim = ((MonoObject *)input)->vtable->klass->rank;
3092 *indices = g_malloc (dim * sizeof (int));
3093 bounds = alloca (dim * sizeof (SAFEARRAYBOUND));
3094 (*(int*)empty) = (max_array_length == 0);
3097 for (i=0; i<dim; ++i) {
3098 ((int*)*indices) [i] = bounds [i].lLbound = input->bounds [i].lower_bound;
3099 bounds [i].cElements = input->bounds [i].length;
3102 ((int*)*indices) [0] = 0;
3103 bounds [0].cElements = max_array_length;
3104 bounds [0].lLbound = 0;
3108 *newsafearray = SafeArrayCreate (VT_VARIANT, dim, bounds);
3110 *newsafearray = safe_array_create_ms (VT_VARIANT, dim, bounds);
3117 void mono_marshal_safearray_set_value (gpointer safearray, gpointer indices, gpointer value)
3120 int hr = SafeArrayPutElement (safearray, indices, value);
3122 cominterop_raise_hr_exception (hr);
3124 if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
3125 int hr = safe_array_put_element_ms (safearray, indices, value);
3127 cominterop_raise_hr_exception (hr);
3130 g_assert_not_reached ();
3135 void mono_marshal_safearray_free_indices (gpointer indices)
3140 #else /* DISABLE_COM */
3143 mono_cominterop_init (void)
3148 mono_cominterop_cleanup (void)
3153 cominterop_release_all_rcws (void)
3158 mono_marshal_free_ccw (MonoObject* object)
3164 mono_string_to_bstr (MonoString *string_obj)
3169 return SysAllocStringLen (mono_string_chars (string_obj), mono_string_length (string_obj));
3172 int slen = mono_string_length (string_obj);
3173 /* allocate len + 1 utf16 characters plus 4 byte integer for length*/
3174 char *ret = g_malloc ((slen + 1) * sizeof(gunichar2) + sizeof(guint32));
3177 memcpy (ret + sizeof(guint32), mono_string_chars (string_obj), slen * sizeof(gunichar2));
3178 * ((guint32 *) ret) = slen * sizeof(gunichar2);
3179 ret [4 + slen * sizeof(gunichar2)] = 0;
3180 ret [5 + slen * sizeof(gunichar2)] = 0;
3188 mono_string_from_bstr (gpointer bstr)
3193 return mono_string_new_utf16 (mono_domain_get (), bstr, SysStringLen (bstr));
3195 return mono_string_new_utf16 (mono_domain_get (), bstr, *((guint32 *)bstr - 1) / sizeof(gunichar2));
3200 mono_free_bstr (gpointer bstr)
3205 SysFreeString ((BSTR)bstr);
3207 g_free (((char *)bstr) - 4);
3212 ves_icall_System_Runtime_InteropServices_Marshal_AddRefInternal (gpointer pUnk)
3214 g_assert_not_reached ();
3219 ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (gpointer pUnk)
3221 g_assert_not_reached ();
3226 ves_icall_System_Runtime_InteropServices_Marshal_QueryInterfaceInternal (gpointer pUnk, gpointer riid, gpointer* ppv)
3228 g_assert_not_reached ();
3232 #endif /* DISABLE_COM */
3235 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringBSTR (gpointer ptr)
3237 MONO_ARCH_SAVE_REGS;
3239 return mono_string_from_bstr(ptr);
3243 ves_icall_System_Runtime_InteropServices_Marshal_StringToBSTR (MonoString* ptr)
3245 MONO_ARCH_SAVE_REGS;
3247 return mono_string_to_bstr(ptr);
3251 ves_icall_System_Runtime_InteropServices_Marshal_FreeBSTR (gpointer ptr)
3253 MONO_ARCH_SAVE_REGS;
3255 mono_free_bstr (ptr);