Esempio n. 1
0
        /// <summary>
        /// Retrieve the dispid of the property that we are to use as the name
        /// member.  In this case, the grid will put parens around the name.
        /// </summary>
        public static int GetNameDispId(UnsafeNativeMethods.IDispatch obj)
        {
            int dispid = NativeMethods.DISPID_UNKNOWN;

            string[] names = null;

            ComNativeDescriptor cnd = ComNativeDescriptor.Instance;
            bool succeeded          = false;

            // first try to find one with a valid value
            cnd.GetPropertyValue(obj, "__id", ref succeeded);

            if (succeeded)
            {
                names = new string[] { "__id" };
            }
            else
            {
                cnd.GetPropertyValue(obj, NativeMethods.ActiveX.DISPID_Name, ref succeeded);
                if (succeeded)
                {
                    dispid = NativeMethods.ActiveX.DISPID_Name;
                }
                else
                {
                    cnd.GetPropertyValue(obj, "Name", ref succeeded);
                    if (succeeded)
                    {
                        names = new string[] { "Name" };
                    }
                }
            }

            // now get the dispid of the one that worked...
            if (names != null)
            {
                int[] pDispid = new int[] { NativeMethods.DISPID_UNKNOWN };
                Guid  g       = Guid.Empty;
                int   hr      = obj.GetIDsOfNames(ref g, names, 1, SafeNativeMethods.GetThreadLCID(), pDispid);
                if (NativeMethods.Succeeded(hr))
                {
                    dispid = pDispid[0];
                }
            }

            return(dispid);
        }
Esempio n. 2
0
        /// <summary>
        ///  Retrieve the dispid of the property that we are to use as the name
        ///  member.  In this case, the grid will put parens around the name.
        /// </summary>
        public unsafe static Ole32.DispatchID GetNameDispId(UnsafeNativeMethods.IDispatch obj)
        {
            Ole32.DispatchID dispid = Ole32.DispatchID.UNKNOWN;
            string[]         names  = null;

            ComNativeDescriptor cnd = ComNativeDescriptor.Instance;
            bool succeeded          = false;

            // first try to find one with a valid value
            cnd.GetPropertyValue(obj, "__id", ref succeeded);

            if (succeeded)
            {
                names = new string[] { "__id" };
            }
            else
            {
                cnd.GetPropertyValue(obj, Ole32.DispatchID.Name, ref succeeded);
                if (succeeded)
                {
                    dispid = Ole32.DispatchID.Name;
                }
                else
                {
                    cnd.GetPropertyValue(obj, "Name", ref succeeded);
                    if (succeeded)
                    {
                        names = new string[] { "Name" };
                    }
                }
            }

            // now get the dispid of the one that worked...
            if (names != null)
            {
                Ole32.DispatchID pDispid = Ole32.DispatchID.UNKNOWN;
                Guid             g       = Guid.Empty;
                HRESULT          hr      = obj.GetIDsOfNames(&g, names, 1, Kernel32.GetThreadLocale(), &pDispid);
                if (hr.Succeeded())
                {
                    dispid = pDispid;
                }
            }

            return(dispid);
        }
        internal int GetPropertyValue(object component, int dispid, object[] retval)
        {
            if (!(component is UnsafeNativeMethods.IDispatch))
            {
                return(NativeMethods.E_NOINTERFACE);
            }
            UnsafeNativeMethods.IDispatch iDispatch = (UnsafeNativeMethods.IDispatch)component;
            try
            {
                Guid g = Guid.Empty;
                NativeMethods.tagEXCEPINFO pExcepInfo = new NativeMethods.tagEXCEPINFO();
                int hr;

                try
                {
                    hr = iDispatch.Invoke(dispid,
                                          ref g,
                                          SafeNativeMethods.GetThreadLCID(),
                                          NativeMethods.DISPATCH_PROPERTYGET,
                                          new NativeMethods.tagDISPPARAMS(),
                                          retval,
                                          pExcepInfo, null);

                    /*if (hr != NativeMethods.S_OK){
                     * Com2PropertyDescriptor.PrintExceptionInfo(pExcepInfo);
                     *
                     * } */
                    if (hr == NativeMethods.DISP_E_EXCEPTION)
                    {
                        hr = pExcepInfo.scode;
                    }
                }
                catch (ExternalException ex)
                {
                    hr = ex.ErrorCode;
                }
                return(hr);
            }
            catch
            {
                //Debug.Fail(e.ToString() + " " + component.GetType().GUID.ToString() + " " + component.ToString());
            }
            return(NativeMethods.E_FAIL);
        }
        /// <include file='doc\COM2TypeInfoProcessor.uex' path='docs/doc[@for="Com2TypeInfoProcessor.FindTypeInfo"]/*' />
        /// <devdoc>
        /// Given an Object, this attempts to locate its type ifo
        /// </devdoc>
        public static UnsafeNativeMethods.ITypeInfo FindTypeInfo(object obj, bool wantCoClass)
        {
            UnsafeNativeMethods.ITypeInfo pTypeInfo = null;

            // this is kind of odd.  What's going on here is that
            // if we want the CoClass (e.g. for the interface name),
            // we need to look for IProvideClassInfo first, then
            // look for the typeinfo from the IDispatch.
            // In the case of many OleAut32 operations, the CoClass
            // doesn't have the interface members on it, although
            // in the shell it usually does, so
            // we need to re-order the lookup if we _actually_ want
            // the CoClass if it's available.
            //

            for (int i = 0; pTypeInfo == null && i < 2; i++)
            {
                if (wantCoClass == (i == 0))
                {
                    if (obj is NativeMethods.IProvideClassInfo)
                    {
                        NativeMethods.IProvideClassInfo pProvideClassInfo = (NativeMethods.IProvideClassInfo)obj;
                        try {
                            pTypeInfo = pProvideClassInfo.GetClassInfo();
                        }
                        catch {
                        }
                    }
                }
                else
                {
                    if (obj is UnsafeNativeMethods.IDispatch)
                    {
                        UnsafeNativeMethods.IDispatch iDispatch = (UnsafeNativeMethods.IDispatch)obj;
                        try {
                            pTypeInfo = iDispatch.GetTypeInfo(0, SafeNativeMethods.GetThreadLCID());
                        }
                        catch {
                        }
                    }
                }
            }
            return(pTypeInfo);
        }
