在localhost中加载站点时,c#bho中的函数未暴露

时间:2014-02-06 14:53:23

标签: c# bho

我使用c#创建了一个样本bho,并根据这篇文章公开了1个函数

Calling C# BHO methods from Javascript

bho已成功注册到IE 9,我可以在网络上托管的网站上访问javascript中的公开功能。

我使用我的本地服务器创建了一个测试页面,我的bho函数现在未定义。

我尝试将url从localhost更改为ip地址,但仍无效果。

下面是我的代码。如果有人可以检查我的代码是否有问题,那就太好了;

感谢。

BHO.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.Design;
using SHDocVw;
using mshtml;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Expando;
using Microsoft.Win32;

namespace MySampleBHO
{
    [
        ComVisible(true),
        Guid("7bb16759-fdfc-4e3f-a081-eb65e4683640"),
        ClassInterface(ClassInterfaceType.None),
        ProgId("SIV"), ComDefaultInterface(typeof(IExtension))
    ]
    public class BHO:IObjectWithSite, IExtension
    {
        WebBrowser webBrowser;
        HTMLDocument document;

        public void OnDocumentComplete(object pDisp, ref object URL) {
            document = (HTMLDocument)webBrowser.Document;

            dynamic window = webBrowser.Document.parentWindow;
            IExpando windowEx = (IExpando)window;
            windowEx.AddProperty("sEnhancer");
            window.sEnhancer = this;
        }

        public String goEnhance(String ImageId, int Width, int Height) {
            var img = document.getElementById(ImageId);
            var src = img.getAttribute("src");
            return src.ToString();
        }

        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.QueryInterface(punk, ref guid, out ppvSite);
            Marshal.Release(punk);

            return hr;
        }

        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);
        }
    }
}

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms.Design; using SHDocVw; using mshtml; using System.Runtime.InteropServices; using System.Runtime.InteropServices.Expando; using Microsoft.Win32; namespace MySampleBHO { [ ComVisible(true), Guid("7bb16759-fdfc-4e3f-a081-eb65e4683640"), ClassInterface(ClassInterfaceType.None), ProgId("SIV"), ComDefaultInterface(typeof(IExtension)) ] public class BHO:IObjectWithSite, IExtension { WebBrowser webBrowser; HTMLDocument document; public void OnDocumentComplete(object pDisp, ref object URL) { document = (HTMLDocument)webBrowser.Document; dynamic window = webBrowser.Document.parentWindow; IExpando windowEx = (IExpando)window; windowEx.AddProperty("sEnhancer"); window.sEnhancer = this; } public String goEnhance(String ImageId, int Width, int Height) { var img = document.getElementById(ImageId); var src = img.getAttribute("src"); return src.ToString(); } 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.QueryInterface(punk, ref guid, out ppvSite); Marshal.Release(punk); return hr; } 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); } } }

IObjectWithSite.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace MySampleBHO
{
    [
        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);

    }
}

IExtension.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.InteropServices; namespace MySampleBHO { [ 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); } }

1 个答案:

答案 0 :(得分:2)

我已经找到了我的问题的解决方案......只需要更改onDocumentComplete函数中的某些部分......

<强>解决方案

更改以下代码:

public void OnDocumentComplete(object pDisp, ref object URL) {
  document = (HTMLDocument)webBrowser.Document;

  dynamic window = webBrowser.Document.parentWindow;
  IExpando windowEx = (IExpando)window;
  windowEx.AddProperty("sEnhancer");
  window.sEnhancer = this;
}

public void OnDocumentComplete(object pDisp, ref object URL) { document = (HTMLDocument)webBrowser.Document; dynamic window = webBrowser.Document.parentWindow; IExpando windowEx = (IExpando)window; windowEx.AddProperty("sEnhancer"); window.sEnhancer = this; }

为:

public void OnDocumentComplete(object pDisp, ref object URL) {
  document = (HTMLDocument)webBrowser.Document;

  dynamic window = webBrowser.Document.parentWindow;
  IExpando windowEx = (IExpando)window;
  PropertyInfo p = windowEx.AddProperty("sEnhancer");
  p.SetValue(windowEx, this);
}

解决方案基于这篇文章BHO exposing javascript method works in IE 9+ but fails in earlier versions