views:

51

answers:

2

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.

+1  A: 

Hello guy,

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

Sebastian
thanks! i'm going to run through the sample now.
evan
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
+1  A: 

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!

evan
ShellWindows probably doesn't work for Low Rights IE windows on Vista+.
jeffamaphone