Environment: Internet Explorer 4 (and above)
Connecting to a running instance of the browser from your application has always been problematic. You need to either write a "Browser Helper Object" or something which the browser needs to instantiate. However, all of the methods I've personally been able to find are either over complex or buggy. Therefore, I decided to find a simpler (and bug free) way.
After a little research, I came across a shell interface that would do the trick. It turns out that this is how the shell keeps track of all the shell browsers (both Windows Explorer and Internet Explorer instances!).
Not only does the shell keep track of all Explorer instances, it also notifies when a new Explorer instance is created or deleted.
When you enumerate these Explorer instances, you can query whether as to whether the entry supports the IWebBrowser interface. If it does, it could either be a WebBrowser or a ShellBrowser. Once you obtain the IWebBrowser2 interface, you can either use that interface to manipulate (and communicate with) the Explorer instance or you can trap its events using the DWebBrowserEvents.
Before you get started, you'll need to include the following two lines somewhere in your project:
#import "mshtml.tlb" <span class="codeComment">// Internet Explorer 5</span>
Now, simply use the following code to connect to a Web browser instance
if(m_spSHWinds == 0)
<span class="codeComment">// Get reference to ShellWindows interface</span>
if(m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) == S_OK)
<span class="codeComment">// Event Sink </span>
if ((m_spSHWinds != NULL) &&
ASSERT(pConnPtCont != NULL);
LPCONNECTIONPOINT pConnPt = NULL;
DWORD dwCookie = 0;
ASSERT(pConnPt != NULL);
pConnPt->Advise( this->GetIDispatch(FALSE), &dwCookie);
AfxMessageBox("Shell Windows interface is not avilable");