?
Solved

Advice on how to obtain HTML from a specific instance of IE running on my desktop

Posted on 2009-07-03
1
Medium Priority
?
189 Views
Last Modified: 2013-12-17
I have a .NET 2.0 (C#) WinForms application running on windows desktop.  From this application, I need to access the HTML (as a string) in an instance of IE (V6/V7/V8?) also running on that desktop.  I have the window handle for the IE instance.

My question is - what is the best way to access (read-only) the HTML for the currently active page in IE (6/7/8?)?   I am aware of two methods:  

First would be to install the PIAREDIST.exe and use MSHTML.  This presents the problem of installing the PIAREDIST everywhere and i dont know if its supported in IE 7/Vista or IE 8?

Second would be to build an IE add-on.  This is more complicated for sure, but it may be more robust and better supported going forward.

Maybe there is another way?

I welcome all knowledgable advice on this.
0
Comment
Question by:RXCTO
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1500 total points
ID: 24785255
Try something like this:

Add a COM reference to 'Microsoft Internet Controls' to the project.
Add a .NET reference to Microsoft.mshtml to the project.

Example:
Dictionary<string, string> windowList = InternetExplorerWindows.GetWindows()

Key = "URL", and Value = "HTML"
// Add a COM reference to 'Microsoft Internet Controls' to the project. 
// Add a .NET reference to Microsoft.mshtml to the project. 
 
// NOTE: 
// Don't add an Imports mshtml if you want your Intellisense to work. 
 
using System;
using System.Collections.Generic;
using SHDocVw;
 
public class InternetExplorerWindows
{
 
    public static Dictionary<string, string> GetWindows()
    {
        Dictionary<string, string> list = new Dictionary<string, string>();
        foreach (InternetExplorer window in new ShellWindows())
        {
 
            // Skip Windows Explorer instances, since the Document type is IShellFolderViewDual2. 
            if (window.Document is mshtml.HTMLDocument)
            {
                list.Add(window.LocationURL, ((mshtml.HTMLDocument)window.Document).body.innerHTML);
            }
        }
        return list;
    }
 
    public static Dictionary<string, string> GetIFrameHTML(string frameID)
    {
        Dictionary<string, string> list = new Dictionary<string, string>();
        foreach (InternetExplorer window in new ShellWindows())
        {
 
            // Skip Windows Explorer instances, since the Document type is IShellFolderViewDual2. 
            if (window.Document is mshtml.HTMLDocument)
            {
                mshtml.HTMLDocument doc = (mshtml.HTMLDocument)window.Document;
                if (doc.getElementById(frameID) != null)
                {
                    mshtml.HTMLIFrame frame = (mshtml.HTMLIFrame)doc.getElementById(frameID);
                    mshtml.HTMLWindow2 window2 = (mshtml.HTMLWindow2)frame.contentWindow;
                    mshtml.IHTMLDocument2 windowDocument = (mshtml.IHTMLDocument2)window2.document;
                    list.Add(window.LocationName, windowDocument.body.outerHTML);
                }
            }
        }
        return list;
    }
 
}

Open in new window

0

Featured Post

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question