Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 307,003 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,966 people online right now. Registration is fast and FREE... Join Now!




Using dll Library in java application using jacob

 
Reply to this topicStart new topic

> Using dll Library in java application using jacob, illuststrate using dll libraries in java application

knowledgestudent
Group Icon



post 10 Jul, 2009 - 12:58 PM
Post #1


Hi all, I wanted to share you what I have learned about using dll libraries in java application using Jacob,

Introduction:
In this article I am going to demonstrate shortly, with a simple example, how to use dll libraries inside java applications, in order to test the sample program in this article you need to have at least jdk 5, the Jacob package API, also created dll file with complete knowledge of the structure of the library and its functions.
Attached is the sample code zipped with the Jacob jar file, the Com.dll file and the ReadDLL.java file that uses the Jacob.

“Dynamic link library” is Microsoft’s implementation of the shared library concepts in the Microsoft windows operating system. Dlls considered as a native application, not like any applications.
Using the dll libraries within the .Net framework is such an easy operation, just we need to load the library within the environment, initiate an object from it, then use it like any other object in the program.

Such an immediate operation does not exist for the java environment, so that there are some interfaces and frameworks has been built that allow java programmers to interact with dll libraries and use their functionality within java applications, such as the Jacob, “JACOB is a JAva-COm Bridge that allows you to call COM automation components from Java. It uses JNI internally to make native calls into COM and Win32 libraries. In simple words, you can now call any of the .dll file functions from Java and use the result in your Java program (provided you know which function to call)”, JNI is the Java Native Interface.

To start we need to:
• Load the jacob.dll file into the system path, described later,
• Register your dll file as Com, described later,
• Set the jacob jar file, within the class path of the project,
• Then start loading the Lib in the program, and using it.

Here in this article I am using a Com.dll that I have created using the Visual Studio .Net 2005 /VB, by the help of this article.
Creating Com dll using vs.2005
This Library is used simply to sum, subtract, multiply, and divide two integers passed to its methods, the first three return integers, and the last one return double.

Load the Jacob.dll file:
Just paste the Jacob.dll to the system path in your environment; in mine I have put it into the WINDOWS\System32 folder, some environments require a registration for the Jacob.dll using regsvr32 command.

Register the dll file “the Library”
To register the dll file, we use the “[font=Lucida Sans Unicode]regasm /codebase” command from the Microsoft.Net either from the windows command prompt, or from the .Net command prompt itself as follows:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
The “v2.0.50727” may change from environment to another; you better search for the command to know the right path of it. In other words find the correct path of the regam.exe. you should expect to receive confirmation message that the file has been registered :
Microsoft ® .NET Framework Assembly Registration Utility 2.0.50727.42
Copyright © Microsoft Corporation 1998-2004. All rights reserved.
Types registered successfully

Otherwise there must be something wrong with the building of the dll file, or some files are missing in the path of your system, [in case of some error, just google it, so that we are repeating].
To know more about the regasm command refer to: Assemply Registeration Tool

After registration, you are supposed to find the library loaded to the registry, you can check it to see the loaded Strong Library name, in our example it is: Com.Calculation
You can check the registry by the command: regedit from the Run.

Inside the java application:
To go with it, we need to have full description about the dll library we are using, in our example here; we have the description of the Com.Calculation Library as follows:
sum(int x,int y)
Div(int x, int y) // check if y = 0, returned as double
multi(int x, int y)
subtract(int x, int y)

Add the Jacob.jar file to your class path; the following is the code segments of needed operations for using the Library:


First load the library:
CODE
//Loading the library:
        ActiveXComponent comp=new ActiveXComponent("Com.Calculation");        
        System.out.println("The Library been loaded, and an activeX component been created");

