首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >IE打开新选项卡关于:空白而不是网页

IE打开新选项卡关于:空白而不是网页
EN

Stack Overflow用户
提问于 2020-07-03 14:27:07
回答 1查看 237关注 0票数 0

我正在为IE在C#中开发一个BHO,通过检查onBeforeNavigateEvent中的网址来控制IE的导航流。

一切都很好,除了当一个链接在一个新的标签中打开,然后一些标签被打开为关于:空白。

我已经检查了日志,也调试了BHO,没有抛出异常。然而,在About:空白BHO被初始化的情况下,将调用SetSite和GetSite方法,但不会触发导航事件。

此外,当链接在新标签页中快速打开时,也会发生这种情况。

出于测试目的,我禁用了插件,IE运行正常,即没有关于:空白页。

BHO加载时间为0.1s,导航时间为0.5s

那么,问题可能是什么呢?

我遵循的构建这个BHO的源码是here

当前环境: IE 11、Windows 10

Interop.cs

代码语言:javascript
运行
复制
using System;
using System.Runtime.InteropServices;

namespace IE_BHO
{
    [
        ComImport(),
        ComVisible(true),
        InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
        Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352")
    ]
    interface IObjectWithSite
    {
        [PreserveSig]
        int SetSite([In, MarshalAs(UnmanagedType.IUnknown)] object site);

