首页 技术 正文
技术 2022年11月16日
0 收藏 907 点赞 2,751 浏览 3732 个字

一丶接口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 ;
} 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;
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,076
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,812
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894