Microsoft KB Archive/196894

= 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:

 Create a directory and call it as JavaCOM.  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; };     };                     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. 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. The javaLib.tlb appears in your JavaCOM project (see the Project Explorer window). Right-click the JavaCOM project, and click JavaCOM Project Properties. In the JavaCOM Project Properties dialog box, click the COM Classes tab, click Use Existing Type Library, and click Select. 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.</li>  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 }                   </li> Now package the JavaCOM project into a DLL by doing the following: <ol style="list-style-type: lower-alpha;"> 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.</li> Select the Enable Packaging check box and select COM DLL as the Packaging type.</li> Click OK.</li></ol> </li> Build the JavaCOM project to create the JavaCOM.DLL.</li></ol>

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

<ol> 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.</li>  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); }                   </li> Run the form under the debugger.</li></ol>

<div class="references_section">