Example #1
0
        internal bool TryGetPropertySetter(string name, out ComMethodDesc method, Type limitType)
        {
            EnsureScanDefinedMethods();

            if (ComBinderHelpers.PreferPut(limitType))
            {
                return(_comTypeDesc.Puts.TryGetValue(name, out method) ||
                       _comTypeDesc.PutRefs.TryGetValue(name, out method));
            }
            else
            {
                return(_comTypeDesc.PutRefs.TryGetValue(name, out method) ||
                       _comTypeDesc.Puts.TryGetValue(name, out method));
            }
        }
Example #2
0
        internal ComInvokeBinder(IList<ArgumentInfo> arguments, MetaObject[] args, Restrictions restrictions, Expression method, Expression dispatch, ComMethodDesc methodDesc) {
            ContractUtils.RequiresNotNull(arguments, "arguments");
            ContractUtils.RequiresNotNull(args, "args");
            ContractUtils.RequiresNotNull(method, "method");
            ContractUtils.RequiresNotNull(dispatch, "dispatch");
            ContractUtils.Requires(TypeUtils.AreReferenceAssignable(typeof(ComMethodDesc), method.Type), "method");
            ContractUtils.Requires(TypeUtils.AreReferenceAssignable(typeof(IDispatch), dispatch.Type), "dispatch");

            _method = method;
            _dispatch = dispatch;
            _methodDesc = methodDesc;

            _arguments = arguments;
            _args = args;
            _restrictions = restrictions;

            // Set Instance to some value so that CallBinderHelper has the right number of parameters to work with
            _instance = dispatch;
        }
Example #3
0
        public bool SlowTryGetSetItem(out DispCallable value)
        {
            EnsureScanDefinedMethods();

            ComMethodDesc methodDesc = _comTypeDesc.SetItem;

            // The following attempts to get a method corresponding to "[PROPERTYPUT, DISPID(0)] HRESULT Item(...)".
            // However, without type information, we really don't know whether or not we have a property setter.
            // All we can do is verify that the found dispId is DISPID_VALUE.  So, if we find a dispId of DISPID_VALUE,
            // we happily package it up as a property setter; otherwise, it's a no go...
            if (methodDesc == null)
            {
                int    dispId;
                string name    = "Item";
                int    hresult = GetIDsOfNames(_dispatchObject, name, out dispId);
                if (hresult == ComHresults.DISP_E_UNKNOWNNAME)
                {
                    value = null;
                    return(false);
                }
                else if (hresult != ComHresults.S_OK)
                {
                    throw Error.CouldNotGetDispId(name, string.Format(CultureInfo.InvariantCulture, "0x{1:X})", hresult));
                }
                else if (dispId != ComDispIds.DISPID_VALUE)
                {
                    value = null;
                    return(false);
                }

                methodDesc           = new ComMethodDesc(name, dispId, ComTypes.INVOKEKIND.INVOKE_PROPERTYPUT);
                _comTypeDesc.SetItem = methodDesc;
            }

            Interlocked.CompareExchange(
                ref _setter,
                new DispCallable(_dispatchObject, methodDesc),
                null);

            value = _setter;
            return(true);
        }
Example #4
0
        internal ComInvokeBinder(IList <ArgumentInfo> arguments, MetaObject[] args, Restrictions restrictions, Expression method, Expression dispatch, ComMethodDesc methodDesc)
        {
            ContractUtils.RequiresNotNull(arguments, "arguments");
            ContractUtils.RequiresNotNull(args, "args");
            ContractUtils.RequiresNotNull(method, "method");
            ContractUtils.RequiresNotNull(dispatch, "dispatch");
            ContractUtils.Requires(TypeUtils.AreReferenceAssignable(typeof(ComMethodDesc), method.Type), "method");
            ContractUtils.Requires(TypeUtils.AreReferenceAssignable(typeof(IDispatch), dispatch.Type), "dispatch");

            _method     = method;
            _dispatch   = dispatch;
            _methodDesc = methodDesc;

            _arguments    = arguments;
            _args         = args;
            _restrictions = restrictions;

            // Set Instance to some value so that CallBinderHelper has the right number of parameters to work with
            _instance = dispatch;
        }
