IE插件BHO
2016-08-14 21:49
120 查看
一丶接口IObjectWithSite
二丶实现接口IObjectWithSite
三、安装与卸载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更改网页内容
//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; } } }
相关文章推荐
- 用VS2005开发BHO揭开IE插件的神秘面纱
- VB做IE插件1 BHO
- C#为IE编写BHO插件心得
- IE插件技术 BHO C# IE 插件
- IE插件BHO开发
- IE插件技术 BHO C# IE 插件
- Bho编定ie插件
- IE插件技术 BHO C# IE 插件
- c# IE插件 Browser Helper Object (BHO)
- IE和FF下查看HTTP 头文件信息的插件
- IE 下载的插件安装目录
- IE 插件搜集
- VC++开发BHO插件——定制你的浏览器
- BHO开发中的IE事件响应原理
- 编写IE插件的框架Add-in Express开发 Internet Explorer
- ie下调试javasscript的一个小插件工具
- 用户用老版IE访问网站时的jquery提示插件
- ie自动 下载jre插件
- IE和FF下查看HTTP头文件信息的插件
- [IE技巧] 禁止/允许显示网页上Flash的小插件