Connecting to Running Instances of Internet Explorer

There has been always a problem connecting to web browsers,
Either you need to write Browser Helper Object or something which browser needs to instantiate.
But I found all the other methods had one or other way bug in it.
Decided to find simple and bug free way, And came across this ShellWindows interface,
which is used by Shell to keep track all the shell browsers including Shell Folders and Internet explorers
I Believe shell uses name spaces to keep track of this windows.

Not only shell windows keeps track of all shell windows and also notifies when new shell window is created or deleted.

When you enumerate, you can query whether the entry supports IWebBrowser interface,
if it does it could be either WebBrowser or ShellBrowser. Once you get hold of IWebBrowser2 interface,
you can either use it or listen to DWebBrowserEvents or through it away.

Before you start cranking in, you should include

#import "mshtml.tlb": // Internet Explorer 5
#import "shdocvw.dl"

Connecting To web browser using shell windows interface

void CIEEnumWindowsDlg::ConnectToShell()
{
CoInitialize(NULL);

if(m_spSHWinds == 0) {
//
// Get reference to ShellWindows interface
//
if(m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows))
== S_OK) {
// Event Sink
//
LPCONNECTIONPOINTCONTAINER pConnPtCont;

if ((m_spSHWinds != NULL) &&
SUCCEEDED(m_spSHWinds->QueryInterface(IID_IConnectionPointContainer,
(LPVOID*)&pConnPtCont))) {
ASSERT(pConnPtCont != NULL);
LPCONNECTIONPOINT pConnPt = NULL;
DWORD dwCookie = 0;

if (SUCCEEDED(pConnPtCont->FindConnectionPoint(
__uuidof(SHDocVw::DShellWindowsEvents), &pConnPt)))
{
ASSERT(pConnPt != NULL);
pConnPt->Advise( this->GetIDispatch(FALSE), &dwCookie);
pConnPt->Release();
}

pConnPtCont->Release();
}
}
else {
AfxMessageBox("Shell Windows interface is not avilable");
}
}
}

Downloads

Download source - 15 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read