public override IValue GetPropValue(int propNum)
        {
            var prop = _props[propNum];

            var dispId = prop.DispatchId;

            try
            {
                try
                {
                    var result = DispatchUtility.Invoke(Instance, dispId, null);
                    return(CreateIValue(result));
                }
                catch (System.Reflection.TargetInvocationException e)
                {
                    throw e.InnerException ?? e;
                }
            }
            catch (System.MissingMemberException)
            {
                throw RuntimeException.PropNotFoundException(prop.Name);
            }
            catch (System.MemberAccessException)
            {
                throw RuntimeException.PropIsNotReadableException(prop.Name);
            }
        }
        public override void CallAsFunction(int methodNumber, IValue[] arguments, out IValue retValue)
        {
            var method = _methods[methodNumber];

            if (!(method.IsFunction ?? true))
            {
                throw RuntimeException.UseProcAsAFunction();
            }

            var dispId = method.DispatchId;

            try
            {
                try
                {
                    var result = DispatchUtility.Invoke(Instance, dispId, MarshalArguments(arguments));
                    retValue = CreateIValue(result);
                }
                catch (System.Reflection.TargetInvocationException e)
                {
                    throw e.InnerException ?? e;
                }
            }
            catch (System.MissingMemberException)
            {
                throw RuntimeException.MethodNotFoundException(method.Name);
            }
        }
Пример #3
0
        public override int FindMethod(string name)
        {
            int dispId;

            if (!_dispIdCache.TryGetValue(name, out dispId))
            {
                if (DispatchUtility.TryGetDispId(_instance, name, out dispId))
                {
                    if (_dispatchedType != null)
                    {
                        var memberInfo = _dispatchedType.GetMember(name);
                        if (memberInfo.Length == 0 || !(memberInfo[0].MemberType == MemberTypes.Method || memberInfo[0].MemberType == MemberTypes.Property))
                        {
                            throw RuntimeException.MethodNotFoundException(name);
                        }
                        else
                        {
                            _membersCache.Add(dispId, memberInfo[0]);
                            _dispIdCache.Add(name, dispId);
                        }
                    }
                    else
                    {
                        _dispIdCache.Add(name, dispId);
                    }
                }
                else
                {
                    throw RuntimeException.MethodNotFoundException(name);
                }
            }

            return(dispId);
        }
Пример #4
0
 private void InitByInstance()
 {
     if (!DispatchUtility.ImplementsIDispatch(_instance))
     {
         _instance = null;
         throw new RuntimeException("Объект не реализует IDispatch.");
     }
 }
        private bool TryFindProperty(string name, out RcwPropertyMetadata md)
        {
            if (_props.Names.TryGetValue(name, out md))
            {
                return(true);
            }

            if (DispatchUtility.TryGetDispId(Instance, name, out var dispId))
            {
                md = new RcwPropertyMetadata(name, dispId);
                _props.Add(md);
                return(true);
            }

            return(false);
        }
        private bool TryFindMethod(string name, out RcwMethodMetadata md)
        {
            if (_methods.Names.TryGetValue(name, out md))
            {
                return(true);
            }

            if (DispatchUtility.TryGetDispId(Instance, name, out var dispId))
            {
                md = new RcwMethodMetadata(name, dispId, null);
                _methods.Add(md);
                return(true);
            }

            return(false);
        }
Пример #7
0
        public static IValue CreateIValue(object objParam)
        {
            if (objParam == null)
            {
                return(ValueFactory.Create());
            }

            var type = objParam.GetType();

            if (typeof(IValue).IsAssignableFrom(type))
            {
                return((IValue)objParam);
            }
            else if (type == typeof(string))
            {
                return(ValueFactory.Create((string)objParam));
            }
            else if (type == typeof(int))
            {
                return(ValueFactory.Create((int)objParam));
            }
            else if (type == typeof(double))
            {
                return(ValueFactory.Create((decimal)objParam));
            }
            else if (type == typeof(DateTime))
            {
                return(ValueFactory.Create((DateTime)objParam));
            }
            else if (type == typeof(bool))
            {
                return(ValueFactory.Create((bool)objParam));
            }
            else if (DispatchUtility.ImplementsIDispatch(objParam))
            {
                var ctx = new COMWrapperContext(objParam);
                return(ValueFactory.Create(ctx));
            }
            else if (type.IsArray)
            {
                return(new SafeArrayWrapper(objParam));
            }
            else
            {
                throw new RuntimeException("Тип " + type + " невозможно преобразовать в один из поддерживаемых типов");
            }
        }
Пример #8
0
        private void InitByInstance()
        {
            if (!DispatchUtility.ImplementsIDispatch(_instance))
            {
                _instance = null;
                throw new RuntimeException("Объект не реализует IDispatch.");
            }

            try
            {
                _dispatchedType = DispatchUtility.GetType(_instance, false);
            }
            catch
            {
                _instance = null;
                throw;
            }
        }
Пример #9
0
 public override void CallAsProcedure(int methodNumber, IValue[] arguments)
 {
     try
     {
         try
         {
             DispatchUtility.Invoke(_instance, methodNumber, MarshalArguments(arguments));
         }
         catch (System.Reflection.TargetInvocationException e)
         {
             throw e.InnerException;
         }
     }
     catch (System.MissingMemberException)
     {
         throw RuntimeException.MethodNotFoundException("dispid[" + methodNumber.ToString() + "]");
     }
 }
