I have a problem with Java Native Access: I have a C-library with one function, let' s say foo(). This function has a memory - a counter - with increases with every call. Is it possible to create two instances of this library within the same java process so that the counters are independent?
Thank you very much.
Here is some code:
public class A
{
public static class Lib
{
NativeLibrary libInstance = NativeLibrary.getInstance("myLibrary");
Function fn = lib.getFunction("foo");
}
private Lib lib = new Lib();
public foo()
{
lib.fn.invoke(new Object[] {});
}
}
If I call:
A a = new A();
A b = new A();
a.foo(); // >1
a.foo(); // >2
b.foo(); // >3
a.foo(); // >4
b.foo(); // >5
a.foo(); // >6
but I want a and b to work independent with the library:
a.foo(); // >1
a.foo(); // >2
b.foo(); // >1
a.foo(); // >3
b.foo(); // >2
a.foo(); // >4
Many thanks
This is how I try to create an instance of a lib:
public class DriverLib
{
private static int counter = 1;
NativeLibrary lib;
Function stepAction;
Function initialize;
Function terminate;
Pointer input;
Pointer output;
public DriverLib()
{
// create options
HashMap<String, Integer> options = new HashMap<>();
options.put(Library.OPTION_OPEN_FLAGS, new Integer(counter++));
lib = NativeLibrary.getInstance("mylib_win64", options);
stepAction = lib.getFunction("step");
initialize = lib.getFunction("initialize");
terminate = lib.getFunction("terminate");
input = lib.getGlobalVariableAddress("model_U");
output = lib.getGlobalVariableAddress("model_Y");
}
}