• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JNI - dll method invocation not working despite correct signature

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Just let me do the talking. Ahem ... so ... you see ... we have this tiny ad...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic