Click here to Skip to main content
11,637,662 members (79,444 online)
Click here to Skip to main content

Working With Microsoft.mshtml.dll and SHDocVw.dll

, 24 Jul 2008 CPOL 135.5K 48
Rate this:
Please Sign up or sign in to vote.
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.

//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:

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

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:

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

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

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.

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:

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:

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)

Share

About the Author

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

You may also be interested in...

Comments and Discussions

 
QuestionHow i handle file save dialog in Internet Explorer? Pin
MuhammadUSman113-Apr-15 19:21
memberMuhammadUSman113-Apr-15 19:21 
AnswerRe: How i handle file save dialog in Internet Explorer? Pin
EmersioN14-Apr-15 8:56
memberEmersioN14-Apr-15 8:56 
GeneralRe: How i handle file save dialog in Internet Explorer? Pin
MuhammadUSman114-Apr-15 20:55
memberMuhammadUSman114-Apr-15 20:55 
QuestionHow to change the browser Html Pin
Aakash Jain22-Jul-13 0:09
memberAakash Jain22-Jul-13 0:09 
AnswerRe: How to change the browser Html Pin
EmersioN23-Jul-13 22:30
memberEmersioN23-Jul-13 22:30 
GeneralRe: How to change the browser Html Pin
Aakash Jain26-Aug-13 19:13
memberAakash Jain26-Aug-13 19:13 
QuestionNot implemented! Pin
Member 83534878-Feb-12 5:54
memberMember 83534878-Feb-12 5:54 
AnswerRe: Not implemented! Pin
Member 83534879-Feb-12 4:22
memberMember 83534879-Feb-12 4:22 
GeneralRe: Not implemented! Pin
Vigas223-Jan-13 0:26
memberVigas223-Jan-13 0:26 
QuestionFrame/IFrame operation success ! Pin
linda83103-Sep-11 23:59
memberlinda83103-Sep-11 23:59 
AnswerRe: Frame/IFrame operation success ! Pin
EmersioN4-Sep-11 1:19
memberEmersioN4-Sep-11 1:19 
QuestionHow to automatically click on alert confirm box ? Pin
binhvtt13-Oct-09 18:38
memberbinhvtt13-Oct-09 18:38 
GeneralMy vote of 1 Pin
Member 132861023-Jul-09 3:39
memberMember 132861023-Jul-09 3:39 
Questionget IE menubar, toolbar click notifications Pin
sumedh.shrikrushana16-Jul-09 0:03
membersumedh.shrikrushana16-Jul-09 0:03 
AnswerRe: get IE menubar, toolbar click notifications Pin
EmersioN16-Jul-09 1:22
memberEmersioN16-Jul-09 1:22 
GeneralRe: get IE menubar, toolbar click notifications Pin
sumedh.shrikrushana16-Jul-09 18:49
membersumedh.shrikrushana16-Jul-09 18:49 
GeneralRe: get IE menubar, toolbar click notifications Pin
EmersioN16-Jul-09 20:13
memberEmersioN16-Jul-09 20:13 
Hi,

You can write Mouse hooks and on mouse events you can find active element by mouse position and you can have event details as well..
if control beneath is in tool bar then you can record it otherwise
ignore it...

this is very typical way and hooks can crash your browser
so becareful..

EmersioN

