File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JNI - dll method invocation not working despite correct signature

 
Shajid Johnny
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear members,
i am trying to use some dll methods from java through JNI.

the documentation i got with the dll clearly explained the method api, i am providing some:



to use them, i have written a java class: TestDLL.java:



i placed my PaymentManager.dll in windows/system32 directory.
i compiled the class, made a header file of that. when i tried to run the class- the dll file gets loaded successfully, but the method is not invoked. i know there is an issue- the signature of the dll native method and my called method must be exactly same. i took a good care of that. but every time the console gives the following error:



notice the console error message: at the end of the method- a text I is appended. so is that the reason behind the error?

would anyone please help me on this?
 
Rob Spoor
Sheriff
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That I is the return type - int.

Can you show us your .h and .c files as well? You can leave out the body of the .c file's functions but we need to see the signatures of those methods as well.
 
Shajid Johnny
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob!
Rob Prime wrote:
Can you show us your .h and .c files as well? You can leave out the body of the .c file's functions but we need to see the signatures of those methods as well.


you know, this is where i have always been confused! all the tutorials i went through on the topic "how to integrate dll in java/JNI" described things in following manner:

1) write a java class which loads the dll in a static block, and invoke the native method
2) compile and create a header file of that java class
3) write a .c/.cpp which includes the header file of the java class, and implements the native method maintaining the exact signature
4) compile the .c/.cpp file
5) run the java class


but this is not my case. in my case there is a dll named- PaymentManager.dll written in C++ , we need to build a KIOSK payment system implementing the functions of that dll. i do NOT have the .cpp file. all i have is the documentation of that dll which describes the API of the functions, some of them i've already mentioned previously.

the documentation says, there is a function to open the PanymaneManager:
& my generated header file is:


so my question is: Can't i do what i am trying to do? do i need the .cpp file of the dll to invoke its methods? is #3 and #4 points above is mandatory to implement JNI??
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you can't do what you're trying to do, not the way you're doing it, anyway. Yes, you have to write a native method, generate a header, and write a C/C++ wrapper for each of the DLL methods before you can call them from Java.

But for a Windows-specific solution, check out JNA, which lets you use DLLs without writing any native code at all. JNA includes its own native code which already knows how to invoke methods in DLLs, so you can call them from Java automatically.
 
Rob Spoor
Sheriff
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you can use that DLL, but you have to write a wrapper in C or C++ for it. The header file already helps you. All you need to do is create a .c or .cpp file that includes this header and implements the Java_TestDLL_openpaymentmanager function. You'll probably also need a header file for the DLL, which you may have to create manually.

But JNA probably is the easier way if you have an existing DLL. The header and implementation files will then be bundled into one Java class.
 
Shajid Johnny
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest!
Thanks Rob!

yes, JNA has the answer to my need. i am trying that. lets see if i can do this.

thanks again
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic