Microsoft SDK for Java

Q218595 FIX: Automation Object Not Being Released

The information in this article applies to:

SYMPTOMS

When automating an Office product as an out-of-proc server, such as Microsoft® Excel or Microsoft® Word, even though the automation object has been released (through the use of ComLib.release()), its resources may not be freed and the Microsoft® Office product process may not exit after the Java program has exited. For more details on ComLib.release(), see the following article in the Microsoft Knowledge Base:

Q179062 PRB: COM Objects are not Being Released in Java

CAUSE

The actual proxy Interface pointer is on another Microsoft VM thread, waiting to be released, and may not get enough CPU time to make the call.

RESOLUTION

In some cases on more recent builds of the Microsoft VM, this workaround will fix the problem. Make a call to ComLib.release as usual, but then also include a call to System.gc and put the thread to sleep long enough for the object to be released. This problem has been fixed in build 3167 of the Microsoft VM.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.

This bug was corrected in the Microsoft VM version 3167. Build 3167 is the build associated with the SDK for Java version 3.2. The Microsoft® Visual Studio® Service Pack 3 will also contain this fix in build 3176 of the Microsoft VM.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Compile and run the code listed below.

  2. Check the running processes list in the Task Manager by pressing CTRL+ALT+DEL.

  3. If the problem occurs, there will still be an Excel.exe process listed after the Java application has exited.

Note   You will either need to add a COM wrapper in Visual J++ 6.0, or run JactiveX on the Excel object library to generate the wrapper classes required to run this sample. For more information on automating Excel, see the following article in the Microsoft Knowledge Base:

Q169796 HOWTO: Automate Excel from Java

   import excel8.*;
   import com.ms.com.*;
   import com.ms.win32.*;
   public class JExcel
   {
     _Application app = null;
     _Global global = null;
     Workbooks workbooks = null;
     _Workbook workbook = null;
     Sheets sheets = null;
     _Worksheet worksheet = null;
     String name = null;
     Variant vNoParam = null;
     Range range = null;
     
     int index = 0;
     public static void main(String args[])
     {
       new JExcel().go();
     }
     public void go()
     {
       try 
       {
         global = (_Global) new Global();     
         app = (_Application)global.getApplication();
         app.setVisible(0, true);
         workbooks = app.getWorkbooks();
         
         vNoParam = new Variant();
         vNoParam.noParam();
         workbook = workbooks.Add(vNoParam, 0);
         name = workbook.getName();
         User32.MessageBox(0, "Workbook Name:  " + name,
                           "Application Message" , 
                           0);
         
         sheets = workbook.getWorksheets();
         
         index = sheets.getCount();
         User32.MessageBox(0, 
                           "Number of sheets:  " + index, 
                           "Application Message" , 
                           0);
         
         for (int i = 1; i <= index; i++)
         {
           worksheet = (_Worksheet)sheets.getItem(new Variant(i));
           worksheet.Activate(0);
           worksheet.setName("Bob's Sheet "+ i);
         }
         
         User32.MessageBox(0, 
                           "Changed sheets' names.", 
                           "Application Message" , 
                           0);
         
         range = worksheet.getColumns();
         range.setColumnWidth(new Variant(40));
         
         User32.MessageBox(0, 
                           "Set column width to 40.", 
                           "Application Message" , 
                           0);
         
         range = worksheet.getCells();
         
         range.setItem(new Variant(1), 
                       new Variant(1), 
                       new Variant("This is a result of a call to Range.setItem"));
         
         User32.MessageBox(0, 
                           "Set the value of cell 1:A.", 
                           "Application Message" , 
                           0);
         
         User32.MessageBox(0, 
                           "Closing Excel Application.", 
                           "Application Message" , 
                           0);
         app.Quit();
         ComLib.release(app);
         ComLib.release(global);
         ComLib.release(workbooks);
         ComLib.release(workbook);
         ComLib.release(sheets);
         ComLib.release(worksheet);
         ComLib.release(name);
         ComLib.release(range);
         //      System.gc();
         //      Thread.currentThread().sleep(1000);
       }
       
       catch(Throwable t)
       {
         User32.MessageBox(0, 
                           t.toString(), 
                           "Exception", 
                           0);
       }
     }
   }

REFERENCES

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

http://support.microsoft.com/support/visualj/ This link takes you to a site on microsoft.com

http://support.microsoft.com/support/java/ This link takes you to a site on microsoft.com

The latest information on updates can be found at http://www.microsoft.com/java/download.htm This link takes you to a site on microsoft.com. For more information on known issues in the SDK for Java version 4.0, please consult the SDK for Java 4.0 release notes. For more information on Visual J++ fixes included in Service Pack 3, please consult the Visual J++ Fixes page.

© 1999 Microsoft Corporation. All rights reserved. Terms of use.