GeneralTransform mshtml.HtmlImgClass to bytes or File Or Image etc Pin
llpnet27-Jan-09 11:00
memberllpnet27-Jan-09 11:00 
AnswerRe: Transform mshtml.HtmlImgClass to bytes or File Or Image etc Pin
EmersioN27-Jan-09 17:20
memberEmersioN27-Jan-09 17:20 
QuestionHow to Access the Contents/tags of iframe in BHO Pin
harshkapoors20-Jan-09 4:36
memberharshkapoors20-Jan-09 4:36 
AnswerRe: How to Access the Contents/tags of iframe in BHO Pin
EmersioN20-Jan-09 18:20
memberEmersioN20-Jan-09 18:20 
GeneralRe: How to Access the Contents/tags of iframe in BHO Pin
harshkapoors21-Jan-09 1:56
memberharshkapoors21-Jan-09 1:56 
GeneralRe: How to Access the Contents/tags of iframe in BHO Pin
EmersioN21-Jan-09 17:14
memberEmersioN21-Jan-09 17:14 
GeneralRe: How to Access the Contents/tags of iframe in BHO Pin
harshkapoors22-Jan-09 4:26
memberharshkapoors22-Jan-09 4:26 
GeneralRe: How to Access the Contents/tags of iframe in BHO Pin
EmersioN22-Jan-09 17:34
memberEmersioN22-Jan-09 17:34 
GeneralRe: How to Access the Contents/tags of iframe in BHO Pin
harshkapoors22-Jan-09 19:57
memberharshkapoors22-Jan-09 19:57 
GeneralRe: How to Access the Contents/tags of iframe in BHO Pin
harshkapoors23-Jan-09 3:10
memberharshkapoors23-Jan-09 3:10 
AnswerRe: How to Access the Contents/tags of iframe in BHO [modified] Pin
EmersioN26-Jan-09 17:31
memberEmersioN26-Jan-09 17:31 
Generalwhere to add Pin
tsahiB17-Jan-09 5:02
membertsahiB17-Jan-09 5:02 
GeneralRe: where to add Pin
EmersioN18-Jan-09 17:20
memberEmersioN18-Jan-09 17:20 
GeneralRe: where to add Pin
tsahiB14-Feb-09 9:56
membertsahiB14-Feb-09 9:56 
GeneralRe: where to add Pin
EmersioN15-Feb-09 17:38
memberEmersioN15-Feb-09 17:38 
GeneralRe: where to add Pin
tsahiB19-Feb-09 10:00
membertsahiB19-Feb-09 10:00 
GeneralRe: where to add Pin
EmersioN19-Feb-09 18:39
memberEmersioN19-Feb-09 18:39 
GeneralExplorer.IWebBrowser_Document is null Pin
Maverickcool4-Jan-09 10:27
memberMaverickcool4-Jan-09 10:27 
GeneralRe: Explorer.IWebBrowser_Document is null Pin
EmersioN4-Jan-09 17:39
memberEmersioN4-Jan-09 17:39 
GeneralRe: Explorer.IWebBrowser_Document is null Pin
Maverickcool4-Jan-09 19:39
memberMaverickcool4-Jan-09 19:39 
GeneralRe: Explorer.IWebBrowser_Document is null Pin
EmersioN5-Jan-09 17:33
memberEmersioN5-Jan-09 17:33 
GeneralRe: Explorer.IWebBrowser_Document is null Pin
Maverickcool9-Jan-09 1:51
memberMaverickcool9-Jan-09 1:51 
GeneralRe: Explorer.IWebBrowser_Document is null Pin
EmersioN9-Jan-09 17:12
memberEmersioN9-Jan-09 17:12 
GeneralNotImplemantedException [modified] Pin
a_markun20-Dec-08 18:59
membera_markun20-Dec-08 18:59 
GeneralThanks too! Pin
Rafael Spínola24-Nov-08 2:27
memberRafael Spínola24-Nov-08 2:27 
GeneralRe: Thanks too! Pin
EmersioN24-Nov-08 17:25
memberEmersioN24-Nov-08 17:25 
GeneralThanks Pin
Salim Ansari13-Nov-08 7:47
memberSalim Ansari13-Nov-08 7:47 
GeneralRe: Thanks Pin
EmersioN13-Nov-08 17:02
memberEmersioN13-Nov-08 17:02 
Generalquestion Pin
Kishan Hathiwala26-Jul-08 3:17
memberKishan Hathiwala26-Jul-08 3:17 
AnswerRe: question Pin
Emersion27-Jul-08 19:00
memberEmersion27-Jul-08 19:00 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150728.1 | Last Updated 25 Jul 2008
Article Copyright 2008 by EmersioN
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid