Microsoft SDK for Java

Creating a Custom Class Object

This section describes how to extend the Microsoft VM's default class object (class factory) exposed for Java-implemented COM objects.

For a given Java class, <JavaClass>, that implements a COM coclass, when the Microsoft VM creates a class object for the Java/COM coclass, it will attempt to load the inner class <JavaClass>.COMClassObject. The inner class must extend com.ms.com.StdCOMClassObject and must have a default constructor.

If a valid inner class is found, a new instance of the class is created and aggregated into the Microsoft VM's class object. Autodispatch and autoserialization are turned off on the COMClassObject's COM-Callable Wrapper (CCW). More importantly, the free-threaded marshaler (FTM) is turned off, because the class object should be pinned to the current context/apartment.

Although the FTM is disabled on the COMClassObject instance, there is still only one global instance of the Microsoft VM's class object and COMClassObject instance per class ID (CLSID). This means that the COMClassObject must be thread-safe.

Although only one COMClassObject instance will be created per CLSID, an implementation should not expect that only one instance of the COMClassObject will ever be created. If multiple threads invoke the Microsoft VM's DllGetClassObject, all threads may create an instance of the COMClassObject, but only one will be returned to COM. Thus, do not treat your default constructor as a class initializer. For example, don't allocate a lot of worker threads in your default constructor. Instead, wait for the first CreateInstance call.

For aggregation to work, it is necessary to know at the time the Microsoft VM's class object is created whether or not the COMClassObject class exists. Therefore, all the class loading takes place during the Microsoft VM's DllGetClassObject call.

The COMClassObject can implement IClassFactory itself in order to implement custom activation, such as singletons or the use of pooled threads. The createInstanceImpl and createControlImpl methods of com.ms.com.StdCOMClassObject facilitate such an implementation.

The Microsoft VM's default class object will still expose the IPersistJavaTypeLib interface used by vjreg and javareg to create the class's typelib. The COMClassObject cannot override this.

See Also

%SDKDIR%\Samples\COM\COMClassObject
@com.method
@com.parameters

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