3
How do I include files dll
in a Java project using Eclipse?
3
How do I include files dll
in a Java project using Eclipse?
4
You should wear one of the Java Apis available to access native code, are those:
JNI (Java Native Interface) and JNA (Java Native Access).
JNI is the official Java API (Oracle) and JNA is open source maintained by the community.
While both have the same goal, the development of the JNA was focused on simplifying the implementation to the maximum, common problem in the implementation using JNI.
I recommend using JNA for you to achieve your goal, follow an example you can use as reference to your implementation:
Example of native application in DLL originator C++
#include "simpleDLL.h"
#include <stdexcept>
using namespace std;
namespace simpleDLLNS
{
char simpleDLL::giveVoidPtrGetChar(void* param)
{
if(param != 0)
{
char* paramChrPtr = (char*)param;
return *paramChrPtr;
}
else
{
return 'x';
}
}
int simpleDLL::giveIntGetInt(int a)
{
return 2*a;
}
void simpleDLL::simpleCall(void)
{
int x = 3;
return;
}
int simpleDLL::giveVoidPtrGetInt(void* param)
{
if(param!=0)
{
int* x = (int*)param;
return *x;
}
else
{
return -1;
}
}
}
Call JNA to DLL methods above:
package jnahelloworldtest;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Platform;
import com.sun.jna.*;
/** Simple example of native library declaration and usage. */
public class Main {
public interface simpleDLL extends Library {
simpleDLL INSTANCE = (simpleDLL) Native.loadLibrary(
(Platform.isWindows() ? "simpleDLL" : "simpleDLLLinuxPort"), simpleDLL.class);
// it's possible to check the platform on which program runs, for example purposes we assume that there's a linux port of the library (it's not attached to the downloadable project)
byte giveVoidPtrGetChar(Pointer param); // char giveVoidPtrGetChar(void* param);
int giveVoidPtrGetInt(Pointer param); //int giveVoidPtrGetInt(void* param);
int giveIntGetInt(int a); // int giveIntGetInt(int a);
void simpleCall(); // void simpleCall();
}
public static void main(String[] args) {
simpleDLL sdll = simpleDLL.INSTANCE;
sdll.simpleCall(); // call of void function
int a = 3;
int result1 = sdll.giveIntGetInt(a); // calling function with int parameter&result
System.out.println("giveIntGetInt("+a+"): " + result1);
String testStr = "ToBeOrNotToBe";
Memory mTest = new Memory(testStr.length()+1); // '+1' remember about extra byte for \0 character!
mTest.setString(0, testStr);
String testReturn = mTest.getString(0); // you can see that String got properly stored in Memory object
System.out.println("String in Memory:"+testReturn);
Memory intMem = new Memory(4); // allocating space
intMem.setInt(0, 666); // setting allocated memory to an integer
Pointer intPointer = intMem.getPointer(0);
int int1 = sdll.giveVoidPtrGetInt(Pointer.NULL); // passing null, getting default result
System.out.println("giveVoidPtrGetInt(null):" + int1); // passing int stored in Memory object, getting it back
int int2 = sdll.giveVoidPtrGetInt(intMem);
//int int2 = sdll.giveVoidPtrGetInt(intPointer); causes JVM crash, use memory object directly!
System.out.println("giveVoidPtrGetInt(666):" + int2);
byte char1 = sdll.giveVoidPtrGetChar(Pointer.NULL); // passing null, getting default result
byte char2 = sdll.giveVoidPtrGetChar(mTest); // passing string stored in Memory object, getting first letter
System.out.println("giveVoidPtrGetChar(null):" + (char)char1);
System.out.println("giveVoidPtrGetChar('ToBeOrNotToBe'):" + (char)char2);
}
}
Mapping Table
More references:
Getting Started with JNA
Example source
-1
Browser other questions tagged java eclipse dll
You are not signed in. Login or sign up in order to post.
Want to include and use its methods?
– Ricardo
That’s what I’m talking about. I am trying to access the serial port through the jSSC library, and this library has the jSSC-2.8_x86_64.dll file which I must add to the project. When to add, how can I include and use her methods?
– Duds