javacom4j

Why is my Com4J interface hanging during iteration?


I have to interface a third party COM API into an Java application. So I decided to use Com4j, and so far I've been satisfied, but now I've run into a problem.

After running the tlbgen I have an object called IAddressCollection which according to the original API documentation conforms to the IEnum interface definition. The object provides an iterator() function that returns a java.util.Iterator<Com4jObject>. The object comes from another object called IMessage when I want to find all the addresses for the message. So I would expect the code to work like this:

 IAddressCollection adrCol = IMessage.getAddressees();
 Iterator<Com4jObject> adrItr = adrCol.iterator();
 while(adrItr.hasNext()){
      Com4jObject adrC4j = adrItr.next();
      // normally here I would handle the queryInterface 
      // and work with the rest of the API
 }

My problem is that when I attempt the adrItr.next() nothing happens, the code stops working but hangs. No exception is thrown and I usually have to kill it through the task manager. So I'm wondering is this a problem that is common with Com4j, or am I handling this wrong, or is it possibly a problem with the API?


Solution

  • Ok, I hate answering my own question but in this case I found the problem. The issue was the underlying API. The IAddressCollection uses a 1 based indexing instead of a 0 based as I would have expected. It didn't provide this information in the API documentation. There is an item function where I can pull the object this way and so I can handle this with

     IAddressCollection adrCol = IMessage.getAddressees();
     for(int i = 1; i <= adrCol.count(); i++){
          IAddress adr = adrCol.item(i);
          // IAddress is the actual interface that I wanted and this works
     }
    

    So sorry for the annoyance on this.