Microsoft KB Archive/299356

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

Article ID: 299356

Article Last Modified on 6/29/2004

-

APPLIES TO


 * 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
 * Microsoft Internet Explorer (Programming) 6.0

-



This article was previously published under Q299356



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



SUMMARY
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



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:  Create a Visual J++ 6.0 Console Application project.  Replace the default Class1.java file in the Visual J++ Console Application project with the following code: import shdocvw.*; import com.ms.com.Variant;

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. com.ms.debug.Debugger.out.println(iwb.getFullName); com.ms.debug.Debugger.out.println(iwb.getLocationName); com.ms.debug.Debugger.out.println(&quot;&quot;); String s = iwb.getLocationName; if(s.equals(&quot;Page1.htm&quot;)) {

iwb.setAddressBar(false); iwb.setMenuBar(false); iwb.setToolBar(0); iwb.setStatusBar(false); }           }        }    } }                     On the Project menu, click Add COM Wrapper to add a Component Object Model (COM) wrapper for the Internet Explorer shdocvw object. 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. Compile and build your Java application. 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.</li> 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.</li> In a File Explorer window that is external to Visual J++, double-click Page1.htm to start Internet Explorer.</li> 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.</li></ol>

<div class="references_section">