Microsoft SDK for Java

Standard Java/COM Marshaling

Whenever a program spans languages, the difference between the languages' data types must be handled by marshaling. The Microsoft VM uses marshaling to wrap COM libraries as Java classes.

A COM library uses various data types as parameters and return values for its methods. For these methods to be callable from Java, the data types must be converted from COM types to Java types. For some primitive types, the Microsoft VM provides automatic marshaling and type conversion.

The Microsoft virtual machine automatically converts a Java object to a COM component complete with interfaces like IUnknown, IDispatch, IMarshal, and others used for implementing Microsoft® ActiveX® Controls and COM components. The Microsoft VM automatically handles object references, cleanup, and interface queries for any COM object used in Java, whether the component was written in Java or another COM-compliant language.

The following table lists the default COM interfaces provided by the CCW for Java objects.

Interface Description
IUnknown The Microsoft VM handles the QueryInterface and reference counts for all COM objects (Java implemented or not); there is no need to expose any IUnknown functions to Java developers.
IDispatch Provides Automation support. IDispatch assumes that the underlying object is static.
IDispatchEx Extends IDispatch; provides Automation support for dynamic languages such as scripting languages. IDispatchEx was developed to support dynamic objects.
IProvideClassInfo Provides a single method for accessing the type information for an object's coclass entry in its type library.
IProvideClassInfo2 Extends IProvideClassInfo with GetGUID, which returns the object's outgoing IID for its default event set.
ISupportErrorInfo Ensures that error information can be correctly propagated up the call chain. Automation objects that use the error handling interfaces must implement this interface.
IMarshal Specifies how a COM object can be marshaled between processes and remote computers.
IConnectionPointContainer Supports connection points for connection objects. Used to locate a specific connection point.
IExternalConnection Manages a served object's count of marshaled (external) connections. The COM object that implements such a count can detect when it has no external connections and correctly shuts itself down. This interface is used by the Microsoft VM for handling the reference counts of COM objects.

In addition to conversion, the COM types must be represented as Java types to be callable from Java. This requires a Java .class file with type declarations in Java format. This class file is usually created by running a type-conversion tool such as jactivex on the type library of the COM server. These conversion tools generate the correct type mappings for all types that are supported by the intrinsic marshaling of the Microsoft VM.

Note   Jactivex replaces jcom or javatlb in older versions of the SDK.

Java proxies are not created only when a COM object is instantiated using the new keyword. They are also created whenever a Java method is passed an object from COM and whenever Java receives an object from a COM method through a return value. In both cases, no registry entry provides the threading model of the object. The Microsoft VM must be told explicitly whether the interface is thread-safe. If the interface is not thread-safe, the Microsoft VM must marshal all calls to the thread on which the interface was received. Jactivex marks all interface parameters as requiring marshaling.

Even when the interface parameter is marked as requiring marshaling, the Microsoft VM produces a non-marshaling proxy if the COM object indicates that it is thread-safe by aggregating the free-threaded marshaler (using CoCreateFreeThreadedMarshaler).

The virtual machine creates marshaling proxies only on apartment-hostable threads. If a call is made to a Java proxy that is hosted on an apartment thread, proxies for the parameters to that call are made on that apartment thread. As a result, this requirement is automatically satisfied. If the thread is not apartment-hostable, the proxy creation throws a ClassCastException.

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