问题描述
>
1)我试图在C#中创建一个简单的BHO,如下所示已经回答:
2)但是不幸的是,他们都尝试了少于IE11,其中一些使它工作,一些失败了
你可以在IE7的Windows 7 64位上运行。下载我准备的Visual Studio 2013版本:其中包括所有的源代码和IE11的详细信息:
Q。任何人都可以建议/建议/帮助我如何使这个BHO的一个你好世界?
我也试过其他人从codeproject中取样,但仍然没有人能够工作,从4个星期,我失去了,请告诉什么是在我的ClassLibrary2.rar中是错误的,这是不是文本浏览器?
我完全失去了,请指教。
编辑:
IEAddon.cs
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
使用Microsoft.Win32;
using mshtml;
使用SHDocVw;
命名空间InternetExplorerExtension
{
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid(D40C654D-7C51-4EB3 -95B2-1E23905C2A2D)]
[ProgId(MyBHO.WordHighlighter)]
public class WordHighlighterBHO:IObjectWithSite,IOleCommandTarget
{
const string DefaultTextToHighlight =browser
IWebBrowser2浏览器;
私有对象站点;
#region高亮文本
void OnDocumentComplete(object pDisp,ref object URL)
{
try
{
/ /这将防止此方法被执行多次。
if(pDisp!= this.site)
return;
var document2 = browser.Document as IHTMLDocument2;
var document3 = browser.Document as IHTMLDocument3;
var window = document2.parentWindow;
window.execScript(@function FncAddedByAddon(){alert('Message added by addon。');});
队列< IHTMLDOMNode> queue = new Queue< IHTMLDOMNode>();
foreach(IHTMLDOMNode eachChild in document3.childNodes)
queue.Enqueue(eachChild);
while(queue.Count> 0)
{
//将所需文本替换为高亮版本
var domNode = queue.Dequeue();
var textNode = domNode as IHTMLDOMTextNode;
if(textNode!= null)
{
if(textNode.data.Contains(TextToHighlight))
{
var newText = textNode.data.Replace ,< span style ='background-color:yellow; cursor:hand;'onclick ='javascript:FncAddedByAddon()'title ='点击打开基于脚本的警报窗口。>+ TextToHighlight + span>);
var newNode = document2.createElement(span);
newNode.innerHTML = newText;
domNode.replaceNode((IHTMLDOMNode)newNode);
}
}
else
{
//将子元素添加到集合
var x =(IHTMLDOMChildrenCollection)(domNode.childNodes);
foreach(IHTMLDOMNode eachChild in x)
{
if(eachChild是mshtml.IHTMLScriptElement)
continue;
if(eachChild是mshtml.IHTMLStyleElement)
continue;
queue.Enqueue(eachChild);
}
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region加载和保存数据
static string TextToHighlight = DefaultTextToHighlight;
public static string RegData =Software\\MyIEExtension;
[DllImport(ieframe.dll)]
public static extern int IEGetWriteableHKCU(ref IntPtr phKey);
private static void SaveOptions()
{
//在IE 7,8,9中,(桌面)10个选项卡以受保护模式运行
//禁止写到HKLM,HKCU。
//必须要求IE为Writable注册表区段指针
//这将像HKU / S-1-7 *** / Software / AppDataLow /
//在 metroIE 10模式,标签运行在增强保护模式
//其中BHO不允许运行,除非在边缘情况下。
//请参阅http://blogs.msdn.com/b/ieinternals/archive/2012/03/23/understanding-ie10-enhanced-protected-mode-network-security-addons-cookies-metro-desktop .aspx
IntPtr phKey = new IntPtr();
var answer = IEGetWriteableHKCU(ref phKey);
RegistryKey writeable_registry = RegistryKey.FromHandle(
new Microsoft.Win32.SafeHandles.SafeRegistryHandle(phKey,true)
);
RegistryKey registryKey = writeable_registry.OpenSubKey(RegData,true);
if(registryKey == null)
registryKey = writeable_registry.CreateSubKey(RegData);
registryKey.SetValue(Data,TextToHighlight);
writeable_registry.Close();
}
private static void LoadOptions()
{
//在IE 7,8,9中,(桌面)10个选项卡以受保护模式运行
//禁止写入HKLM,HKCU。
//必须要求IE为Writable注册表区段指针
//这将像HKU / S-1-7 *** / Software / AppDataLow /
//在 metroIE 10模式,标签运行在增强保护模式
//其中BHO不允许运行,除非在边缘情况下。
//请参阅http://blogs.msdn.com/b/ieinternals/archive/2012/03/23/understanding-ie10-enhanced-protected-mode-network-security-addons-cookies-metro-desktop .aspx
IntPtr phKey = new IntPtr();
var answer = IEGetWriteableHKCU(ref phKey);
RegistryKey writeable_registry = RegistryKey.FromHandle(
new Microsoft.Win32.SafeHandles.SafeRegistryHandle(phKey,true)
);
RegistryKey registryKey = writeable_registry.OpenSubKey(RegData,true);
if(registryKey == null)
registryKey = writeable_registry.CreateSubKey(RegData);
registryKey.SetValue(Data,TextToHighlight);
if(registryKey == null)
{
TextToHighlight = DefaultTextToHighlight;
}
else
{
TextToHighlight =(string)registryKey.GetValue(Data);
}
writeable_registry.Close();
}
#endregion
[Guid(6D5140C1-7436-11CE-8034-00AA006009FA)]
[InterfaceType(1)]
public interface IServiceProvider
{
int QueryService(ref Guid guidService,ref Guid riid,out IntPtr ppvObject);
}
#region IObjectWithSite的实现
int IObjectWithSite.SetSite(对象站点)
{
this.site = site;
if(site!= null)
{
LoadOptions();
var serviceProv =(IServiceProvider)this.site;
var guidIWebBrowserApp = Marshal.GenerateGuidForType(typeof(IWebBrowserApp)); // new Guid(0002DF05-0000-0000-C000-000000000046);
var guidIWebBrowser2 = Marshal.GenerateGuidForType(typeof(IWebBrowser2)); // new Guid(D30C1661-CDAF-11D0-8A3E-00C04FC9E26E);
IntPtr intPtr;
serviceProv.QueryService(ref guidIWebBrowserApp,ref guidIWebBrowser2,out intPtr);
browser =(IWebBrowser2)Marshal.GetObjectForIUnknown(intPtr);
((DWebBrowserEvents2_Event)browser).DocumentComplete + =
new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
}
else
{
((DWebBrowserEvents2_Event)browser).DocumentComplete - =
new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
browser = null;
}
return 0;
}
int IObjectWithSite.GetSite(ref Guid guid,out IntPtr ppvSite)
{
IntPtr punk = Marshal.GetIUnknownForObject(browser);
int hr = Marshal.QueryInterface(punk,ref guid,out ppvSite);
Marshal.Release(punk);
return hr;
}
#endregion
#region IOleCommandTarget的实现
int IOleCommandTarget.QueryStatus(IntPtr pguidCmdGroup,uint cCmds,ref OLECMD prgCmds,IntPtr pCmdText)
{
return 0;
}
int IOleCommandTarget.Exec(IntPtr pguidCmdGroup,uint nCmdID,uint nCmdexecopt,IntPtr pvaIn,IntPtr pvaOut)
{
try
{
//从命令栏访问文档。
var document = browser.Document as IHTMLDocument2;
var window = document.parentWindow;
var result = window.execScript(@alert('现在将允许您配置文本以突出显示...'););
var form = new HighlighterOptionsForm();
form.InputText = TextToHighlight;
if(form.ShowDialog()!= DialogResult.Cancel)
{
TextToHighlight = form.InputText;
SaveOptions();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
return 0;
}
#endregion
#region注册regasm
public static string RegBHO =Software\\Microsoft\\Windows\\CurrentVersion \\Explorer\\浏览器辅助对象;
public static string RegCmd =Software\\Microsoft\\Internet Explorer\\Extensions;
[ComRegisterFunction]
public static void RegisterBHO(Type type)
{
string guid = type.GUID.ToString(B);
// BHO
{
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(RegBHO,true);
if(registryKey = null)
registryKey = Registry.LocalMachine.CreateSubKey(RegBHO);
RegistryKey key = registryKey.OpenSubKey(guid);
if(key == null)
key = registryKey.CreateSubKey(guid);
key.SetValue(Alright,1);
registryKey.Close();
key.Close();
}
//命令
{
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(RegCmd,true);
if(registryKey == null)
registryKey = Registry.LocalMachine.CreateSubKey(RegCmd);
RegistryKey key = registryKey.OpenSubKey(guid);
if(key == null)
key = registryKey.CreateSubKey(guid);
key.SetValue(ButtonText,Highlighter options);
key.SetValue(CLSID,{1FBA04EE-3024-11d2-8F1F-0000F87ABD16});
key.SetValue(ClsidExtension,guid);
key.SetValue(Icon,);
key.SetValue(HotIcon,);
key.SetValue(Default Visible,Yes);
key.SetValue(MenuText,& Highlighter options);
key.SetValue(ToolTip,Highlighter options);
//key.SetValue (\"KeyPath,no);
registryKey.Close();
key.Close();
}
}
[ComUnregisterFunction]
public static void UnregisterBHO(Type type)
{
string guid = type.GUID.ToString (B);
// BHO
{
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(RegBHO,true);
if(registryKey!= null)
registryKey.DeleteSubKey(guid,false);
}
//命令
{
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(RegCmd,true);
if(registryKey!= null)
registryKey.DeleteSubKey(guid,false);
}
}
#endregion
}
}
我试图做同样的 - 我只是注意到在构建日志中有一个错误
所以我添加了* .snk和突出显示工作(使用ie11到x64),但突出显示选项菜单项不起作用
1) I am trying to make a simple BHO in C# like here already answered: http://stackoverflow.com/a/5740004/285594
2) But unfortunately they all tried less then IE11, where some made it work and some failed too
3) after following everything as mentioned in that answer, i also purchased official code sign but it simply does not working in IE11 Windows 7 64-bit.
You can download my prepared version of Visual studio 2013: which includes all the source code and details for IE11:
https://www.dropbox.com/s/60kg212vkjb7yud/ClassLibrary2.rar
Q. Can anyone please advise/suggest/help how can i make one hello world of this BHO?
I have also tried others sample from codeproject, but still none of them i was able to make work yet, trying since 4 weeks, i am lost, please advise what is wrong in my ClassLibrary2.rar which is not hilighting the text "browser"?
I am completely lost, please advise.
EDIT:
IEAddon.cs
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Win32;
using mshtml;
using SHDocVw;
namespace InternetExplorerExtension
{
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("D40C654D-7C51-4EB3-95B2-1E23905C2A2D")]
[ProgId("MyBHO.WordHighlighter")]
public class WordHighlighterBHO : IObjectWithSite, IOleCommandTarget
{
const string DefaultTextToHighlight = "browser";
IWebBrowser2 browser;
private object site;
#region Highlight Text
void OnDocumentComplete(object pDisp, ref object URL)
{
try
{
// This will prevent this method being executed more than once.
if (pDisp != this.site)
return;
var document2 = browser.Document as IHTMLDocument2;
var document3 = browser.Document as IHTMLDocument3;
var window = document2.parentWindow;
window.execScript(@"function FncAddedByAddon() { alert('Message added by addon.'); }");
Queue<IHTMLDOMNode> queue = new Queue<IHTMLDOMNode>();
foreach (IHTMLDOMNode eachChild in document3.childNodes)
queue.Enqueue(eachChild);
while (queue.Count > 0)
{
// replacing desired text with a highlighted version of it
var domNode = queue.Dequeue();
var textNode = domNode as IHTMLDOMTextNode;
if (textNode != null)
{
if (textNode.data.Contains(TextToHighlight))
{
var newText = textNode.data.Replace(TextToHighlight, "<span style='background-color: yellow; cursor: hand;' onclick='javascript:FncAddedByAddon()' title='Click to open script based alert window.'>" + TextToHighlight + "</span>");
var newNode = document2.createElement("span");
newNode.innerHTML = newText;
domNode.replaceNode((IHTMLDOMNode)newNode);
}
}
else
{
// adding children to collection
var x = (IHTMLDOMChildrenCollection)(domNode.childNodes);
foreach (IHTMLDOMNode eachChild in x)
{
if (eachChild is mshtml.IHTMLScriptElement)
continue;
if (eachChild is mshtml.IHTMLStyleElement)
continue;
queue.Enqueue(eachChild);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region Load and Save Data
static string TextToHighlight = DefaultTextToHighlight;
public static string RegData = "Software\\MyIEExtension";
[DllImport("ieframe.dll")]
public static extern int IEGetWriteableHKCU(ref IntPtr phKey);
private static void SaveOptions()
{
// In IE 7,8,9,(desktop)10 tabs run in Protected Mode
// which prohibits writes to HKLM, HKCU.
// Must ask IE for "Writable" registry section pointer
// which will be something like HKU/S-1-7***/Software/AppDataLow/
// In "metro" IE 10 mode, tabs run in "Enhanced Protected Mode"
// where BHOs are not allowed to run, except in edge cases.
// see http://blogs.msdn.com/b/ieinternals/archive/2012/03/23/understanding-ie10-enhanced-protected-mode-network-security-addons-cookies-metro-desktop.aspx
IntPtr phKey = new IntPtr();
var answer = IEGetWriteableHKCU(ref phKey);
RegistryKey writeable_registry = RegistryKey.FromHandle(
new Microsoft.Win32.SafeHandles.SafeRegistryHandle(phKey, true)
);
RegistryKey registryKey = writeable_registry.OpenSubKey(RegData, true);
if (registryKey == null)
registryKey = writeable_registry.CreateSubKey(RegData);
registryKey.SetValue("Data", TextToHighlight);
writeable_registry.Close();
}
private static void LoadOptions()
{
// In IE 7,8,9,(desktop)10 tabs run in Protected Mode
// which prohibits writes to HKLM, HKCU.
// Must ask IE for "Writable" registry section pointer
// which will be something like HKU/S-1-7***/Software/AppDataLow/
// In "metro" IE 10 mode, tabs run in "Enhanced Protected Mode"
// where BHOs are not allowed to run, except in edge cases.
// see http://blogs.msdn.com/b/ieinternals/archive/2012/03/23/understanding-ie10-enhanced-protected-mode-network-security-addons-cookies-metro-desktop.aspx
IntPtr phKey = new IntPtr();
var answer = IEGetWriteableHKCU(ref phKey);
RegistryKey writeable_registry = RegistryKey.FromHandle(
new Microsoft.Win32.SafeHandles.SafeRegistryHandle(phKey, true)
);
RegistryKey registryKey = writeable_registry.OpenSubKey(RegData, true);
if (registryKey == null)
registryKey = writeable_registry.CreateSubKey(RegData);
registryKey.SetValue("Data", TextToHighlight);
if (registryKey == null)
{
TextToHighlight = DefaultTextToHighlight;
}
else
{
TextToHighlight = (string)registryKey.GetValue("Data");
}
writeable_registry.Close();
}
#endregion
[Guid("6D5140C1-7436-11CE-8034-00AA006009FA")]
[InterfaceType(1)]
public interface IServiceProvider
{
int QueryService(ref Guid guidService, ref Guid riid, out IntPtr ppvObject);
}
#region Implementation of IObjectWithSite
int IObjectWithSite.SetSite(object site)
{
this.site = site;
if (site != null)
{
LoadOptions();
var serviceProv = (IServiceProvider)this.site;
var guidIWebBrowserApp = Marshal.GenerateGuidForType(typeof(IWebBrowserApp)); // new Guid("0002DF05-0000-0000-C000-000000000046");
var guidIWebBrowser2 = Marshal.GenerateGuidForType(typeof(IWebBrowser2)); // new Guid("D30C1661-CDAF-11D0-8A3E-00C04FC9E26E");
IntPtr intPtr;
serviceProv.QueryService(ref guidIWebBrowserApp, ref guidIWebBrowser2, out intPtr);
browser = (IWebBrowser2)Marshal.GetObjectForIUnknown(intPtr);
((DWebBrowserEvents2_Event)browser).DocumentComplete +=
new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
}
else
{
((DWebBrowserEvents2_Event)browser).DocumentComplete -=
new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
browser = null;
}
return 0;
}
int IObjectWithSite.GetSite(ref Guid guid, out IntPtr ppvSite)
{
IntPtr punk = Marshal.GetIUnknownForObject(browser);
int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
Marshal.Release(punk);
return hr;
}
#endregion
#region Implementation of IOleCommandTarget
int IOleCommandTarget.QueryStatus(IntPtr pguidCmdGroup, uint cCmds, ref OLECMD prgCmds, IntPtr pCmdText)
{
return 0;
}
int IOleCommandTarget.Exec(IntPtr pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
{
try
{
// Accessing the document from the command-bar.
var document = browser.Document as IHTMLDocument2;
var window = document.parentWindow;
var result = window.execScript(@"alert('You will now be allowed to configure the text to highlight...');");
var form = new HighlighterOptionsForm();
form.InputText = TextToHighlight;
if (form.ShowDialog() != DialogResult.Cancel)
{
TextToHighlight = form.InputText;
SaveOptions();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return 0;
}
#endregion
#region Registering with regasm
public static string RegBHO = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects";
public static string RegCmd = "Software\\Microsoft\\Internet Explorer\\Extensions";
[ComRegisterFunction]
public static void RegisterBHO(Type type)
{
string guid = type.GUID.ToString("B");
// BHO
{
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(RegBHO, true);
if (registryKey == null)
registryKey = Registry.LocalMachine.CreateSubKey(RegBHO);
RegistryKey key = registryKey.OpenSubKey(guid);
if (key == null)
key = registryKey.CreateSubKey(guid);
key.SetValue("Alright", 1);
registryKey.Close();
key.Close();
}
// Command
{
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(RegCmd, true);
if (registryKey == null)
registryKey = Registry.LocalMachine.CreateSubKey(RegCmd);
RegistryKey key = registryKey.OpenSubKey(guid);
if (key == null)
key = registryKey.CreateSubKey(guid);
key.SetValue("ButtonText", "Highlighter options");
key.SetValue("CLSID", "{1FBA04EE-3024-11d2-8F1F-0000F87ABD16}");
key.SetValue("ClsidExtension", guid);
key.SetValue("Icon", "");
key.SetValue("HotIcon", "");
key.SetValue("Default Visible", "Yes");
key.SetValue("MenuText", "&Highlighter options");
key.SetValue("ToolTip", "Highlighter options");
//key.SetValue("KeyPath", "no");
registryKey.Close();
key.Close();
}
}
[ComUnregisterFunction]
public static void UnregisterBHO(Type type)
{
string guid = type.GUID.ToString("B");
// BHO
{
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(RegBHO, true);
if (registryKey != null)
registryKey.DeleteSubKey(guid, false);
}
// Command
{
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(RegCmd, true);
if (registryKey != null)
registryKey.DeleteSubKey(guid, false);
}
}
#endregion
}
}
i'm trying to do the same - and i've just noticed that in build log there was an error
so i've added *.snk and highlighting worked (using ie11 to, x64), but 'highlight options' menu item isn't working
这篇关于开发Internet Explorer,浏览器辅助对象扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!