HOWTO: Converting Between IEnumVARIANT and java.util.Enumeration

ID: Q169764


The information in this article applies to:
  • Microsoft Visual J++, versions 1.0, 1.1
    on the following platforms: x86
  • Microsoft virtual machine


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.


REFERENCES

Win32 SDK Documentation

For the latest Knowledge Base articles and other support information on Visual J++ and the SDK for Java, see the following page on the Microsoft Technical Support site:

http://support.microsoft.com/support/visualj/

http://support.microsoft.com/support/java/
(c) Microsoft Corporation 1997, All Rights Reserved. Contributions by Gregory W. Singleton, Microsoft Corporation

Additional query words:

Keywords : kbcode kbnokeyword kbVJ
Version : WINDOWS:1.0,1.1
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: November 8, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.