C#开发BHO插件
用c#编译一个简单的BHO插件,修改百度首页按钮“百度一下”的值为“修改一下”:BHO.cs如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SHDocVw;
using mshtml;
using System.IO;
using Microsoft.Win32;
using System.Runtime.InteropServices;
namespace BHO.BHO
{
[
ComVisible(true),
Guid("8a194578-81ea-4850-9911-13ba2d71efbd"),
ClassInte易做图ce(ClassInte易做图ceType.None)
]
public class BHO : IObjectWithSite
{
WebBrowser webBrowser;
HTMLDocument document;
public void OnDocumentComplete(object pDisp, ref object URL)
{
document = (HTMLDocument)webBrowser.Document;
foreach (IHTMLInputElement tempElement in document.getElementsByTagName("INPUT"))
{
if (document.url == "http://www.baidu.com/" && tempElement.type.ToLower() == "button" && ((IHTMLElement)tempElement).id == "su")
{
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);
}
public int SetSite(object site)
{
if (site != null)
{
webBrowser = (WebBrowser)site;
webBrowser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
}
else
{
webBrowser.DocumentComplete -= new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
webBrowser = null;
}
return 0;
}
public int GetSite(ref Guid guid, out IntPtr ppvSite)
{
IntPtr punk = Marshal.GetIUnknownForObject(webBrowser);
int hr = Marshal.QueryInte易做图ce(punk, ref guid, out ppvSite);
Marshal.Release(punk);
return hr;
}
#endregion
}
}
IObjectWithSIte.cs代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace BHO_HelloWorld
{
[
ComVisible(true),
Inte易做图ceType(ComInte易做图ceType.Inte易做图ceIsIUnknown),
Guid("e7b9b609-19ad-40a4-a288-b300a3087464")
]
public inte易做图ce IObjectWithSite
{
[PreserveSig]
int SetSite([MarshalAs(UnmanagedType.IUnknown)]object site);
[PreserveSig]
int GetSite(ref Guid guid, out IntPtr ppvSite);
}
}
生成了BHO.dll,生成时出现了无法启动带有“类库输出类型”的项目。
然后我用regsvr32 D:\BHO.dll注册dll文件,出现问题了“模块“d:\BHO.dll”已加载,但找不到入口点DllRegisterServer",初学bho,希望大家能多多指教啊,也希望大神能帮忙解决下。
--------------------编程问答-------------------- 好长的代码啊!
有点不懂啊! --------------------编程问答-------------------- C# 用 RegAsm 注册
regasm /codebase BHO.dll --------------------编程问答-------------------- 测试了一下,没有实现功能啊!
补充:.NET技术 , 其他语言