EZ Jcom

Java to COM/ActiveX Bridge

COM Iterator processing in EZ JCom

Some COM objects have an iterator model, where all objects of a collection are returned serially, one at a time. In Visual Basic, this is typically handled by using "For... Next".

If the COM object provides such an iterator, EZ JCom will generate a Java method named ezjcomEnumerator in the corresponding Java class. This method will take a single argument, of type ezjcom.JComIterator. This is a Java interface, with a single method named eachElement.

You must provide an implementation of ezjcom.JComIterator, and process the returned values in the eachElement method. The values returned are JComVariant objects. If you don't know the returned class, you can call the "JComVariant.getObject()" method and get its class.

The following program shows how to use this iterator with the j2c.IShellWindows class (from the tutorial.)

import j2c.tutorial.*;

 

public class Test { 

  public static void main(String args[]) 

  { 

    try {

      ShellWindows sw = new ShellWindows();

      IShellWindows isw = sw.getIShellWindows();

      isw.ezjcomEnumerator( new ezjcom.JComIterator() {

            public boolean eachElement( int index, ezjcom.JComVariant var ) {

                  System.out.println( var.getJcomObject().getClass());

                  return false;

            }

      } );

    } catch (Exception ex) { 

      ex.printStackTrace(); 

    } 

  } 

} 

Here we return “false” from the “eachElement” method, because we simply want to find out the class of the object contained in the variant.

Running this program prints the class “j2c.tutorial.IWebBrowser”, so we know that the returned elements are of class IWebBrowser and can be cast as such.  Now we can write a program to take advantage of this knowledge.

import j2c.tutorial.*;

 

public class Test { 

  public static void main(String args[]) 

  { 

    try {

      ShellWindows sw = new ShellWindows();

      IShellWindows isw = sw.getIShellWindows();

      isw.ezjcomEnumerator( new ezjcom.JComIterator() {

            public boolean eachElement( int index, ezjcom.JComVariant var ) {

                  try {

                        System.out.println(((IWebBrowser) var.getObject()).getLocationURL());

                  } catch (ezjcom.JComException ex) {ex.printStackTrace();}

                  return true;

            }

      } );

    } catch (Exception ex) { 

      ex.printStackTrace(); 

    } 

  } 

} 

This will print the URL for all browser instances (including instances of the Windows Explorer) on the system.