I have a third-party VB.Net dll that I want to call from Java.
The VB.Net dll has the following signature (pseudo code, but feels like Java...):
class MyClass1 {
public Object method1(StringRef arg1, StringRef arg2) {
// do something here...
return someResult;
}
}
class MyClass2 {
public Object method2(StringRef arg1, StringRef arg2) {
// do something here...
return someOtherResult;
}
}
Note: StringRef
is my way of saying the method expects me to pass in strings by reference.
I am trying to call this dll object from within Java. Using JNA, I have the following:
import com.sun.jna.Library;
import com.sun.jna.Native;
public interface DllName extends Library {
DllName INSTANCE = (DllName)Native.loadLibrary("DllName", DllName.class);
public static interface MyClass1 {
public Object method1(String arg1, String arg2);
}
public static interface MyClass2 {
public Object method2(String arg1, String arg2);
}
}
The INSTANCE
object here loads just fine. However, I cannot figure out what structure the body of DllName
should take to map to the dll's class, method signature. Also, I have not seen how I might call Native
in a way that would load the object directly. For example, if I do:
DllName INSTANCE = (DllName)Native.loadLibrary("DllName.MyClass1", DllName.class);
This results in an UnsatisfiedLinkError
since the dll is named DllName
. Making this call requires a different interface than shown above.
Questions:
DllName
need to have to properly map to the class MyClass1
and MyClass2
? This is my core question here.DllName.MyClass1
call above, is there some alternative way?I have explored the following alternatives:
Please let me know if you would like me to add any additional color here.
javOnet already provides support for arguments passed by ref or out since version 1.2. You can read more at: http://www.javonet.com/quick-start-guide/#Passing_arguments_by_reference_with_ref_and_out_keywrods
You must wrap you JAVA type in "AtomicReference" so it can be updated within the method call and your JAVA variable let's say integer will be automatically modified on .NET side. You can see the usage sample below:
NObject refEx = Javonet.New("RefExample");
//Wrap Java integer in AtomicReference to allow passing by reference
AtomicReference<Integer> myInt = new AtomicReference<Integer>(10);
refEx.invoke("Method",new NRef(myInt));
System.out.println(myInt.get());
//Output will display number "55" because int passed by reference has been modified within the method body.