2011-10-28 20 views
0

我已經寫了示例IE加載項(基於Internet上的樣本)。這個插件生成日誌文件並在其中寫入來自OnDocumentComplete和OnBeforeNavigate2事件的信息。 不幸的是,這是行不通的。我成功地在IE中安裝了這個插件,它是可見的 - 但正如我所說,它不工作(信息不會寫入日誌文件)。我的Internet Explorer加載項不起作用

這裏是這個插件的代碼:

using System; 
using System.Collections.Generic; 
using System.Text; 
using SHDocVw; 
using mshtml; 
using System.IO; 
using Microsoft.Win32; 
using System.Runtime.InteropServices; 
using System.Security.AccessControl; 

namespace BHO_SampleApp 
{ 

    [ComVisible(true), 
    Guid("8a194578-81ea-4850-9911-13ba2d71efbd"), 
    ClassInterface(ClassInterfaceType.None) 
    ] 
    public class BHO:IObjectWithSite 
    { 
     WebBrowser webBrowser; 
     public void OnDocumentComplete(object pDisp, ref object URL) 
     { 
      using (StreamWriter sw = new StreamWriter(@"C:\log.txt")) 
      { 
       sw.WriteLine(String.Format("site: {0}, {1}", URL, DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"))); 
      } 
     } 

     public void OnBeforeNavigate2(object pDisp, ref object URL, ref object Flags, ref object TargetFrameName, ref object PostData, ref object Headers, ref bool Cancel) 
     { 
      using (StreamWriter sw = new StreamWriter(@"C:\log.txt")) 
      { 
       sw.WriteLine(String.Format("site: {0}, {1}", URL, DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"))); 
      }   
     } 

     #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); 
       webBrowser.BeforeNavigate2+=new DWebBrowserEvents2_BeforeNavigate2EventHandler(this.OnBeforeNavigate2); 
      } 
      else 
      { 
       webBrowser.DocumentComplete -= new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete); 
       webBrowser.BeforeNavigate2 -= new DWebBrowserEvents2_BeforeNavigate2EventHandler(this.OnBeforeNavigate2); 
       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; 
     } 
     #endregion 
    } 

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

什麼是錯的這個插件?

+0

代碼有很多錯誤。你做了什麼來調試它?你確定你的日誌語句正在成功,我沒有看到任何catch語句,所以他們可能只是失敗。請不要以這種方式寫入註冊表。 –

+2

只讀這個 - http://stackoverflow.com/questions/352217/how-do-i-create-a-ie-add-on-in-net –

回答

0

如果您的瀏覽器處於保護模式(默認),則您的加載項無權寫入c:\ log.txt。嘗試禁用他的保護模式來檢查它是否開始工作。

相關問題