Example #5
0
        internal bool TryGetPropertySetterExplicit(string name, out ComMethodDesc method, Type limitType)
        {
            EnsureScanDefinedMethods();

            // TODO: We have a thread-safety issue here right now
            // TODO: since we are mutating _funcs array
            // TODO: The workaround is to use Hashtable (which is thread-safe
            // TODO: on read operations) to fetch the value out.
            int dispId;
            int hresult = GetIDsOfNames(_dispatchObject, name, out dispId);

            if (hresult == ComHresults.S_OK)
            {
                // we do not know whether we have put or putref here
                // and we will not guess and pretend we found both.
                ComMethodDesc put = new ComMethodDesc(name, dispId, ComTypes.INVOKEKIND.INVOKE_PROPERTYPUT);
                _comTypeDesc.Puts.Add(name, put);

                ComMethodDesc putref = new ComMethodDesc(name, dispId, ComTypes.INVOKEKIND.INVOKE_PROPERTYPUTREF);
                _comTypeDesc.PutRefs.Add(name, putref);

                if (ComBinderHelpers.PreferPut(limitType))
                {
                    method = put;
                }
                else
                {
                    method = putref;
                }
                return(true);
            }
            else if (hresult == ComHresults.DISP_E_UNKNOWNNAME)
            {
                method = null;
                return(false);
            }
            else
            {
                throw Error.CouldNotGetDispId(name, string.Format(CultureInfo.InvariantCulture, "0x{1:X})", hresult));
            }
        }
        private void EnsureScanDefinedMethods() {
            if (_comTypeDesc != null && _comTypeDesc.Funcs != null)
                return;

            ComTypes.ITypeInfo typeInfo = ComRuntimeHelpers.GetITypeInfoFromIDispatch(_dispatchObject, true);
            if (typeInfo == null) {
                _comTypeDesc = ComTypeDesc.CreateEmptyTypeDesc();
                return;
            }

            ComTypes.TYPEATTR typeAttr = ComRuntimeHelpers.GetTypeAttrForTypeInfo(typeInfo);

            if (_comTypeDesc == null) {
                lock (_CacheComTypeDesc) {
                    if (_CacheComTypeDesc.TryGetValue(typeAttr.guid, out _comTypeDesc) == true &&
                        _comTypeDesc.Funcs != null) {
                        return;
                    }
                }
            }

            ComTypeDesc typeDesc = ComTypeDesc.FromITypeInfo(typeInfo);

            ComMethodDesc getItem = null;
            ComMethodDesc setItem = null;
            Dictionary<string, ComMethodDesc> funcs = new Dictionary<string, ComMethodDesc>(typeAttr.cFuncs);
            Dictionary<string, ComMethodDesc> puts = new Dictionary<string, ComMethodDesc>();
            Dictionary<string, ComMethodDesc> putrefs = new Dictionary<string, ComMethodDesc>();
            Set<int> usedDispIds = new Set<int>();

            for (int definedFuncIndex = 0; definedFuncIndex < typeAttr.cFuncs; definedFuncIndex++) {
                IntPtr funcDescHandleToRelease = IntPtr.Zero;

                try {
                    ComTypes.FUNCDESC funcDesc;
                    GetFuncDescForDescIndex(typeInfo, definedFuncIndex, out funcDesc, out funcDescHandleToRelease);

                    if ((funcDesc.wFuncFlags & (int)ComTypes.FUNCFLAGS.FUNCFLAG_FRESTRICTED) != 0) {
                        // This function is not meant for the script user to use.
                        continue;
                    }

                    ComMethodDesc method = new ComMethodDesc(typeInfo, funcDesc);

                    if ((funcDesc.invkind & ComTypes.INVOKEKIND.INVOKE_PROPERTYPUT) != 0) {
                        puts.Add(method.Name, method);
                        continue;
                    }
                    if ((funcDesc.invkind & ComTypes.INVOKEKIND.INVOKE_PROPERTYPUTREF) != 0) {
                        putrefs.Add(method.Name, method);
                        continue;
                    }

                    usedDispIds.Add(funcDesc.memid);

                    if (funcDesc.memid == ComDispIds.DISPID_NEWENUM) {
                        funcs.Add("GetEnumerator", method);
                        continue;
                    }

                    funcs.Add(method.Name, method);

                    // for the special dispId == 0, we need to store the method descriptor 
                    // for the Do(GetItem) binder. 
                    if (funcDesc.memid == ComDispIds.DISPID_VALUE) {
                        getItem = method;
                    }
                } finally {
                    if (funcDescHandleToRelease != IntPtr.Zero) {
                        typeInfo.ReleaseFuncDesc(funcDescHandleToRelease);
                    }
                }
            }

            ProcessPut(funcs, puts, usedDispIds, ref setItem);
            ProcessPut(funcs, putrefs, usedDispIds, ref setItem);

            lock (_CacheComTypeDesc) {
                ComTypeDesc cachedTypeDesc;
                if (_CacheComTypeDesc.TryGetValue(typeAttr.guid, out cachedTypeDesc)) {
                    _comTypeDesc = cachedTypeDesc;
                } else {
                    _comTypeDesc = typeDesc;
                    _CacheComTypeDesc.Add(typeAttr.guid, _comTypeDesc);
                }
                _comTypeDesc.Funcs = funcs;
                _comTypeDesc.Puts = puts;
                _comTypeDesc.PutRefs = putrefs;
                _comTypeDesc.GetItem = getItem;
                _comTypeDesc.SetItem = setItem;
            }
        }
        internal bool TryGetPropertySetterExplicit(string name, out ComMethodDesc method, Type limitType) {
            EnsureScanDefinedMethods();

            // TODO: We have a thread-safety issue here right now
            // TODO: since we are mutating _funcs array
            // TODO: The workaround is to use Hashtable (which is thread-safe
            // TODO: on read operations) to fetch the value out.
            int dispId;
            int hresult = GetIDsOfNames(_dispatchObject, name, out dispId);

            if (hresult == ComHresults.S_OK) {
                // we do not know whether we have put or putref here
                // and we will not guess and pretend we found both.
                ComMethodDesc put = new ComMethodDesc(name, dispId, ComTypes.INVOKEKIND.INVOKE_PROPERTYPUT);
                _comTypeDesc.Puts.Add(name, put);

                ComMethodDesc putref = new ComMethodDesc(name, dispId, ComTypes.INVOKEKIND.INVOKE_PROPERTYPUTREF);
                _comTypeDesc.PutRefs.Add(name, putref);

                if (ComBinderHelpers.PreferPut(limitType)) {
                    method = put;
                } else {
                    method = putref;
                }
                return true;
            } else if (hresult == ComHresults.DISP_E_UNKNOWNNAME) {
                method = null;
                return false;
            } else {
                throw Error.CouldNotGetDispId(name, string.Format(CultureInfo.InvariantCulture, "0x{1:X})", hresult));
            }
        }
        internal bool TryGetMemberMethodExplicit(string name, out ComMethodDesc method) {
            EnsureScanDefinedMethods();

            // TODO: We have a thread-safety issue here right now
            // TODO: since we are mutating _funcs array
            // TODO: The workaround is to use Hashtable (which is thread-safe
            // TODO: on read operations) to fetch the value out.
            int dispId;
            int hresult = GetIDsOfNames(_dispatchObject, name, out dispId);

            if (hresult == ComHresults.S_OK) {
                ComMethodDesc cmd = new ComMethodDesc(name, dispId);
                _comTypeDesc.Funcs.Add(name, cmd);
                method = cmd;
                return true;
            } else if (hresult == ComHresults.DISP_E_UNKNOWNNAME) {
                method = null;
                return false;
            } else {
                throw Error.CouldNotGetDispId(name, string.Format(CultureInfo.InvariantCulture, "0x{1:X})", hresult));
            }
        }
 internal bool TryGetMemberMethod(string name, out ComMethodDesc method) {
     EnsureScanDefinedMethods();
     return _comTypeDesc.Funcs.TryGetValue(name, out method);
 }
