BHO.cs :  » Development » AdblockIE » af0 » Adblock » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Development » AdblockIE 
AdblockIE » af0 » Adblock » BHO.cs
using System;
using System.Runtime.InteropServices;
using SHDocVw;
using Microsoft.Win32;
using System.IO;
using System.Diagnostics;
using System.Linq;
using System.Xml.Linq;
using MSHTML;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace af0.Adblock{
  [  
  ComVisible(true),
    Guid("90EFF544-3981-4d46-85C9-C0361D0931D6"), 
  ClassInterface(ClassInterfaceType.None)
  ]
  public class BHO : IObjectWithSite
  {
    // The web browser object for the host ie
    WebBrowser _webBrowser;

    public BHO()
    {
            //Debugger.Launch();
#if TRACE
            TextWriterTraceListener listener = new TextWriterTraceListener(Path.Combine(Path.GetTempPath(), "adblock.log"));
            Trace.AutoFlush = true;
            //Trace.Listeners.Clear();
            Trace.Listeners.Add(listener);
#endif
        }
    
    public static string BHOKEYNAME = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects";

    [ComRegisterFunction]
    public static void RegisterBHO(Type t)
    {
      RegistryKey key = Registry.LocalMachine.OpenSubKey(BHOKEYNAME, true);

      if (key == null)
        key = Registry.LocalMachine.CreateSubKey(BHOKEYNAME);

      string guidString = t.GUID.ToString("B");
      RegistryKey bhoKey = key.OpenSubKey(guidString);
      
      if (bhoKey == null)
        bhoKey = key.CreateSubKey(guidString);

      key.Close();
      bhoKey.Close();
    }
    
    [ComUnregisterFunction]
    public static void UnregisterBHO(Type t)
    {
      RegistryKey key = Registry.LocalMachine.OpenSubKey(BHOKEYNAME, true);
      string guidString = t.GUID.ToString("B");
      
      if (key != null)
        key.DeleteSubKey(guidString, false);
    }

    #region IObjectWithSite Members

        private void Filter()
        {
            try
            {
                HTMLDocument doc = _webBrowser.Document as HTMLDocument;
                if (doc != null)
                {
                    AdblockEngine.Instance.Filter(doc, doc.url);
                }
            }
            catch (Exception e)
            {
                Trace.Fail(e.Message, e.StackTrace);
            }
        }

    public int SetSite(object site)
    {
            //Debugger.Launch();

            if (site != null)
            {
                _webBrowser = (WebBrowser)site;
                if (!_webBrowser.FullName.ToUpper().EndsWith("IEXPLORE.EXE"))
                {
                    _webBrowser = null;
                    return 0;
                }

                _webBrowser.DownloadComplete += new DWebBrowserEvents2_DownloadCompleteEventHandler(Filter);
                //_webBrowser.NavigateComplete2 += new DWebBrowserEvents2_NavigateComplete2EventHandler((object x, ref object y) => AdblockEngine.Instance.PreFilter((x as IWebBrowser2).Document as HTMLDocument, y as string));
                //_webBrowser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler((object x, ref object y) => AdblockEngine.Instance.Filter((x as IWebBrowser2).Document as HTMLDocument, y as string));
      }
      else
            {
                _webBrowser.DownloadComplete -= new DWebBrowserEvents2_DownloadCompleteEventHandler(Filter);
                //_webBrowser.NavigateComplete2 -= new DWebBrowserEvents2_NavigateComplete2EventHandler((object x, ref object y) => _adblocker.PreFilter((x as IWebBrowser2).Document as HTMLDocument, y as string));
                //_webBrowser.DocumentComplete -= new DWebBrowserEvents2_DocumentCompleteEventHandler((object x, ref object y) => _adblocker.Filter((x as IWebBrowser2).Document as HTMLDocument, y as string));

        _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
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.