2 messages in net.java.dev.jna.usersRe: [jna-users] Works in Linux but no...
FromSent OnAttachments
Timothy WallSep 10, 2007 2:33 pm 
Gregor B. RosenauerSep 11, 2007 2:42 pm 
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] Works in Linux but not in WindowsActions...
From:Timothy Wall (twal@dev.java.net)
Date:Sep 10, 2007 2:33:19 pm
List:net.java.dev.jna.users

On Sep 10, 2007, at 5:21 PM, Gregor B. Rosenauer wrote:

I have the sadly-not-so-uncommon problem that I can successfully use JNA under Linux with a 3rd-party closed-source commercial C library (.so) but not in Windows as a .dll

I always get the infamous java.lang.UnsatisfiedLinkError: Cannot locate function 'apiInitLibrary' at com.sun.jna.NativeLibrary.getFunctionAddress(NativeLibrary.java: 191) at com.sun.jna.Function.<init>(Function.java:103) at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:178) at com.sun.jna.Library$Handler.invoke(Library.java:140) at $Proxy0.apiInitLibrary(Unknown Source)

The lib path is OK, library loads, I even debugged into the JNA- sources - here, function lookup does indeed fail because the function is never added to the lookup-table. Whenever the instruction pointer is supposed to move past the line where the function is added to the Hashmap, it jumps right to the end of the method (?!). However the libary and method name are passed in successfully, as I could verify in Eclipse's debug view.

The header of the library seems to indicate that this is a StdCallLibrary, but all combinations and tips from the list, also the examples did not help. I tried Library, StdCallLibrary, and all combinations of Method- Mappings, a static INSTANCE inside the interface and a local library-handle.

If it *is* a stdcall library, then its symbols will likely be in the form "func_name@NN", where "NN" is the size of its stack arguments. You can double-check by using "nm" or similar tool to dump the library's exported symbols (I don't recall the w32 equivalent offhand, but "nm" is available under cygwin).

Use the StdCallFunctionMapper as a library option to get the proper translation from the Java to the native name.

See W32StdCallTest for an example.

From the header (abbreviated): #if defined(_WIN32) #def... API_WIN32 #ifndef API_DLL #def... API_DLL _declspec(dllexport) #def... API_CALL _stdcall #endif

Here is a function I want to call: (void) init (char* x, char* y, char* z);

On the Java-side, I do: MyLib INSTANCE = (MyLib) Native.loadLibrary("theapi", MyLib.class); myLib.apiInitLibrary(null, null, null);

Again this works perfectly on Linux (Kubuntu 7.0.4 x86, Java5 as well as Java6) but not on WindowsXP SP2 (Java5, 6).

I'd be grateful for any inspiration as I'd need the solution to work primarily under Windows (sadly).

Thanks for this fine project anyway, it is a very elegant solution that saves me from a close encounter with JNI;)