Esempio n. 5
0
        internal unsafe HRESULT GetPropertyValue(object component, Ole32.DispatchID dispid, object[] retval)
        {
            if (!(component is UnsafeNativeMethods.IDispatch))
            {
                return(HRESULT.E_NOINTERFACE);
            }

            UnsafeNativeMethods.IDispatch iDispatch = (UnsafeNativeMethods.IDispatch)component;
            try
            {
                Guid g          = Guid.Empty;
                var  pExcepInfo = new Ole32.EXCEPINFO();
                var  dispParams = new Ole32.DISPPARAMS();
                try
                {
                    HRESULT hr = iDispatch.Invoke(
                        dispid,
                        &g,
                        Kernel32.GetThreadLocale(),
                        NativeMethods.DISPATCH_PROPERTYGET,
                        &dispParams,
                        retval,
                        &pExcepInfo,
                        null);
                    if (hr == HRESULT.DISP_E_EXCEPTION)
                    {
                        return(pExcepInfo.scode);
                    }

                    return(hr);
                }
                catch (ExternalException ex)
                {
                    return((HRESULT)ex.ErrorCode);
                }
            }
            catch
            {
            }

            return(HRESULT.E_FAIL);
        }
Esempio n. 6
0
        public object GetNativeValue(object component)
        {
            if (component == null)
            {
                return(null);
            }
            if (component is ICustomTypeDescriptor)
            {
                component = ((ICustomTypeDescriptor)component).GetPropertyOwner(this);
            }
            if (((component == null) || !Marshal.IsComObject(component)) || !(component is UnsafeNativeMethods.IDispatch))
            {
                return(null);
            }
            UnsafeNativeMethods.IDispatch dispatch = (UnsafeNativeMethods.IDispatch)component;
            object[] pVarResult = new object[1];
            System.Windows.Forms.NativeMethods.tagEXCEPINFO pExcepInfo = new System.Windows.Forms.NativeMethods.tagEXCEPINFO();
            Guid empty     = Guid.Empty;
            int  errorCode = dispatch.Invoke(this.dispid, ref empty, SafeNativeMethods.GetThreadLCID(), 2, new System.Windows.Forms.NativeMethods.tagDISPPARAMS(), pVarResult, pExcepInfo, null);

            switch (errorCode)
            {
            case 0:
            case 1:
                if ((pVarResult[0] != null) && !Convert.IsDBNull(pVarResult[0]))
                {
                    this.lastValue = pVarResult[0];
                    break;
                }
                this.lastValue = null;
                break;

            case -2147352567:
                return(null);

            default:
                throw new ExternalException(System.Windows.Forms.SR.GetString("DispInvokeFailed", new object[] { "GetValue", errorCode }), errorCode);
            }
            return(this.lastValue);
        }
