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

For a Microsoft Visual Basic and Microsoft Visual C++ version of this article, see 176792.


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


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.*;import;public class Class1{public static void main (String[] args){//From Q176792ShellWindows 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;;"");String s = iwb.getLocationName();if(s.equals("Page1.htm")) {iwb.setAddressBar(false);iwb.setMenuBar(false);iwb.setToolBar(0);iwb.setStatusBar(false);}}}}}
  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.


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

Artikelnummer: 299356 – Letzte Überarbeitung: 14.02.2017 – Revision: 1

Microsoft Visual J++ 6.0 Standard Edition, Microsoft Software Development Kit for Java 4.0, Microsoft Internet Explorer 4.0 128-Bit Edition, Microsoft Internet Explorer 4.01 Service Pack 2, Microsoft Internet Explorer 5.0, Microsoft Internet Explorer 5.01, Microsoft Internet Explorer 5.5