Example #10
0
        public bool SlowTryGetSetItem(out DispCallable value) {
            EnsureScanDefinedMethods();

            ComMethodDesc methodDesc = _comTypeDesc.SetItem;

            // The following attempts to get a method corresponding to "[PROPERTYPUT, DISPID(0)] HRESULT Item(...)".
            // However, without type information, we really don't know whether or not we have a property setter.
            // All we can do is verify that the found dispId is DISPID_VALUE.  So, if we find a dispId of DISPID_VALUE,
            // we happily package it up as a property setter; otherwise, it's a no go...
            if (methodDesc == null) {
                int dispId;
                string name = "Item";
                int hresult = GetIDsOfNames(_dispatchObject, name, out dispId);
                if (hresult == ComHresults.DISP_E_UNKNOWNNAME) {
                    value = null;
                    return false;
                } else if (hresult != ComHresults.S_OK) {
                    throw Error.CouldNotGetDispId(name, string.Format(CultureInfo.InvariantCulture, "0x{1:X})", hresult));
                } else if (dispId != ComDispIds.DISPID_VALUE) {
                    value = null;
                    return false;
                }

                methodDesc = new ComMethodDesc(name, dispId, ComTypes.INVOKEKIND.INVOKE_PROPERTYPUT);
                _comTypeDesc.SetItem = methodDesc;
            }

            Interlocked.CompareExchange(
                            ref _setter,
                            new DispCallable(_dispatchObject, methodDesc),
                            null);

            value = _setter;
            return true;
        }