        [PreserveSig]
        int GetSite(ref Guid guid, out IntPtr ppvSite);
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct OLECMDTEXT
    {
        public uint cmdtextf;
        public uint cwActual;
        public uint cwBuf;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
        public char rgwz;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct OLECMD
    {
        public uint cmdID;
        public uint cmdf;
    }

    [ComImport(), ComVisible(true),
    Guid("B722BCCB-4E68-101B-A2BC-00AA00404770"),
    InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IOleCommandTarget
    {

        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int QueryStatus(
            [In] IntPtr pguidCmdGroup,
            [In, MarshalAs(UnmanagedType.U4)] uint cCmds,
            [In, Out, MarshalAs(UnmanagedType.Struct)] ref OLECMD prgCmds,
            //This parameter must be IntPtr, as it can be null
            [In, Out] IntPtr pCmdText);

        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int Exec(
            //[In] ref Guid pguidCmdGroup,
            //have to be IntPtr, since null values are unacceptable
            //and null is used as default group!
            [In] IntPtr pguidCmdGroup,
            [In, MarshalAs(UnmanagedType.U4)] uint nCmdID,
            [In, MarshalAs(UnmanagedType.U4)] uint nCmdexecopt,
            [In] IntPtr pvaIn,
            [In, Out] IntPtr pvaOut);
    }

    [Guid("6D5140C1-7436-11CE-8034-00AA006009FA")]
    [InterfaceType(1)]
    public interface IServiceProvider
    {
        int QueryService(ref Guid guidService, ref Guid riid, out IntPtr ppvObject);
    }

    [
        ComVisible(true),
        Guid("4C1D2E51-018B-4A7C-8A07-618452573E42"),
        InterfaceType(ComInterfaceType.InterfaceIsDual)
    ]
    public interface IExtension
    {
        [DispId(1)]
        string ActivateEndPoint(string licenseKey, string userAgent);
    }
}

BHO.cs

代码语言:javascript
运行
复制
using Microsoft.Win32;
using SHDocVw;
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Expando;
using System.Security.Permissions;
using System.Threading.Tasks;
using mshtml;
using System.Reflection;
using System.Diagnostics;

namespace IE_BHO
{
    [
        SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode),
        ComVisible(true),
        Guid("BDCB9FDA-8370-40D9-96C9-9D4B4C25C0D8"),
        ClassInterface(ClassInterfaceType.None),
        ProgId("myExtension"),
        ComDefaultInterface(typeof(IExtension))
    ]
    public class BHO : IObjectWithSite, IOleCommandTarget, IExtension
    {
        object _site;
        IWebBrowser2 _webBrowser2;

        #region Implementation of IObjectWithSite
        int IObjectWithSite.SetSite(object site)
        {
            _site = site;

            if (site != null)
            {
                var serviceProv = (IServiceProvider)_site;
                var guidIWebBrowserApp = Marshal.GenerateGuidForType(typeof(IWebBrowserApp));
                var guidIWebBrowser2 = Marshal.GenerateGuidForType(typeof(IWebBrowser2));

                IntPtr intPtr;
                serviceProv.QueryService(ref guidIWebBrowserApp, ref guidIWebBrowser2, out intPtr);

                _webBrowser2 = (IWebBrowser2)Marshal.GetObjectForIUnknown(intPtr);

                ((DWebBrowserEvents2_Event)_webBrowser2).BeforeNavigate2 += OnBeforeNavigate2;
                ((DWebBrowserEvents2_Event)_webBrowser2).BeforeScriptExecute += S2_BeforeScriptExecute;
                ((DWebBrowserEvents2_Event)_webBrowser2).DownloadComplete += BHO_DownloadComplete;                  
            }
            else
            {
                ((DWebBrowserEvents2_Event)_webBrowser2).BeforeNavigate2 -= OnBeforeNavigate2;
                ((DWebBrowserEvents2_Event)_webBrowser2).BeforeScriptExecute -= S2_BeforeScriptExecute;
                ((DWebBrowserEvents2_Event)_webBrowser2).DownloadComplete -= BHO_DownloadComplete;

                _webBrowser2 = null;
            }                

            return 0;
        }

        int IObjectWithSite.GetSite(ref Guid guid, out IntPtr ppvSite)
        {
            IntPtr punk = Marshal.GetIUnknownForObject(_webBrowser2);
            int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
            Marshal.Release(punk);
            return hr;
        }

        public void OnBeforeNavigate2(object pDisp, ref object URL, ref object Flags, ref object TargetFrameName, ref object PostData, ref object Headers, ref bool Cancel)
        {
            try
            {
                UrlObj dto = UrlManager.CheckUrl(URL.ToString());

                if (dto.IsInList)
                {
                     Cancel = true;
                     _webBrowser2.Navigate2("www.google.com");
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void S2_BeforeScriptExecute(object pDispWindow)
        {
            ExposeMethodstoJS();
        }

        private void BHO_DownloadComplete()
        {
            ExposeMethodstoJS();
        }

        private void ExposeMethodstoJS(string calledBy)
        {
            try
            {
                HTMLDocument doc = _webBrowser.Document as HTMLDocument;

                if (doc != null)
                {
                    IHTMLWindow2 tmpWindow = doc.parentWindow;
                    dynamic window = tmpWindow;
                    IExpando windowEx = (IExpando)window;
                    PropertyInfo p = windowEx.AddProperty("myExtension");
                    p.SetValue(windowEx, this);
                }
            }
            catch (Exception ex)
            {
            }
        }
        #endregion


        #region Implementation of IOleCommandTarget
        int IOleCommandTarget.QueryStatus(IntPtr pguidCmdGroup, uint cCmds, ref OLECMD prgCmds, IntPtr pCmdText)
        {
            return 0;
        }

        int IOleCommandTarget.Exec(IntPtr pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            return 0;
        }
        #endregion

        #region Implementation of IExtension 
        string IExtension.GetDetails()
        {
            return "Methods Exposed";
        }
        #endregion


        #region COMRegistration
        [ComRegisterFunction]
        public static void RegisterBHO(Type type)
        {
            RegistryKey registryKey = Registry.LocalMachine.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\Browser Helper Objects");
            RegistryKey guidKey = registryKey.CreateSubKey(type.GUID.ToString("B"));

            registryKey.Close();
            guidKey.Close();
        }

        [ComUnregisterFunction]
        public static void UnregisterBHO(Type type)
        {
            RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\Browser Helper Objects", true);

            string guid = type.GUID.ToString("B");

            if (registryKey != null)
            {
                registryKey.DeleteSubKey(guid, false);
            }
        }
        #endregion COMRegistration
    }
}
EN

回答 1

Stack Overflow用户

发布于 2020-07-06 13:34:46

您使用的是哪个版本的IE和操作系统?

我认为这个问题的发生是因为新标签页几乎同时打开和导航。有关详细信息,您可以参考this link

您还可以尝试本文中的解决方案:为Internet Explorer安装最新的累积安全更新。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62709619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档