Click here to Skip to main content
6,595,854 members and growing! (18,618 online)
Email Password   helpLost your password?
Desktop Development » Toolbars & Docking windows » Libraries     Intermediate License: The Code Project Open License (CPOL)

Working With Microsoft.mshtml.dll and SHDocVw.dll

By EmersioN

How to work With Microsoft.mshtml.dll and SHDocVw.dll
C# (C# 1.0, C# 2.0, C# 3.0), Windows (WinXP, Vista), .NET (.NET 2.0)
Posted:24 Jul 2008
Views:28,841
Bookmarked:21 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
8 votes for this article.
Popularity: 2.09 Rating: 2.31 out of 5
4 votes, 50.0%
1
1 vote, 12.5%
2
1 vote, 12.5%
3

4
2 votes, 25.0%
5

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{}

License

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

About the Author

EmersioN


Member
M.E. Computer
B.E. Computer
Diploma Comupter
Working as Software Engineer
@SPEC-india,
Gujarat
Ahmedabad
India
Occupation: Engineer
Company: SPEC-India, www.spec-india.com
Location: India India

Other popular Toolbars & Docking windows articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 36 (Total in Forum: 36) (Refresh)FirstPrevNext
GeneralHow to automatically click on alert confirm box ? Pinmemberbinhvtt19:38 13 Oct '09  
GeneralMy vote of 1 PinmemberMember 13286104:39 23 Jul '09  
Questionget IE menubar, toolbar click notifications Pinmembersumedh.shrikrushana1:03 16 Jul '09  
AnswerRe: get IE menubar, toolbar click notifications PinmemberEmersioN2:22 16 Jul '09  
GeneralRe: get IE menubar, toolbar click notifications Pinmembersumedh.shrikrushana19:49 16 Jul '09  
GeneralRe: get IE menubar, toolbar click notifications PinmemberEmersioN21:13 16 Jul '09  
GeneralTransform mshtml.HtmlImgClass to bytes or File Or Image etc Pinmemberllpnet12:00 27 Jan '09  
AnswerRe: Transform mshtml.HtmlImgClass to bytes or File Or Image etc PinmemberEmersioN18:20 27 Jan '09  
GeneralHow to Access the Contents/tags of iframe in BHO Pinmemberharshkapoors5:36 20 Jan '09  
GeneralRe: How to Access the Contents/tags of iframe in BHO PinmemberEmersioN19:20 20 Jan '09  
GeneralRe: How to Access the Contents/tags of iframe in BHO Pinmemberharshkapoors2:56 21 Jan '09  
GeneralRe: How to Access the Contents/tags of iframe in BHO PinmemberEmersioN18:14 21 Jan '09  
GeneralRe: How to Access the Contents/tags of iframe in BHO Pinmemberharshkapoors5:26 22 Jan '09  
GeneralRe: How to Access the Contents/tags of iframe in BHO PinmemberEmersioN18:34 22 Jan '09  
GeneralRe: How to Access the Contents/tags of iframe in BHO Pinmemberharshkapoors20:57 22 Jan '09  
GeneralRe: How to Access the Contents/tags of iframe in BHO Pinmemberharshkapoors4:10 23 Jan '09  
AnswerRe: How to Access the Contents/tags of iframe in BHO [modified] PinmemberEmersioN18:31 26 Jan '09  
Generalwhere to add PinmembertsahiB6:02 17 Jan '09  
GeneralRe: where to add PinmemberEmersioN18:20 18 Jan '09  
GeneralRe: where to add PinmembertsahiB10:56 14 Feb '09  
GeneralRe: where to add PinmemberEmersioN18:38 15 Feb '09  
GeneralRe: where to add PinmembertsahiB11:00 19 Feb '09  
GeneralRe: where to add PinmemberEmersioN19:39 19 Feb '09  
GeneralExplorer.IWebBrowser_Document is null PinmemberMaverickcool11:27 4 Jan '09  
GeneralRe: Explorer.IWebBrowser_Document is null PinmemberEmersioN18:39 4 Jan '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Jul 2008
Editor: Sean Ewington
Copyright 2008 by EmersioN
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project