using System;
using System.Collections.Generic;
using System.Text;
using SHDocVw;
using mshtml;
using System.IO;
using Microsoft.Win32;
using System.Runtime.InteropServices;
namespace BHO_HelloWorld
{
/// <summary>
///
/// </summary>
[
ComVisible(true),
Guid("8a194578-81ea-4850-9911-13ba2d71efbd"),
ClassInterface(ClassInterfaceType.None)
]
public class BHO:IObjectWithSite
{
WebBrowser webBrowser;
HTMLDocument document;
/// <summary>
/// 页面执行完毕
/// </summary>
/// <param name="pDisp"></param>
/// <param name="URL"></param>
public void OnDocumentComplete(object pDisp, ref object URL)
{
System.Windows.Forms.MessageBox.Show("test");
document = (HTMLDocument)webBrowser.Document;
foreach (IHTMLInputElement tempElement in document.getElementsByTagName("INPUT"))
{
System.Windows.Forms.MessageBox.Show(
tempElement.name!=null?tempElement.name:"it sucks, no name, try id"+((IHTMLElement)tempElement).id
);
}
}
/// <summary>
/// 页面迁移至前的时间
/// </summary>
/// <param name="pDisp"></param>
/// <param name="URL"></param>
/// <param name="Flags"></param>
/// <param name="TargetFrameName"></param>
/// <param name="PostData"></param>
/// <param name="Headers"></param>
/// <param name="Cancel"></param>
public void OnBeforeNavigate2(object pDisp, ref object URL, ref object Flags, ref object TargetFrameName, ref object PostData, ref object Headers, ref bool Cancel)
{
document = (HTMLDocument)webBrowser.Document;
foreach(IHTMLInputElement tempElement in document.getElementsByTagName("INPUT"))
{
if(tempElement.type.ToLower()=="password")
{
System.Windows.Forms.MessageBox.Show(tempElement.value);
}
}
}
#region BHO Internal Functions
public static string BHOKEYNAME = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects";
[ComRegisterFunction]
public static void RegisterBHO(Type type)
{
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHOKEYNAME, true);
if (registryKey == null)
registryKey = Registry.LocalMachine.CreateSubKey(BHOKEYNAME);
string guid = type.GUID.ToString("B");
RegistryKey ourKey = registryKey.OpenSubKey(guid);
if (ourKey == null)
ourKey = registryKey.CreateSubKey(guid);
ourKey.SetValue("Alright", 1);
registryKey.Close();
ourKey.Close();
}
[ComUnregisterFunction]
public static void UnregisterBHO(Type type)
{
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHOKEYNAME, true);
string guid = type.GUID.ToString("B");
if (registryKey != null)
registryKey.DeleteSubKey(guid, false);
}
/// <summary>
/// 设置网页
/// </summary>
/// <param name="site"></param>
/// <returns></returns>
public int SetSite(object site)
{
if (site != null)
{
webBrowser = (WebBrowser)site;
webBrowser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
webBrowser.BeforeNavigate2 += new DWebBrowserEvents2_BeforeNavigate2EventHandler(this.OnBeforeNavigate2);
}
else
{
webBrowser.DocumentComplete -= new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
webBrowser.BeforeNavigate2 -= new DWebBrowserEvents2_BeforeNavigate2EventHandler(this.OnBeforeNavigate2);
webBrowser = null;
}
return 0;
}
/// <summary>
/// 获取设置
/// </summary>
/// <param name="guid"></param>
/// <param name="ppvSite"></param>
/// <returns></returns>
public int GetSite(ref Guid guid, out IntPtr ppvSite)
{
IntPtr punk = Marshal.GetIUnknownForObject(webBrowser);
int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
Marshal.Release(punk);
return hr;
}
#endregion
}
}
今天写了一个BHO扩展程序,功能很简单,弹出对话框,主要是熟悉了BHO程序的流程。以前都没听过。。。。
废话不多说,写看程序
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace BHO_HelloWorld
{
[
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);
}
}
http://thinkhejie.iteye.com/upload/picture/pic/38015/c35597cf-0fa9-303f-a1a2-804edb2e7540.jpg
.netframework 框架 vs 2005 工具 。
BHO扩展实践
233

被折叠的 条评论
为什么被折叠?



