Microsoft KB Archive/169764

= How To Converting Between IEnumVARIANT and java.util.Enumeration =

Article ID: 169764

Article Last Modified on 8/25/2005

-

APPLIES TO


 * Microsoft Visual J++ 1.0 Standard Edition, when used with:
 * the hardware: Intel x86
 * Microsoft Visual J++ 1.1 Standard Edition, when used with:
 * the hardware: Intel x86
 * Microsoft Java Virtual Machine

-



This article was previously published under Q169764



SUMMARY
IEnumVARIANT is the COM interfaced used by Automation Collection objects. A COM object that supports the IEnumVARIANT interface is returned by a call to the get_NewEnum method of the Automation object that implements the collection. Due to the nature of type mapping between Java and COM, controlling an IEnumVARIANT interface can sometimes be difficult.

The code in the MORE INFORMATION section can be used to wrap the Java standard java.util.Enumeration interface around an IEnumVARIANT COM interface.

The VarEnumeration class benefits any programmer dealing with Active Server Pages (ASP) cookies and forms, Microsoft Office, or any Automation objects that have collections.



MORE INFORMATION
Copy the source to a file named VarEnumeration.java and add it to your project. Include the following line to all your source files that use the VarEnumeration object: import VarEnumeration; NOTE : the file name has to have the correct capitalization. // VarEnumeration.java //   // Written by Gregory W Singleton // of Microsoft Technical Support, Developer Support // Copyright (c) 1997 Microsoft Corporation. All rights reserved. //   // This class implements Enumeration to support Conversion from // IEnumVARIANT. //   // The constructor takes a reference to an existing IEnumVARIANT.

import com.ms.com.*; import stdole2.*; import java.util.*;

public class VarEnumeration implements Enumeration {     private IEnumVARIANT enum = null; private Variant var = null;

public VarEnumeration(IEnumVARIANT iev) {        enum = iev;

// Get first Element and store it        nextElement; }

public Object nextElement {        // Temporary variable to hold outgoing VARAINT Variant retval = var;

// Create Variant to hold return from Next Variant tempVar = new Variant; tempVar.putEmpty;

// Create Array of ints to hold number of returned items int[] numItems = new int[1]; numItems[0] = 0;

// Get next Item try {           enum.Next(1, tempVar, numItems); }        catch(Exception ex) {           // enum.Next throws an exception when it cannot // return the amount of items requested numItems[0] = 0; }

// Check to see if numItems == 0 if(numItems[0] == 0) var = null; // If so, store null in var else var = tempVar; // If not, store it in var

// Return previous VARAINT return retval; }

public boolean hasMoreElements {        if(var == null) return false; else return true; }  } The class VarEnumeration has two public member functions that are used to implement the Enumeration interface. The first returns a Boolean if the collection has more elements: public boolean hasMoreElements The second returns the next Object from the collection: public Object nextElement This object will always be of type com.ms.com.Variant or null.

The following code illustrates using these functions. m_autoObject is a valid Automation object interface. The automation object exposes a method named "get_NewEnum" of type com.ms.com.IUnknown. ...  import stdole2.IEnumVARIANT; import com.ms.com.Variant; import java.util.Enumeration; import VarEnumeration; class TestEnum {     ...      public void tryTest {        VarEnumeration varEnum = new VarEnumeration((IEnumVARIANT)m_autoObject.get_NewEnum);

While(varEnum.hasMoreElements) {           Variant var = (Variant)varEnum.nextElement;

System.out.println("Variant Type: " + var.getvt); }     }      ...   } Once the above code is executed the automation objects collection is enumerated. A println call is made for each Variant returned showing its VT type.

