Friday, June 17, 2011

JAVA DLL BRIDGE

Hi All,
I made a small research in the java program , on how to call windows DLL files using Java program.
Actually this is task provided for me by the project Manager. In our project, there has been a requirement that, when the client clicks on a link.It must call the scanner machine, scan the Image selected by the client and paste it in some location in the system after scanning.

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.
So, almost this has taken 3 days for me to get the solution. Might be more but have some reasons behind it. Coming directly to the solution.

Firstly, i have googled for loading the DLL files using JAVA program. They are providing the solution using the System.loadLibrary() or System.load(). But they din't worked out for me.In the middle got frustated might not be possible. Latter, thought that how come this will not come for me. After coming from office to home sat for 2 or 3 hours on the system googled it out. Came across JACOB . Thought this might work out for me for bringing the solution for my problem.
As the enviroment is not available at my home thought to implement it at the office.

Nex day went to the office, opened the same link.

The JACOB project is a generic COM bridge. It is licensed as open-source on sourceforge.net.
it is an ancronym for JAva COm Bridge. It only implements a generic bridge to call COM components from Java.
It is not the Java interface of Microsoft Office nor it supports directly its COM components
The way jacob is implemented is using JNI
and a c++ DLL designed for the windows platform. COM is a binary standard
defined by Microsoft as it hasn’t been ported over (as far as I know) to any
other platforms yet. Jacob is only a java bridge for COM which defines how
components talk to each other when they are on the same machine.

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.
Written a program to invoke the DLL file using Jacob – JAva COm Bridge.

Made a Sample Project in Eclipse having a standalone java program. This program makes use of methods available in Jacob jar files. These methods make the java program to invoke the methods available in the dll file.

Pre-requisite before running the program are:-

Put Jacob.dll in the directory that Windows searches for the libraries(in my computer – “Windows/system32”).

Put Jacob.jar in my CLASSPATH or BuildPath.

Registered the user-defined DLL or my.DLL using the .net framework – located in my system at :- “C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727” through command prompt - C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727> RegAsm/codebase "E:/JARS/ImageAssembly/ImageAssembly.dll".

If the registration has been successfully done. There will not be any errors related to the

dll file. If the library is not registered, or not registered properly, an exception will be

thrown (ComFailException: Can't get object clsid from progid) .

Now write the java program to access the methods available in dll file.

> First load library using ActiveXComponent class available in Jacob.jar as below:-
Ex: ActiveXComponent comp = new

ActiveXComponent("ImageAssembly.CallScanner");
//Where ImageAssembly - Name of the dll file
CallScanner - Class name

Then call the Dispatch.call method of the Jacob.jar file. This returns a variant.Depending

on the return type of the method you are calling in the Dispatch.call - append respective

return type to Dispatch.call().
Ex:- Dispatch.call().toString();
Dispatch.call().toInt();

Now I’m able to access the methods available in the dll using the dispatch class methods available from Jacob jar.

Thanks a lot to JACOB for providing such a simple, efficient app.

If anybody wants the link here it is:-

No comments:

Post a Comment