Пример #10
0
        public override int FindMethod(string name)
        {
            int dispId;

            if (!_dispIdCache.TryGetValue(name, out dispId))
            {
                if (DispatchUtility.TryGetDispId(_instance, name, out dispId))
                {
                    _dispIdCache.Add(name, dispId);
                }
                else
                {
                    throw RuntimeException.MethodNotFoundException(name);
                }
            }

            return(dispId);
        }
Пример #11
0
 public override void CallAsFunction(int methodNumber, IValue[] arguments, out IValue retValue)
 {
     try
     {
         try
         {
             var result = DispatchUtility.Invoke(_instance, methodNumber, MarshalArguments(arguments));
             retValue = CreateIValue(result);
         }
         catch (System.Reflection.TargetInvocationException e)
         {
             throw e.InnerException;
         }
     }
     catch (System.MissingMemberException)
     {
         throw RuntimeException.MethodNotFoundException("dispid[" + methodNumber.ToString() + "]");
     }
 }
Пример #12
0
        public override void SetPropValue(int propNum, IValue newVal)
        {
            var prop = _props[propNum];

            var dispId = prop.DispatchId;

            try
            {
                try
                {
                    object argToPass;
                    if (newVal.DataType == Machine.DataType.Date)
                    {
                        var date = newVal.AsDate();
                        if (date == DateTime.MinValue)
                        {
                            argToPass = new DateTime(100, 1, 1); // Min OLEAuth Date
                        }
                        else
                        {
                            argToPass = MarshalIValue(newVal);
                        }
                    }
                    else
                    {
                        argToPass = MarshalIValue(newVal);
                    }
                    DispatchUtility.InvokeSetProperty(Instance, dispId, argToPass);
                }
                catch (System.Reflection.TargetInvocationException e)
                {
                    throw e.InnerException ?? e;
                }
            }
            catch (System.MissingMemberException)
            {
                throw RuntimeException.PropNotFoundException(prop.Name);
            }
            catch (System.MemberAccessException)
            {
                throw RuntimeException.PropIsNotWritableException(prop.Name);
            }
        }
Пример #13
0
        public int FindMemberIndex(string name)
        {
            int knownDiIndex;

            if (!_dispIdIndexes.TryGetValue(name, out knownDiIndex))
            {
                int dispId;
                if (DispatchUtility.TryGetDispId(_instance, name, out dispId))
                {
                    knownDiIndex = _dispIds.Count;
                    _dispIds.Add(dispId);
                    _dispIdIndexes.Add(name, knownDiIndex);
                }
                else
                {
                    knownDiIndex = -1;
                }
            }

            return(knownDiIndex);
        }
Пример #14
0
 public override void SetPropValue(int propNum, IValue newVal)
 {
     try
     {
         try
         {
             object argToPass;
             if (newVal.DataType == Machine.DataType.Date)
             {
                 var date = newVal.AsDate();
                 if (date == DateTime.MinValue)
                 {
                     argToPass = new DateTime(100, 1, 1); // Min OLEAuth Date
                 }
                 else
                 {
                     argToPass = MarshalIValue(newVal);
                 }
             }
             else
             {
                 argToPass = MarshalIValue(newVal);
             }
             DispatchUtility.InvokeSetProperty(_instance, propNum, argToPass);
         }
         catch (System.Reflection.TargetInvocationException e)
         {
             throw e.InnerException;
         }
     }
     catch (System.MissingMemberException)
     {
         throw RuntimeException.PropNotFoundException("dispid[" + propNum.ToString() + "]");
     }
     catch (System.MemberAccessException)
     {
         throw RuntimeException.PropIsNotWritableException("dispid[" + propNum.ToString() + "]");
     }
 }
Пример #15
0
 public override void SetPropValue(int propNum, IValue newVal)
 {
     try
     {
         try
         {
             DispatchUtility.InvokeSetProperty(_instance, propNum, MarshalIValue(newVal));
         }
         catch (System.Reflection.TargetInvocationException e)
         {
             throw e.InnerException;
         }
     }
     catch (System.MissingMemberException)
     {
         throw RuntimeException.PropNotFoundException("dispid[" + propNum.ToString() + "]");
     }
     catch (System.MemberAccessException)
     {
         throw RuntimeException.PropIsNotWritableException("dispid[" + propNum.ToString() + "]");
     }
 }
Пример #16
0
        public override void CallAsProcedure(int methodNumber, IValue[] arguments)
        {
            var method = _methods[methodNumber];

            var dispId = method.DispatchId;

            try
            {
                try
                {
                    DispatchUtility.Invoke(Instance, dispId, MarshalArguments(arguments));
                }
                catch (System.Reflection.TargetInvocationException e)
                {
                    throw e.InnerException ?? e;
                }
            }
            catch (System.MissingMemberException)
            {
                throw RuntimeException.MethodNotFoundException(method.Name);
            }
        }
Пример #17
0
 public override IValue GetPropValue(int propNum)
 {
     try
     {
         try
         {
             var result = DispatchUtility.Invoke(_instance, propNum, null);
             return(CreateIValue(result));
         }
         catch (System.Reflection.TargetInvocationException e)
         {
             throw e.InnerException;
         }
     }
     catch (System.MissingMemberException)
     {
         throw RuntimeException.PropNotFoundException("dispid[" + propNum.ToString() + "]");
     }
     catch (System.MemberAccessException)
     {
         throw RuntimeException.PropIsNotReadableException("dispid[" + propNum.ToString() + "]");
     }
 }