コード例 #1
0
        public object InvokeScript(HTMLDocument htmlDocument_, string scriptName, params object[] args)
        {
            var    htmlDocument = htmlDocument_ as IHTMLDocument2;
            object invokeScript = null;
            var    pDispParams  = new DispParams
            {
                rgvarg = IntPtr.Zero
            };

            try
            {
                var script = htmlDocument.Script as IDispatch;
                if (script == null)
                {
                    return(null);
                }
                var      empty      = Guid.Empty;
                string[] rgszNames  = { scriptName };
                int[]    rgDispId   = { -1 };
                var      iDsOfNames = script.GetIDsOfNames(ref empty, rgszNames, 1,
                                                           Win32Interop.GetThreadLCID(), rgDispId);
                if (!MsHtmlNativeUtility.Succeeded(iDsOfNames) || rgDispId[0] == -1)
                {
                    return(null);
                }
                if (args != null)
                {
                    Array.Reverse(args);
                }
                pDispParams.rgvarg            = args == null ? IntPtr.Zero : MsHtmlNativeUtility.ArrayToVariantVector(args);
                pDispParams.cArgs             = args?.Length ?? 0;
                pDispParams.rgdispidNamedArgs = IntPtr.Zero;
                pDispParams.cNamedArgs        = 0;
                var pVarResult = new object[1];
                var pExcepInfo = new ExcepInfo();
                if (script.Invoke(rgDispId[0], ref empty, Win32Interop.GetThreadLCID(), 1, pDispParams, pVarResult,
                                  pExcepInfo, null) == 0)
                {
                    invokeScript = pVarResult[0];
                }
            }
            catch (Exception exception)
            {
                if (MsHtmlNativeUtility.IsSecurityOrCriticalException(exception))
                {
                    throw;
                }
            }
            finally
            {
                if (pDispParams.rgvarg != IntPtr.Zero)
                {
                    if (args != null)
                    {
                        MsHtmlNativeUtility.FreeVariantVector(pDispParams.rgvarg, args.Length);
                    }
                }
            }
            return(invokeScript);
        }