HOWTO: Connecting to a Running Instance of Internet ExplorerID: Q176792
|
It is possible to connect to a running instance of Internet Explorer version 4.0 or later using the SHDocVw.ShellWindows collection.
Normally, an application connects to a running instance of another
application using the Running Object table. Because Internet Explorer 4.0
does not 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.
In order to run the following code, it is necessary to add a reference to
Shdocvw.dll and 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
#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());
}
}
}
}
For more information, please visit the MSDN Online Web Workshop at:
http://msdn.microsoft.com/workshop/The Internet Client SDK; search on Internet Tools and Technologies - Windows Shell API
Q179230 FILE: IEHelper-Attaching to IE4 using a Browser Helper Object
Additional query words: helper connect
Keywords : kbcode kbIE400 kbIE500 InetSDKShellObjMod
Version : WINDOWS:4.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: April 29, 1999