Skip to main content

NLink: native library linker

Posted by kohsuke on February 26, 2007 at 3:11 PM PST

For some reason, the blogsphere suddenly noticed my year-old NLink project, so I'm getting a lot of traffic lately. Maybe I've never sent out the announcement, when I originally released it, so here it goes...

Historically, JNI has been the only option to invoke native libraries. The problem with calling native libraries with JNI is that, for every method, you need to write a java method declaration, and then a bit of native code to do the parameter conversion. This makes it difficult to casually call into native libraries, because you'd have to write another native library just for JNI handling.

NLink overcomes this problem by providing a general-purpose method invocation converter driven by annotation. With NLink, calling a native library is as easy as follows, and then the NLink runtime invokes the corresponding method for you:

@DllClass
public interface User32 {
    @DllMethod
    int MessageBox(int hwnd, String text, String caption, int flags );
}

public class Test {
    public static void main(String[] args) {
        User32 user32 = NLink.create(User32.class);
        System.out.println(user32.MessageBox(0,"Hello","Testing",0x24));
    }
}

This uses the same engine and technique used in com4j.

As usual, I'm always looking for committers for the project, so if you are interested in hacking the project, let me know. Also, if you are faimilar with the calling convention in other operating systems and architectures, please join us so that we can make NLink work for platforms other than Win32.

Related Topics >>