예제 #1
0
 internal int GetPropertyValue(object component, int dispid, object[] retval)
 {
     if (!(component is UnsafeNativeMethods.IDispatch))
     {
         return(-2147467262);
     }
     UnsafeNativeMethods.IDispatch dispatch = (UnsafeNativeMethods.IDispatch)component;
     try
     {
         int  scode;
         Guid empty = Guid.Empty;
         System.Windows.Forms.NativeMethods.tagEXCEPINFO pExcepInfo = new System.Windows.Forms.NativeMethods.tagEXCEPINFO();
         try
         {
             scode = dispatch.Invoke(dispid, ref empty, SafeNativeMethods.GetThreadLCID(), 2, new System.Windows.Forms.NativeMethods.tagDISPPARAMS(), retval, pExcepInfo, null);
             if (scode == -2147352567)
             {
                 scode = pExcepInfo.scode;
             }
         }
         catch (ExternalException exception)
         {
             scode = exception.ErrorCode;
         }
         return(scode);
     }
     catch
     {
     }
     return(-2147467259);
 }
예제 #2
0
        public object InvokeMember(string methodName, params object[] parameter)
        {
            object retVal = null;

            NativeMethods.tagDISPPARAMS dp = new NativeMethods.tagDISPPARAMS();
            dp.rgvarg = IntPtr.Zero;
            try
            {
                UnsafeNativeMethods.IDispatch scriptObject = this.NativeHtmlElement as UnsafeNativeMethods.IDispatch;
                if (scriptObject != null)
                {
                    Guid     g       = Guid.Empty;
                    string[] names   = new string[] { methodName };
                    int[]    dispids = new int[] { NativeMethods.ActiveX.DISPID_UNKNOWN };
                    int      hr      = scriptObject.GetIDsOfNames(ref g, names, 1,
                                                                  SafeNativeMethods.GetThreadLCID(), dispids);
                    if (NativeMethods.Succeeded(hr) && (dispids[0] != NativeMethods.ActiveX.DISPID_UNKNOWN))
                    {
                        // Reverse the arg order below so that parms are read properly thru IDispatch. (
                        if (parameter != null)
                        {
                            // Reverse the parm order so that they read naturally after IDispatch. (
                            Array.Reverse(parameter);
                        }
                        dp.rgvarg            = (parameter == null) ? IntPtr.Zero : HtmlDocument.ArrayToVARIANTVector(parameter);
                        dp.cArgs             = (parameter == null) ? 0 : parameter.Length;
                        dp.rgdispidNamedArgs = IntPtr.Zero;
                        dp.cNamedArgs        = 0;

                        object[] retVals = new object[1];

                        hr = scriptObject.Invoke(dispids[0], ref g, SafeNativeMethods.GetThreadLCID(),
                                                 NativeMethods.DISPATCH_METHOD, dp,
                                                 retVals, new NativeMethods.tagEXCEPINFO(), null);
                        if (hr == NativeMethods.S_OK)
                        {
                            retVal = retVals[0];
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (ClientUtils.IsSecurityOrCriticalException(ex))
                {
                    throw;
                }
            }
            finally
            {
                if (dp.rgvarg != IntPtr.Zero)
                {
                    HtmlDocument.FreeVARIANTVector(dp.rgvarg, parameter.Length);
                }
            }
            return(retVal);
        }
예제 #3
0
            public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
            {
                object instance = context.Instance;

                if (Marshal.IsComObject(instance) && (instance is UnsafeNativeMethods.IDispatch))
                {
                    UnsafeNativeMethods.IDispatch dispatch = (UnsafeNativeMethods.IDispatch)instance;
                    System.Windows.Forms.NativeMethods.tagEXCEPINFO pExcepInfo = new System.Windows.Forms.NativeMethods.tagEXCEPINFO();
                    Guid empty = Guid.Empty;
                    dispatch.Invoke(-552, ref empty, SafeNativeMethods.GetThreadLCID(), 1, new System.Windows.Forms.NativeMethods.tagDISPPARAMS(), null, pExcepInfo, null);
                }
                return(value);
            }
예제 #4
0
        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);
        }
예제 #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);
        }
예제 #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);
        }
예제 #7
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);
            }
예제 #8
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);
            }
예제 #9
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();
                }
            }
        }