Beispiel #1
0
        public static object GetTypedObjectForIUnknown(IntPtr pUnk, Type t)
        {
            ComInteropProxy proxy = new ComInteropProxy(pUnk, t);
            __ComObject     co    = (__ComObject)proxy.GetTransparentProxy();

            foreach (Type itf in t.GetInterfaces())
            {
                if ((itf.Attributes & TypeAttributes.Import) == TypeAttributes.Import)
                {
                    if (co.GetInterface(itf) == IntPtr.Zero)
                    {
                        return(null);
                    }
                }
            }
            return(co);
        }
Beispiel #2
0
        public bool CanCastTo(Type fromType, object o)
        {
            __ComObject co = o as __ComObject;

            if (co == null)
            {
                throw new NotSupportedException("Only RCWs are currently supported");
            }

            if ((fromType.Attributes & TypeAttributes.Import) == 0)
            {
                return(false);
            }

            if (co.GetInterface(fromType, false) == IntPtr.Zero)
            {
                return(false);
            }
            return(true);
        }
Beispiel #3
0
        public static object CreateWrapperOfType(object o, Type t)
        {
            __ComObject co = o as __ComObject;

            if (co == null)
            {
                throw new ArgumentException("o must derive from __ComObject", "o");
            }
            if (t == null)
            {
                throw new ArgumentNullException("t");
            }

            Type[] itfs = o.GetType().GetInterfaces();
            foreach (Type itf in itfs)
            {
                if (itf.IsImport && co.GetInterface(itf) == IntPtr.Zero)
                {
                    throw new InvalidCastException();
                }
            }

            return(ComInteropProxy.GetProxy(co.IUnknown, t).GetTransparentProxy());
        }
        public bool CanCastTo(Type fromType, object o)
        {
            __ComObject _ComObject = o as __ComObject;

            if (_ComObject == null)
            {
                throw new NotSupportedException("Only RCWs are currently supported");
            }
            return((fromType.Attributes & TypeAttributes.Import) != TypeAttributes.NotPublic && !(_ComObject.GetInterface(fromType, false) == IntPtr.Zero));
        }