Merge pull request #4621 from alexanderkyte/strdup_env
[mono.git] / mono / metadata / cominterop.c
index 3ebe507d0d185472c7aada8bed2713c8b11847df..86309ef20234d4dd559726f0c9fcfe1a32f08e32 100644 (file)
@@ -1,5 +1,6 @@
-/*
- * cominterop.c: COM Interop Support
+/**
+ * \file
+ * COM Interop Support
  * 
  *
  * (C) 2002 Ximian, Inc.  http://www.ximian.com
@@ -7,6 +8,7 @@
  */
 
 #include "config.h"
+#include <glib.h>
 #ifdef HAVE_ALLOCA_H
 #include <alloca.h>
 #endif
@@ -31,7 +33,6 @@
 #include "mono/metadata/threads-types.h"
 #include "mono/metadata/string-icalls.h"
 #include "mono/metadata/attrdefs.h"
-#include "mono/metadata/gc-internals.h"
 #include "mono/utils/mono-counters.h"
 #include "mono/utils/strenc.h"
 #include "mono/utils/atomic.h"
 #include "mono/utils/mono-error-internals.h"
 #include <string.h>
 #include <errno.h>
+#include <mono/utils/w32api.h>
+
+#if defined(HOST_WIN32)
+#include <oleauto.h>
+#include "mono/metadata/cominterop-win32-internals.h"
+#endif
 
 /*
 Code shared between the DISABLE_COM and !DISABLE_COM
@@ -51,6 +58,15 @@ register_icall (gpointer func, const char *name, const char *sigstr, gboolean sa
        mono_register_jit_icall (func, name, sig, save);
 }
 
+gpointer
+mono_string_to_bstr(MonoString* ptr)
+{
+       if (!ptr)
+               return NULL;
+
+       return mono_ptr_to_bstr(mono_string_chars(ptr), mono_string_length(ptr));
+}
+
 #ifndef DISABLE_COM
 
 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
@@ -88,15 +104,15 @@ static mono_mutex_t cominterop_mutex;
 #define STDCALL
 #endif
 
-GENERATE_GET_CLASS_WITH_CACHE (interop_proxy, Mono.Interop, ComInteropProxy)
-GENERATE_GET_CLASS_WITH_CACHE (idispatch,     Mono.Interop, IDispatch)
-GENERATE_GET_CLASS_WITH_CACHE (iunknown,      Mono.Interop, IUnknown)
+GENERATE_GET_CLASS_WITH_CACHE (interop_proxy, "Mono.Interop", "ComInteropProxy")
+GENERATE_GET_CLASS_WITH_CACHE (idispatch,     "Mono.Interop", "IDispatch")
+GENERATE_GET_CLASS_WITH_CACHE (iunknown,      "Mono.Interop", "IUnknown")
 
-GENERATE_GET_CLASS_WITH_CACHE (com_object, System, __ComObject)
-GENERATE_GET_CLASS_WITH_CACHE (variant,    System, Variant)
+GENERATE_GET_CLASS_WITH_CACHE (com_object, "System", "__ComObject")
+GENERATE_GET_CLASS_WITH_CACHE (variant,    "System", "Variant")
 
-static GENERATE_GET_CLASS_WITH_CACHE (interface_type_attribute, System.Runtime.InteropServices, InterfaceTypeAttribute)
-static GENERATE_GET_CLASS_WITH_CACHE (guid_attribute, System.Runtime.InteropServices, GuidAttribute)
+static GENERATE_GET_CLASS_WITH_CACHE (interface_type_attribute, "System.Runtime.InteropServices", "InterfaceTypeAttribute")
+static GENERATE_GET_CLASS_WITH_CACHE (guid_attribute, "System.Runtime.InteropServices", "GuidAttribute")
 
 /* Upon creation of a CCW, only allocate a weak handle and set the
  * reference count to 0. If the unmanaged client code decides to addref and
@@ -347,7 +363,8 @@ cominterop_get_method_interface (MonoMethod* method)
                                gboolean found = FALSE;
                                ic = (MonoClass *)g_ptr_array_index (ifaces, i);
                                offset = mono_class_interface_offset (method->klass, ic);
-                               for (j = 0; j < ic->method.count; ++j) {
+                               int mcount = mono_class_get_method_count (ic);
+                               for (j = 0; j < mcount; ++j) {
                                        if (method->klass->vtable [j + offset] == method) {
                                                found = TRUE;
                                                break;
@@ -387,7 +404,8 @@ cominterop_get_com_slot_for_method (MonoMethod* method)
                ic = cominterop_get_method_interface (method);
                offset = mono_class_interface_offset (method->klass, ic);
                g_assert(offset >= 0);
-               for(i = 0; i < ic->method.count; ++i) {
+               int mcount = mono_class_get_method_count (ic);
+               for(i = 0; i < mcount; ++i) {
                        if (method->klass->vtable [i + offset] == method)
                        {
                                slot = ic->methods[i]->slot;
@@ -500,7 +518,7 @@ cominterop_get_interface_checked (MonoComObject* obj, MonoClass* ic, MonoError *
        g_assert (ic);
        g_assert (MONO_CLASS_IS_INTERFACE (ic));
 
-       mono_error_init (error);
+       error_init (error);
 
        mono_cominterop_lock ();
        if (obj->itf_hash)
@@ -582,13 +600,15 @@ cominterop_type_from_handle (MonoType *handle)
 void
 mono_cominterop_init (void)
 {
-       const char* com_provider_env;
+       char* com_provider_env;
 
        mono_os_mutex_init_recursive (&cominterop_mutex);
 
        com_provider_env = g_getenv ("MONO_COM");
        if (com_provider_env && !strcmp(com_provider_env, "MS"))
                com_provider = MONO_COM_MS;
+       if (com_provider_env)
+               g_free (com_provider_env);
 
        register_icall (cominterop_get_method_interface, "cominterop_get_method_interface", "ptr ptr", FALSE);
        register_icall (cominterop_get_function_pointer, "cominterop_get_function_pointer", "ptr ptr int32", FALSE);
@@ -599,7 +619,7 @@ mono_cominterop_init (void)
        register_icall (cominterop_get_interface, "cominterop_get_interface", "ptr object ptr int32", FALSE);
 
        register_icall (mono_string_to_bstr, "mono_string_to_bstr", "ptr obj", FALSE);
-       register_icall (mono_string_from_bstr, "mono_string_from_bstr", "obj ptr", FALSE);
+       register_icall (mono_string_from_bstr_icall, "mono_string_from_bstr_icall", "obj ptr", FALSE);
        register_icall (mono_free_bstr, "mono_free_bstr", "void ptr", FALSE);
        register_icall (cominterop_type_from_handle, "cominterop_type_from_handle", "object ptr", FALSE);
 
@@ -913,9 +933,8 @@ cominterop_get_native_wrapper_adjusted (MonoMethod *method)
 
 /**
  * mono_cominterop_get_native_wrapper:
- * @method: managed method
- *
- * Returns: the generated method to call
+ * \param method managed method
+ * \returns the generated method to call
  */
 MonoMethod *
 mono_cominterop_get_native_wrapper (MonoMethod *method)
