Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 开发Internet Explorer、浏览器辅助对象扩展?_C#_C++_Internet Explorer_Atl_Browser Extension - Fatal编程技术网

C# 开发Internet Explorer、浏览器辅助对象扩展?

C# 开发Internet Explorer、浏览器辅助对象扩展?,c#,c++,internet-explorer,atl,browser-extension,C#,C++,Internet Explorer,Atl,Browser Extension,1) 我试图用C#制作一个简单的BHO,就像这里已经回答的: 2) 但不幸的是,他们的尝试都少于IE11,其中有些成功了,有些也失败了 3) 在遵循了回答中提到的所有内容后,我还购买了官方代码标志,但它在IE11 Windows 7 64位中根本不起作用 您可以下载我准备的Visual studio 2013:版本,其中包括IE11的所有源代码和详细信息: 问:有谁能建议/建议/帮助我如何让这个BHO成为一个你好的世界 我也尝试过codeproject中的其他示例,但仍然没有一个我能够成功,尝

1) 我试图用C#制作一个简单的BHO,就像这里已经回答的:

2) 但不幸的是,他们的尝试都少于IE11,其中有些成功了,有些也失败了

3) 在遵循了回答中提到的所有内容后,我还购买了官方代码标志,但它在IE11 Windows 7 64位中根本不起作用

您可以下载我准备的Visual studio 2013:版本,其中包括IE11的所有源代码和详细信息:

问:有谁能建议/建议/帮助我如何让这个BHO成为一个你好的世界

我也尝试过codeproject中的其他示例,但仍然没有一个我能够成功,尝试了4周后,我迷路了,请告知我的ClassLibrary2.rar有什么问题,它没有点亮文本“browser”

我完全迷路了,请告诉我

编辑:

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
  }
}
使用系统;
使用System.Collections.Generic;
使用System.Runtime.InteropServices;
使用System.Windows.Forms;
使用Microsoft.Win32;
使用mshtml;
使用SHDocVw;
命名空间InternetExplorerExtension
{
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid(“D40C654D-7C51-4EB3-95B2-1E23905C2A2D”)]
[ProgId(“MyBHO.WordHighlighter”)]
公共类单词HighlighterHo:IObjectWithSite、IOleCommandTarget
{
常量字符串DefaultTextToHighlight=“browser”;
IWebBrowser2浏览器;
私有对象站点;
#区域突出显示文本
void OnDocumentComplete(对象pDisp,引用对象URL)
{
尝试
{
//这将防止此方法被执行多次。
如果(pDisp!=此.site)
返回;
var document2=browser.Document作为IHTMLDocument2;
var document3=browser.Document作为IHTMLDocument3;
var window=document2.parentWindow;
execScript(@“function fncadedbyaddon(){alert('Message addon.');}”);
队列=新队列();
foreach(文档3.childNodes中的IHTMLDOMNode每个child)
排队。排队(每个孩子);
而(queue.Count>0)
{
//用突出显示的文本替换所需文本
var domNode=queue.Dequeue();
var textNode=domNode作为IHTMLDOMTextNode;
if(textNode!=null)
{
if(textNode.data.Contains(TextToHighlight))
{
var newText=textNode.data.Replace(TextToHighlight,“+TextToHighlight+”);
var newNode=document2.createElement(“span”);
newNode.innerHTML=newText;
replaceNode((IHTMLDOMNode)newNode);
}
}
其他的
{
//将子项添加到集合
var x=(IHTMLDOMChildrenCollection)(domNode.childNodes);
foreach(IHTMLDOMNode x中的每个孩子)
{
if(每个child是mshtml.IHTMLScriptElement)
继续;
if(每个child是mshtml.IHTMLStyleElement)
继续;
排队。排队(每个孩子);
}
}
}
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
}
}
#端区
#区域加载和保存数据
静态字符串TextToHighlight=DefaultTextToHighlight;
公共静态字符串RegData=“软件\\MyIEExtension”;
[DllImport(“ieframe.dll”)]
公共静态外部输入IEGetWriteableHKCU(参考IntPtr phKey);
私有静态void SaveOptions()
{
//在IE 7,8,9中,(桌面)10个选项卡以保护模式运行
//禁止向HKLM、HKCU写信。
//必须向IE请求“可写”注册表节指针
//这将类似于HKU/S-1-7***/Software/AppDataLow/
//在“metro”IE 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=新的IntPtr();
var应答=IEGetWriteableHKCU(参考phKey);
RegistryKey可写_registry=RegistryKey.FromHandle(
新的Microsoft.Win32.SafeHandles.SafeRegistryHandle(phKey,true)
);
RegistryKey RegistryKey=writeable_registry.OpenSubKey(RegData,true);
如果(registryKey==null)
registryKey=writeable_registry.CreateSubKey(RegData);
设置值(“数据”,TextToHighlight);
可写的_注册表。关闭();
}
私有静态void LoadOptions()
{
//在IE 7,8,9中,(桌面)10个选项卡以保护模式运行
//禁止向HKLM、HKCU写信。
//必须向IE请求“可写”注册表节指针
//这将类似于HKU/S-1-7***/Software/AppDataLow/
//在“metro”IE 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=新的IntPtr();
var应答=IEGetWriteableHKCU(参考phKey);
RegistryKey可写_registry=RegistryKey.FromHandle(
新的Microsoft.Win32.SafeHandles.SafeRegistryHandle(phKey,true)
);
RegistryKey RegistryKey=writeable_registry.OpenSubKey(RegData,true);
如果(registryKey==null)
registryKey=writeable_registry.CreateSubKey(RegData);
设置值(“数据”,TextToHighlight);
如果(registryKey==null)
{
TextToHighlight=默认TextToHighlight;
}
其他的
{
TextToHighlight=(字符串)registryKey.GetValue(“数据”);
}
可写的_注册表。关闭();
}
#端区
[Guid(“6D5140C1-7436-11CE-8034-00AA006009FA”)]
[接口类型(1)]
公共干涉