Esempio n. 7
0
        public static int GetNameDispId(UnsafeNativeMethods.IDispatch obj)
        {
            int num = -1;

            string[]            rgszNames = null;
            ComNativeDescriptor instance  = ComNativeDescriptor.Instance;
            bool succeeded = false;

            instance.GetPropertyValue(obj, "__id", ref succeeded);
            if (succeeded)
            {
                rgszNames = new string[] { "__id" };
            }
            else
            {
                instance.GetPropertyValue(obj, -800, ref succeeded);
                if (succeeded)
                {
                    num = -800;
                }
                else
                {
                    instance.GetPropertyValue(obj, "Name", ref succeeded);
                    if (succeeded)
                    {
                        rgszNames = new string[] { "Name" };
                    }
                }
            }
            if (rgszNames != null)
            {
                int[] rgDispId = new int[] { -1 };
                Guid  empty    = Guid.Empty;
                if (System.Windows.Forms.NativeMethods.Succeeded(obj.GetIDsOfNames(ref empty, rgszNames, 1, SafeNativeMethods.GetThreadLCID(), rgDispId)))
                {
                    num = rgDispId[0];
                }
            }
            return(num);
        }
Esempio n. 8
0
            /// <include file='doc\COM2AboutBoxPropertyDescriptor.uex' path='docs/doc[@for="Com2AboutBoxPropertyDescriptor.AboutBoxUITypeEditor.EditValue"]/*' />
            /// <devdoc>
            ///      Edits the given object value using the editor style provided by
            ///      GetEditorStyle.  A service provider is provided so that any
            ///      required editing services can be obtained.
            /// </devdoc>
            public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
            {
                object component = context.Instance;

                if (Marshal.IsComObject(component) && component is UnsafeNativeMethods.IDispatch)
                {
                    UnsafeNativeMethods.IDispatch pDisp      = (UnsafeNativeMethods.IDispatch)component;
                    NativeMethods.tagEXCEPINFO    pExcepInfo = new NativeMethods.tagEXCEPINFO();
                    Guid g = Guid.Empty;

                    int hr = pDisp.Invoke(NativeMethods.ActiveX.DISPID_ABOUTBOX,
                                          ref g,
                                          SafeNativeMethods.GetThreadLCID(),
                                          NativeMethods.DISPATCH_METHOD,
                                          new NativeMethods.tagDISPPARAMS(),
                                          null,
                                          pExcepInfo, null);

                    Debug.Assert(NativeMethods.Succeeded(hr), "Failed to launch about box.");
                }
                return(value);
            }
Esempio n. 9
0
            public unsafe override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
            {
                object component = context.Instance;

                if (Marshal.IsComObject(component) && component is UnsafeNativeMethods.IDispatch)
                {
                    UnsafeNativeMethods.IDispatch pDisp = (UnsafeNativeMethods.IDispatch)component;
                    var     pExcepInfo = new Ole32.EXCEPINFO();
                    var     dispParams = new Ole32.DISPPARAMS();
                    Guid    g          = Guid.Empty;
                    HRESULT hr         = pDisp.Invoke(
                        Ole32.DispatchID.ABOUTBOX,
                        &g,
                        Kernel32.GetThreadLocale(),
                        Oleaut32.DISPATCH.METHOD,
                        &dispParams,
                        null,
                        &pExcepInfo,
                        null);
                    Debug.Assert(hr.Succeeded(), "Failed to launch about box.");
                }

                return(value);
            }