Example #11
0
 public static DispCallable CreateDispCallable(IDispatch dispatch, ComMethodDesc method)
 {
     return(new DispCallable(dispatch, method));
 }
Example #12
0
 internal DispCallable(IDispatch dispatch, ComMethodDesc methodDesc) {
     _dispatch = dispatch;
     _methodDesc = methodDesc;
 }
Example #13
0
        private static void ProcessPut(Dictionary <string, ComMethodDesc> funcs, Dictionary <string, ComMethodDesc> methods, Set <int> usedDispIds, ref ComMethodDesc setItem)
        {
            foreach (ComMethodDesc method in methods.Values)
            {
                if (!usedDispIds.Contains(method.DispId))
                {
                    funcs.Add(method.Name, method);
                    usedDispIds.Add(method.DispId);
                }

                // for the special dispId == 0, we need to store
                // the method descriptor for the Do(SetItem) binder.
                if (method.DispId == ComDispIds.DISPID_VALUE && setItem == null)
                {
                    setItem = method;
                }
            }
        }
Example #14
0
        private void EnsureScanDefinedMethods()
        {
            if (_comTypeDesc != null && _comTypeDesc.Funcs != null)
            {
                return;
            }

            ComTypes.ITypeInfo typeInfo = ComRuntimeHelpers.GetITypeInfoFromIDispatch(_dispatchObject, true);
            if (typeInfo == null)
            {
                _comTypeDesc = ComTypeDesc.CreateEmptyTypeDesc();
                return;
            }

            ComTypes.TYPEATTR typeAttr = ComRuntimeHelpers.GetTypeAttrForTypeInfo(typeInfo);

            if (_comTypeDesc == null)
            {
                lock (_CacheComTypeDesc) {
                    if (_CacheComTypeDesc.TryGetValue(typeAttr.guid, out _comTypeDesc) == true &&
                        _comTypeDesc.Funcs != null)
                    {
                        return;
                    }
                }
            }

            ComTypeDesc typeDesc = ComTypeDesc.FromITypeInfo(typeInfo);

            ComMethodDesc getItem = null;
            ComMethodDesc setItem = null;
            Dictionary <string, ComMethodDesc> funcs   = new Dictionary <string, ComMethodDesc>(typeAttr.cFuncs);
            Dictionary <string, ComMethodDesc> puts    = new Dictionary <string, ComMethodDesc>();
            Dictionary <string, ComMethodDesc> putrefs = new Dictionary <string, ComMethodDesc>();
            Set <int> usedDispIds = new Set <int>();

            for (int definedFuncIndex = 0; definedFuncIndex < typeAttr.cFuncs; definedFuncIndex++)
            {
                IntPtr funcDescHandleToRelease = IntPtr.Zero;

                try {
                    ComTypes.FUNCDESC funcDesc;
                    GetFuncDescForDescIndex(typeInfo, definedFuncIndex, out funcDesc, out funcDescHandleToRelease);

                    if ((funcDesc.wFuncFlags & (int)ComTypes.FUNCFLAGS.FUNCFLAG_FRESTRICTED) != 0)
                    {
                        // This function is not meant for the script user to use.
                        continue;
                    }

                    ComMethodDesc method = new ComMethodDesc(typeInfo, funcDesc);

                    if ((funcDesc.invkind & ComTypes.INVOKEKIND.INVOKE_PROPERTYPUT) != 0)
                    {
                        puts.Add(method.Name, method);
                        continue;
                    }
                    if ((funcDesc.invkind & ComTypes.INVOKEKIND.INVOKE_PROPERTYPUTREF) != 0)
                    {
                        putrefs.Add(method.Name, method);
                        continue;
                    }

                    usedDispIds.Add(funcDesc.memid);

                    if (funcDesc.memid == ComDispIds.DISPID_NEWENUM)
                    {
                        funcs.Add("GetEnumerator", method);
                        continue;
                    }

                    funcs.Add(method.Name, method);

                    // for the special dispId == 0, we need to store the method descriptor
                    // for the Do(GetItem) binder.
                    if (funcDesc.memid == ComDispIds.DISPID_VALUE)
                    {
                        getItem = method;
                    }
                } finally {
                    if (funcDescHandleToRelease != IntPtr.Zero)
                    {
                        typeInfo.ReleaseFuncDesc(funcDescHandleToRelease);
                    }
                }
            }

            ProcessPut(funcs, puts, usedDispIds, ref setItem);
            ProcessPut(funcs, putrefs, usedDispIds, ref setItem);

            lock (_CacheComTypeDesc) {
                ComTypeDesc cachedTypeDesc;
                if (_CacheComTypeDesc.TryGetValue(typeAttr.guid, out cachedTypeDesc))
                {
                    _comTypeDesc = cachedTypeDesc;
                }
                else
                {
                    _comTypeDesc = typeDesc;
                    _CacheComTypeDesc.Add(typeAttr.guid, _comTypeDesc);
                }
                _comTypeDesc.Funcs   = funcs;
                _comTypeDesc.Puts    = puts;
                _comTypeDesc.PutRefs = putrefs;
                _comTypeDesc.GetItem = getItem;
                _comTypeDesc.SetItem = setItem;
            }
        }
