Accessing an ATL DLL from Java

Accessing an ATL DLL from Java

Java/J2EE COM Interoperability Products Page

This example demonstrates how to access an Microsoft ATL DLL from Java "in process". J-Integra® for COM is a Java interoperability component that bridges Java and Microsoft Active Template Library (ATL) applications. It provides bi-directional access of Java objects and COM components.

Contents

  1. Introduction
  2. Configure your environment
  3. Create the Visual C++ COM Component
  4. Run the Java Client on Local Windows Machine
    1. Generate Java Proxies
    2. Create the Example
    3. Compile and Run the example
  5. Run the Java Client on Remote Machine, e.g. Windows, UNIX, Linux and etc

1 Introduction

This example demonstrates how you can access COM components that are hosted in a Dynamic Link Library (DLL) from pure Java software, using J-Integra®. We also show in this example that J-Integra® handles COM Classes that implement multiple COM interfaces. You can run the Java client on a Windows machine to access its local ATL DLL, or run the Java client on a non-Windows machine (such as Linux) to access ATL DLL installed on a remote Windows machine.

This example will require access to a machine that is running Windows, and has Visual C++ installed on it. This example assumes you are using Microsoft Visual Studio 6.0.

2 Configure your environment

If you do wish to try out this example, you should first install the Microsoft Visual C++, download and install the Java Developers Kit. You should also download and install the J-Integra® for COM.

We will be performing this example under D:\pure. Create that directory (if you have not already created it for the Excel example). Set your path environment variable to include the JDK and J-Integra® bin directories, and update your CLASSPATH environment to include the J-Integra® runtime:

Accessing an ATL DLL from Java: Set environment variables

3 Create the Visual C++ COM Component

  1. Start up Visual C++, and create a new project:

    Accessing an ATL DLL from Java: Create new project

  2. Create a project of type ATL COM AppWizard, set the Location to D:\pure, and then set the Project name to atldll -- the Location will be automatically updated to include the project name:

    Accessing an ATL DLL from Java: Edit project details

    Click on OK

  3. Specify that you want to create a DLL:

    Accessing an ATL DLL from Java: Specify DLL

    Click on Finish, and then click on OK to create the new project

  4. Create a new ATL Class using Insert|New Class:

    Accessing an ATL DLL from Java: Create new class

  5. Set the class name to Apple, specify that it is a Dual interface, and change the number of interfaces to 2:

    Accessing an ATL DLL from Java: Edit class details

  6. Click on the Edit button, and change the interface names to Edible and Cookable:

    Accessing an ATL DLL from Java: Change interface names

    Click on OK to create the new COM class.

  7. Right-click on the ICookable COM interface in the hierarchy on the left of your screen, and select Add Method from the popup menu:

    Accessing an ATL DLL from Java: Add new method

  8. Set the method name to heat and the parameters to LONG temperature:

    Accessing an ATL DLL from Java: Edit method details

    Click on OK to create the method in the interface.

  9. Right-click on the IEdible COM interface in the hierarchy on the left of your screen, and select Add Method from the popup menu:

    Accessing an ATL DLL from Java: Add new method

  10. Set the method name to bite:

    Accessing an ATL DLL from Java: Edit method details

    Click on OK to create the method in the interface.

  11. Open up the Apple Class in the hierarchy on the left of your screen, and double-click on the heat method:

    Accessing an ATL DLL from Java: Edit heat method

  12. Implement the heat method as follows:

    	STDMETHODIMP Apple::heat(long temperature)
    	{
    		MessageBox(0, "The Apple has been heated", "ICookable::heat", MB_OK);
    		
    		return S_OK;
    	}
  13. Implement the bite method as follows:

    	STDMETHODIMP Apple::bite()
    	{
    		MessageBox(0, "The Apple has been bitten", "IEdible::bite", 
    	 MB_OK);
    		return S_OK;
    	}
  14. Click on Build|Build atldll.dll to build the component:

    Accessing an ATL DLL from Java: Build the component

4 Run the Java Client on Local Windows Machine

You can try this example on local Windows machine first to get a feel for how easy it is to use J-Integra® to access ATL DLL from Java. Once you make it working on local machine, you can then try to run the Java client on a non-Windows machine to remotely access ATL DLL on another Windows machine.

4.1 Generate the Java proxies

In order to produce Java proxies for the ATL DLL you just created, launch com2java and take the following steps:
  1. Select D:\pure\atldll\atldll.tlb as the type library to open -- this file was automatically created by Visual C++ when you built your project.
  2. Enter 'D:\pure' into the Output Dir field.
  3. Enter 'atldll' into the Java Package field.
  4. Click the Generate Proxies; com2java will generate Java files corresponding to the two COM interfaces (ICookable and IEdible), and the COM class (Apple):

Accessing an ATL DLL from Java: Generate proxies

4.2 Create the example

  1. Create the file d:\pure\AtlExample.java, by cutting and pasting from your Web Browser.

    Here is the file:

    import com.linar.jintegra.AuthInfo;
    import atldll.Apple;
    public class AtlExample {  
      public static void main(java.lang.String[] args) throws Exception {    
        // com.linar.jintegra.AuthInfo.setDefault("NT DOMAIN", "USER", "PASSWORD");    
        String host = "localhost";    
        Apple apple = new Apple(host);
        apple.heat(451);    
        apple.bite();    
        apple.release();  
      }
    }

    The example simply creates an instance of the Apple class, and then invokes the two methods from the two interfaces.

  2. Compile the example using the, javac AtlExample.java command.

4.3 Compile and run the example

On the Java client machine, make sure your CLASSPATH and PATH environment variables are set up according to J-Integra® installation instructions. Compile and run the example in  J-Integra®'s native mode (you need to use DCOM mode if remotely accessing ATL DLL):

javac AtlExample.java
java -DJINTEGRA_NATIVE_MODE AtlExample

Accessing an ATL DLL from Java: Display output 1 Accessing an ATL DLL from Java: Display output 2

5 Run the Java Client on Remote Machine, e.g. Windows, UNIX, Linux and etc

Of course you should be able run your java program from any machine that supports a standard JVM. If you were to run the example on your Linux machine, for example, and your COM component is on an NT box with the TCP/IP name of host.domain.com, then do the following:

  1. Copy the com2java tool to the Windows machine to generate the Java proxies of ATL DLL, and then move the Java proxies from the Windows machine to the Linux machine.
  2. Install J-Integra® (the jintegra.jar file) on the Linux machine and include the jintegra.jar file and generated Java proxies in CLASSPATH.
  3. Please read Configuring DCOM for Remote Access for details on configuring DCOM. You may need to modify the properties of your COM component, which will be listed as Apple Class for this example.

    Accessing an ATL DLL from Java: DCOMCNFG window

  4. Specify the host as an additional parameter when creating the Apple. Also, uncomment the AuthInfo.setDefault() line, and specify an appropriate NT Domain, User and Password. Refer to Configuring DCOM for Remote Access for more information about AuthInfo.setDefault.

    import com.linar.jintegra.AuthInfo;
    import atldll.Apple;
    public class AtlExample {
      public static void main(java.lang.String[] args) throws Exception {
        com.linar.jintegra.AuthInfo.setDefault("NTDOMAIN", "NTUSER", "NTPASSWORD");
        String host = "host.domain.com";
        Apple apple = new Apple(host);
        apple.heat(451);
        apple.bite();
        apple.release();
      }
    }

  5. Move AtlExample.java to Linux machine. Compile and run it in DCOM mode without using DJINTEGRA_NATIVE_MODE property:
    java VcExample