| Invoking Win32 API and Activex Components (Com dlls) from Java Part 1 |
| Java Concepts | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Written by rajesh | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Tuesday, 01 July 2008 17:33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Java is a platform independent programming language and hence does not come with built in support to directly invoke API services provided by the operating system. However, several free and commercial libraries that allow programmers to invoke Win32 API without writing any native code (The libraries will contain all the needed native code).
I searched on the internet and found a few libraries that can do this. I wanted to rate these libraries on various aspects including:
These libraries can be categorized into three categories
In this part of the series I will review some libraries that support only invoking only win32 dll functions. I decided to write a simple program to block input using the BlockInput function in user32.dll to find how easy it is to use these libraries.
J/Invoke http://www.jinvoke.com/After reviewing all available libraries for invoking Win32 API from java I felt that J/Invoke is the easiest to use. J/Invoke is simple to use, and uses Java annotations to provide information for linking and invoking DLL functions. The J/Invoke API is easy to understand, with just a handful of classes, annotations and enumerations. It does not introduce a plethora of classes corresponding to native types that make calling native code akin to programming in C using the Java language. Instead, J/Invoke maps Java types intuitively to C/native types. At the same time, J/Invoke is powerful – it has support for primitive types, arrays of primitive types, String, structures and callbacks. It works with both Ansi and Unicode character sets, and all popular calling conventions. It was pretty easy to use , It took less than 5 minutes to understand and use J/Invoke. I could open the documentation, and write this simple program to block use input in less than 5 minutes. However I could not find any way to invoke ActiveX components using J/Invoke. And from their forums I realize that invoking COM dlls is not supported and is planned for future releases. /**
* {Java By Example}
* Licensed under GPL 3.0 http://www.gnu.org/licenses/gpl-3.0.html
*/
package org.jbe.win32.jinvoke;
import com.jinvoke.JInvoke;
import com.jinvoke.NativeImport;
/**
* @author Rajesh
*
*/
public class BlockInput
{
@NativeImport(library="User32")
public static native int BlockInput(boolean block);
public static void main(String[] args)
{
JInvoke.initialize();
BlockInput(true);
try
{
Thread.sleep(10000);
}
catch(InterruptedException ie)
{
}
}
}
Usage is pretty simple, we have to declare a native method with the same signature/prototype as that of the Win32 function which we wish to call and annotate it with the @NativeImport annotation specifying the win32 dll that contains the method to be invoked. Then, we initialize the J/Invoke runtime by calling JInvoke.initialize();This statement tells the J/Invoke runtime to load the native DLL and link the native methods declared in this class with the specified exported functions in the DLL. Finally, we can invoke the native method that we have declared just like any other native method. Pros: Easy to use, good documentation, excellent support and active development Cons: No free license for open source projects, Uses annotations and needs JDK 1.5 or higher (I have seen that there are many projects that still use JDK 1.4), cannot invoke ActiveX components, no linux support.
JNative http://jnative.free.frJNative a free opensource java framework for accessing on dll windows and .so libraries on linux. Using JNative it is possible to invoke any Win32 function. You can access any function in any dll that is registered with windows. It is very simple to use, but the documentation is quite bad (almost nil in fact). However, the API’s were very simple that I just needed to look at a sample usage program to get going. Below is the code for the invoking the BlockInput function in User32.dll. /**
* {Java by Example}
* Licensed under GPL 3.0 http://www.gnu.org/licenses/gpl-3.0.html
*/
package org.jbe.win32.jnative;
import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;
/**
* @author Rajesh
*
*/
public class BlockInput
{
public static void main(String[] args) throws NativeException, IllegalAccessException
{
JNative BlockInput = new JNative("User32", "BlockInput");
BlockInput.setRetVal(Type.INT);
//passing int becaue Jnative does not have a Type for boolean
BlockInput.setParameter(0, 1);
System.out.println(BlockInput.getRetValAsInt());
BlockInput.invoke();
try
{
Thread.sleep(10000);
}
catch(InterruptedException ie)
{
}
BlockInput.dispose();
}
}
Since, there is no documentation I first struggled to pass a Boolean value to the function and the org.xvolks.jnative.Type enumeration didn’t have an entry for Boolean types. So I just tried my luck by passing int instead of Boolean; 0 for false and 1 for true. And it worked!!. Usage is pretty simple, you just have to mention the dll name and the method name to get the method proxy , then set the arguments and then invoke it by calling the invoke() method , optionally you can get the return value using getRetValAsInt() or getRetVal () methods. Finally you have to destroy the method proxy by calling dispose().If you forget to do that you will have many memory leaks in your application. Pros: Easy to use, free, small size, linux support, works with JDK 1.4 Cons: Very Poor Documentation and Support, Development seems to be inactive.
NativeCall http://johannburkard.de/software/nativecall/Nativecall is another opensource library for accessing just like JNative but without the linux support. NativeCall lets you call native methods from Java without JNI code. The current version 0.4.1 supports structs, Strings, primitive types (int and boolean), byte and char arrays and output parameters. Usage of NativeCall Api is very similar to that of JNative Api. Below is the code for the invoking the BlockInput function in User32.dll. /**
* {Java by Example}
* Licensed under GPL 3.0 http://www.gnu.org/licenses/gpl-3.0.html
*/
package org.jbe.win32.nativecall;
import java.io.IOException;
import sun.misc.ServiceConfigurationError;
import com.eaio.nativecall.IntCall;
import com.eaio.nativecall.NativeCall;
/**
* @author Rajesh
*
*/
public class BlockInput
{
public static void main(String[] args) throws SecurityException,
UnsatisfiedLinkError, UnsupportedOperationException, IOException,
ServiceConfigurationError
{
NativeCall.init();
IntCall BlockInput = new IntCall("User32", "BlockInput");
int retval = BlockInput.executeCall(Boolean.TRUE);
System.out.println(retval);
try
{
Thread.sleep(10000);
}
catch(InterruptedException ie)
{
}
BlockInput.destroy();
}
}
As, you can realize the APIs are very similar, except that you have to call NativeCall.init() to initialize the Nativecall dll, then everything is similar to Jnative get the method proxy and invoke it by calling an execute() method, which in this case returns the value returned by the native method. Finally you have to destroy the method proxy to release the resources. Pros: Easy to use, decent documentation and support, free, small size, works with JDK 1.4 SWT Win32 Extension http://feeling.sourceforge.net/index.php
It is a similar library for use with SWT. It come handy when developing eclipse plugins. It is enables you to work with native code from Java programs without using JNI. With SWT Win32 Extension, you don't need to create native libraries to call a function of the operating system API or a function from any dynamic library. You write code in the Java language only, and SWT Win32 Extension does the rest. SWT Win32 Extension provides quite a number of integration features to make your Java application look and behave like a Win32 citizen. I am not much familiar with developing eclipse plugins so, I didn’t try to use this library. I will publish the usage in the next part of this article.
Powered by !JoomlaComment 3.22
3.22 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved." |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Last Updated ( Friday, 18 July 2008 01:35 ) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||








