How to connect to a running instance of Internet Explorer

Important

The Internet Explorer 11 desktop application will be retired and go out of support on June 15, 2022 (for a list of what's in scope, see the FAQ). The same IE11 apps and sites you use today can open in Microsoft Edge with Internet Explorer mode. Learn more here.

You can use the SHDocVw.ShellWindows collection to connect to a running instance of Internet Explorer.

Original product version:   Internet Explorer
Original KB number:   176792

More information

Typically, an application connects to a running instance of another application using the Running Object table. Because Internet Explorer doesn't register itself in the running object table, another method is necessary.

The ShellWindows collection is described in the Internet Client SDK as follows:

The ShellWindows object represents a collection of the open windows that belong to the shell. In fact, this collection contains references to Internet Explorer as well as other windows belonging to the shell, such as the Windows Explorer.

The following Visual Basic code obtains a reference to the ShellWindows collection. The collection is enumerated and the LocationName for each object added to a list box. If the document associated with the object is of type HTMLDocument (a Web Page), the title for the page is added to another list box.

To run the following code, it is necessary to add a reference to Microsoft Internet Controls (Shdocvw.dll) and Microsoft HTML Object Library (Mshtml.dll) to the Visual Basic project:

Dim SWs As New SHDocVw.ShellWindows
Dim IE As SHDocVw.InternetExplorer

Private Sub Form_Load()
    Dim Doc
    List1.Clear
    List2.Clear

    Text1.Text = SWs.count

    For Each IE In SWs
        List1.AddItem IE.LocationName

        Set Doc = IE.Document
        If TypeOf Doc Is HTMLDocument Then
            'if this is an HTML page, display the title
            'may or may not be the same as LocationName
            List2.AddItem Doc.Title
        End If
    Next
End Sub

In C++, a connection can be accomplished in roughly the same way. Visual C++ Native COM Support is used here for the sake of brevity.

Add references to Shdocvw.dll and Mshtml.dll to the project:

#import <mshtml.dll> // Internet Explorer 4.0x
#import <mshtml.tlb> // Internet Explorer 5
#import <shdocvw.dll>

Declare an instance of an IShellWindows pointer in your view class:

SHDocVw::IShellWindowsPtr m_spSHWinds;

Create an instance of a ShellWindows object in your view's constructor:

m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows));

Use the ShellWindows object in your view's OnInitialUpdate function:

void CConnectIEView::OnInitialUpdate()
{
    CFormView::OnInitialUpdate();

    ASSERT(m_spSHWinds != NULL);

    CString strCount;
    long nCount = m_spSHWinds->GetCount();

    strCount.Format("%i", nCount);
    m_strWinCount = strCount;

    UpdateData(FALSE);

    IDispatchPtr spDisp;
    for (long i = 0; i < nCount; i++)
    {
        _variant_t va(i, VT_I4);
        spDisp = m_spSHWinds->Item(va);

        SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);
        if (spBrowser != NULL)
        {
            m_ctlListLoc.AddString(spBrowser->GetLocationName());

            MSHTML::IHTMLDocument2Ptr spDoc(spBrowser->GetDocument());
            if (spDoc != NULL)
            {
                m_ctlListTitle.AddString(spDoc->Gettitle());
            }
        }
    }
}

The previous method for connecting to a running instance of the Internet Explorer doesn't work if Shell Integration isn't installed or if Browse in a new process is selected in Internet Explorer.

If these factors cannot be controlled, there is still one possible method that may work. A browser helper object can be written to register Internet Explorer in the running object table (ROT). There are many implementations possible here depending on how the application is to determine the instance of Internet Explorer with which to connect. Just one possible solution: The browser helper object, having access to the object model of the instance of Explorer that launched it, would determine if this is the instance of the browser that should be registered in the running object table.

The interface that the consumer is interested in can be registered in the ROT with the RegisterActiveObject function and a dummy CLSID that the consumer will recognize. Another solution, that would allow multiple instances of the explorer to be registered in the ROT, would be to have the Browser Helper object compose an Item moniker based on a GUID and piece of data unique to each instance of Internet Explorer. The moniker would be registered in the ROT with the IRunningObjectTable::Register method. Again, the consumer would have to know how to recognize this moniker.

References

For more information about Windows Shell SDK, see Windows Shell.

For more information about provides developer specific to Internet Explorer, see Internet Explorer for Developers.