tags:

views:

1588

answers:

3

Hi, how do I access custom .lib / .dll functions using JNA? Can someone provide an example?

Thank you.

+3  A: 

Example (from Wikipedia):

import com.sun.jna.Library;
import com.sun.jna.Native;

/** Simple example of Windows native library declaration and usage. */
public class BeepExample {
   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 static void main(String[] args) {
    Kernel32 lib = (Kernel32) Native.loadLibrary("kernel32", 
           Kernel32.class);
    lib.Beep(698, 500);
    lib.Sleep(500);
    lib.Beep(698, 500);
   }
}

In this case, we load it from the "kernel32.dll" library. I hope this makes JNA clearer.

EDIT: I'll explain the code: You need to define an interface(that extends com.sun.jna.Library) with the functions you need from the library. Then, call com.sun.jna.Native.loadLibrary("LibraryName", InterfaceName.class). Finally, store the output in a variable with the type of the interface. Just call the functions from that variable.

luiscubal
A: 

He asked how to link a custim dll .. as far as I know kerne32 isn't custom. unless you wrote a new kernel :)

Replace the "kernel32" library name parameter of LoadLibrary with "something_useful_lib" and it works. (If the library is present and the method signatures are translated correctly).
mjustin
A: 

Just use it like you would use any other standard library.

Hannes de Jager