Microsoft SDK for Java

Generating COM Interfaces from Type Libraries

Jactivex with the /javatlb command-line option generates COM interfaces from type libraries. This requires more work, but it ensures that the exposed interface exactly matches the typelib description.

This approach requires an interface definition language (.idl) file describing your interface and a coclass, which uses the [default] attribute to define the default interface. The following example shows a simple .idl file for the IWidget interface:

[
      object,
      uuid(...),
      helpstring("IWidget"),
      pointer_default(unique)
           ]
           interface IWidget : IUnknown
           {
       HRESULT add([in] long x, [in] long y, [out,retval] long*pz);
           };

   [
      uuid(...),
      helpstring("CWidget")
   ]
   coclass CWidget
   {
      [default] interface IWidget;
   };

When you invoke jactivex on the type library using /javatlb, jactivex generates an extra file named CWidgetImpl.java:

public class CWidgetImpl implements IWidgetDefault,com.ms.com.NoAutoScripting
   {
       public int add(int x, int y)
                {
          throw new com.ms.com.ComFailException(0x80004001);  //E_NOTIMPL
                }
   }

To create a Java implementation of CWidget, you can either extend the class or use CWidgetImpl as a template for MyWidget (this is preferable as it eliminates a superclass). The following code shows the superclass implementation:

public class MyWidget extends CWidgetImpl
   {
       public int add(int x, int y)
        {
           return x+y;
       }
    }

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