FIX: java.lang.ClassCastException on Variant Objects

Article translations Article translations
Article ID: 260280 - View products that this article applies to.
This article was previously published under Q260280
Expand all | Collapse all

On This Page


A java.lang.ClassCastException is generated when you use a COM object obtained from a or Variant.toDispatch(). Specifically, the Variant, generated through Variant.clone() or Variant.cloneIndirect(), contains a reference to a COM proxy (COM interface pointer marshaled to another thread).


The Microsoft virtual machine's implementation of Variant.clone() and Variant.cloneIndirect() invokes the Win32 VariantCopy and VariantCopyInd functions, respectively, on the caller's thread instead of the Variant's home thread. If the underlying object stored in the Variant is a COM proxy, an attempt to perform a QueryInterface to the COM interface causes a COM error, which is represented as a java.lang.ClassCastException in Java.

An example that demonstrates this problem is included in the "More Information" section.


A workaround for this problem is to use ComLib.executeOnContext() to call Variant.clone() or Variant.cloneIndirect() on the Variant's home thread by passing the target Variant as the first parameter to ComLib.executeOnContext().


Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

This problem was corrected in Windows 2000 Service Pack 1.


Steps to Reproduce Behavior

This code causes the java.lang.ClassCastException that is described in the "Symptoms" section:

public class Class1 implements NoAutoMarshaling { 
  static Object m_obj;
  static Variant m_variant;
  static Variant m_cloned_variant;
  public static void main (String[] args) { 
    // COM hostable thread
    // force creation of Java/COM wrapper for instance
    // of Class1
    m_obj = ComLib.makeProxyRef(new Class1());
    // invoke on new thread
    Thread t1 = new Thread(new Thread1());
    // pump messages for COM
  public static class Thread1 implements Runnable { 
    // marshals m_obj to this thread, and stores instance
    // in m_variant
    public void run() { 
      // COM hostable thread
      // Force virtual machine to marshal m_obj
      // to this thread
      int nPtr = ComLib.unknownToPtr(m_obj, 
      Object objProxy = 
      // Store proxy to m_obj in a variant.
      // Note that COM proxy (objProxy) is only valid 
      // on this thread:
      // --performing any COM operation on this proxy 
      // will cause a COM error
      m_variant = new Variant(Variant.VariantObject, 
      // Invoke on a new thread
      Thread t2 = new Thread(new Thread2());

  public static class Thread2 implements Runnable { 
    // Calls Variant.clone() on m_variant and performs COM
    // QueryInterface on internal COM object
    public void run() { 
      // COM hostable thread
      try {
        // Clone m_variant
        m_cloned_variant = 
        System.out.println("after clone");
        // Get COM proxy to m_obj from cloned 
        // variant
        Object obj = m_cloned_variant.getObject();
        System.out.println("after getObject");
        // Try to perform COM QueryInterface on 
        // COM proxy. This fails with a 
        // ClassCastException because the COM
        // proxy is being invoked on the wrong 
        // thread
        boolean b = ComLib.supportsInterface(obj, 
        System.out.println("supportsInterface "+b);
      catch (Exception e) {
      try {
        System.out.print("Please press enter "+
                         "to exit..");;
      catch (Exception e) {
  // standard Win32 message pump
  public static void PumpMessages() { msg = new;

    while( msg, 0, 0, 0 ) ) { msg ); msg );


For additional information about the latest service pack for Windows 2000, click the article number below to view the article in the Microsoft Knowledge Base:
260910 How to Obtain the Latest Windows 2000 Service Pack
For support information about Visual J++ and the SDK for Java, visit the following Microsoft Web site:


Article ID: 260280 - Last Review: June 14, 2006 - Revision: 2.0
  • Microsoft Java Virtual Machine
kbbug kbfix kbjava kbjavavm33xxfix kbwin2000sp1fix KB260280
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

Give Feedback


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from