IE插件BHO

一丶接口IObjectWithSite

//BHO项目(类库)添加引用两个COM
//Microsoft HTML Object Library, Microsoft Internet Controls;

using System.Runtime.InteropServices;

namespace TestBho
{
    [ComVisible(true), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352")]
    public interface IObjectWithSite
    {
        [PreserveSig]
        int SetSite([MarshalAs(UnmanagedType.IUnknown)]object site);
        [PreserveSig]
        int GetSite(ref Guid guid, out IntPtr ppvSite);
    }
}

二丶实现接口IObjectWithSite

using Microsoft.Win32;
using mshtml;
using SHDocVw;

namespace TestBho
{
    //每开启IE浏览器器选项页都会创建一个MyBho类的实例来对应IE选项页
    //IE8 是每个Tab 一个独立进程,当IE的Tab进程被创建的时候,都会创建一个MyBho类的实例
    [ComVisible(true),Guid("8a194578-81ea-4850-9911-13ba2d71efbd"),ClassInterface(ClassInterfaceType.None)]
    public class MyBHO : IObjectWithSite
    {
        InternetExplorer ie;
        List<string> logs = new List<string>();
        public int SetSite(object site)
        {
// ie进程创建,则创建BHO并ie进程以参数site传入,关闭ie进程将Null以参数site传入 if (site != null) { ie = (InternetExplorer)site; logs.Add("BHO构建"); ie.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(ie_DocumentComplete); } else { ie.DocumentComplete -= new DWebBrowserEvents2_DocumentCompleteEventHandler(ie_DocumentComplete); } return 0; } private void ie_DocumentComplete(object pDisp, ref object URL) { if (URL.ToString().StartsWith("http://192.168.1.1:8000/")) { logs.Add(URL.ToString()); logs.Add("-----------------------------------------"); logs.Add(ie.LocationURL); //ie地址栏的url logs.Add(ie.LocationName); //ie标题或选项项标题 logs.Add(ie.Name); //ie应用的名称 logs.Add("-----------------------------------------"); } } public int GetSite(ref Guid guid, out IntPtr ppvSite) { IntPtr punk = Marshal.GetIUnknownForObject(ie); int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite); Marshal.Release(punk); return hr; } #region 注册Bho public static string key = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects"; [ComRegisterFunction] public static void RegisterBHO(Type type) { RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(key, true); if (registryKey == null) registryKey = Registry.LocalMachine.CreateSubKey(key); string guid = type.GUID.ToString("B"); //当前类的GUID字符串(注意:不是接口GUID) RegistryKey ourKey = registryKey.OpenSubKey(guid); if (ourKey == null) ourKey = registryKey.CreateSubKey(guid); registryKey.Close(); ourKey.Close(); } [ComUnregisterFunction] public static void UnregisterBHO(Type type) { RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(key, true); string guid = type.GUID.ToString("B"); //当前类的GUID字符串(注意:不是接口GUID) if (registryKey != null) registryKey.DeleteSubKey(guid, false); } #endregion }

三、安装与卸载BHO

Regasm.exe MyBHO.dll /codebase     
//注册BHO,将安装到ie中,通过ie【管理加载项】管理BHO
Regasm.exe MyBHO.dll /unregister   //卸载BHO
Regasm.exe /c MyBHO.dll   //注册BHO,将安装到ie中,通过ie【管理加载项】管理BHO
Regasm.exe /u MyBHO.dll   //卸载BHO

补充说明

在win7以上操作系统,当BHO写访问本地磁盘时,BHO写访问失败(Try捕获出现异常,可看到异常信息),这是由于win7操作系统提高了安全等级所致。 但BHO还是可以写访问″C:\Users\[操作系统当前用户名]\″ (获取当前用户文件夹∶ Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)), 但其实写访问的是″C:\Users\[操作系统当前用户名]\AppData\Local\Microsoft\Windows\INetCache\Virtualized\C\Users\[操作系统当前用户名]″,具体的原因请查阅"Win7或IE7安全特性"。
 
四、BHO更改网页内容
    //每开启IE浏览器器选项页都会创建一个Bho类的实例来对应IE选项页
    //IE8 是每个Tab 一个独立进程,当IE的Tab进程被创建的时候,都会创建一个Bho类的实例
    [ComVisible(true),Guid("BAD13015-0CE2-4220-9ADC-ED513C101155"),ClassInterface(ClassInterfaceType.None)]
    public class BHO_ChangeHtml : IObjectWithSite
    {
        InternetExplorer ie;
        Logs logs = new Logs();
        public int SetSite(object site)
        {
            if (site != null)
            {
                ie = (InternetExplorer)site;
                ie.DownloadComplete += new DWebBrowserEvents2_DownloadCompleteEventHandler(ie_DownloadComplete);
            }
            else
            {
                ie.DownloadComplete -= new DWebBrowserEvents2_DownloadCompleteEventHandler(ie_DownloadComplete);
            }
            return 0;
        }

        void ie_DownloadComplete()
        {
            if (ie.LocationURL.ToString().StartsWith("https://www.baidu.com"))
            {
                if((ie.Document as IHTMLDocument2) != null) 
                {
                    var html = (ie.Document as IHTMLDocument2).body.innerHTML.Replace("百度一下", "百度检索");
                    (ie.Document as IHTMLDocument2).body.innerHTML = html;
                }
            }            
        }

 



转载于:https://www.cnblogs.com/cztaxlyh/p/5771135.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值