2014-02-06 66 views
2

加載網站時不會暴露我使用C#創建了一個示例BHO和暴露這個職位功能在C#BHO在本地主機

Calling C# BHO methods from Javascript

的BHO得到註冊到IE 9成功以1個功能,我可以訪問在網站上託管的網站上的JavaScript中暴露的函數。

我用我的本地服務器創建了一個測試頁面,我的bho函數現在是未定義的。

我試圖將本地主機的IP地址更改爲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); 
     } 
    } 
} 

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.InterfaceIsDual), 
     Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352") 
    ] 
    public interface IExtension 
    { 
     String goEnhance(String ImageId, int Width, int Height); 
    } 
} 

+0

可能是一個安全問題congiguration基礎。比較區域「內聯網」和區域「本地內聯網」 – manuell

+0

@mauell設置感謝您的頭......我試着比較「互聯網」區和「內聯網」區..他們有相同的設置.. 。我也嘗試啓用/禁用EPM ...但仍然沒有效果 – blackcondor88

回答

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; 
    PropertyInfo p = windowEx.AddProperty("sEnhancer"); 
    p.SetValue(windowEx, this); 
} 

溶液從這個帖子BHO exposing javascript method works in IE 9+ but fails in earlier versions