Generate and Compile the Java and C++ Code

Overview

  1. Call tlb2java to generate the Java and C++ files.
  2. Create a simple Windows DLL Project
  3. Add the generated C++ files
  4. Add the necessary #include lines
  5. Add a #import line
  6. Add the JNI_OnLoad function
  7. Add a <TYPELIB>.h file
  8. Link to com_moesol_bindings.dll
  9. Compile the Java and C++ code

If you are using VC++ 6.0 sp6, we have developed an AppWizard that creates a project that already has steps 2, 4, and 6 completed. See the jSegue AppWizard.

Details

  1. We used tlb2java to generate the bindings for ComServer.tlb. ComServer.tlb was generated from ComServer.idl. You can see the entire code for the sample COM server here.

    Below is the ant target we use to run tlb2java. Note that you must rename the stdole package name to com.moesol.bindings.platform_sdk.component_services to pickup the jSegue implementations of IUnknown and IDispatch.

    <target name="tlb-tests" depends="com-server-compile"
        description="generate code for IDual which serves as a test of tlb2java">
        <mkdir dir="src/com/moesol/tests/com_server"/>
        <antcall target="a-tlb-gen">
            <param name="tlb.rename.args" value="-rename_package COMSERVERLib com.moesol.tests.com_server"/>
            <param name="tlb.libs" value="tests/lib/ComServer/ComServer.tlb"/>
        </antcall>
    </target>
    
    <target name="a-tlb-gen" depends="prepare">
        <exec executable="${tlb2java}"
              os="Windows NT,Windows 2000,Windows XP" failonerror="true">
            <arg line="-q ${tlb.rename.args}"/>
            <arg line="-rename_package stdole com.moesol.bindings.platform_sdk.component_services"/>
            <arg line="-java -hjni -jni"/>
            <arg line="-d ${src.dir}"/>
            <arg line="-check_apartment"/>
            <arg line="${tlb.libs}"/>
        </exec>
    </target>
    

    The above invocation of tlb2java generates these files:

    DTestServerEvents.cpp
    DTestServerEvents.h
    DTestServerEvents.java
    DispOnly.cpp
    DispOnly.h
    DispOnly.java
    Dual.cpp
    Dual.h
    Dual.java
    IDispOnly.cpp
    IDispOnly.h
    IDispOnly.java
    IDual.cpp
    IDual.h
    IDual.java
    IDual2.cpp
    IDual2.h
    IDual2.java
    IExtraDual.cpp
    IExtraDual.h
    IExtraDual.java
    _DispOnly.cpp
    _DispOnly.h
    _DispOnly.java
    
    For more information about calling tlb2java see its usage.

  2. Open Visual C++ and click the File|New... menu item. Select the Project Tab. Select Win32 Dynamic-Link Library.

  3. Click on the Project|Add to Project|Files... menu item. Add all of the generated C++ .h and .cpp files.

  4. tlb2java generates code that depends on a few jSegue header files. Add these to StdAfx.h

    #include <comdef.h>
    #include <jni.h>
    
    #include "com/moesol/bindings/j.h"
    #include "com/moesol/bindings/jcom.h"
    
  5. tlb2java generates C++ code that expects to call the COM interfaces as they appear from a VC++ #import.

    #import "ComServer.tlb"
    
  6. The tlb2java generated code must register the JNI methods with Java. Add the JNI_OnLoad function to the same .cpp file as DllMain appears in.

    extern "C"
    JNIEXPORT jint JNICALL
    JNI_OnLoad(JavaVM *vm, void *reserved)
    {
        ::com::moesol::bindings::java_vm::on_load(vm);
        return JNI_VERSION_1_4;
    }
    
  7. Every tlb2java generated file #include's a header files that is the same names as the Type Library that is being generated. You can use this file to add the #import line or setup missing #include's or #define's, etc. Normally, this file can be left empty, but it must exist. For our ComServer sample it is named:

    COMSERVERLib.h
    
  8. You can create a work space that includes both your new DLL and com_moesol_bindings.dsp and then make your DLL depend on com_moesol_bindings. Or you can add com_moesol_bindings.lib to your linker settings. We include a complete workspace to build the JNI code for ComServer here.

  9. Use the Java SDK and VC++ to compile the Java and C++ code. We use Ant to orchestrate our builds. The entire build.xml file can be found here.


$Id: generate.html 3769 2007-06-08 19:06:43Z hastings $