this may throw an exception (ComFailException: Can't get object clsid from progid) if the library is not registered, or not registered properly.

Then call the functions of the library:
CODE
int summation=Dispatch.call(comp, "sum",arg1,arg2).toInt();
         System.out.println("Summation= "+ summation);

        int subtraction= Dispatch.call(comp,"subtract",arg1,arg2).toInt();
        System.out.println("Subtraction= "+ subtraction);
        
        int multiplication=Dispatch.call(comp,"multi",arg1,arg2).toInt();
        System.out.println("Multiplication= "+ multiplication);
        
        double division=Dispatch.call(comp,"div",arg1,arg2).toDouble();
        System.out.println("Division= "+ division);


using the Dispatch class that is from the jacb package, where the first argument is the component that we need to use its functions, the second is the function name as string, and then as many parameters you need to pass to the function, there are other variations. This may through an exception (ComFailException: Can't map name to dispid).

If some of the library functions (methods) return some kind of struct or a class of some type, then its properties could be accessed using the get method of the Dispatch Class:
CODE
Dispatch. get(object, “Property Name”);

it returns value with the data type specified.
Again you need to know the specification of the library.

The full ReadDLL.java follows:
CODE
package jacobtest;

import com.jacob.activeX.*;
import com.jacob.com.*;
/**
* This class uses the the Jacob tech
* to use and interact with a Com cmponent
* in a java application
*/
public class ReadDLL {  
    
    public static void main(String[] args){
        
        //Loading the library:
        ActiveXComponent comp=new ActiveXComponent("Com.Calculation");        
        System.out.println("The Library been loaded, and an activeX component been created");
        
        int arg1=100;
        int arg2=50;
        //using the functions from the library:        
        int summation=Dispatch.call(comp, "sum",arg1,arg2).toInt();
        System.out.println("Summation= "+ summation);
        
        int subtraction= Dispatch.call(comp,"subtract",arg1,arg2).toInt();
        System.out.println("Subtraction= "+ subtraction);
        
        int multiplication=Dispatch.call(comp,"multi",arg1,arg2).toInt();
        System.out.println("Multiplication= "+ multiplication);
        
        double division=Dispatch.call(comp,"div",arg1,arg2).toDouble();
        System.out.println("Division= "+ division);
        
        /**The following code is abstract of using the get,
         * when the library contains a function that return
         * some kind of a struct, and then get its properties and values
         **/
//        Dispatch disp=Dispatch.call(comp,"sum",100,10).toDispatch();
//        DataType Var=Dispatch.get(disp,"Property Name");
    }
}


The output of this simple program:
The Library been loaded, and an activeX component been created
Summation= 150
Subtraction= 50
Multiplication= 5000
Division= 2.0


More details about Jacob solution:

Hope that this article helps to have a basic idea about using the jacob and save time for some people,
please if you have any question, feel free to contact me and ask!

Good Luck everybodyAttached File  Jacob_Test.zip ( 58.13k ) Number of downloads: 172
smile.gif
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

born2c0de
Group Icon



post 12 Jul, 2009 - 08:47 AM
Post #2
Using the Jacob library seriously limits your code's portability but if your target platform is windows and you're hell bent on using Java, it's a good option.

Good Job. icon_up.gif
Go to the top of the page
+Quote Post

sharpener104
*



post 19 Oct, 2009 - 05:20 AM
Post #3
Hi, your article is very helpful indeed. I wonder if you could give me some light on how to use the system application component COMSVCS.TrackerServer with Jacob. I've been trying to create an instance of that component but I haven't been successful. The simple code " ActiveXComponent a = new ActiveXComponent("COMSVCS.TrackerServer"); " returns the following error:

Exception in thread "main" com.jacob.com.ComFailException: Can't QI object for IDispatch
at com.jacob.com.Dispatch.createInstanceNative(Native Method)
at com.jacob.com.Dispatch.<init>(Dispatch.java:101)
at com.jacob.activeX.ActiveXComponent.<init>(ActiveXComponent.java:58)
at app.Teste.main(Teste.java:20)

I'm new to Jacob and COM+ and I haven't a clue why this error occurs. Thanks in advance.
Go to the top of the page
+Quote Post

knowledgestudent
Group Icon



post 19 Oct, 2009 - 11:43 AM
Post #4
QUOTE(sharpener104 @ 19 Oct, 2009 - 05:20 AM) *

Hi, your article is very helpful indeed. I wonder if you could give me some light on how to use the system application component COMSVCS.TrackerServer with Jacob. I've been trying to create an instance of that component but I haven't been successful. The simple code " ActiveXComponent a = new ActiveXComponent("COMSVCS.TrackerServer"); " returns the following error:

Exception in thread "main" com.jacob.com.ComFailException: Can't QI object for IDispatch
at com.jacob.com.Dispatch.createInstanceNative(Native Method)
at com.jacob.com.Dispatch.<init>(Dispatch.java:101)
at com.jacob.activeX.ActiveXComponent.<init>(ActiveXComponent.java:58)
at app.Teste.main(Teste.java:20)

I'm new to Jacob and COM+ and I haven't a clue why this error occurs. Thanks in advance.


Hi sharpener ,
I actually have not seen this exception before, but as I googled it, you need to know the exact functions of the "COMSVCS.TrackerServer' COM ,
but in all cases I can assume that it can not create an instance from the object type, so that you need to understand what this system application component do, then if you knew and got no specific issue you need to make sure that you have done all the process to get the jacob works,
in addition have tried some different COM, to test that there is no any problem with the system !!!
CODE
at com.jacob.com.Dispatch.createInstanceNative(Native Method)
        at com.jacob.com.Dispatch.<init>(Dispatch.java:101)

as I can read from the exception that it can not create an instance and this could be from several reasons,

hope that help u even with thoughts , Good Luck with jacob,
jacob is noisy at the beginning but when u got it , it makes world easier smile.gif
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 06:44AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month