Esempio n. 10
0
        public override void SetValue(object component, object value)
        {
            if (this.readOnly)
            {
                throw new NotSupportedException(System.Windows.Forms.SR.GetString("COM2ReadonlyProperty", new object[] { this.Name }));
            }
            if (component is ICustomTypeDescriptor)
            {
                component = ((ICustomTypeDescriptor)component).GetPropertyOwner(this);
            }
            if (((component != null) && Marshal.IsComObject(component)) && (component is UnsafeNativeMethods.IDispatch))
            {
                if (this.valueConverter != null)
                {
                    bool cancelSet = false;
                    value = this.valueConverter.ConvertManagedToNative(value, this, ref cancelSet);
                    if (cancelSet)
                    {
                        return;
                    }
                }
                UnsafeNativeMethods.IDispatch dispatch = (UnsafeNativeMethods.IDispatch)component;
                System.Windows.Forms.NativeMethods.tagDISPPARAMS pDispParams = new System.Windows.Forms.NativeMethods.tagDISPPARAMS();
                System.Windows.Forms.NativeMethods.tagEXCEPINFO  pExcepInfo  = new System.Windows.Forms.NativeMethods.tagEXCEPINFO();
                pDispParams.cArgs      = 1;
                pDispParams.cNamedArgs = 1;
                int[]    numArray = new int[] { -3 };
                GCHandle handle   = GCHandle.Alloc(numArray, GCHandleType.Pinned);
                try
                {
                    pDispParams.rgdispidNamedArgs = Marshal.UnsafeAddrOfPinnedArrayElement(numArray, 0);
                    IntPtr ptr = Marshal.AllocCoTaskMem(0x10);
                    SafeNativeMethods.VariantInit(new HandleRef(null, ptr));
                    Marshal.GetNativeVariantForObject(value, ptr);
                    pDispParams.rgvarg = ptr;
                    try
                    {
                        Guid   empty       = Guid.Empty;
                        int    dwMessageId = dispatch.Invoke(this.dispid, ref empty, SafeNativeMethods.GetThreadLCID(), 4, pDispParams, null, pExcepInfo, new IntPtr[1]);
                        string message     = null;
                        if ((dwMessageId == -2147352567) && (pExcepInfo.scode != 0))
                        {
                            dwMessageId = pExcepInfo.scode;
                            message     = pExcepInfo.bstrDescription;
                        }
                        switch (dwMessageId)
                        {
                        case -2147467260:
                        case -2147221492:
                            return;

                        case 0:
                        case 1:
                            this.OnValueChanged(component, EventArgs.Empty);
                            this.lastValue = value;
                            return;
                        }
                        if (dispatch is UnsafeNativeMethods.ISupportErrorInfo)
                        {
                            empty = typeof(UnsafeNativeMethods.IDispatch).GUID;
                            if (System.Windows.Forms.NativeMethods.Succeeded(((UnsafeNativeMethods.ISupportErrorInfo)dispatch).InterfaceSupportsErrorInfo(ref empty)))
                            {
                                UnsafeNativeMethods.IErrorInfo errorInfo = null;
                                UnsafeNativeMethods.GetErrorInfo(0, ref errorInfo);
                                if ((errorInfo != null) && System.Windows.Forms.NativeMethods.Succeeded(errorInfo.GetDescription(null)))
                                {
                                    message = pBstrDescription;
                                }
                            }
                        }
                        else if (message == null)
                        {
                            StringBuilder lpBuffer = new StringBuilder(0x100);
                            if (SafeNativeMethods.FormatMessage(0x1200, System.Windows.Forms.NativeMethods.NullHandleRef, dwMessageId, CultureInfo.CurrentCulture.LCID, lpBuffer, 0xff, System.Windows.Forms.NativeMethods.NullHandleRef) == 0)
                            {
                                message = string.Format(CultureInfo.CurrentCulture, System.Windows.Forms.SR.GetString("DispInvokeFailed", new object[] { "SetValue", dwMessageId }), new object[0]);
                            }
                            else
                            {
                                message = lpBuffer.ToString();
                                while (((message.Length > 0) && (message[message.Length - 1] == '\n')) || (message[message.Length - 1] == '\r'))
                                {
                                    message = message.Substring(0, message.Length - 1);
                                }
                            }
                        }
                        throw new ExternalException(message, dwMessageId);
                    }
                    finally
                    {
                        SafeNativeMethods.VariantClear(new HandleRef(null, ptr));
                        Marshal.FreeCoTaskMem(ptr);
                    }
                }
                finally
                {
                    handle.Free();
                }
            }
        }