4

Can you use COM/OLE in a C# program to connect to a running instances of internet explorer?

Ideally I'd like to find the URLs of all webpages open in IE.

Evan
  • 4,450
  • 10
  • 40
  • 58
  • 1
    a webbrowser control won't work in this case because i don't want the program to be responsible for displaying the webpage, just to figure out what is being displayed in an open instance of ie. – Evan Jul 19 '10 at 23:30

2 Answers2

4

I found the answer here and the code excerpt is:

public class Form1 : System.Windows.Forms.Form
{
    static private SHDocVw.ShellWindows shellWindows = new
    SHDocVw.ShellWindowsClass();

    public Form1()
    {
       InitializeComponent();    
       foreach(SHDocVw.InternetExplorer ie in shellWindows)
       {
           MessageBox.Show("ie.Location:" + ie.LocationURL);
           ie.BeforeNavigate2 += new
           SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHandler(this.ie_BeforeNavigate2);
       }
}

 public void ie_BeforeNavigate2(object pDisp , ref object url, ref object Flags, ref object TargetFrameName, ref object PostData, ref object Headers, ref bool Cancel)
 {
  MessageBox.Show("event received!");
 } 
}

Anyone know if the code on that webpage would also work with IE 6? I tested it on 7. Thanks!

RameshVel
  • 64,778
  • 30
  • 169
  • 213
Evan
  • 4,450
  • 10
  • 40
  • 58
  • ShellWindows probably doesn't work for Low Rights IE windows on Vista+. – i_am_jorf Jul 20 '10 at 15:29
  • I got the error "Interop type cannot be embedded. Use the applicable interface instead", so I changed the first line from "new SHDocVw.ShellWindowsClass()" to "new SHDocVw.ShellWindows()" and was then able to cycle through the open instances of IE. – James Toomey Nov 13 '15 at 19:00
2

Manisha Mehta shows on http://www.codeproject.com/KB/cs/runninginstanceie.aspx how to do this.

Sebastian
  • 1,186
  • 9
  • 6
  • this sample finds all running instances of IE (the handles) but doesn't show how to use those handles in way that would enable COM or OLE automation so that you could find out which URLs were open. Any ideas? – Evan Jul 19 '10 at 23:28