Ejemplo n.º 1
0
        public unsafe object InvokeMember(string methodName, params object[] parameter)
        {
            object retVal = null;

            NativeMethods.tagDISPPARAMS dp = new NativeMethods.tagDISPPARAMS
            {
                rgvarg = IntPtr.Zero
            };
            try
            {
                if (NativeHtmlElement is UnsafeNativeMethods.IDispatch scriptObject)
                {
                    Guid             g      = Guid.Empty;
                    string[]         names  = new string[] { methodName };
                    Ole32.DispatchID dispid = Ole32.DispatchID.UNKNOWN;
                    HRESULT          hr     = scriptObject.GetIDsOfNames(&g, names, 1, Kernel32.GetThreadLocale(), &dispid);
                    if (hr.Succeeded() && dispid != Ole32.DispatchID.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(
                            dispid,
                            ref g,
                            Kernel32.GetThreadLocale(),
                            NativeMethods.DISPATCH_METHOD,
                            dp,
                            retVals,
                            new NativeMethods.tagEXCEPINFO(),
                            null);
                        if (hr == HRESULT.S_OK)
                        {
                            retVal = retVals[0];
                        }
                    }
                }
            }
            catch (Exception ex) when(!ClientUtils.IsSecurityOrCriticalException(ex))
            {
            }
            finally
            {
                if (dp.rgvarg != IntPtr.Zero)
                {
                    HtmlDocument.FreeVARIANTVector(dp.rgvarg, parameter.Length);
                }
            }

            return(retVal);
        }
Ejemplo n.º 2
0
        public object InvokeMember(string methodName, params object[] parameter)
        {
            object retVal = null;

            NativeMethods.tagDISPPARAMS dp = new NativeMethods.tagDISPPARAMS
            {
                rgvarg = IntPtr.Zero
            };
            try
            {
                if (NativeHtmlElement is UnsafeNativeMethods.IDispatch scriptObject)
                {
                    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);
        }
Ejemplo n.º 3
0
        /// <include file='doc\HtmlDocument.uex' path='docs/doc[@for="HtmlDocument.InvokeScript"]/*' />
        public object InvokeScript(string scriptName, object[] args)
        {
            object retVal = null;

            NativeMethods.tagDISPPARAMS dp = new NativeMethods.tagDISPPARAMS();
            dp.rgvarg = IntPtr.Zero;
            try
            {
                UnsafeNativeMethods.IDispatch scriptObject = this.NativeHtmlDocument2.GetScript() as UnsafeNativeMethods.IDispatch;
                if (scriptObject != null)
                {
                    Guid     g       = Guid.Empty;
                    string[] names   = new string[] { scriptName };
                    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))
                    {
                        if (args != null)
                        {
                            // Reverse the arg order so that parms read naturally after IDispatch. (
                            Array.Reverse(args);
                        }
                        dp.rgvarg            = (args == null) ? IntPtr.Zero : HtmlDocument.ArrayToVARIANTVector(args);
                        dp.cArgs             = (args == null) ? 0 : args.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, args.Length);
                }
            }
            return(retVal);
        }
