Click here to Skip to main content
15,879,613 members
Articles / Desktop Programming / Windows Forms

Internet Explorer Late Binding Automation

Rate me:
Please Sign up or sign in to vote.
4.69/5 (7 votes)
9 Jun 2009CPOL2 min read 82.1K   908   25   36
Internet Explorer automation sample code using late binding, without Microsoft.mshtml and shdocvw dependency.

Introduction

Most developers often need Internet Explorer automation, which basically means opening a browser, filling some forms, and posting data programmatically.

The most common approach is to use shdocvw.dll (the Microsoft Web Browser control) and Mshtml.dll (the HTML Parsing and Rendering Component), or Microsoft.Mshtml.dll which is actually a .NET wrapper for Mshtml.dll. You can get more information about Internet Explorer - About the Browser here.

If you pick the above method and DLLs, let's see some of the problems you may have to deal with:

  • You have to distribute these DLLs because your project would be dependent to these DLLs, and this is a serious problem if you cannot deploy them correctly. Simply do some Googling about shdocvw and mshtml.dll distributing problems, and you'll see what I'm talking about.
  • You have to deploy an 8 MB Microsoft.mshtml.dll because this DLL is not part of the .NET framework.

In this case, what we need to do is use a late binding technique. Writing our own wrappers for the above mentioned DLLs. And of course, we'll do this as it is more useful than using these DLLs. For instance, we won't need to check if the document download operation is complete because IEHelper will do this for us.

Background

What we need to do here is use a late binding technique which is successfully explained here in this article. Thanks to Ariadne for this great article.

The missing point for me here is attaching to COM events which we really need to know for querying if the document download operation is complete.

Here is how we connect to the web browser's events:

C#
private IConnectionPointContainer connectionPointContainer;
private IConnectionPoint connectionPoint;
private int pdwCookie = 0;

private void registerDocumentEvents()
{
    connectionPointContainer = IeApplication as IConnectionPointContainer;
    Guid guid = new Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D");

    connectionPointContainer.FindConnectionPoint(ref guid, out connectionPoint);
    browserEvents = new DWebBrowserEvents2_Helper();

    connectionPoint.Advise(browserEvents, out pdwCookie);
    DocumentStatus ds = DocumentStatus.Instance;
    ds.DownloadComplete = false;
}

After we have attached to the web browser events, we need to use a singleton class to query the document status. That is what DocumentStatus does.

After we get feedback from the DocumentComplete event, we set DownloadComplete to true so our code can flow.

C#
void DWebBrowserEvents2.DocumentComplete(object pDisp, ref object URL)
{
    DocumentStatus ds = DocumentStatus.Instance;
    ds.DownloadComplete = true;
}

And, of course, we need to unregister events when we are done...

C#
private void unregisteDocumentEvents()
{
    connectionPoint.Unadvise(pdwCookie);
    Marshal.ReleaseComObject(connectionPoint);
}

If you are curious about how I wrote those COM wrappers, let me give you a clue: Use Comto.net by aurigma.

Using the code

Simply start a new IEHelper instance. Navigate to the pages you want to be in. Find some input elements by name or ID. Set their values and post them.

C#
IEHelper ie = new IEHelper();
ie.OpenAVisibleBlankDocument();

object p = null;
string url = @"http://mail.google.com/mail/?hl=en&tab=wm";
bool ret = ie.Navigate(url, ref p, ref p, ref p, ref p);

ie.SetValueById("Email", txtUserName.Text);
ie.SetValueById("Passwd", txtPassword.Text);
ie.ClickButtonByName("signIn");

What to do next

What you need to do is fill in the blanks. Because when you download the source code, you'll see that most of the events are not implemented yet. If you think that you need those events, evolve them according to your requirements.

C#
void DWebBrowserEvents2.OnQuit()
{
    throw new Exception("The method or operation is not implemented.");
}

License

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


Written By
Software Developer (Senior) MikroKom Yazilim A.S.
Turkey Turkey
c# , vb6 , mssql , oracle , mysql , asp , asp.net developer.

Comments and Discussions

 
QuestionOnly works in ie 8? Pin
Felipe Garcia26-Aug-14 13:39
Felipe Garcia26-Aug-14 13:39 
AnswerRe: Only works in ie 8? Pin
yincekara19-Oct-14 0:05
yincekara19-Oct-14 0:05 
GeneralUsing POST operation Pin
kizhakk29-Jun-10 10:41
kizhakk29-Jun-10 10:41 
GeneralClickButton which has no name Pin
Lehoanq6-Sep-09 7:56
Lehoanq6-Sep-09 7:56 
GeneralRe: ClickButton which has no name Pin
yincekara6-Sep-09 10:18
yincekara6-Sep-09 10:18 
GeneralRe: ClickButton which has no name Pin
Lehoanq6-Sep-09 15:25
Lehoanq6-Sep-09 15:25 
GeneralRe: ClickButton which has no name Pin
Lehoanq6-Sep-09 17:40
Lehoanq6-Sep-09 17:40 
GeneralRe: ClickButton which has no name [modified] Pin
yincekara29-Sep-09 1:08
yincekara29-Sep-09 1:08 
GeneralRe: ClickButton which has no name Pin
dwegmann30-Mar-10 21:50
dwegmann30-Mar-10 21:50 
GeneralRe: ClickButton which has no name Pin
yincekara30-Mar-10 22:07
yincekara30-Mar-10 22:07 
Questionget IE menubar, toolbar click notifications Pin
sumedh.shrikrushana15-Jul-09 23:44
sumedh.shrikrushana15-Jul-09 23:44 
AnswerRe: get IE menubar, toolbar click notifications [modified] Pin
yincekara15-Jul-09 23:56
yincekara15-Jul-09 23:56 
GeneralExample of event handling from Form1 Pin
honcho2610-Jul-09 5:27
honcho2610-Jul-09 5:27 
GeneralRe: Example of event handling from Form1 Pin
honcho2610-Jul-09 5:57
honcho2610-Jul-09 5:57 
GeneralRe: Example of event handling from Form1 Pin
yincekara15-Jul-09 23:53
yincekara15-Jul-09 23:53 
GeneralRe: Example of event handling from Form1 Pin
honcho2616-Jul-09 3:15
honcho2616-Jul-09 3:15 
GeneralRe: Example of event handling from Form1 Pin
yincekara16-Jul-09 4:01
yincekara16-Jul-09 4:01 
GeneralRe: Example of event handling from Form1 Pin
honcho2616-Jul-09 4:42
honcho2616-Jul-09 4:42 
GeneralRe: Example of event handling from Form1 Pin
yincekara20-Jul-09 2:38
yincekara20-Jul-09 2:38 
GeneralRe: Example of event handling from Form1 Pin
honcho2620-Jul-09 2:46
honcho2620-Jul-09 2:46 
GeneralRe: Example of event handling from Form1 Pin
yincekara20-Jul-09 4:23
yincekara20-Jul-09 4:23 
GeneralRe: Example of event handling from Form1 Pin
honcho2620-Jul-09 4:32
honcho2620-Jul-09 4:32 
GeneralRe: Example of event handling from Form1 Pin
yincekara20-Jul-09 4:48
yincekara20-Jul-09 4:48 
GeneralRe: Example of event handling from Form1 Pin
honcho2620-Jul-09 4:55
honcho2620-Jul-09 4:55 
I didn't make a cast on it, no. I was trying to access pEvtObj in the onClick handler, specifically pEvtObj.srcElement.innerHTML. The idea was to try to get the text of a hyperlink I clicked in the document I loaded.

It returns this error in the visualizer object or when I try to send the value of that property to a message box.

Thanks!
GeneralRe: Example of event handling from Form1 Pin
yincekara20-Jul-09 5:24
yincekara20-Jul-09 5:24 

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.