简体   繁体   中英

C# calling Java code loading unmanaged dll

I'm working with some java code wich loads an unmanaged dll, just as the following:

public void Foo(){
    System.loadLibrary("absolute_path_to_my_dll.dll")
}

It works fine from eclipse or console.

Next step: I'm using IKVM tools to get a managed dll from my java app (ikvmc.exe). Everything works fine and my dll is built perfectly. In fact, I've imported that library in a Test Solution (and I've add all IKVM refereces needed -IKVM.Core.JDK, IKVM.Runtime.JNI, etc-).

Well, here is the problem: when I run C# code the java call to System.loadLibrary(...) fails and I get an "UnsatisfiedLinkError: Can't load absolute_path_to_my_dll.dll" . Absolute path is ok and dll is there.

Any help? Thanks in advance.

Edit 1

I'll try to explain more in detail:

Java step

public void Foo(){
    System.loadLibrary("absolute_path_to_my_dll.dll")
}

If path is not correct this call gives the "UnsatisfiedLinkError" mentioned above. This is not the case, everything works well. The dll file is a 32 bit one, so I compiled this java project with jre7 x86 (in 64 bit mode loadLibrary call falis, obviously, saying we can't load 32 bit dll in 64 bit AMD machine).

Ikvm step

Now I compile java code in a managed dll wich can be imported in a .net project. First of all I export my java project to a jar file, "myJar.jar". Then i apply ikvmc.exe to generate a managed dll, let's say "myNewDll.dll".

ikvm.exe -target:library -out:"myNewDll.dll" "myJar.jar"

This step works nicely and i get a new dll I can import in my VS project.

C# step

Now I create a new VS tester solution. I add "myNewDll.dll" as reference so I can use it in my C# code. Also, I add IKVM.Core.JDK reference (if not, project can't compile) and configure project in 32 bit mode. Problems start here:

  • Running this setup gives "IKVM.Runtime.JNI error". I've added that reference.
  • Running again gives "can't find ikvm-native". As I can't add ikvm-native-win32-x86.dll or ikvm-native-win32-x64.dll as reference I put both of them in output path (bin/Debug).
  • Next try it gives the "UnsatisfiedLinkError: Can't load xxx.dll".

How can it be possible if that dll is loaded flawlessly in java step?

Annother try

Just before start crying I tried annother way: I created a java main program that only called Foo(). Then I generated an executable file with ikvmc.exe tool and called it in windows console. It gave me the same error, UnsatisfiedLinkError so maybe it's ikvmc.exe problem but I can't understand why.

Any idea?

Edit 2

Good news. It seems I've solved the problem. First of all, I had to add -platform:x86 to ikvmc.exe call (step 2). The dll generated was succesfully imported in my C# project and it works fine if we run in execution mode (NOT debugging) . If I try to debug in VS it gives me System.Runtime.InteropServices.SEHException.

Is there any kind of bug related to debugging native code (dll loaded in java code) under native code (that java code loaded in C# using ikvm)?

Is your dll a 32 bit and you run it on a 64 bit platform?

If yes then the problem is that IKVM is platform independent. This means on a 64 bit system it runs 64 bit process.

To solve this you need a 64 bit version of your dll or mark ikvm.exe as 32 bit process.

Adding an option of reference to IKVM.OpenJDK.Core.dll at ikvm's /bin directory will resolve the error.

Maybe the .jar file needs some native DLLs, which was same as Native library location parameter of JAR in Eclipse. If so, find the native DLLs and copy them to de /bin directory of IKVM, and make ikvmc to load them automatically.

That's all what I've found out. Try and see if it would bring some ideas.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM