HOWTO: Create Dual Interface COM Objects Using VJ++ 6.0

ID: Q196894

The information in this article applies to:

SUMMARY

This article provides step-by-step instructions on how to create or use a dual-interface Java COM component using Visual J++ 6.0

MORE INFORMATION

Visual J++ 6.0 does not automatically provide you with an option to create dual interfaces. To do this, you can do one of the following:

The following steps demonstrate how to use an existing type library that implements a dual interface using Visual J++ 6.0:

1. Create a directory and call it as JavaCOM.

2. Copy your IDL, which implements dual interfaces, to it.

   NOTE: You can also use Visual J++ 1.1 JAVAIDL, ATL, or MFC wizards to
   help create an IDL file. Following is an example javaLib.idl file:

      [
         uuid(D544BFC0-BC81-11d0-A982-00AA00C0177B),
         helpstring("javaLib Type Library 1.2a"),
         version(1.2)
      ]
      library javaLib
      {
        importlib("stdole32.tlb");

        [
          object,
          dual,
          uuid(D544BFC1-BC81-11d0-A982-00AA00C0177B),
          helpstring("Itest Interface")
        ]
        interface Itest : IDispatch
        {
            [id(1), helpstring("method TestLong")] HRESULT TestLong([in]
            long parm1, [out,retval] long *ret);
            [id(2), helpstring("method TestString")] HRESULT
            TestString([in] BSTR str, [out,retval] BSTR *ret);

        }

        [
          uuid(D544BFC2-BC81-11d0-A982-00AA00C0177B),
          helpstring("test Object")
        ]
        coclass test
        {
          [default] interface Itest;
        };
      };

3. To create a javaLib.tlb file, run MIDL.exe on your IDL file from the
   command line as follows:

      midl javaLib.idl

   NOTE: Visual J++ 6.0 does not install MIDL.exe directly. It ships with
   Visual C++ and is also available with the platform SDK.

4. Start Visual J++ 6.0. Under the File menu, click New Project,
   click the Existing tab, find the JavaCOM project folder, and click
   Import Project Folder.

5. The javaLib.tlb appears in your JavaCOM project (see the Project
   Explorer window). Right-click the JavaCOM project, and click JavaCOM
   Project Properties.

6. In the JavaCOM Project Properties dialog box, click the COM Classes tab,
   click Use Existing Type Library, and click Select.

7. In the COM Templates dialog box, click Browse. Go to the JavaCOM project
   folder, and open the .tlb file (created in step 3). Click OK in the
   COM Templates dialog box, and click OK in the JavaCOM Project Properties
   dialog box.

8. A folder with the same name as the IDL file you created containing all
   of the wrappers for your COM interfaces and co-classes appears in the
   JavaCOM project.

   In this example, you will see a co-class named test, and a corresponding
   test.java file, which implements all of the test interfaces. The default
   implementation of its methods will be:

      throw new com.ms.com.ComFailException(0x80004001); // E_NOTIMPL

   You need to comment out this line and write your implementation for the
   interfaces. For example:

      /** @com.register(clsid=D544BFC2-BC81-11D0-A982-00AA00C0177B,
      typelib=D544BFC0-BC81-11D0-A982-00AA00C0177B, version="1.2",
      description="test Object")*/ 
      public class test implements
      IUnknown,com.ms.com.NoAutoScripting,javalib.ItestDefault
      {
        public int TestLong(int parm1) {
        // throw new com.ms.com.ComFailException(0x80004001); // E_NOTIMPL
        return parm1;
      }

      public String TestString(String str) {
         //throw new com.ms.com.ComFailException(0x80004001);
         return str;// E_NOTIMPL
      }

9. Now package the JavaCOM project into a DLL by doing the following:

   a. In the Project Explorer, right-click the JavaCOM project and click
      JavaCOM Project Properties. Click the Output Format tab in the
      JavaCOM Project Properties dialog box.

   b. Select the Enable Packaging check box and select COM DLL as the
      Packaging type.

   c. Click OK.

10. Build the JavaCOM project to create the JavaCOM.DLL.

To test this COM Component from a Windows Foundation Classes for Java application, create a new WFC application and do the following:

1. From the Project menu, click Add COM Wrapper. In the COM Wrappers dialog

   box, click Browse, go to the JavaCOM project folder, and open the
   JavaCOM.dll file. This will create the wrapper classes for you.

2. Add a button to the form and double-click the button. This adds the
   following stub and inserts your instantiation code in the stub:

      private void button1_click(Object source, Event e)
      {
        // Javacom is the package name, and test is the co-class name.
        javacom.Itest t = (javacom.Itest) new javacom.test();
        int i = t.TestLong(100);
        String str = t.TestString("Hello");
        System.out.println(i);
        System.out.println(str);
       }

3. Run the form under the debugger.

REFERENCES

For the latest Knowledge Base articles and other support information on Visual J++ and the SDK for Java, see the following pages on the Microsoft Technical Support site:

   http://support.microsoft.com/support/visualj/ 
   http://support.microsoft.com/support/java/ 

(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Radhika Rajamani, Microsoft Corporation.

Additional query words: dual

Keywords          : kbCOMt kbVJ600 kbInetDev 
Version           : WINDOWS:6.0
Issue type        : kbhowto

Last Reviewed: December 10, 1998