@@ -1028,9 +1047,8 @@ mono_cominterop_get_native_wrapper (MonoMethod *method)
 
 /**
  * mono_cominterop_get_invoke:
- * @method: managed method
- *
- * Returns: the generated method that calls the underlying __ComObject
+ * \param method managed method
+ * \returns the generated method that calls the underlying \c __ComObject
  * rather than the proxy object.
  */
 MonoMethod *
@@ -1493,7 +1511,7 @@ ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (gpointer pUnk)
 
 static gboolean cominterop_can_support_dispatch (MonoClass* klass)
 {
-       if (!(klass->flags & TYPE_ATTRIBUTE_PUBLIC) )
+       if (!mono_class_is_public (klass))
                return FALSE;
 
        if (!cominterop_com_visible (klass))
@@ -1505,7 +1523,7 @@ static gboolean cominterop_can_support_dispatch (MonoClass* klass)
 static void*
 cominterop_get_idispatch_for_object (MonoObject* object, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        if (!object)
                return NULL;
 
@@ -1696,9 +1714,11 @@ ves_icall_System_ComObject_CreateRCW (MonoReflectionType *type)
         * Constructor does not need to be called. Will be called later.
        */
        MonoVTable *vtable = mono_class_vtable_full (domain, klass, &error);
-       mono_error_raise_exception (&error);
+       if (mono_error_set_pending_exception (&error))
+               return NULL;
        obj = mono_object_new_alloc_specific_checked (vtable, &error);
-       mono_error_raise_exception (&error);
+       if (mono_error_set_pending_exception (&error))
+               return NULL;
 
        return obj;
 }
@@ -1923,7 +1943,7 @@ cominterop_get_ccw_checked (MonoObject* object, MonoClass* itf, MonoError *error
        GList *ccw_list, *ccw_list_item;
        MonoCustomAttrInfo *cinfo = NULL;
 
-       mono_error_init (error);
+       error_init (error);
        
        if (!object)
                return NULL;
@@ -1982,8 +2002,7 @@ cominterop_get_ccw_checked (MonoObject* object, MonoClass* itf, MonoError *error
                g_hash_table_insert (ccw_hash, GINT_TO_POINTER (mono_object_hash (object)), ccw_list);
                mono_cominterop_unlock ();
                /* register for finalization to clean up ccw */
-               mono_object_register_finalizer (object, error);
-               return_val_if_nok (error, NULL);
+               mono_object_register_finalizer (object);
        }
 
        cinfo = mono_custom_attrs_from_class_checked (itf, error);
@@ -2008,7 +2027,7 @@ cominterop_get_ccw_checked (MonoObject* object, MonoClass* itf, MonoError *error
                start_slot = 7;
        }
        else {
-               method_count += iface->method.count;
+               method_count += mono_class_get_method_count (iface);
                start_slot = cominterop_get_com_slot_begin (iface);
                iface = NULL;
        }
@@ -2023,7 +2042,7 @@ cominterop_get_ccw_checked (MonoObject* object, MonoClass* itf, MonoError *error
                        memcpy (vtable+3, idispatch, sizeof (idispatch));
 
                iface = itf;
-               for (i = iface->method.count-1; i >= 0;i--) {
+               for (i = mono_class_get_method_count (iface) - 1; i >= 0; i--) {
                        int param_index = 0;
                        MonoMethodBuilder *mb;
                        MonoMarshalSpec ** mspecs;
@@ -2113,12 +2132,14 @@ cominterop_get_ccw_checked (MonoObject* object, MonoClass* itf, MonoError *error
                        wrapper_method = mono_mb_create_method (mb, m.csig, m.csig->param_count + 16);
                        mono_cominterop_unlock ();
 
-                       vtable [vtable_index--] = mono_compile_method (wrapper_method);
+                       vtable [vtable_index--] = mono_compile_method_checked (wrapper_method, error);
 
+                       // cleanup, then error out if compile_method failed
                        for (param_index = sig_adjusted->param_count; param_index >= 0; param_index--)
                                if (mspecs [param_index])
                                        mono_metadata_free_marshal_spec (mspecs [param_index]);
                        g_free (mspecs);
+                       return_val_if_nok (error, NULL);
                }
 
                ccw_entry = g_new0 (MonoCCWInterface, 1);
@@ -2159,9 +2180,8 @@ mono_marshal_free_ccw_entry (gpointer key, gpointer value, gpointer user_data)
 
 /**
  * mono_marshal_free_ccw:
- * @object: the mono object
- *
- * Returns: whether the object had a CCW
+ * \param object the mono object
+ * \returns whether the object had a CCW
  */
 gboolean
 mono_marshal_free_ccw (MonoObject* object)
@@ -2424,15 +2444,15 @@ static const IID MONO_IID_IMarshal = {0x3, 0x0, 0x0, {0xC0, 0x0, 0x0, 0x0, 0x0,
 #ifdef HOST_WIN32
 /* All ccw objects are free threaded */
 static int
-cominterop_ccw_getfreethreadedmarshaler (MonoCCW* ccw, MonoObject* object, gpointer* ppv)
+cominterop_ccw_getfreethreadedmarshaler (MonoCCW* ccw, MonoObject* object, gpointer* ppv, MonoError *error)
 {
+       error_init (error);
 #ifdef HOST_WIN32
-       MonoError error;
        if (!ccw->free_marshaler) {
                int ret = 0;
                gpointer tunk;
-               tunk = cominterop_get_ccw_checked (object, mono_class_get_iunknown_class (), &error);
-               mono_error_raise_exception (&error); /* FIXME don't raise here */
+               tunk = cominterop_get_ccw_checked (object, mono_class_get_iunknown_class (), error);
+               return_val_if_nok (error, MONO_E_NOINTERFACE);
                ret = CoCreateFreeThreadedMarshaler (tunk, (LPUNKNOWN*)&ccw->free_marshaler);
        }
                
@@ -2491,7 +2511,9 @@ cominterop_ccw_queryinterface (MonoCCWInterface* ccwe, guint8* riid, gpointer* p
 #ifdef HOST_WIN32
        /* handle IMarshal special */
        if (0 == memcmp (riid, &MONO_IID_IMarshal, sizeof (IID))) {
-               return cominterop_ccw_getfreethreadedmarshaler (ccw, object, ppv);      
+               int res = cominterop_ccw_getfreethreadedmarshaler (ccw, object, ppv, &error);
+               mono_error_assert_ok (&error);
+               return res;
        }
 #endif
        klass_iter = klass;
@@ -2737,37 +2759,37 @@ init_com_provider_ms (void)
 }
 
 gpointer
-mono_string_to_bstr (MonoString *string_obj)
+mono_ptr_to_bstr(gpointer ptr, int slen)
 {
-       if (!string_obj)
+       if (!ptr)
                return NULL;
 #ifdef HOST_WIN32
-       return SysAllocStringLen (mono_string_chars (string_obj), mono_string_length (string_obj));
+       return SysAllocStringLen (ptr, slen);
 #else
        if (com_provider == MONO_COM_DEFAULT) {
-               int slen = mono_string_length (string_obj);
                /* allocate len + 1 utf16 characters plus 4 byte integer for length*/
-               char *ret = (char *)g_malloc ((slen + 1) * sizeof(gunichar2) + sizeof(guint32));
+               char *ret = (char *)g_malloc((slen + 1) * sizeof(gunichar2) + sizeof(guint32));
                if (ret == NULL)
                        return NULL;
-               memcpy (ret + sizeof(guint32), mono_string_chars (string_obj), slen * sizeof(gunichar2));
-               * ((guint32 *) ret) = slen * sizeof(gunichar2);
-               ret [4 + slen * sizeof(gunichar2)] = 0;
-               ret [5 + slen * sizeof(gunichar2)] = 0;
+               memcpy(ret + sizeof(guint32), ptr, slen * sizeof(gunichar2));
+               *((guint32 *)ret) = slen * sizeof(gunichar2);
+               ret[4 + slen * sizeof(gunichar2)] = 0;
+               ret[5 + slen * sizeof(gunichar2)] = 0;
 
                return ret + 4;
-       } else if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
+       }
+       else if (com_provider == MONO_COM_MS && init_com_provider_ms()) {
                gpointer ret = NULL;
                gunichar* str = NULL;
-               guint32 len;
-               len = mono_string_length (string_obj);
-               str = g_utf16_to_ucs4 (mono_string_chars (string_obj), len,
+               guint32 len = slen;
+               str = g_utf16_to_ucs4(ptr, len,
                        NULL, NULL, NULL);
-               ret = sys_alloc_string_len_ms (str, len);
+               ret = sys_alloc_string_len_ms(str, len);
                g_free(str);
                return ret;
-       } else {
-               g_assert_not_reached ();
+       }
+       else {
+               g_assert_not_reached();
        }
 #endif
 }
@@ -2776,22 +2798,41 @@ MonoString *
 mono_string_from_bstr (gpointer bstr)
 {
        MonoError error;
+       MonoString *result = mono_string_from_bstr_checked (bstr, &error);
+       mono_error_cleanup (&error);
+       return result;
+}
+
+MonoString *
+mono_string_from_bstr_icall (gpointer bstr)
+{
+       MonoError error;
+       MonoString *result = mono_string_from_bstr_checked (bstr, &error);
+       mono_error_set_pending_exception (&error);
+       return result;
+}
+
+MonoString *
+mono_string_from_bstr_checked (gpointer bstr, MonoError *error)
+{
        MonoString * res = NULL;
        
+       error_init (error);
+
        if (!bstr)
                return NULL;
 #ifdef HOST_WIN32
-       res = mono_string_new_utf16_checked (mono_domain_get (), bstr, SysStringLen (bstr), &error);
+       res = mono_string_new_utf16_checked (mono_domain_get (), bstr, SysStringLen (bstr), error);
 #else
        if (com_provider == MONO_COM_DEFAULT) {
-               res = mono_string_new_utf16_checked (mono_domain_get (), (const mono_unichar2 *)bstr, *((guint32 *)bstr - 1) / sizeof(gunichar2), &error);
+               res = mono_string_new_utf16_checked (mono_domain_get (), (const mono_unichar2 *)bstr, *((guint32 *)bstr - 1) / sizeof(gunichar2), error);
        } else if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
                MonoString* str = NULL;
                glong written = 0;
                gunichar2* utf16 = NULL;
 
                utf16 = g_ucs4_to_utf16 ((const gunichar *)bstr, sys_string_len_ms (bstr), NULL, &written, NULL);
-               str = mono_string_new_utf16_checked (mono_domain_get (), utf16, written, &error);
+               str = mono_string_new_utf16_checked (mono_domain_get (), utf16, written, error);
                g_free (utf16);
                res = str;
        } else {
@@ -2799,7 +2840,6 @@ mono_string_from_bstr (gpointer bstr)
        }
 
 #endif
-       mono_error_raise_exception (&error); /* FIXME don't raise here */
        return res;
 }
 
@@ -3070,54 +3110,97 @@ mono_cominterop_emit_marshal_safearray (EmitMarshalContext *m, int argnum, MonoT
        return conv_arg;
 }
 
-static 
-guint32 mono_marshal_safearray_get_dim (gpointer safearray)
+#ifdef HOST_WIN32
+#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT | HAVE_UWP_WINAPI_SUPPORT)
+static inline guint32
+mono_marshal_win_safearray_get_dim (gpointer safearray)
+{
+       return SafeArrayGetDim (safearray);
+}
+#endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT | HAVE_UWP_WINAPI_SUPPORT) */
+
+static guint32
+mono_marshal_safearray_get_dim (gpointer safearray)
+{
+       return mono_marshal_win_safearray_get_dim (safearray);
+}
+
+#else /* HOST_WIN32 */
+
+static guint32
+mono_marshal_safearray_get_dim (gpointer safearray)
 {
        guint32 result=0;
-#ifdef HOST_WIN32
-       result = SafeArrayGetDim (safearray);
-#else
        if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
                result = safe_array_get_dim_ms (safearray);
        } else {
                g_assert_not_reached ();
        }
-#endif
        return result;
 }
+#endif /* HOST_WIN32 */
 
-static 
-int mono_marshal_safe_array_get_lbound (gpointer psa, guint nDim, glong* plLbound)
+#ifdef HOST_WIN32
+#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT | HAVE_UWP_WINAPI_SUPPORT)
+static inline int
+mono_marshal_win_safe_array_get_lbound (gpointer psa, guint nDim, glong* plLbound)
+{
+       return SafeArrayGetLBound (psa, nDim, plLbound);
+}
+#endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT | HAVE_UWP_WINAPI_SUPPORT) */
+
+static int
+mono_marshal_safe_array_get_lbound (gpointer psa, guint nDim, glong* plLbound)
+{
+       return mono_marshal_win_safe_array_get_lbound (psa, nDim, plLbound);
+}
+
+#else /* HOST_WIN32 */
+
+static int
+mono_marshal_safe_array_get_lbound (gpointer psa, guint nDim, glong* plLbound)
 {
        int result=MONO_S_OK;
-#ifdef HOST_WIN32
-       result = SafeArrayGetLBound (psa, nDim, plLbound);
-#else
        if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
                result = safe_array_get_lbound_ms (psa, nDim, plLbound);
        } else {
                g_assert_not_reached ();
        }
-#endif
        return result;
 }
+#endif /* HOST_WIN32 */
 
-static 
-int mono_marshal_safe_array_get_ubound (gpointer psa, guint nDim, glong* plUbound)
+#ifdef HOST_WIN32
+#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT | HAVE_UWP_WINAPI_SUPPORT)
+inline static int
+mono_marshal_win_safe_array_get_ubound (gpointer psa, guint nDim, glong* plUbound)
+{
+       return SafeArrayGetUBound (psa, nDim, plUbound);
+}
+#endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT | HAVE_UWP_WINAPI_SUPPORT) */
+
+static int
+mono_marshal_safe_array_get_ubound (gpointer psa, guint nDim, glong* plUbound)
+{
+       return mono_marshal_win_safe_array_get_ubound (psa, nDim, plUbound);
+}
+
+#else /* HOST_WIN32 */
+
+static int
+mono_marshal_safe_array_get_ubound (gpointer psa, guint nDim, glong* plUbound)
 {
        int result=MONO_S_OK;
-#ifdef HOST_WIN32
-       result = SafeArrayGetUBound (psa, nDim, plUbound);
-#else
        if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
                result = safe_array_get_ubound_ms (psa, nDim, plUbound);
        } else {
                g_assert_not_reached ();
        }
-#endif
        return result;
 }
+#endif /* HOST_WIN32 */
 
+/* This is an icall */
 static gboolean
 mono_marshal_safearray_begin (gpointer safearray, MonoArray **result, gpointer *indices, gpointer empty, gpointer parameter, gboolean allocateNewArray)
 {
@@ -3161,14 +3244,16 @@ mono_marshal_safearray_begin (gpointer safearray, MonoArray **result, gpointer *
                                hr = mono_marshal_safe_array_get_lbound (safearray, i+1, &lbound);
                                if (hr < 0) {
                                        cominterop_set_hr_error (&error, hr);
-                                       mono_error_raise_exception (&error); /* FIXME don't raise here */
+                                       if (mono_error_set_pending_exception (&error))
+                                               return FALSE;
                                }
                                if (lbound != 0)
                                        bounded = TRUE;
                                hr = mono_marshal_safe_array_get_ubound (safearray, i+1, &ubound);
                                if (hr < 0) {
                                        cominterop_set_hr_error (&error, hr);
-                                       mono_error_raise_exception (&error); /* FIXME don't raise here */
+                                       if (mono_error_set_pending_exception (&error))
+                                               return FALSE;
                                }
                                cursize = ubound-lbound+1;
                                sizes [i] = cursize;
@@ -3183,7 +3268,8 @@ mono_marshal_safearray_begin (gpointer safearray, MonoArray **result, gpointer *
                        if (allocateNewArray) {
                                aklass = mono_bounded_array_class_get (mono_defaults.object_class, dim, bounded);
                                *result = mono_array_new_full_checked (mono_domain_get (), aklass, sizes, bounds, &error);
-                               mono_error_raise_exception (&error); /* FIXME don't raise here */
+                               if (mono_error_set_pending_exception (&error))
+                                       return FALSE;
                        } else {
                                *result = (MonoArray *)parameter;
                        }
@@ -3192,31 +3278,55 @@ mono_marshal_safearray_begin (gpointer safearray, MonoArray **result, gpointer *
        return TRUE;
 }
 
-static 
-gpointer mono_marshal_safearray_get_value (gpointer safearray, gpointer indices)
+/* This is an icall */
+#ifdef HOST_WIN32
+#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT | HAVE_UWP_WINAPI_SUPPORT)
+static inline int
+mono_marshal_win_safearray_get_value (gpointer safearray, gpointer indices, gpointer *result)
+{
+       return SafeArrayPtrOfIndex (safearray, indices, result);
+}
+#endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT | HAVE_UWP_WINAPI_SUPPORT) */
+
+static gpointer
+mono_marshal_safearray_get_value (gpointer safearray, gpointer indices)
 {
        MonoError error;
        gpointer result;
-#ifdef HOST_WIN32
-       int hr = SafeArrayPtrOfIndex (safearray, indices, &result);
+
+       int hr = mono_marshal_win_safearray_get_value (safearray, indices, &result);
        if (hr < 0) {
                        cominterop_set_hr_error (&error, hr);
-                       mono_error_raise_exception (&error); /* FIXME don't raise here */
+                       mono_error_set_pending_exception (&error);
+                       result = NULL;
        }
-#else
+
+       return result;
+}
+
+#else /* HOST_WIN32 */
+
+static gpointer
+mono_marshal_safearray_get_value (gpointer safearray, gpointer indices)
+{
+       MonoError error;
+       gpointer result;
+
        if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
                int hr = safe_array_ptr_of_index_ms (safearray, (glong *)indices, &result);
                if (hr < 0) {
                        cominterop_set_hr_error (&error, hr);
-                       mono_error_raise_exception (&error); /* FIXME don't raise here */
+                       mono_error_set_pending_exception (&error);
+                       return NULL;
                }
        } else {
                g_assert_not_reached ();
        }
-#endif
        return result;
 }
+#endif /* HOST_WIN32 */
 
+/* This is an icall */
 static 
 gboolean mono_marshal_safearray_next (gpointer safearray, gpointer indices)
 {
@@ -3234,7 +3344,8 @@ gboolean mono_marshal_safearray_next (gpointer safearray, gpointer indices)
                hr = mono_marshal_safe_array_get_ubound (safearray, i+1, &ubound);
                if (hr < 0) {
                        cominterop_set_hr_error (&error, hr);
-                       mono_error_raise_exception (&error); /* FIXME don't raise here */
+                       mono_error_set_pending_exception (&error);
+                       return FALSE;
                }
 
                if (++pIndices[i] <= ubound) {
@@ -3244,7 +3355,8 @@ gboolean mono_marshal_safearray_next (gpointer safearray, gpointer indices)
                hr = mono_marshal_safe_array_get_lbound (safearray, i+1, &lbound);
                if (hr < 0) {
                        cominterop_set_hr_error (&error, hr);
-                       mono_error_raise_exception (&error); /* FIXME don't raise here */
+                       mono_error_set_pending_exception (&error);
+                       return FALSE;
                }
 
                pIndices[i] = lbound;
@@ -3255,20 +3367,62 @@ gboolean mono_marshal_safearray_next (gpointer safearray, gpointer indices)
        return ret;
 }
 
-static 
-void mono_marshal_safearray_end (gpointer safearray, gpointer indices)
+#ifdef HOST_WIN32
+#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT | HAVE_UWP_WINAPI_SUPPORT)
+static inline void
+mono_marshal_win_safearray_end (gpointer safearray, gpointer indices)
 {
        g_free(indices);
-#ifdef HOST_WIN32
        SafeArrayDestroy (safearray);
-#else
+}
+#endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT | HAVE_UWP_WINAPI_SUPPORT) */
+
+static void
+mono_marshal_safearray_end (gpointer safearray, gpointer indices)
+{
+       mono_marshal_win_safearray_end (safearray, indices);
+}
+
+#else /* HOST_WIN32 */
+
+static void
+mono_marshal_safearray_end (gpointer safearray, gpointer indices)
+{
+       g_free(indices);
        if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
                safe_array_destroy_ms (safearray);
        } else {
                g_assert_not_reached ();
        }
-#endif
 }
+#endif /* HOST_WIN32 */
+
+#ifdef HOST_WIN32
+#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT | HAVE_UWP_WINAPI_SUPPORT)
+static inline gboolean
+mono_marshal_win_safearray_create_internal (UINT cDims, SAFEARRAYBOUND *rgsabound, gpointer *newsafearray)
+{
+       *newsafearray = SafeArrayCreate (VT_VARIANT, cDims, rgsabound);
+       return TRUE;
+}
+#endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT | HAVE_UWP_WINAPI_SUPPORT) */
+
+static gboolean
+mono_marshal_safearray_create_internal (UINT cDims, SAFEARRAYBOUND *rgsabound, gpointer *newsafearray)
+{
+       return mono_marshal_win_safearray_create_internal (cDims, rgsabound, newsafearray);
+}
+
+#else /* HOST_WIN32 */
+
+static inline gboolean
+mono_marshal_safearray_create_internal (UINT cDims, SAFEARRAYBOUND *rgsabound, gpointer *newsafearray)
+{
+       *newsafearray = safe_array_create_ms (VT_VARIANT, cDims, rgsabound);
+       return TRUE;
+}
+
+#endif /* HOST_WIN32 */
 
 static gboolean
 mono_marshal_safearray_create (MonoArray *input, gpointer *newsafearray, gpointer *indices, gpointer empty)
@@ -3307,36 +3461,48 @@ mono_marshal_safearray_create (MonoArray *input, gpointer *newsafearray, gpointe
                bounds [0].lLbound = 0;
        }
 
-#ifdef HOST_WIN32
-       *newsafearray = SafeArrayCreate (VT_VARIANT, dim, bounds);
-#else
-       *newsafearray = safe_array_create_ms (VT_VARIANT, dim, bounds);
-#endif
+       return mono_marshal_safearray_create_internal (dim, bounds, newsafearray);
+}
 
-       return TRUE;
+/* This is an icall */
+#ifdef HOST_WIN32
+#if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT | HAVE_UWP_WINAPI_SUPPORT)
+static inline int
+mono_marshal_win_safearray_set_value (gpointer safearray, gpointer indices, gpointer value)
+{
+       return SafeArrayPutElement (safearray, indices, value);
 }
+#endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT | HAVE_UWP_WINAPI_SUPPORT) */
 
-static 
-void mono_marshal_safearray_set_value (gpointer safearray, gpointer indices, gpointer value)
+static void
+mono_marshal_safearray_set_value (gpointer safearray, gpointer indices, gpointer value)
 {
        MonoError error;
-#ifdef HOST_WIN32
-       int hr = SafeArrayPutElement (safearray, indices, value);
+       int hr = mono_marshal_win_safearray_set_value (safearray, indices, value);
        if (hr < 0) {
                cominterop_set_hr_error (&error, hr);
-               mono_error_raise_exception (&error); /* FIXME don't raise here */
+               mono_error_set_pending_exception (&error);
+               return;
        }
-#else
+}
+
+#else /* HOST_WIN32 */
+
+static void
+mono_marshal_safearray_set_value (gpointer safearray, gpointer indices, gpointer value)
+{
+       MonoError error;
        if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
                int hr = safe_array_put_element_ms (safearray, (glong *)indices, (void **)value);
                if (hr < 0) {
                        cominterop_set_hr_error (&error, hr);
-                       mono_error_raise_exception (&error); /* FIXME don't raise here */
+                       mono_error_set_pending_exception (&error);
+                       return;
                }
        } else
                g_assert_not_reached ();
-#endif
 }
+#endif /* HOST_WIN32 */
 
 static 
 void mono_marshal_safearray_free_indices (gpointer indices)
@@ -3360,7 +3526,7 @@ mono_cominterop_init (void)
        emit an exception in the generated IL.
        */
        register_icall (mono_string_to_bstr, "mono_string_to_bstr", "ptr obj", FALSE);
-       register_icall (mono_string_from_bstr, "mono_string_from_bstr", "obj ptr", FALSE);
+       register_icall (mono_string_from_bstr_icall, "mono_string_from_bstr_icall", "obj ptr", FALSE);
        register_icall (mono_free_bstr, "mono_free_bstr", "void ptr", FALSE);
 }
 
@@ -3375,20 +3541,19 @@ cominterop_release_all_rcws (void)
 }
 
 gpointer
-mono_string_to_bstr (MonoString *string_obj)
+mono_ptr_to_bstr (gpointer ptr, int slen)
 {
-       if (!string_obj)
+       if (!ptr)
                return NULL;
 #ifdef HOST_WIN32
-       return SysAllocStringLen (mono_string_chars (string_obj), mono_string_length (string_obj));
+       return SysAllocStringLen (ptr, slen);
 #else
        {
-               int slen = mono_string_length (string_obj);
                /* allocate len + 1 utf16 characters plus 4 byte integer for length*/
                char *ret = g_malloc ((slen + 1) * sizeof(gunichar2) + sizeof(guint32));
                if (ret == NULL)
                        return NULL;
-               memcpy (ret + sizeof(guint32), mono_string_chars (string_obj), slen * sizeof(gunichar2));
+               memcpy (ret + sizeof(guint32), ptr, slen * sizeof(gunichar2));
                * ((guint32 *) ret) = slen * sizeof(gunichar2);
                ret [4 + slen * sizeof(gunichar2)] = 0;
                ret [5 + slen * sizeof(gunichar2)] = 0;
@@ -3398,19 +3563,37 @@ mono_string_to_bstr (MonoString *string_obj)
 #endif
 }
 
+
 MonoString *
 mono_string_from_bstr (gpointer bstr)
 {
-       MonoString *res = NULL;
        MonoError error;
+       MonoString *result = mono_string_from_bstr_checked (bstr, &error);
+       mono_error_cleanup (&error);
+       return result;
+}
+
+MonoString *
+mono_string_from_bstr_icall (gpointer bstr)
+{
+       MonoError error;
+       MonoString *result = mono_string_from_bstr_checked (bstr, &error);
+       mono_error_set_pending_exception (&error);
+       return result;
+}
+
+MonoString *
+mono_string_from_bstr_checked (gpointer bstr, MonoError *error)
+{
+       MonoString *res = NULL;
+       error_init (error);
        if (!bstr)
                return NULL;
 #ifdef HOST_WIN32
-       res = mono_string_new_utf16_checked (mono_domain_get (), bstr, SysStringLen (bstr), &error);
+       res = mono_string_new_utf16_checked (mono_domain_get (), bstr, SysStringLen (bstr), error);
 #else
-       res = mono_string_new_utf16_checked (mono_domain_get (), bstr, *((guint32 *)bstr - 1) / sizeof(gunichar2), &error);
+       res = mono_string_new_utf16_checked (mono_domain_get (), bstr, *((guint32 *)bstr - 1) / sizeof(gunichar2), error);
 #endif
-       mono_error_raise_exception (&error); /* FIXME don't raise here */
        return res;
 }
 
@@ -3458,7 +3641,10 @@ ves_icall_System_Runtime_InteropServices_Marshal_QueryInterfaceInternal (gpointe
 MonoString *
 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringBSTR (gpointer ptr)
 {
-       return mono_string_from_bstr(ptr);
+       MonoError error;
+       MonoString *result = mono_string_from_bstr_checked (ptr, &error);
+       mono_error_set_pending_exception (&error);
+       return result;
 }
 
 gpointer
@@ -3467,6 +3653,12 @@ ves_icall_System_Runtime_InteropServices_Marshal_StringToBSTR (MonoString* ptr)
        return mono_string_to_bstr(ptr);
 }
 
+gpointer
+ves_icall_System_Runtime_InteropServices_Marshal_BufferToBSTR (MonoArray* ptr, int len)
+{
+       return mono_ptr_to_bstr (ptr->vector, len);
+}
+
 void
 ves_icall_System_Runtime_InteropServices_Marshal_FreeBSTR (gpointer ptr)
 {