public unsafe object InvokeScript(string scriptName, object[] args) { object retVal = null; var dispParams = new Ole32.DISPPARAMS { rgvarg = IntPtr.Zero }; try { if (NativeHtmlDocument2.GetScript() is UnsafeNativeMethods.IDispatch scriptObject) { Guid g = Guid.Empty; string[] names = new string[] { scriptName }; Ole32.DispatchID dispid = Ole32.DispatchID.UNKNOWN; HRESULT hr = scriptObject.GetIDsOfNames(&g, names, 1, Kernel32.GetThreadLocale(), &dispid); if (hr.Succeeded() && dispid != Ole32.DispatchID.UNKNOWN) { if (args != null) { // Reverse the arg order so that parms read naturally after IDispatch. ( Array.Reverse(args); } dispParams.rgvarg = (args == null) ? IntPtr.Zero : HtmlDocument.ArrayToVARIANTVector(args); dispParams.cArgs = (args == null) ? 0 : (uint)args.Length; dispParams.rgdispidNamedArgs = IntPtr.Zero; dispParams.cNamedArgs = 0; object[] retVals = new object[1]; var pExcepInfo = new Ole32.EXCEPINFO(); hr = scriptObject.Invoke( dispid, &g, Kernel32.GetThreadLocale(), Oleaut32.DISPATCH.METHOD, &dispParams, retVals, &pExcepInfo, null); if (hr == HRESULT.S_OK) { retVal = retVals[0]; } } } } catch (Exception ex) when(!ClientUtils.IsSecurityOrCriticalException(ex)) { } finally { if (dispParams.rgvarg != IntPtr.Zero) { HtmlDocument.FreeVARIANTVector(dispParams.rgvarg, args.Length); } } return(retVal); }
public unsafe object InvokeMember(string methodName, params object[] parameter) { object retVal = null; var dispParams = new Ole32.DISPPARAMS(); 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); } dispParams.rgvarg = (parameter == null) ? IntPtr.Zero : HtmlDocument.ArrayToVARIANTVector(parameter); dispParams.cArgs = (parameter == null) ? 0 : (uint)parameter.Length; dispParams.rgdispidNamedArgs = IntPtr.Zero; dispParams.cNamedArgs = 0; object[] retVals = new object[1]; var pExcepInfo = new Ole32.EXCEPINFO(); hr = scriptObject.Invoke( dispid, &g, Kernel32.GetThreadLocale(), NativeMethods.DISPATCH_METHOD, &dispParams, retVals, &pExcepInfo, null); if (hr == HRESULT.S_OK) { retVal = retVals[0]; } } } } catch (Exception ex) when(!ClientUtils.IsSecurityOrCriticalException(ex)) { } finally { if (dispParams.rgvarg != IntPtr.Zero) { HtmlDocument.FreeVARIANTVector(dispParams.rgvarg, parameter.Length); } } return(retVal); }
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); }
public unsafe override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { object component = context.Instance; if (Marshal.IsComObject(component) && component is Oleaut32.IDispatch pDisp) { 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); }