public object InvokeScript(string scriptName, object[] args)
        {
            object obj2 = null;

            System.Windows.Forms.NativeMethods.tagDISPPARAMS pDispParams = new System.Windows.Forms.NativeMethods.tagDISPPARAMS {
                rgvarg = IntPtr.Zero
            };
            try
            {
                System.Windows.Forms.UnsafeNativeMethods.IDispatch script = this.NativeHtmlDocument2.GetScript() as System.Windows.Forms.UnsafeNativeMethods.IDispatch;
                if (script != null)
                {
                    Guid     empty     = Guid.Empty;
                    string[] rgszNames = new string[] { scriptName };
                    int[]    rgDispId  = new int[] { -1 };
                    if (!System.Windows.Forms.NativeMethods.Succeeded(script.GetIDsOfNames(ref empty, rgszNames, 1, System.Windows.Forms.SafeNativeMethods.GetThreadLCID(), rgDispId)) || (rgDispId[0] == -1))
                    {
                        return(obj2);
                    }
                    if (args != null)
                    {
                        Array.Reverse(args);
                    }
                    pDispParams.rgvarg            = (args == null) ? IntPtr.Zero : ArrayToVARIANTVector(args);
                    pDispParams.cArgs             = (args == null) ? 0 : args.Length;
                    pDispParams.rgdispidNamedArgs = IntPtr.Zero;
                    pDispParams.cNamedArgs        = 0;
                    object[] pVarResult = new object[1];
                    if (script.Invoke(rgDispId[0], ref empty, System.Windows.Forms.SafeNativeMethods.GetThreadLCID(), 1, pDispParams, pVarResult, new System.Windows.Forms.NativeMethods.tagEXCEPINFO(), null) == 0)
                    {
                        obj2 = pVarResult[0];
                    }
                }
                return(obj2);
            }
            catch (Exception exception)
            {
                if (System.Windows.Forms.ClientUtils.IsSecurityOrCriticalException(exception))
                {
                    throw;
                }
            }
            finally
            {
                if (pDispParams.rgvarg != IntPtr.Zero)
                {
                    FreeVARIANTVector(pDispParams.rgvarg, args.Length);
                }
            }
            return(obj2);
        }
Exemplo n.º 2
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();
                }
            }
        }
 public object InvokeMember(string methodName, params object[] parameter)
 {
     object obj2 = null;
     System.Windows.Forms.NativeMethods.tagDISPPARAMS pDispParams = new System.Windows.Forms.NativeMethods.tagDISPPARAMS {
         rgvarg = IntPtr.Zero
     };
     try
     {
         System.Windows.Forms.UnsafeNativeMethods.IDispatch nativeHtmlElement = this.NativeHtmlElement as System.Windows.Forms.UnsafeNativeMethods.IDispatch;
         if (nativeHtmlElement != null)
         {
             Guid empty = Guid.Empty;
             string[] rgszNames = new string[] { methodName };
             int[] rgDispId = new int[] { -1 };
             if (!System.Windows.Forms.NativeMethods.Succeeded(nativeHtmlElement.GetIDsOfNames(ref empty, rgszNames, 1, System.Windows.Forms.SafeNativeMethods.GetThreadLCID(), rgDispId)) || (rgDispId[0] == -1))
             {
                 return obj2;
             }
             if (parameter != null)
             {
                 Array.Reverse(parameter);
             }
             pDispParams.rgvarg = (parameter == null) ? IntPtr.Zero : HtmlDocument.ArrayToVARIANTVector(parameter);
             pDispParams.cArgs = (parameter == null) ? 0 : parameter.Length;
             pDispParams.rgdispidNamedArgs = IntPtr.Zero;
             pDispParams.cNamedArgs = 0;
             object[] pVarResult = new object[1];
             if (nativeHtmlElement.Invoke(rgDispId[0], ref empty, System.Windows.Forms.SafeNativeMethods.GetThreadLCID(), 1, pDispParams, pVarResult, new System.Windows.Forms.NativeMethods.tagEXCEPINFO(), null) == 0)
             {
                 obj2 = pVarResult[0];
             }
         }
         return obj2;
     }
     catch (Exception exception)
     {
         if (System.Windows.Forms.ClientUtils.IsSecurityOrCriticalException(exception))
         {
             throw;
         }
     }
     finally
     {
         if (pDispParams.rgvarg != IntPtr.Zero)
         {
             HtmlDocument.FreeVARIANTVector(pDispParams.rgvarg, parameter.Length);
         }
     }
     return obj2;
 }
        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();
                }
            }
        }