Simple example of Windows native library declaration and usage in XPages

September 9, 2012 – 7:56 am

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. :-)

  1. 7 Responses to “Simple example of Windows native library declaration and usage in XPages”

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

    By Stephan H. Wissel on Sep 9, 2012

  3. @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. :-)

    By Nathan T. Freeman on Sep 9, 2012

  4. Interesting post, native access that works on any platform.
    could be useful if you want to connect XPages to the world around it.

    Thanx for sharing.

    By Fredrik Norling (@XPageDeveloper) on Sep 9, 2012

  5. JNA is really nice :-)

    2 years ago I wrote about how to call the Domino C-API from Java on my blog.

    You could this my example to call the Domino C-API from an XPage.

    Have a look at:

    http://nevermind.dk/nevermind/blog.nsf/subject/calling-the-domino-c-api-from-an-xpage-or-a-java-agent

    By Jesper Kiaer on Sep 9, 2012

  6. @Nathan: missed the tongue-in-cheek?

    By Stephan H. Wissel on Sep 10, 2012

  7. 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 :)

    By Mark Roden on Sep 13, 2012

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

    By Ulrich Krause on Sep 13, 2012

Sorry, comments for this entry are closed at this time.