Bill Agee's blog

🤔 Reflections on test infrastructure, with a twist of user empathy.

SHDocVw.ShellWindows and IWebBrowser2 in C#, the easy way

Not too long ago I needed to write some test setup code in C# to check for open IE windows.

The idea was to kill any running IE instances before entering the main portion of the test case - and just for reference, I wanted to record the URL that each closed IE window had been viewing.

My first thought was to use this common technique:

  • Enumerate the SHDocVw.ShellWindows collection, looking for IE processes
  • Use IE's IWebBrowser2 COM interface to interact with any IE instances found

Turns out this is a common question for C# projects - and there seem to be some overly convoluted solutions floating around.

However, one solution is quite easy - the key point is that the friendly name of the SHDocVw type library is "Microsoft Internet Controls".

So, if you add a reference to the "Microsoft Internet Controls" COM component in your C# project, you'll be able to use SHDocVw.ShellWindows and SHDocVw.IWebBrowser2.

Visual Studio screenshot

This example code (for VS 2012) shows how to access the IWebBrowser2 interface of each running IE:

Back in the VS 2008 days, different code was used to accomplish the same thing:

Full disclosure - I also wrote a stackoverflow answer about this topic.

Comments