Click here to Skip to main content
15,884,425 members
Articles / Programming Languages / C#
Article

Internet Explorer Activity Monitor

Rate me:
Please Sign up or sign in to vote.
3.64/5 (8 votes)
29 Oct 2005CPOL 132.4K   2.7K   49   31
The Internet Activity Monitor captures what you are doing on Internet Explorer like opening a browser, requesting a Website and closing the browser.
Sample image

Introduction

This application is a simple but useful one. I used this application in a Windows Service which monitors the activity of the browser and the person logged-in at that time.

Adding Reference

Sample Image - IE_Activity_Monitor.jpg

Add a reference in your project to SHDocVw.dll. This is a COM component named Microsoft Internet Controls. It contains the definitions of the InternetExplorer and ShellWindows classes.

About the Code

The code can be divided into three parts:

  1. Browser Open Activity
  2. Browser Navigation Activity
  3. Browser Close Activity

Following are the event handlers that are fired at their respective events:

C#
// THIS EVENT IS FIRED WHEN A NEW BROWSER IS OPENED
        private void shellWindows_WindowRegistered(int z)
        {            
            string filnam;        
            
            foreach (SHDocVw.InternetExplorer ie in shellWindows)
            {
                filnam = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
    
                if (filnam.Equals("iexplore"))
                {
                    browser = ie;    
                        
                    bool check=true;

                    for (int i=0; i<Current_IE_Handles.Count; i++)
                    {
                        if (Current_IE_Handles[i].ToString()==browser.HWND.ToString())
                        {
                            check=false;
                        }
                    }
                        
                    if (check==true)
                    {
                        Current_IE_Handles.Add(browser.HWND.ToString());
                        this.listBox1.Items.Add("Browser Open : Handle
					( "+browser.HWND.ToString()+" )");
                        browser.BeforeNavigate2+=
			new SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHandler
			(browser_BeforeNavigate2);
                    }                        
                }
            }            
        }

When you type something like www.codeproject.com, this event is fired and here you can capture all the information about the Internet Explorer Navigation.

C#
// THIS EVENT IS FIRED WHEN THE BROWSER IS JUST ABOUT TO NAVIGATE TO A WEBSITE
        private void browser_BeforeNavigate2(object a,ref object b,
		ref object c,ref object d,ref object e,ref object f,ref bool g)
        {
            this.listBox1.Items.Add(browser.HWND.ToString()+ " : "+b.ToString());
        }
C#
// THIS EVENT IS FIRED WHEN A NEW BROWSER IS CLOSED
        private void shellWindows_WindowRevoked(int z)        
        {            
            string filnam;
            ArrayList Closed_IE=new ArrayList();
              
            foreach (SHDocVw.InternetExplorer ie in shellWindows)
            {
                filnam = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
    
                if (filnam.Equals("iexplore"))
                {
                    Closed_IE.Add(ie.HWND.ToString());
                }
            }    
        
            for (int i=0; i<this.Current_IE_Handles.Count; i++)
            {
                bool check=false;

                for (int j=0; j<Closed_IE.Count; j++)
                {
                    if (Convert.ToInt32(this.Current_IE_Handles[i])==
			Convert.ToInt32(Closed_IE[j]))
                        check=true;
                }

                if (check==false)
                {
                    this.listBox1.Items.Add("Browser Closed : Handle
			( "+this.Current_IE_Handles[i].ToString()+" )");
                    this.Current_IE_Handles.RemoveAt(i);
                    break;
                }
            }
        } 

Currently this application captures the activity of the Internet Explorer. A new and improved version will be posted shortly that can monitor the activity of user defined browsers.

Initial Work

I carried forward the work done by Steven M. Cohn. Check out his post at this link.

History

  • 29th October, 2005: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Pakistan Pakistan
Ali Raza Shaikh did his BS (CS) from FAST-NUCES, Karachi Campus. He is also a Microsoft Certified Application Developer. He have a great passion for programming and all the programming related stuff. He can be reached at alirazashaikh.blogspot.com

Comments and Discussions

 
GeneralBUG whan open more windows Pin
EmailSolidale22-Nov-05 22:54
EmailSolidale22-Nov-05 22:54 
GeneralRe: BUG whan open more windows Pin
Ali Raza Shaikh28-Jan-06 22:30
Ali Raza Shaikh28-Jan-06 22:30 
GeneralRe: BUG whan open more windows Pin
Tadas Danielius11-Nov-06 2:59
Tadas Danielius11-Nov-06 2:59 
GeneralRe: BUG whan open more windows Pin
scippyone6-May-07 5:22
scippyone6-May-07 5:22 
QuestionMonitor explorer Pin
nagarsoft6-Nov-05 23:26
nagarsoft6-Nov-05 23:26 
QuestionAccessing Internet Explorer history Pin
nagarsoft1-Nov-05 9:57
nagarsoft1-Nov-05 9:57 
GeneralGUI Bug Pin
nadav7430-Oct-05 21:19
nadav7430-Oct-05 21:19 
GeneralRe: GUI Bug Pin
nadav7431-Oct-05 1:43
nadav7431-Oct-05 1:43 
Below is my fixed version.
I would also advise you to set the parameter names as they are in the Object Browser. Using (a..g) is a bad habbit.

Nadav

--

/*
* ----------------------------------------------------------------------------------
* NAME : ALI RAZA SHAIKH
* EMAIL : ali_raza_shaikh@yahoo.com
* ali_raza_shaikh@hotmail.com
* DATED : 30th October, 2005
* URLS : www.programmersparadise.cjb.net
* alirazashaikh.blogspot.com
*
* CONTRIBUTION : nadav74 (nadav74 at Google's web mail service)
* GUI update only in GUI thread fix
* APPLICATION : Browser Activity Monitor
* VERSION : 1.01
* ----------------------------------------------------------------------------------
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace IEActivityMonitor
{
public class ActivityMonitor : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private SHDocVw.InternetExplorer browser;
private SHDocVw.ShellWindows shellWindows;
private ArrayList Current_IE_Handles;
private System.ComponentModel.Container components = null;
private delegate void WindowRegisteredRevokedDelegate(int param);
private delegate void BeforeNavigateDelegate(object a, ref object b, ref object c, ref object d, ref object e, ref object f, ref bool g);

#region Constructor

public ActivityMonitor()
{
InitializeComponent();
Current_IE_Handles=new ArrayList();
shellWindows=new SHDocVw.ShellWindowsClass();
shellWindows.WindowRegistered+=new SHDocVw.DShellWindowsEvents_WindowRegisteredEventHandler(shellWindows_WindowRegistered);
shellWindows.WindowRevoked+=new SHDocVw.DShellWindowsEvents_WindowRevokedEventHandler(shellWindows_WindowRevoked);

}

#endregion

#region Dispose

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#endregion

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listBox1.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(0, 0);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(429, 303);
this.listBox1.TabIndex = 0;
//
// ActivityMonitor
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(429, 305);
this.Controls.Add(this.listBox1);
this.Name = "ActivityMonitor";
this.Text = "Browser Activity Monitor";
this.ResumeLayout(false);

}
#endregion

// THE MAIN ENTRY POINT FOR THE APPLICATION.
[STAThread]
static void Main()
{
Application.Run(new ActivityMonitor());
}

// THIS EVENT IS FIRED WHEN THE BROWSER IS JUST ABOUT THE NAVIGATE TO A WEBSITE
private void browser_BeforeNavigate2(object a,ref object b,ref object c,ref object d,ref object e,ref object f,ref bool g)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new BeforeNavigateDelegate(this.browser_BeforeNavigate2), new object[] { a, b, c, d, e, f, g } );
}
else
{
this.listBox1.Items.Add(browser.HWND.ToString()+ " : "+b.ToString());
}
}

// THIS EVENT IS FIRED WHEN THE A NEW BROWSER IS CLOSED
private void shellWindows_WindowRevoked(int z)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new WindowRegisteredRevokedDelegate(shellWindows_WindowRevoked), new object[] { z });
}
else
{
string filnam;
ArrayList Closed_IE = new ArrayList();

foreach (SHDocVw.InternetExplorer ie in shellWindows)
{
filnam = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();

if (filnam.Equals("iexplore"))
{
Closed_IE.Add(ie.HWND.ToString());
}
}

for (int i = 0; i < this.Current_IE_Handles.Count; i++)
{
bool check = false;

for (int j = 0; j < Closed_IE.Count; j++)
{
if (Convert.ToInt32(this.Current_IE_Handles[i]) == Convert.ToInt32(Closed_IE[j]))
check = true;
}

if (check == false)
{
this.listBox1.Items.Add("Browser Closed : Handle( " + this.Current_IE_Handles[i].ToString() + " )");
this.Current_IE_Handles.RemoveAt(i);
break;
}
}
}
}

// THIS EVENT IS FIRED WHEN THE A NEW BROWSER IS OPEN
private void shellWindows_WindowRegistered(int z)
{
string filnam;

if (this.InvokeRequired)
{
this.BeginInvoke(new WindowRegisteredRevokedDelegate(shellWindows_WindowRegistered), new object[] { z });
}
else
{
foreach (SHDocVw.InternetExplorer ie in shellWindows)
{
try
{
filnam = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();

if (filnam.Equals("iexplore"))
{
browser = ie;

bool check = true;

for (int i = 0; i < Current_IE_Handles.Count; i++)
{
if (Current_IE_Handles[i].ToString() == browser.HWND.ToString())
{
check = false;
}
}

if (check == true)
{
Current_IE_Handles.Add(browser.HWND.ToString());
this.listBox1.Items.Add("Browser Open : Handle( " + browser.HWND.ToString() + " )");
browser.BeforeNavigate2 += new SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHandler(browser_BeforeNavigate2);
}
}

}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.ToString());
}
}
}
}
}
}

GeneralRe: GUI Bug Pin
Fresh Mexican Food Fan15-Mar-07 11:11
Fresh Mexican Food Fan15-Mar-07 11:11 
GeneralRe: GUI Bug Pin
nadav7415-Mar-07 11:43
nadav7415-Mar-07 11:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.