10 messages in net.java.dev.jna.usersRe: [jna-users] Using custom librarie...
FromSent OnAttachments
Jorge Nieto-MadridJan 7, 2009 4:14 am 
Timothy WallJan 7, 2009 5:38 am 
Jorge Nieto-MadridJan 7, 2009 5:55 am 
Timothy WallJan 7, 2009 7:08 am 
Jorge Nieto-MadridJan 7, 2009 8:04 am 
Timothy WallJan 7, 2009 8:09 am 
Jorge Nieto-MadridJan 7, 2009 8:24 am 
Timothy WallJan 7, 2009 12:15 pm 
Jorge Nieto-MadridJan 8, 2009 6:36 am 
Timothy WallJan 8, 2009 6:47 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] Using custom libraries (dll)Actions...
From:Timothy Wall (twal@dev.java.net)
Date:Jan 7, 2009 7:08:43 am
List:net.java.dev.jna.users

On Jan 7, 2009, at 8:56 AM, Jorge Nieto-Madrid wrote:

Thank you so much Timothy for your fast response. I used depends.exe to see the names of the functions and did just as you said. Now I found in the documentation (for Saphira). That the library defines a struct variable "sfRobot" used a s a state reflection of a robot to read its position and velocity and and integer "sfIsConnected" to determine the connection between robot and client PC. I used this in my C programs and it worked like this:

int v, w, x, y; v = sfRobot.v; w = sfRobot.w; x = sfRobot.ax; y = sfRobot.ay;

if(sfIsConnected) { //do something; }

As I said, sfRobot and sfIsConnected are variables defined by the DLL. As you know, Interfaces in Java allow to declare constants and functions only. How can I declare thiese variables in JNA? I would really appreciate your help.

NativeLibrary.getGlobalVariableAddress(String) provides you with a Pointer, which is the address of the variable.

boolean sfIsConnected() { // assumes sfIsConnected is a 32-bit value return Saphira.INSTANCE.getGlobalVariableAddress('sfIsConnected').getInt(0); }

As for the robot, you need to define the structure in java and use Structure.useMemory() with the variable's address followed by a Structure.read(). The Structure address is probably at getGlobalVariableAddress('sfRobot').getPointer(0).

PS. Is it allowed to write the full path of a dll in LoadLibrary, like: Saphira INSTANCE = (Saphira)Native.loadLibrary("C:\\Saphira\\VER62\ \BIN\\SH", Saphira.class);

You *can*, but you shouldn't.

Instead, do

java -Djna.library.path=c:/saphira/ver62/bin/sh ...

Timothy Wall schrieb:

On Jan 7, 2009, at 7:15 AM, Jorge Nieto-Madrid wrote:

Hi Timothy,

I stopped using JNA for a while. Now I started using it again and have a trivial question: How do I declare an interface for Non- native/Custom DLLs?

I know that for all DLLs in C:\windows\system32 I must just extend StdCallLibrary, like in this code snipset (for Kernel32.dll):

public interface Kernel32 extends StdCallLibrary { // Method declarations, constant and structure definitions go here Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class, W32API.DEFAULT_OPTIONS); // Optional: wraps every call to the native library in a // synchronized block, limiting native calls to one at a time Kernel32 SYNC_INSTANCE = (Kernel32) Native.synchronizedLibrary(INSTANCE); .......

So the vital part is: Native.loadLibrary("kernel32", Kernel32.class, W32API.DEFAULT_OPTIONS);

Lets assume I have the library Saphira.dll in the path C:\Spahira\

How can define interface for it??

You can use depends.exe to examine the contents of the library (function names). If you have a C header for it, that will tell you whether it uses the stdcall or c calling convention. You can probably omit the options to the loadLibrary call. You should put the dll in your PATH or in java.library.path, or in jna.library.path.

Native.loadLibrary("Saphira", Saphira.class)