Microsoft SDK for Java

Creating a Custom Interface

In order for COM to correctly marshal and unmarshal parameters, it must know the exact method signature, including all data types, types of structure members, and sizes of any arrays in the parameter list. Java can implement custom interfaces that contain methods for passing standard Automation data types, as well as structures, custom data types, and COM objects. You implement custom interfaces in a COM object by defining the object and all of its interfaces using IDL, and then compiling it with a MIDL compiler. The Microsoft-provided MIDL compiler is Midl.exe, available in the Platform SDK on the MSDN™ Library and with Microsoft® Visual C++®.

The MIDL compiler takes the .idl file as input and generates a type library (.tlb file) and some C code that implements a proxy and a stub for the custom object.

Java classes that represent the custom interfaces and the custom object can be generated using Jactivex. Jactivex takes any standard type library file and generates corresponding .java files for the COM objects, interfaces, and structures defined in the type library. These Java files contain the correct @com directives for specifying the .java files as Java-Callable Wrappers (JCWs). Clients can use the COM object remotely through the IDispatch interface with only the type library installed on the client and server machines. In this case, no proxy/stub is required.

A simple .tlb file can be generated for Java developed COM components using javareg. In order to customize the type library, or to generate the proxy/stub code required for using custom interfaces from the Java COM object remotely, an IDL-defined interface is required. Once a type library file has been created (either through using MIDL to compile an IDL file, or from javareg) jactivex tool can be run on the type library to generate Java COM wrapper classes that represent the interfaces and the COM object. These classes can simply be imported into a Java project and used like any other Java class.

To create a COM interface

  1. Decide how you want to provide marshaling support for your interface—either with type library-driven marshaling or with a proxy/stub DLL. Even in-process interfaces must be marshaled if they are to be used across apartment boundaries. You should build marshaling support into every COM interface, even if you don't think you will need it.

  2. Describe the interface or interfaces in an interface definition (.idl) file In addition, you can specify certain local aspects of your interface in an application configuration (ACF) file. If you are using type library-driven marshaling, add a library statement that references the interfaces for which you want to generate type information.

  3. Use the MIDL compiler to generate a type library file and header file, or C-language proxy/stub files, interface identifier file, DLL data file and header file.

  4. Depending on the marshaling method you chose, write a module definition (.def) file, compile and link all the MIDL-generated files into a single proxy DLL, and register the interface in the system registry, or register the type library.

Use ComLib.ptrRelease to release a reference on the interface, given a raw COM interface pointer.

Use ComLib.ptrToUnknown to map a raw pointer to a Java-callable wrapper.

Finally, explicitly call ComLib.release to release all outstanding reference counts on the com.IStream objects before returning from the com.IMarshal methods that have an IStream parameter.

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