Simple example of Windows native library declaration and usage in XPages
I would like to share a simple sample on how to access (Windows) DLLs from Java / XPages.
Java’s JVM allows you to do many smart things but sometimes you may be forced to directly use external library or writing code in pure java would be very time-consuming comparing it with something more low-leveled.
So, how does it work? The only thing you have to do is to download and import JNA (Java Native Access) to your project and write a simple class.
package de.eknori.c;
import com.sun.jna.Library;
import com.sun.jna.Native;
public class Beeper {
private Kernel32 lib;
public interface Kernel32 extends Library {
// FREQUENCY is expressed in hertz and ranges from 37 to 32767
// DURATION is expressed in milliseconds
public boolean Beep(int FREQUENCY, int DURATION);
public void Sleep(int DURATION);
}
public Beeper() {
lib = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
}
public void test() {
lib.Beep(500, 500);
lib.Sleep(500);
lib.Beep(1000, 500);
}
}
From your XPage you can now make a call to the class in a button
var beep:de.eknori.c.Beeper = new de.eknori.c.Beeper();
beep.test()
For a simple beep, this solution might be a bit of an overkill; but imagine what else you can do with it. Accessing elements in an application using the C-API for example. 🙂