Beispiel #1
0
        /// <summary>
        /// Get value of this property
        /// </summary>
        /// <param name="target">instance of the object from which to get the property value</param>
        /// <returns>value of the property</returns>
        internal object GetValue(Object target)
        {
            try
            {
                return(ComInvoker.Invoke(target as IDispatch, _dispId, null, null, COM.INVOKEKIND.INVOKE_PROPERTYGET));
            }
            catch (TargetInvocationException te)
            {
                //First check if this is a severe exception.
                CommandProcessorBase.CheckForSevereException(te.InnerException);

                var innerCom = te.InnerException as COMException;
                if (innerCom == null || innerCom.HResult != ComUtil.DISP_E_MEMBERNOTFOUND)
                {
                    throw;
                }
            }
            catch (COMException ce)
            {
                if (ce.HResult != ComUtil.DISP_E_UNKNOWNNAME)
                {
                    throw;
                }
            }

            return(null);
        }
Beispiel #2
0
        /// <summary>
        /// Sets value of this property.
        /// </summary>
        /// <param name="target">instance of the object to which to set the property value.</param>
        /// <param name="setValue">value to set this property.</param>
        internal void SetValue(Object target, object setValue)
        {
            object[] propValue = new object[1];
            setValue     = Adapter.PropertySetAndMethodArgumentConvertTo(setValue, this.Type, CultureInfo.InvariantCulture);
            propValue[0] = setValue;

            try
            {
                ComInvoker.Invoke(target as IDispatch, _dispId, propValue, null, COM.INVOKEKIND.INVOKE_PROPERTYPUT);
            }
            catch (TargetInvocationException te)
            {
                var innerCom = te.InnerException as COMException;
                if (innerCom == null || innerCom.HResult != ComUtil.DISP_E_MEMBERNOTFOUND)
                {
                    throw;
                }
            }
            catch (COMException ce)
            {
                if (ce.HResult != ComUtil.DISP_E_UNKNOWNNAME)
                {
                    throw;
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Try to create an enumerator for a COM object.
        /// </summary>
        /// <returns>
        /// A 'ComEnumerator' instance, or null if we cannot create an enumerator for the COM object.
        /// </returns>
        internal static ComEnumerator Create(object comObject)
        {
            if (comObject == null || !comObject.GetType().IsCOMObject)
            {
                return(null);
            }

            // The passed-in COM object could already be a IEnumVARIANT interface.
            // e.g. user call '_NewEnum()' on a COM collection interface.
            var enumVariant = comObject as COM.IEnumVARIANT;

            if (enumVariant != null)
            {
                return(new ComEnumerator(enumVariant));
            }

            // The passed-in COM object could be a collection.
            var enumerable = comObject as IEnumerable;
            var target     = comObject as IDispatch;

            if (enumerable != null && target != null)
            {
                try
                {
                    var comTypeInfo = ComTypeInfo.GetDispatchTypeInfo(comObject);
                    if (comTypeInfo != null && comTypeInfo.NewEnumInvokeKind.HasValue)
                    {
                        // The COM object is a collection and also a IDispatch interface, so we try to get a
                        // IEnumVARIANT interface out of it by invoking its '_NewEnum (DispId: -4)' function.
                        var result = ComInvoker.Invoke(target, ComTypeInfo.DISPID_NEWENUM,
                                                       args: Utils.EmptyArray <object>(), byRef: null,
                                                       invokeKind: comTypeInfo.NewEnumInvokeKind.Value);
                        enumVariant = result as COM.IEnumVARIANT;
                        if (enumVariant != null)
                        {
                            return(new ComEnumerator(enumVariant));
                        }
                    }
                }
                catch (Exception)
                {
                    // Ignore exceptions. In case of exception, no enumerator can be created
                    // for the passed-in COM object, and we will return null eventually.
                }
            }

            return(null);
        }
Beispiel #4
0
        /// <summary>
        /// Sets the value of the property.
        /// </summary>
        /// <param name="target">instance of the object to which to set the property value</param>
        /// <param name="setValue">value to set this property</param>
        /// <param name="arguments">parameters to set this property.</param>
        internal void SetValue(Object target, Object setValue, Object[] arguments)
        {
            object[] newarguments;
            var      setterCollection = new Collection <int> {
                _hasSetterByRef?_setterByRefIndex : _setterIndex
            };
            var methods    = ComUtil.GetMethodInformationArray(_typeInfo, setterCollection, true);
            var bestMethod = (ComMethodInformation)Adapter.GetBestMethodAndArguments(Name, methods, arguments, out newarguments);

            var finalArguments = new object[newarguments.Length + 1];

            for (int i = 0; i < newarguments.Length; i++)
            {
                finalArguments[i] = newarguments[i];
            }
            finalArguments[newarguments.Length] = Adapter.PropertySetAndMethodArgumentConvertTo(setValue, Type, CultureInfo.InvariantCulture);

            try
            {
                ComInvoker.Invoke(target as IDispatch,
                                  bestMethod.DispId,
                                  finalArguments,
                                  ComInvoker.GetByRefArray(bestMethod.parameters,
                                                           finalArguments.Length,
                                                           isPropertySet: true),
                                  bestMethod.InvokeKind);
                Adapter.SetReferences(finalArguments, bestMethod, arguments);
            }
            catch (TargetInvocationException te)
            {
                //First check if this is a severe exception.
                CommandProcessorBase.CheckForSevereException(te.InnerException);

                var innerCom = te.InnerException as COMException;
                if (innerCom == null || innerCom.HResult != ComUtil.DISP_E_MEMBERNOTFOUND)
                {
                    throw;
                }
            }
            catch (COMException ce)
            {
                if (ce.HResult != ComUtil.DISP_E_UNKNOWNNAME)
                {
                    throw;
                }
            }
        }
Beispiel #5
0
        /// <summary>
        ///  Invokes the method on object
        /// </summary>
        /// <param name="method">represents the instance of the method we want to invoke</param>
        /// <param name="arguments">parameters to be passed to the method</param>
        /// <returns>returns the value of method call</returns>
        internal object InvokeMethod(PSMethod method, object[] arguments)
        {
            try
            {
                object[] newarguments;
                var      methods    = ComUtil.GetMethodInformationArray(_typeInfo, _methods, false);
                var      bestMethod = (ComMethodInformation)Adapter.GetBestMethodAndArguments(Name, methods, arguments, out newarguments);

                object returnValue = ComInvoker.Invoke(method.baseObject as IDispatch,
                                                       bestMethod.DispId, newarguments,
                                                       ComInvoker.GetByRefArray(bestMethod.parameters,
                                                                                newarguments.Length,
                                                                                isPropertySet: false),
                                                       COM.INVOKEKIND.INVOKE_FUNC);
                Adapter.SetReferences(newarguments, bestMethod, arguments);
                return(bestMethod.ReturnType != typeof(void) ? returnValue : AutomationNull.Value);
            }
            catch (TargetInvocationException te)
            {
                //First check if this is a severe exception.
                CommandProcessorBase.CheckForSevereException(te.InnerException);

                var innerCom = te.InnerException as COMException;
                if (innerCom == null || innerCom.HResult != ComUtil.DISP_E_MEMBERNOTFOUND)
                {
                    string message = te.InnerException == null ? te.Message : te.InnerException.Message;
                    throw new MethodInvocationException(
                              "ComMethodTargetInvocation",
                              te,
                              ExtendedTypeSystem.MethodInvocationException,
                              method.Name, arguments.Length, message);
                }
            }
            catch (COMException ce)
            {
                if (ce.HResult != ComUtil.DISP_E_UNKNOWNNAME)
                {
                    throw new MethodInvocationException(
                              "ComMethodCOMException",
                              ce,
                              ExtendedTypeSystem.MethodInvocationException,
                              method.Name, arguments.Length, ce.Message);
                }
            }
            return(null);
        }
Beispiel #6
0
        /// <summary>
        /// Get value of this property
        /// </summary>
        /// <param name="target">instance of the object from which to get the property value</param>
        /// <param name="arguments">parameters to get the property value</param>
        /// <returns>value of the property</returns>
        internal object GetValue(Object target, Object[] arguments)
        {
            try
            {
                object[] newarguments;
                var      getterCollection = new Collection <int> {
                    _getterIndex
                };
                var methods    = ComUtil.GetMethodInformationArray(_typeInfo, getterCollection, false);
                var bestMethod = (ComMethodInformation)Adapter.GetBestMethodAndArguments(Name, methods, arguments, out newarguments);

                object returnValue = ComInvoker.Invoke(target as IDispatch,
                                                       bestMethod.DispId,
                                                       newarguments,
                                                       ComInvoker.GetByRefArray(bestMethod.parameters,
                                                                                newarguments.Length,
                                                                                isPropertySet: false),
                                                       bestMethod.InvokeKind);
                Adapter.SetReferences(newarguments, bestMethod, arguments);
                return(returnValue);
            }
            catch (TargetInvocationException te)
            {
                //First check if this is a severe exception.
                CommandProcessorBase.CheckForSevereException(te.InnerException);

                var innerCom = te.InnerException as COMException;
                if (innerCom == null || innerCom.HResult != ComUtil.DISP_E_MEMBERNOTFOUND)
                {
                    throw;
                }
            }
            catch (COMException ce)
            {
                if (ce.HResult != ComUtil.DISP_E_UNKNOWNNAME)
                {
                    throw;
                }
            }

            return(null);
        }
Beispiel #7
0
        /// <summary>
        /// Get value of this property
        /// </summary>
        /// <param name="target">instance of the object from which to get the property value.</param>
        /// <returns>Value of the property.</returns>
        internal object GetValue(Object target)
        {
            try
            {
                return(ComInvoker.Invoke(target as IDispatch, _dispId, null, null, COM.INVOKEKIND.INVOKE_PROPERTYGET));
            }
            catch (TargetInvocationException te)
            {
                var innerCom = te.InnerException as COMException;
                if (innerCom == null || innerCom.HResult != ComUtil.DISP_E_MEMBERNOTFOUND)
                {
                    throw;
                }
            }
            catch (COMException ce)
            {
                if (ce.HResult != ComUtil.DISP_E_UNKNOWNNAME)
                {
                    throw;
                }
            }

            return(null);
        }