标签:
//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);
}
}
using Microsoft.Win32;
using mshtml;
using SHDocVw;
namespace TestBh
{
//每开启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;
FrmBho frm;
Logs logs = new Logs();
public int SetSite(object 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://116.252.254.204:8001/"))
{
logs.Add(URL.ToString());
logs.Add("-----------------------------------------");
logs.Add(ie.LocationURL);
logs.Add(ie.LocationName);
logs.Add(ie.Name);
logs.Add("-----------------------------------------");
if(frm==null) frm = new FrmBho(logs);
frm.Show();
frm.ShowText();
}
}
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");
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");
if (registryKey != null)
registryKey.DeleteSubKey(guid, false);
}
#endregion
}
标签:
原文地址:http://www.cnblogs.com/cztaxlyh/p/5771135.html