== Java-COM Interop ==

Interop between Java and Microsoft COM <<BR>>
Project '''com4j''' https://com4j.dev.java.net/

A Java library that allows Java applications to seemlessly interoperate with Microsoft Component Object Model.

A Java tool that imports a COM type library and generates the Java definitions of that library. 
The goal of the project is to provide a better integration of Java and COM. 

Takes advantages of J2SE 1.5 features to improve usability. 
Binds directly to the vtable interface (not IDispatch) for improved performance and broeader support for more COM interfaces.

=== Example ===

Use `%WINDIR%\system32\wshom.ocx`, which contains a type library for the Windows Scripting Host. This type library should be available in all the modern flavors of Windows.

To generate Java definitions from a type library, do as follows: 

{{{
> java -jar tlbimp.jar -o wsh -p test.wsh %WINDIR%\system32\wshom.ocx
}}}

This should generate Java definitions in the test.wsh Java package and place all the files under the wsh directory. 

First, take a look at the generated `ClassFactory` class. This class contains a series of create*** methods that are used to create new instances of COM objects. 

{{{#!java
public abstract class ClassFactory {
    public static IFileSystem3 createFileSystemObject() {
        return COM4J.createInstance( IFileSystem3.class, "{0D43FE01-F093-11CF-8940-00A0C9054228}" );
    }
    ...
}
}}}

Calling these methods causes an instanciation of a COM object, and returns a reference to its wrapper. 

tlbimp also generates one Java interface for each interface definition found in a type library. Typically they look like the following: 

{{{#!java
@IID("{2A0B9D10-4B87-11D3-A97A-00104B365C9F}")
public interface IFileSystem3 extends IFileSystem {
    @VTID(32)
    ITextStream getStandardStream(
        StandardStreamTypes standardStreamType,
        boolean unicode);

    @VTID(33)
    java.lang.String getFileVersion(
        java.lang.String fileName);
}
}}}

When you are just trying to use definitions generated from tlbimp, you can ignore all those annotations. Those are used to configure the com4j runtime to do the bridging correctly. These interfaces are implemented by the com4j COM object wrapper, and calling a method on this interface causes the runtime to call the corresponding COM method. 

Additionally, tlbimp generates enumerations when a type library defines them. 

{{{
public enum StandardStreamTypes {
    StdIn, // 0
    StdOut, // 1
    StdErr, // 2
}
}}}

 Using Generated Code::

Using the generated code is simple. The following code illustrates how you can use the `IFileSystem3.getFileVersion` method to obtain the version string of a file. 

{{{
public class Main {
  public static void main(String[] args) {
    IFileSystem3 fs = ClassFactory.createFileSystemObject();
    for( String file : args )
      System.out.println(fs.getFileVersion(file));
  }
}
}}}
