Low-Level Java/COM Integration
 In this topic

*Object Allocation

*Object Release

*Changing Interfaces

 

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

 


COM Programming in Java and C++ Compared

Users who are familiar with COM programming in C++ might find it helpful to learn how C++ COM programming compares to Java COM programming. Since Java's notion of multiple interfaces on an object and its garbage collection facility map readily to COM's paradigm for managing objects, many aspects of COM programming are simpler in Java than in C++.

Object Allocation

In C++, you allocate a new COM object using the following syntax:

IDrawable pDrawable;
CoCreateInstance(CLSID_MyCircle, NULL, CLSCTX_SERVER,
                 IID_IDrawable, (void**)&pDrawable );

In Java, the equivalent code would look like this:

IDrawable drawable = (IDrawable)new MyCircle();

The preceding Java statement is identical to Java's syntax for allocating ordinary Java objects. Behind the scenes, the COM API function CoCreateInstance is called instead of allocating space in the runtime heap. However, this call is invisible to the Java programmer.

Object Release

In C++, you release a COM object using the following syntax.

pDrawable->Release();

In Java, the equivalent code would look like this.

import com.ms.com.*;
ComLib.release(drawable);

Calling the release method causes the drawable object to release all of its references to the COM object that it wraps. Because drawable may have cached one or more of the object's interfaces, this call may result in more than one release.

Note If you do not call ComLib.release, normal garbage-collection will attempt to perform the release for you. However, this mechanism is not guaranteed due to the threading limitations of many COM objects. That is, many COM objects can only be called on the thread on which they were created. Because garbage collection occurs at unpredictable times, the required thread may have expired or may be no longer responding to messages by the time garbage-collection reclaims the object. In addition, this unpredictability can obscure true memory leaks and/or tie up important system resources. For these reasons, it is recommended that you use explicit releases in order to free COM objects in a timely and predictable fashion.

Changing Interfaces

COM objects must implement the IUnknown::QueryInterface function to allow clients to access the object's supported interfaces. In Java, the details are handled by the VM, so that in your source code, you need only do a typecast.

For example, in C++ you might write code like this to access the IPrintable interface of the object that pDrawable points to:


// pDrawable points to an object that supports 
// both IDrawable and IPrintable
IPrintable *pPrintable;
pDrawable->QueryInterface( IID_IPrintable, 
                           (void **)&pPrintable );

In Java, the equivalent code would look like this:


// drawable refers to an object that supports
// both IDrawable and IPrintable
IPrintable printable;
printable = (IPrintable)drawable;

Notice that you use the same syntax that you would normally use to cast between Java object types. If the object does not implement the requested interface, a ClassCastException is thrown when you attempt the cast. If you want, you can use the instanceof operator to determine before the cast is attempted whether the object implements the requested interface.

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