nxt's place

Search


Calendar

« November 2008
MonTueWedThuFriSatSun
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
       
Today


Tag Cloud

3com acer ajax apr awt buitenkunst c++ car charva cross debug design domain drop dwr ejb fold fuse glass google gui ie injection ipv6 jboss jna jni linux looking microsoft mime native newsgroup nntp remoting resource router rss scm software ssl subversion sun superversion svn swing travel ui web zangwerkplaats

JNA: Java Native Access

December 09, 2007 by nxt

If you use JNI to access native code you'll quickly find yourself writing some glue in c/c++ to access the native library. With JNA you can write nice clean code in Java without the need to glue your java code to the native library. This is all done by JNA.

All you need to do is define an interface that  (extends the Library interface and) exposes the methods in the library that you want to access:
import com.sun.jna.*;
// kernel32.dll uses the __stdcall calling convention
// Most C libraries will just extend com.sun.jna.Library
public interface Kernel32 extends StdCallLibrary {
// Method declarations, constant and structure definitions go here
}

Next you load the library using the following code:
Kernel32 kernel32Instance = (Kernel32)
    Native.loadLibrary("kernel32", Kernel32.class);


That's all there is to it, you can now access all methods you defined in the interface.
 Checkout the project page