Example #15
0
 internal bool TryGetMemberMethod(string name, out ComMethodDesc method)
 {
     EnsureScanDefinedMethods();
     return(_comTypeDesc.Funcs.TryGetValue(name, out method));
 }
Example #16
0
        private static void ProcessPut(Dictionary<string, ComMethodDesc> funcs, Dictionary<string, ComMethodDesc> methods, Set<int> usedDispIds, ref ComMethodDesc setItem) {
            foreach (ComMethodDesc method in methods.Values) {
                if (!usedDispIds.Contains(method.DispId)) {
                    funcs.Add(method.Name, method);
                    usedDispIds.Add(method.DispId);
                }

                // for the special dispId == 0, we need to store
                // the method descriptor for the Do(SetItem) binder. 
                if (method.DispId == ComDispIds.DISPID_VALUE && setItem == null) {
                    setItem = method;
                }
            }
        }
Example #17
0
        internal bool TryGetPropertySetter(string name, out ComMethodDesc method, Type limitType) {
            EnsureScanDefinedMethods();

            if (ComBinderHelpers.PreferPut(limitType)) {
                return _comTypeDesc.Puts.TryGetValue(name, out method) ||
                    _comTypeDesc.PutRefs.TryGetValue(name, out method);
            } else {
                return _comTypeDesc.PutRefs.TryGetValue(name, out method) ||
                    _comTypeDesc.Puts.TryGetValue(name, out method);
            }
        }
Example #18
0
 public static DispCallable CreateDispCallable(IDispatch dispatch, ComMethodDesc method) {
     return new DispCallable(dispatch, method);
 }
Example #19
0
 internal DispCallable(IDispatch dispatch, ComMethodDesc methodDesc)
 {
     _dispatch   = dispatch;
     _methodDesc = methodDesc;
 }
        private MetaObject BindGetMember(ComMethodDesc method) {
            Restrictions restrictions = IDispatchRestriction();
            Expression dispatch =
                Expression.Property(
                    Helpers.Convert(Expression, typeof(IDispatchComObject)),
                    typeof(IDispatchComObject).GetProperty("DispatchObject")
                );

            if (method.IsDataMember) {
                if (method.Parameters.Length == 0) {
                    return new ComInvokeBinder(
                        new ArgumentInfo[0],
                        MetaObject.EmptyMetaObjects,
                        restrictions,
                        Expression.Constant(method),
                        dispatch,
                        method
                    ).Invoke();
                }
            }

            return new MetaObject(
                Expression.Call(
                    typeof(ComRuntimeHelpers).GetMethod("CreateDispCallable"),
                    dispatch,
                    Expression.Constant(method)
                ),
                restrictions
            );
        }