Low-Level Java/COM Integration
 In this topic

*Element Types Defined in Type Libraries

*Interface Methods

*Types Supported as Parameters

*Sample Declarations

 

Java & Native Code    PreviousJava/ComNext
Low-Level Java/COM Integration     Previous Java/Com Next

 


Type Mappings Between Java and COM

The Microsoft VM for Java allows most constructs that can be specified in a type library to be accessed from Java. However, only OLE Automation-compatible types are supported fully. Most other types will require Custom Marshaling. COM constructs that cannot be described in a type library are inaccessible from Java. This limitation is in place mainly because the current type library model is not rich enough to describe all interfaces. To learn more about type libraries, see the OLE SDK.

Element Types Defined in Type Libraries

There are five basic types of elements that can be defined in Object Description Language (ODL), and thus in a type library. These elements are mapped to Java as follows:
ODL element Java element
coclass public class
interface public interface
dispinterface public interface
typedef (struct, enum or union) public final class
module public final interface with public static final members

The interface keyword in ODL is used to define custom (that is, vtable based) interfaces.

The dispinterface keyword in ODL is used to define dispatch interfaces. Properties in the dispatch interface are accessible in Java through two methods named get<property> and put<property> in the Java interface.

Interface Methods

The methods generated for an interface do not include those it inherits from IUnknown or IDispatch. Methods declared with the propget or propput attributes are exposed with get or put prepended to their names.

Types Supported as Parameters

The following table lists the ODL types that are supported and the Java type that each ODL type maps to:
ODL Type Java Type
boolean boolean
char char
double double
int int
int64 long
float float
long int
short short
unsigned char byte
BSTR class java.lang.String
CURRENCY/CY long (divide by 10,000 to get the original value as a fixed-point number)
DATE double
SCODE/HRESULT int (see also class ComException)
VARIANT class com.ms.com.Variant
IUnknown * interface com.ms.com.IUnknown
IDispatch * class java.lang.Object
SAFEARRAY(typename) class com.ms.com.SafeArray
typename * single-element array of typename on [out], error on [in]
void void

For information on accessing the contents of a VARIANT structure, see the class Variant.

IUnknown is the interface from which all COM interfaces are derived. The Java versions of COM interfaces are derived from com.ms.com.IUnknown. Because IUnknown is simply a placeholder for COM interfaces, you never have to call any methods on com.ms.com.IUnknown. If your Java program calls a COM method that takes a parameter of type com.ms.com.IUnknown, you can pass any COM interface. If your Java program calls a COM method that has com.ms.com.IUnknown as its return type, you can cast the return value to the COM interface you're expecting.

Certain ODL attributes cause parameters to be treated in a special manner:

  • VARIANT parameters marked with the optional attribute in ODL are exposed as ordinary parameters in Java. If you wish to omit a parameter when calling the method, call the noParam method on a Variant object and pass that as a placeholder.
  • A parameter marked with the retval attribute in ODL is treated as the return value in the corresponding Java method.

Sample Declarations

Following are some ODL declarations and their corresponding Java declarations:

Declarations with simple parameters:
HRESULT Func([in] int x); void Func(int x);
HRESULT Func([in,out] int* x); void Func(int[] x);
HRESULT Func([out,retval] int* x); int Func();

Declarations with string parameters:
HRESULT Func([in] BSTR x); void Func(java.lang.String x);
HRESULT Func([in,out] BSTR* x); void Func(java.lang.String[] x);
HRESULT Func([out,retval] BSTR* x); java.lang.String Func();

Declarations with VARIANT parameters:
HRESULT Func([in] VARIANT x); void Func(com.ms.com.Variant x);
HRESULT Func([in] VARIANT* x); void Func(com.ms.com.Variant x);
HRESULT Func([in,out] VARIANT* x); void Func(com.ms.com.Variant x);
HRESULT Func([out,retval] VARIANT* x); com.ms.com.Variant Func();
HRESULT Func([in,out] VARIANT** x); void Func(com.ms.com.Variant[] x);

Declarations with interface pointer parameters:
HRESULT Func([in] IBar* x); void Func(IBar x);
HRESULT Func([in,out] IBar** x); void Func(IBar[] x);
HRESULT Func([out,retval] IBar** x); IBar Func();

For more information on HRESULTs returned by COM functions, see Handling COM Errors in Java.

Top © 1998 Microsoft Corporation. All rights reserved. Terms of use.