Ejemplo n.º 1
0
        public object TryGetFirstAvailableProperty(string[] propertyNames)
        {
            object      value   = null;
            ComTypeInfo comType = TryGetComTypeInfo();

            if (comType != null)
            {
                foreach (string propertyName in propertyNames)
                {
                    ComPropertyInfo comPropertyInfo = comType.Properties.Where(x => x.Name.Equals(propertyName)).FirstOrDefault();

                    if ((comPropertyInfo != null) && (comPropertyInfo.GetFunction != null))
                    {
                        if (MarshalEx.Succeeded(TryInvokePropertyGet(comPropertyInfo.GetFunction.DispId, out value)))
                        {
                            break;
                        }
                    }
                }
            }

            return(value);
        }
Ejemplo n.º 2
0
        public int TryGetItemCount()
        {
            IDispatch dispatch = null;
            object    count    = 0;

            try
            {
                dispatch = TryGetUniqueRCW <IDispatch>();
                if (MarshalEx.Succeeded(dispatch.InvokePropertyGet("Count", out count)))
                {
                    return(count is int?(int)count : -1);
                }
            }
            catch
            {
            }
            finally
            {
                CleanupUniqueRCW(dispatch);
            }

            return(-1);
        }
Ejemplo n.º 3
0
        public bool Is <T>()
        {
            if (this.IsInvalid)
            {
                return(false);
            }

            var    riid = typeof(T).GUID;
            IntPtr p    = IntPtr.Zero;

            try
            {
                if (MarshalEx.Succeeded(Marshal.QueryInterface(this.handle, ref riid, out p)))
                {
                    int count = Marshal.Release(p);
                    return(true);
                }
            }
            catch
            {
            }

            return(false);
        }
Ejemplo n.º 4
0
        public static int InvokePropertyGet(this IDispatch dispatch, int dispid, out object value)
        {
            int hr = NativeMethods.S_OK;

            value = null;

            var  guid       = Guid.Empty;
            var  lcid       = NativeMethods.LOCALE_SYSTEM_DEFAULT;
            var  flags      = System.Runtime.InteropServices.ComTypes.INVOKEKIND.INVOKE_PROPERTYGET;
            var  dp         = default(System.Runtime.InteropServices.ComTypes.DISPPARAMS);
            var  pExcepInfo = default(System.Runtime.InteropServices.ComTypes.EXCEPINFO);
            uint pArgErr    = 0;

            Variant variant = default(Variant);

            try
            {
                hr = dispatch.Invoke(
                    dispid,
                    ref guid,
                    lcid,
                    flags,
                    ref dp,
                    out variant,
                    ref pExcepInfo,
                    out pArgErr
                    );

                if (MarshalEx.Succeeded(hr))
                {
                    try
                    {
                        switch (variant.VariantType)
                        {
                        case VarEnum.VT_PTR:
                            GlobalExceptionHandler.HandleException();
                            break;

                        case VarEnum.VT_UNKNOWN:
                        case VarEnum.VT_DISPATCH:
                            if (variant.ptr1.Equals(IntPtr.Zero) == false)
                            {
                                value = new ComPtr(variant.ptr1);
                                int count = Marshal.Release(variant.ptr1);
                            }
                            break;

                        case VarEnum.VT_USERDEFINED:
                            value = variant.ToObject();
                            break;

                        default:
                            value = variant.ToObject();
                            break;
                        }
                    }
                    catch (COMException ex)
                    {
                        hr = ex.ErrorCode;
                    }
                }
            }
            catch
            {
                GlobalExceptionHandler.HandleException();
            }
            finally
            {
                variant.Clear();
            }

            return(hr);
        }
Ejemplo n.º 5
0
        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            if (_propertyDescriptorCollection == null)
            {
                _propertyDescriptorCollection = new PropertyDescriptorCollection(new PropertyDescriptor[] { }, true);

                ComTypeInfo comTypeInfo = TryGetComTypeInfo();

                if (comTypeInfo != null)
                {
                    List <ComPtrPropertyDescriptor> list = new List <ComPtrPropertyDescriptor>();

                    foreach (ComPropertyInfo comPropertyInfo in comTypeInfo.GetProperties(true))
                    {
                        ComFunctionInfo getComFunctionInfo = comPropertyInfo.GetFunction;
                        bool            bReadOnly          = comPropertyInfo.SetFunction == null ? true : false;

                        if (getComFunctionInfo != null)
                        {
                            VarEnum variantType = getComFunctionInfo.ReturnParameter.VariantType;

                            switch (variantType)
                            {
                            case VarEnum.VT_PTR:
                            case VarEnum.VT_DISPATCH:
                            case VarEnum.VT_UNKNOWN:
                                continue;

                            case VarEnum.VT_SAFEARRAY:
                                continue;
                            }

                            // Special case. MailSession is a PITA property that causes modal dialog.
                            if (comPropertyInfo.Name.Equals("MailSession"))
                            {
                                ComPtrProperty comPtrProperty = new ComPtrProperty(comPropertyInfo.Name, comPropertyInfo.Description, 0, typeof(int), variantType, true);
                                list.Add(new ComPtrPropertyDescriptor(ref comPtrProperty, comPropertyInfo, attributes));
                                continue;
                            }

                            object value = null;
                            if (MarshalEx.Succeeded(TryInvokePropertyGet(getComFunctionInfo.DispId, out value)))
                            {
                                Type propertyType = typeof(object);

                                if (value != null)
                                {
                                    propertyType = value.GetType();
                                }
                                else
                                {
                                    bReadOnly = true;
                                }

                                ComPtrProperty comPtrProperty = new ComPtrProperty(comPropertyInfo.Name, comPropertyInfo.Description, value, propertyType, variantType, bReadOnly);
                                list.Add(new ComPtrPropertyDescriptor(ref comPtrProperty, comPropertyInfo, attributes));
                            }
                        }
                    }

#if DEBUG
                    var refCountProperty = new ComPtrProperty("[RefCount]", "", this.RefCount, typeof(int), VarEnum.VT_I4, true);
                    list.Add(new ComPtrPropertyDescriptor(ref refCountProperty, null, attributes));
#endif
                    var pointerProperty = new ComPtrProperty("[Pointer]", "", this.handle, typeof(string), VarEnum.VT_INT, true);
                    list.Add(new ComPtrPropertyDescriptor(ref pointerProperty, null, attributes));

                    _propertyDescriptorCollection = new PropertyDescriptorCollection(list.ToArray());
                }
            }

            return(_propertyDescriptorCollection);
        }