Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#

Working With Microsoft.mshtml.dll and SHDocVw.dll

Rate me:
Please Sign up or sign in to vote.
2.95/5 (12 votes)
24 Jul 2008CPOL2 min read 227.4K   50   47
How to work with Microsoft.mshtml.dll and SHDocVw.dll.

Introduction

This article contains a discussion on Microsoft.mshtml.dll and SHDocVw.dll.

Background

I was working on BHO for IE in C#. Initially I did not know any thing about BHO and IE extensions. I read articles about the architecture and related information from MSDN. I also read about the architecture of IE from MSDN.

But I got my best understanding of BHO from CodeProject from this article by Pavel Zolnikov. I have listed here some problems that I faced and the solutions to those problems.

Microsoft.mshtml.dll and SHDocVW.dll

I was working on the BHO toolbar and I was trying to access an Object element (<object> tag), Image element (<img> tag), and Embed element (<embed> tag). I had no idea how to get these elements.

I had two solutions:

  1. Use JavaScript to get an element.
  2. Retrieve the Object directly from the page [like from the IE Developer ToolBar]

I used both of them because in many cases the second approach failed to detect the Object tag.

Injection JavaScript in Web Pages

We can inject our own JavaScript in any web page in IE.

C#
//Explorer is Object of SHDocVw.WebBrowserClass
HTMLDocument htmlDoc = (HTMLDocument)this.Explorer.IWebBrowser_Document;

//inject Script
htmlDoc।parentWindow.execScript("alert('hello world !!')", "javascript");

Handling Web Document Events

You can directly handle events of documents from your BHO as follows:

C#
((mshtml.HTMLDocumentEvents2_Event)htmlDoc).onmousedown += 
newHTMLDocumentEvents2_onmousedownEventHandler(MyToolBar_onmousedown);

//Function is as follows:
//This will be raised when mousedown event is fired
//....when user try to click..(downs the mouse button)

void MyToolBar_onmousedown(IHTMLEventObj pEvtObj)
{
    MessageBox.Show(pEvtObj.srcElement.tagName);
}

There was a problem with this method. Implementing the above code stopped the actual events of the browser. Events on the web browser were not fired. In other words, events of the web browser were locked. This was an annoying problem, but I found the solution for this problem here by Rick Strahl.

C#
namespace MyToolBar
{
    public delegate void DOMEvent(mshtml.IHTMLEventObj e);
    public classDOMEventHandler
    {
        public DOMEvent Handler;
        DispHTMLDocument Document;
        public DOMEventHandler(DispHTMLDocument doc)
        {
            this.Document = doc;
        }
        [DispId(0)]
        public voidCall()
        {
            Handler(Document.parentWindow.@event);
        }
    }
}

The above class can be used as:

C#
//Explorer is Object of SHDocVw.WebBrowserClass
HTMLDocument htmlDoc =(HTMLDocument)this.Explorer.IWebBrowser_Document;

DispHTMLDocument doc = (DispHTMLDocument ) htmlDoc

DOMEventHandler onmousedownhandler = newDOMEventHandler (doc); 
onmousedownhandler.Handler += new DOMEvent(Mouse_Down); 
doc.onmousedown = onmousedownhandler;

//mouse Down Can be like this
public void Mouse_Down(mshtml.IHTMLEventObje)
{
    MessageBox.Show(e.srcElement.tagName);
}

Collecting All the Images From a Web Page

I was trying to access all the images from a web page. It can be done in one of the following ways.

Using the Document.Images Collection

C#
//Explorer is Object of SHDocVw.WebBrowserClass
HTMLDocument htmlDoc = (HTMLDocument)this.Explorer.IWebBrowser_Document;
//get all the images of document
IHTMLElementCollection imgs = htmlDoc.images;
foreach (HTMLImgClass imgTag in imgs)
{
    MessageBox.Show(imgTag.src);
}

Using getElementsByTagName

C#
HTMLDocument htmlDoc = (HTMLDocument)this.Explorer.IWebBrowser_Document;
//get all the images of document

IHTMLElementCollection imgs = htmlDoc.getElementsByTagName("img");
foreach (HTMLImgClass imgTag in imgs)
{
    MessageBox.Show(imgTag.src);
}

Accessing Content From Frames and IFrames

The biggest problem was accessing content from Frames and IFrames. I tried a lot to access content from Frames and IFrames. But it was always giving me an IFrameElement or an Access Denied Exception.

C#
IHTMLElementCollection frames = 
   (IHTMLElementCollection) htmlDoc।getElementsByTagName("frame");

if (frames != null)
{
    foreach (IHTMLElement frminframes)
    {
        (HTMLFrameElement)frm).contentWindow.execScript(
                'alert('Hello From Frame')',"javascript");
    }
}

Using the above code, I was able to access the frame but it was giving me an Access Denied Exception. I was trying to attach an event with a Frame/IFrame Document but there were same problems. Finally I got a solution from here. I implemented a solution as follows:

C#
IHTMLElementCollection elcol = htmlDoc.getElementsByTagName("iframe");
foreach (IHTMLElement iel inelcol)
{
    HTMLFrameElement frm = (HTMLFrameElement )iel;
    DispHTMLDocument doc =(DispHTMLDocument)((SHDocVw.IWebBrowser2)frm).Document;
    DOMEventHandler onmousedownhandler = new DOMEventHandler (doc);
    onmousedownhandler।Handler += new DOMEvent(Mouse_Down); 
    doc.onmousedown = onmousedownhandler;
}

Get All the Attributes of the Tag / Element

I was required to get all the attributes of a tag. Attributes like height, width, src, etc. I was able to get all the attribute as follows:

C#
IHTMLElement element;
IHTMLDOMNode nd = (IHTMLDOMNode)element;

IHTMLAttributeCollection attribs = (IHTMLAttributeCollection )nd.attributes;
try
{
    foreach (IHTMLDOMAttribute2 att in attribs)
    {
        if (((IHTMLDOMAttribute)att).specified)
        {
            MessageBox.Show(att.value);
        }
    }
}catch{}

License

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


Written By
Engineer
India India
M.E. Computer
Working as Sr. Software Engineer
@Patni Computer Systems,
Gujarat
Ahmedabad
India

Comments and Discussions

 
QuestionNot implemented! Pin
Member 83534878-Feb-12 5:54
Member 83534878-Feb-12 5:54 
AnswerRe: Not implemented! Pin
Member 83534879-Feb-12 4:22
Member 83534879-Feb-12 4:22 
GeneralRe: Not implemented! Pin
Vigas223-Jan-13 0:26
Vigas223-Jan-13 0:26 

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.