Click here to Skip to main content
Licence CPOL
First Posted 9 Jun 2009
Views 26,446
Downloads 309
Bookmarked 22 times

Internet Explorer Late Binding Automation

By | 9 Jun 2009 | Article
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:

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.

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...

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.

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.

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)

About the Author

yincekara

Software Developer (Senior)
MikroKom Yazilim A.S.
Turkey Turkey

Member

c# , vb6 , mssql , oracle , mysql , asp , asp.net developer.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralUsing POST operation Pinmemberkizhakk10:41 29 Jun '10  
GeneralClickButton which has no name PinmemberLehoanq7:56 6 Sep '09  
GeneralRe: ClickButton which has no name Pinmemberyincekara10:18 6 Sep '09  
GeneralRe: ClickButton which has no name PinmemberLehoanq15:25 6 Sep '09  
GeneralRe: ClickButton which has no name PinmemberLehoanq17:40 6 Sep '09  
GeneralRe: ClickButton which has no name [modified] Pinmemberyincekara1:08 29 Sep '09  
GeneralRe: ClickButton which has no name Pinmemberdwegmann21:50 30 Mar '10  
GeneralRe: ClickButton which has no name Pinmemberyincekara22:07 30 Mar '10  
Questionget IE menubar, toolbar click notifications Pinmembersumedh.shrikrushana23:44 15 Jul '09  
AnswerRe: get IE menubar, toolbar click notifications [modified] Pinmemberyincekara23:56 15 Jul '09  
GeneralExample of event handling from Form1 Pinmemberhoncho265:27 10 Jul '09  
GeneralRe: Example of event handling from Form1 Pinmemberhoncho265:57 10 Jul '09  
GeneralRe: Example of event handling from Form1 Pinmemberyincekara23:53 15 Jul '09  
GeneralRe: Example of event handling from Form1 Pinmemberhoncho263:15 16 Jul '09  
GeneralRe: Example of event handling from Form1 Pinmemberyincekara4:01 16 Jul '09  
GeneralRe: Example of event handling from Form1 Pinmemberhoncho264:42 16 Jul '09  
GeneralRe: Example of event handling from Form1 Pinmemberyincekara2:38 20 Jul '09  
GeneralRe: Example of event handling from Form1 Pinmemberhoncho262:46 20 Jul '09  
GeneralRe: Example of event handling from Form1 Pinmemberyincekara4:23 20 Jul '09  
GeneralRe: Example of event handling from Form1 Pinmemberhoncho264:32 20 Jul '09  
GeneralRe: Example of event handling from Form1 Pinmemberyincekara4:48 20 Jul '09  
GeneralRe: Example of event handling from Form1 Pinmemberhoncho264:55 20 Jul '09  
GeneralRe: Example of event handling from Form1 Pinmemberyincekara5:24 20 Jul '09  
GeneralRe: Example of event handling from Form1 Pinmemberhoncho266:59 20 Jul '09  
GeneralRe: Example of event handling from Form1 Pinmemberyincekara8:57 20 Jul '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 9 Jun 2009
Article Copyright 2009 by yincekara
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid