Accessing an ATL DLL from Java |
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.
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.
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:
Start up Visual C++, and create a new project:
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:
Click on OK
Specify that you want to create a DLL:
Click on Finish, and then click on OK to create the new project
Create a new ATL Class using Insert|New Class:
Set the class name to Apple, specify that it is a Dual interface, and change the number of interfaces to 2:
Click on the Edit button, and change the interface names to Edible and Cookable:
Click on OK to create the new COM class.
Right-click on the ICookable COM interface in the hierarchy on the left of your screen, and select Add Method from the popup menu:
Set the method name to heat and the parameters to LONG temperature:
Click on OK to create the method in the interface.
Right-click on the IEdible COM interface in the hierarchy on the left of your screen, and select Add Method from the popup menu:
Set the method name to bite:
Click on OK to create the method in the interface.
Open up the Apple Class in the hierarchy on the left of your screen, and double-click on the heat method:
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; } |
Implement the bite method as follows:
STDMETHODIMP Apple::bite() { MessageBox(0, "The Apple has been bitten", "IEdible::bite", MB_OK); return S_OK; } |
Click on Build|Build atldll.dll to build the component:
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.
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.
Compile the example using the, javac AtlExample.java command.
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
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:
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(); } } |