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. 🙂

7 thoughts on “Simple example of Windows native library declaration and usage in XPages

  1. You shall roast in hell for bringing the temptation of platform dependent code to the innocence of the young XPages developer!
    … but nicely done, congratulations
    🙂 stw

    P.S.: insert “tongue-in-cheek” as needed.

  2. @Stephan perhaps if IBM could see their way fit to finishing some of the higher level APIs and provide developers with, say, transaction boundaries, archiving, comprehensive rich text manipulation, and server extension management in a supported higher level slanguage such as Java, then we wouldn’t be forced to create platform dependent code in an innocent little XPage. 🙂

  3. So if I understand you correctly are you basically providing an ActiveX like capability to XPages developers? Without the security issues?????

    Is this the windows Dlls on the server or in the client?

    Can I use create excel files from the MS office installed on my corporate computers – client side?

    Can I Access other corporate standard installed tools with an open API through the DLL?

    if so then this is huge! if not then hey I can dream 🙂

  4. >> So if I understand you correctly are you basically providing an ActiveX like capability to XPages developers? Without the security issues?????
    I’m not sure, if this is really comparable to ActiceX.

    >> Is this the windows Dlls on the server or in the client?
    In this case, yes, the DLL is part of any Wondooze installation

    >> Can I use create excel files from the MS office installed on my corporate computers – client side?
    I would not use the installation on the client. I’m using org.apache.poi.* in an XAgent to create Excel files not only on the client but also on the server without having Excel installed.

    >> Can I Access other corporate standard installed tools with an open API through the DLL?
    Yes, you can. You just have to interface the methods from the DLL.
    IBM itself provides a wrapper to a bunch of C-API Calls that can be used directly in Java. Nathan Freeman has blogged about it http://ntf.gbs.com/nathan/escape.nsf/d6plinks/NTFN-83P6PZ an Karsten Lehman chipped in http://www.mindoo.com/web/blog.nsf/dx/31.03.2010220936KLERN4.htm. So if you find something that is missing in the “com.ibm.domino.napi”, you can provide the missing calls using the method, I have mentioned.
    This is basically the same, we all did back in the good ol’ LS2CAPI days.

Comments are closed.