FIX: Automation Object Not Being Released After Call To ComLib.release()

SYMPTOMS

When automating an Office product as an out-of-proc server, such as Excel or Word, even though the automation object has been released (through the use of ComLib.release()), its resources may not be freed and the 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:
179062 PRB: COM Objects Are Not Being Released in Java

CAUSE

The actual proxy Interface pointer is on another virtual machine 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 virtual machine (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

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

MORE INFORMATION

Steps to Reproduce Behavior

  • Compile and run the code listed below.
  • Check the running processes list in the Task Manager by pressing CTRL+ALT+DEL.
  • If the problem occurs, there will still be an Excel.exe process listed after the Java application has exited.
  • Note that 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:
    169796 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 support information about Visual J++ and the SDK for Java, visit the following Microsoft Web site:

For the lastest version of the Microsoft virtual machine, and for more information on known issues in the SDK for Java version 3.2, visit the following Microsoft Web site:. For more information on Visual J++ fixes included in Service Pack 3, please consult the Visual J++ Fixes page.

Propriétés

ID d'article : 218595 - Dernière mise à jour : 14 févr. 2017 - Révision : 1

Microsoft Visual J++ 1.1 Édition standard, Microsoft Visual J++ 6.0 Édition standard, Microsoft Software Development Kit for Java 2.02, Microsoft Software Development Kit for Java 2.02, Microsoft Software Development Kit for Java 2.01, Microsoft Software Development Kit for Java 2.02, Microsoft Software Development Kit for Java 2.02, Microsoft Software Development Kit for Java 3.0, Microsoft Software Development Kit for Java 3.0, Microsoft Software Development Kit for Java 3.0, Microsoft Software Development Kit for Java 3.0, Microsoft Software Development Kit for Java 3.1

Commentaires