I'm currently trying to call .NET code using jni4net.
I've managed to create the proxy classes from a .NET .dll using proxygen. However, this code:
Bridge.init();
Bridge.LoadAndRegisterAssemblyFrom(new File("clAESEncripcion.j4n.dll"));
aesencryption.AESObjectEnc aesObjectEnc = new aesencryption.AESObjectEnc();
aesObjectEnc.Encrypt(new Ref<String>("To encrypt"));
Throws the following exception:
Exception in thread "main" java.lang.IncompatibleClassChangeError
at aesencryption.AESObjectEnc.Encrypt(Native Method)
at clAESEncripcion.MainApp.main(MainApp.java:19)
at net.sf.jni4net.jni.JNIEnv.ExceptionTest()(:0)
at net.sf.jni4net.jni.JNIEnv.CallIntMethod()(:0)
at net.sf.jni4net.inj.__IClrProxy.getClrHandle()(:0)
at net.sf.jni4net.inj.__IClrProxy.GetObject()(:0)
at net.sf.jni4net.utils.Convertor.StrongJp2CString()(:0)
at net.sf.jni4net.utils.Convertor.FullJ2C()(:0)
at net.sf.jni4net.Ref.GetValue()(:0)
at AESEncryption.__AESObjectEnc.Encrypt1()(:0)
I'm currently workging with jdk1.7.0_75 64bits ,Microsoft.NET\Framework64\v4.0.30319 and jni4net-0.8.6.0.
The AESObjectEnc C# class of the dll has an Encrypt method which expects a String ref.
Thanks in advance.
You cannot pass String to a c# Ref method in the .dll file using jni library. If ref parameter is an Integer or simple c# object, you can pass int value or an object to C# .dll file using jni library.
You can found more details from here.
If you want to pass a String to c# .dll file, create a new function in the .dll file that only accepts String and not Ref String. Then call that function from your java class using jni library. now you can pass String value to c# code. And call the Ref String function from that newly created function. Check out this example code.
[JAVA CODE]
Bridge.init();
Bridge.LoadAndRegisterAssemblyFrom(new File("clAESEncripcion.j4n.dll"));
aesencryption.AESObjectEnc aesObjectEnc = new aesencryption.AESObjectEnc();
public void javaMethod(){
String val = "To encrypt";
String receive_val = "";
receive_val = aesObjectEnc.CallEncrypt(val);
System.out.println(receive_val);
}
[C# CODE]
New Method
public String CallEncrypt(String val){
String send_val = val;
//Call Encrypt Method
Encrypt(ref send_val);
//Get Value
String output = send_val;
//Pass value to Java Class
return output;
}
Existing Encrypt Method
public String Encrypt(ref String receive_val){
receive_val = receive_val + " ok";
}
This will give output
//To encrypt ok