Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How To Connect to a Running Instance of Internet Explorer by Using Java

View products that this article applies to.

This article was previously published under Q299356
For a Microsoft Visual Basic and Microsoft Visual C++ version of this article, see 176792 ( ) .

↑ Back to the top


This article demonstrates how you can connect to a particular running instance of Internet Explorer by using Java code that is running on the Microsoft virtual machine (VM).

For additional information about the limitations and restrictions regarding this method, click the article number below to view the article in the Microsoft Knowledge Base:
176792� How To Connect to a Running Instance of Internet Explorer

↑ Back to the top

More information

This Java code sample uses the SHDocVw.ShellWindows object to iterate the running instances of Internet Explorer. The code obtains an IWebBrowser2 interface object for each instance of Internet Explorer and compares the LocationName property to determine if the necessary instance of Internet Explorer is present. The following code searches for a LocationName of Page1.htm.

To build this application, follow these steps:
  1. Create a Visual J++ 6.0 Console Application project.
  2. Replace the default file in the Visual J++ Console Application project with the following code:
    import shdocvw.*;
    public class Class1
    	public static void main (String[] args)
    		//From Q176792
    		ShellWindows sw = new ShellWindows();
    		int nCount = sw.getCount();
    		for(int i=0; i<nCount ; i++) { 
    			Object spDisp = sw.Item(new Variant(i));
    			// Verify that the returned object supports the IWebBrowser2 interface.
    			// This, in effect, calls a COM QueryInterface. 
    			if( spDisp instanceof IWebBrowser2 )
    				IWebBrowser2 iwb = (IWebBrowser2)spDisp;
    				//Q195175 How To  Sending Text to the Visual J++ Output window.;;"");
    				String s = iwb.getLocationName();
    				if(s.equals("Page1.htm")) {
  3. On the Project menu, click Add COM Wrapper to add a Component Object Model (COM) wrapper for the Internet Explorer shdocvw object.
  4. In the COM Wrappers dialog box, click Microsoft Internet Controls from the drop-down list box, or, alternatively, click Browse and then select Shdocvw.dll from the Microsoft Windows SYSTEM32 directory.

    This step generates a shdocvw package in your Visual J++ project that contains the Java wrapper classes for the Internet Controls COM object.
  5. Compile and build your Java application.
  6. The Java application searches for a page named Page1.htm. To create this Hypertext Markup Language (HTML) page in Visual J++, click Project on the Add Web Page menu.
  7. In the Add Item dialog box, click Web Page, and then click Page. Make sure that the name is set to Page1.htm, and then click Open.

    This creates a new Page1.htm file in your project.
  8. In a File Explorer window that is external to Visual J++, double-click Page1.htm to start Internet Explorer.
  9. After Internet Explorer is running with Page1.htm, run the Java console application. Notice that while the Java code sets properties for the instance of Internet Explorer that is running Page1.htm, the Address bar, Menu bar, Status bar, and Toolbars disappear.

↑ Back to the top


For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
195175� How To Send Text to the Visual J++ Output Window

↑ Back to the top

Keywords: KB299356, kbhowto

↑ Back to the top

Article Info
Article ID : 299356
Revision : 4
Created on : 6/29/2004
Published on : 6/29/2004
Exists online : False
Views : 434