Ejemplo n.º 4
0
        public override void SetValue(Object component, Object value) {

            if (this.readOnly) {
                throw new NotSupportedException(SR.GetString(SR.COM2ReadonlyProperty, this.Name ));
            }

            object owner = component;
            if (owner is ICustomTypeDescriptor) {
                owner = ((ICustomTypeDescriptor)owner).GetPropertyOwner(this);
            }

            if (owner == null || !Marshal.IsComObject(owner) || !(owner is UnsafeNativeMethods.IDispatch)) {
                return;
            }

            // do we need to convert the type?
            if (valueConverter != null) {
                bool cancel = false;
                value = valueConverter.ConvertManagedToNative(value, this, ref cancel);
                if (cancel) {
                    return;
                }
            }

            UnsafeNativeMethods.IDispatch pDisp = (UnsafeNativeMethods.IDispatch)owner;

            NativeMethods.tagDISPPARAMS dp = new NativeMethods.tagDISPPARAMS();
            NativeMethods.tagEXCEPINFO excepInfo = new NativeMethods.tagEXCEPINFO();
            dp.cArgs = 1;
            dp.cNamedArgs = 1;
            int[] namedArgs = new int[]{NativeMethods.DISPID_PROPERTYPUT};
            GCHandle gcHandle = GCHandle.Alloc(namedArgs, GCHandleType.Pinned);

            try {
                dp.rgdispidNamedArgs = Marshal.UnsafeAddrOfPinnedArrayElement(namedArgs, 0);
                IntPtr mem = Marshal.AllocCoTaskMem( 16 /*Marshal.SizeOf(typeof(VARIANT)) */);
                SafeNativeMethods.VariantInit(new HandleRef(null, mem));
                Marshal.GetNativeVariantForObject(value, mem);
                dp.rgvarg = mem;
                try {

                    Guid g = Guid.Empty;
                    int hr = pDisp.Invoke(this.dispid,
                                          ref g,
                                          SafeNativeMethods.GetThreadLCID(),
                                          NativeMethods.DISPATCH_PROPERTYPUT,
                                          dp,
                                          null,
                                          excepInfo, new IntPtr[1]);


                    string errorInfo = null;
                    if (hr == NativeMethods.DISP_E_EXCEPTION && excepInfo.scode != 0) {
                        hr = excepInfo.scode;
                        errorInfo = excepInfo.bstrDescription;
                    }

                    switch (hr) {
                    case NativeMethods.E_ABORT:
                    case NativeMethods.OLE_E_PROMPTSAVECANCELLED:
                        // cancelled checkout, etc.
                        return;
                    case NativeMethods.S_OK:
                    case NativeMethods.S_FALSE:
                        OnValueChanged(component, EventArgs.Empty);
                        lastValue = value;
                        return;
                    default:
                        
                        //Debug.Fail(String.Format("IDispatch::Invoke(INVOKE_PROPPUT) returned hr=0x{0:X}", hr));
                        
                        if (pDisp is UnsafeNativeMethods.ISupportErrorInfo) {
                            g = typeof(UnsafeNativeMethods.IDispatch).GUID;
                            if (NativeMethods.Succeeded(((UnsafeNativeMethods.ISupportErrorInfo)pDisp).InterfaceSupportsErrorInfo(ref g))) {
                                UnsafeNativeMethods.IErrorInfo pErrorInfo = null;
                                UnsafeNativeMethods.GetErrorInfo(0, ref pErrorInfo);
                                string info= null;
                                if (pErrorInfo != null) {
                                    if (NativeMethods.Succeeded(pErrorInfo.GetDescription(ref info))) {
                                        errorInfo = info;
                                    }
                                }
                                
                            }
                        }
                        else if (errorInfo == null) {
                            StringBuilder strMessage = new StringBuilder(256);
                        
                            int result = SafeNativeMethods.FormatMessage(NativeMethods.FORMAT_MESSAGE_FROM_SYSTEM | 
                                                                    NativeMethods.FORMAT_MESSAGE_IGNORE_INSERTS,
                                                                    NativeMethods.NullHandleRef, 
                                                                    hr,
                                                                    CultureInfo.CurrentCulture.LCID,
                                                                    strMessage,
                                                                    255,
                                                                    NativeMethods.NullHandleRef);
                            
                            
                            if (result == 0) {   
                                errorInfo = String.Format(CultureInfo.CurrentCulture, SR.GetString(SR.DispInvokeFailed, "SetValue", hr));
                            }
                            else {       
                                errorInfo = strMessage.ToString();
                                // strip of any trailing cr/lf
                                while (errorInfo.Length > 0 && 
                                        errorInfo[errorInfo.Length -1] == '\n' ||
                                        errorInfo[errorInfo.Length -1] == '\r') {
                                    errorInfo = errorInfo.Substring(0, errorInfo.Length-1);
                                }    
                            }
                        }
                        throw new ExternalException(errorInfo, hr);
                    }
                }
                finally {
                    SafeNativeMethods.VariantClear(new HandleRef(null, mem));
                    Marshal.FreeCoTaskMem(mem);
                }
            }
            finally {
                gcHandle.Free();
            }
        }