Microsoft KB Archive/196894

From BetaArchive Wiki
Knowledge Base


How To Create Dual Interface COM Objects Using VJ++ 6.0

Article ID: 196894

Article Last Modified on 8/25/2005



APPLIES TO

  • Microsoft Visual J++ 6.0 Standard Edition



This article was previously published under Q196894

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:

  • You can manually create an IDL file specifying the dual interface.
  • If you have an existing type library that implements dual interface, you can select Use Existing Type Library in Visual J++ 6.0. To do this, see the following steps.

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:
    1. 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.
    2. Select the Enable Packaging check box and select COM DLL as the Packaging type.
    3. 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:

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


Additional query words: dual

Keywords: kbhowto kbinetdev kbfaq kbjavafaq KB196894