Skip to main content
Email Password   helpLost your password?

Introduction

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

Background

I was working On BHO for IE in C#. Initially I was not knowing any thing about BHO and IE extentions. I read articles and Architecture and related information from MSDN. Also I read Architecture of IE on MSDN.

But I got the best understanding of BHO onCode Project By Pavel Zolnikov. I have listed some problem that i had faced and Solution of those problem.

Microsoft.mshtml.dll and SHDocVW.dll

I was working On BHO TOOL BAR. I was trying to access Object element (<object> Tag), Image Element(<img> Tag), Embed Element (<embed> Tag). I had initially problem that how to get this element.

I had two solutions:

  1. Use JavaScript to get the Element.
  2. Retrieve the Object Directly from Page [as like. IE Developer ToolBar ]

I was using Both of them, because in many case 2nd approach is failed to detect the Object Tag.

Injection Javascript in any 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);
}

Now there was a Problem with this method...After Implementing above way of code...it was stop the actual events of the browser. Means No events on web Browser was fired. In Other words Events of Web Browser was Locked. So it was a little and annoying problem, but I found 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);
}
}
}

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 Web Page

I was trying to access All the images from the Web Page. It can be done one of the following way.

By 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);
}

By 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 to access content from frames and Iframes. I had tried lot to access content from Frame and Iframe. But it was giving me always IFrameElement or 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 Above CodeI was able to access the frame but it was giving Access Denied Exception. I was also trying to attach event with Frame/Iframe Document but there was same problem. Finally I got the solution from HERE. I done 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 the 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{}
You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralHow to automatically click on alert confirm box ? Pin
binhvtt
19:38 13 Oct '09  
GeneralMy vote of 1 Pin
Member 1328610
4:39 23 Jul '09  
Questionget IE menubar, toolbar click notifications Pin
sumedh.shrikrushana
1:03 16 Jul '09  
AnswerRe: get IE menubar, toolbar click notifications Pin
EmersioN
2:22 16 Jul '09  
GeneralRe: get IE menubar, toolbar click notifications Pin
sumedh.shrikrushana
19:49 16 Jul '09  
GeneralRe: get IE menubar, toolbar click notifications Pin
EmersioN
21:13 16 Jul '09  
GeneralTransform mshtml.HtmlImgClass to bytes or File Or Image etc Pin
llpnet
12:00 27 Jan '09  
AnswerRe: Transform mshtml.HtmlImgClass to bytes or File Or Image etc Pin
EmersioN
18:20 27 Jan '09  
GeneralHow to Access the Contents/tags of iframe in BHO Pin
harshkapoors
5:36 20 Jan '09  
GeneralRe: How to Access the Contents/tags of iframe in BHO Pin
EmersioN
19:20 20 Jan '09  
GeneralRe: How to Access the Contents/tags of iframe in BHO Pin
harshkapoors
2:56 21 Jan '09  
GeneralRe: How to Access the Contents/tags of iframe in BHO Pin
EmersioN
18:14 21 Jan '09  
GeneralRe: How to Access the Contents/tags of iframe in BHO Pin
harshkapoors
5:26 22 Jan '09  
GeneralRe: How to Access the Contents/tags of iframe in BHO Pin
EmersioN
18:34 22 Jan '09  
GeneralRe: How to Access the Contents/tags of iframe in BHO Pin
harshkapoors
20:57 22 Jan '09  
GeneralRe: How to Access the Contents/tags of iframe in BHO Pin
harshkapoors
4:10 23 Jan '09  
AnswerRe: How to Access the Contents/tags of iframe in BHO [modified] Pin
EmersioN
18:31 26 Jan '09  
Generalwhere to add Pin
tsahiB
6:02 17 Jan '09  
GeneralRe: where to add Pin
EmersioN
18:20 18 Jan '09  
GeneralRe: where to add Pin
tsahiB
10:56 14 Feb '09  
GeneralRe: where to add Pin
EmersioN
18:38 15 Feb '09  
GeneralRe: where to add Pin
tsahiB
11:00 19 Feb '09  
GeneralRe: where to add Pin
EmersioN
19:39 19 Feb '09  
GeneralExplorer.IWebBrowser_Document is null Pin
Maverickcool
11:27 4 Jan '09  
GeneralRe: Explorer.IWebBrowser_Document is null Pin
EmersioN
18:39 4 Jan '09  


Last Updated 24 Jul 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009