How to get returned values from C# to Java using JNI

1

I need to get the value returned by the method inside C# to a variable inside java class so I used JNI. It worked well for just calling a method inside C# but it is not working while I modify the same method and those corresponding calls to pass a value to java... It shows a fatal error and the below log file is created.

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  Internal Error (0xe0434f4d), pid=4252, tid=4236
#
# JRE version: 7.0_07-b10
# Java VM: Java HotSpot(TM) Client VM (23.3-b01 mixed mode, sharing windows-x86 )
# Problematic frame:
# C  [kernel32.dll+0x1eb33]  RaiseException+0x52
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.sun.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

---------------  T H R E A D  ---------------

Current thread (0x009e8800):  JavaThread "main" [_thread_in_native, id=4236, stack(0x00ad0000,0x00b20000)]

siginfo: ExceptionCode=0xe0434f4d, ExceptionInformation=0x80070002 

Registers:
EAX=0x00b1ed44, EBX=0xe0434f4d, ECX=0x00000000, EDX=0x00000028
ESP=0x00b1ed40, EBP=0x00b1ed94, ESI=0x00b1edd0, EDI=0x000c5570
EIP=0x7c81eb33, EFLAGS=0x00000202

Top of Stack: (sp=0x00b1ed40)
0x00b1ed40:   00b1edcc e0434f4d 00000001 00000000
0x00b1ed50:   7c81eb33 00000001 80070002 02000038
0x00b1ed60:   00b1ed70 79e80024 00b1ed78 02000038
0x00b1ed70:   00b1ed7c 79e80687 79330ba0 00b1ed8c
0x00b1ed80:   79eda76f 793171fc 0000012f 00b1ed9c
0x00b1ed90:   79eda796 00b1edf4 79eda91c e0434f4d
0x00b1eda0:   00000001 00000001 00b1edcc 4f14bdac
0x00b1edb0:   00000000 03b85780 00b1ee80 00ad3000 

Instructions: (pc=0x7c81eb33)
0x7c81eb13:   f9 0f 0f 87 69 5a 02 00 85 c9 89 4d c0 74 07 57
0x7c81eb23:   8d 7d c4 f3 a5 5f 8d 45 b0 50 ff 15 04 15 80 7c
0x7c81eb33:   5e c9 c2 10 00 85 ff 0f 8e e6 d0 fe ff 8b 55 fc
0x7c81eb43:   89 55 0c 0f b7 16 8b 7d f8 8a 14 3a 88 11 8b 78 


Register to memory mapping:

EAX=0x00b1ed44 is pointing into the stack for thread: 0x009e8800
EBX=0xe0434f4d is an unknown value
ECX=0x00000000 is an unknown value
EDX=0x00000028 is an unknown value
ESP=0x00b1ed40 is pointing into the stack for thread: 0x009e8800
EBP=0x00b1ed94 is pointing into the stack for thread: 0x009e8800
ESI=0x00b1edd0 is pointing into the stack for thread: 0x009e8800
EDI=0x000c5570 is an unknown value


Stack: [0x00ad0000,0x00b20000],  sp=0x00b1ed40,  free space=315k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [kernel32.dll+0x1eb33]  RaiseException+0x52
C  [mscorwks.dll+0x6a91c]  GetMetaDataInternalInterface+0x84a9
C  [mscorwks.dll+0xc9e44]  CopyPDBs+0x39f8
C  [mscorwks.dll+0x26154b]  CorLaunchApplication+0x15d35
C  [mscorjit.dll+0x2ea6]
C  [mscorjit.dll+0x717d]
C  [mscorjit.dll+0x43a1]
C  [mscorjit.dll+0x44d6]
C  [mscorjit.dll+0x465c]
C  [mscorjit.dll+0x5b8e]
C  [mscorjit.dll+0x5d33]
C  [mscorjit.dll+0x6448]
C  [mscorwks.dll+0x157198]  StrongNameErrorInfo+0x22d0c
C  [mscorwks.dll+0x15722d]  StrongNameErrorInfo+0x22da1
C  [mscorwks.dll+0x1572a0]  StrongNameErrorInfo+0x22e14
C  [mscorwks.dll+0x157019]  StrongNameErrorInfo+0x22b8d
C  [mscorwks.dll+0x156ddb]  StrongNameErrorInfo+0x2294f
C  [mscorwks.dll+0x111a3]  DllUnregisterServerInternal+0x5187
C  [mscorwks.dll+0x11363]  DllUnregisterServerInternal+0x5347
C  0x0324fb2e
C  0x0352e109
C  0x03250081
j  Test1.displayHelloWorld()Z+0
j  Test1.main([Ljava/lang/String;)V+12
v  ~StubRoutines::call_stub
V  [jvm.dll+0x12a39a]
V  [jvm.dll+0x1d962e]
V  [jvm.dll+0x12a41d]
V  [jvm.dll+0xaee94]
V  [jvm.dll+0xb9217]
C  [java.exe+0x1fd8]
C  [java.exe+0xa0cb]
C  [java.exe+0xa155]
C  [kernel32.dll+0xb50b]  GetModuleFileNameA+0x1b4

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  Test1.displayHelloWorld()Z+0
j  Test1.main([Ljava/lang/String;)V+12
v  ~StubRoutines::call_stub

---------------  P R O C E S S  ---------------

Java Threads: ( => current thread )
  0x02c7c000 JavaThread "Service Thread" daemon [_thread_blocked, id=2376, stack(0x03010000,0x03060000)]
  0x02c76400 JavaThread "C1 CompilerThread0" daemon [_thread_blocked, id=608, stack(0x02fc0000,0x03010000)]
  0x02c74800 JavaThread "Attach Listener" daemon [_thread_blocked, id=4308, stack(0x02f70000,0x02fc0000)]
  0x02c73000 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=4292, stack(0x02f20000,0x02f70000)]
  0x02c64400 JavaThread "Finalizer" daemon [_thread_blocked, id=4304, stack(0x02ed0000,0x02f20000)]
  0x02c5f400 JavaThread "Reference Handler" daemon [_thread_blocked, id=4288, stack(0x02e80000,0x02ed0000)]
=>0x009e8800 JavaThread "main" [_thread_in_native, id=4236, stack(0x00ad0000,0x00b20000)]

Other Threads:
  0x02c5dc00 VMThread [stack: 0x02e30000,0x02e80000] [id=4244]
  0x02c86c00 WatcherThread [stack: 0x03060000,0x030b0000] [id=4320]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Heap
 def new generation   total 4928K, used 281K [0x22cc0000, 0x23210000, 0x28210000)
  eden space 4416K,   6% used [0x22cc0000, 0x22d064f0, 0x23110000)
  from space 512K,   0% used [0x23110000, 0x23110000, 0x23190000)
  to   space 512K,   0% used [0x23190000, 0x23190000, 0x23210000)
 tenured generation   total 10944K, used 0K [0x28210000, 0x28cc0000, 0x32cc0000)
   the space 10944K,   0% used [0x28210000, 0x28210000, 0x28210200, 0x28cc0000)
 compacting perm gen  total 12288K, used 112K [0x32cc0000, 0x338c0000, 0x36cc0000)
   the space 12288K,   0% used [0x32cc0000, 0x32cdc128, 0x32cdc200, 0x338c0000)
    ro space 10240K,  42% used [0x36cc0000, 0x370ff150, 0x370ff200, 0x376c0000)
    rw space 12288K,  54% used [0x376c0000, 0x37d3fad8, 0x37d3fc00, 0x382c0000)

Card table byte_map: [0x02cd0000,0x02d80000] byte_map_base: 0x02bb9a00

Polling page: 0x00b20000

Code Cache  [0x00b50000, 0x00be8000, 0x02b50000)
 total_blobs=133 nmethods=5 adapters=64 free_code_cache=32187Kb largest_free_block=32959808

Compilation events (10 events):
Event: 0.110 Thread 0x02c76400    1             java.lang.String::hashCode (55 bytes)
Event: 0.110 Thread 0x02c76400 nmethod 1 0x00bdfcc8 code [0x00bdfdc0, 0x00bdfea0]
Event: 0.122 Thread 0x02c76400    2             java.lang.String::charAt (29 bytes)
Event: 0.123 Thread 0x02c76400 nmethod 2 0x00be0288 code [0x00be0390, 0x00be04bc]
Event: 0.126 Thread 0x02c76400    3             java.lang.String::indexOf (70 bytes)
Event: 0.127 Thread 0x02c76400 nmethod 3 0x00be0608 code [0x00be0720, 0x00be085c]
Event: 0.127 Thread 0x02c76400    4             java.io.Win32FileSystem::normalize (143 bytes)
Event: 0.128 Thread 0x02c76400 nmethod 4 0x00be0988 code [0x00be0ad0, 0x00be0d6c]
Event: 0.128 Thread 0x02c76400    5             java.lang.Object::<init> (1 bytes)
Event: 0.128 Thread 0x02c76400 nmethod 5 0x00be10c8 code [0x00be11c0, 0x00be1250]

GC Heap History (0 events):
No events

Deoptimization events (0 events):
No events

Internal exceptions (2 events):
Event: 0.017 Thread 0x009e8800 Threw 0x22cc11d8 at C:\jdk7u1_32P\jdk7u7\hotspot\src\share\vm\prims\jni.cpp:3994
Event: 0.123 Thread 0x009e8800 Threw 0x22cf9b00 at C:\jdk7u1_32P\jdk7u7\hotspot\src\share\vm\prims\jvm.cpp:1166

Events (10 events):
Event: 0.124 loading class 0x02ca2820
Event: 0.125 loading class 0x02ca2820 done
Event: 0.125 loading class 0x02ca24a0
Event: 0.125 loading class 0x02ca24a0 done
Event: 0.125 loading class 0x383bd170
Event: 0.125 loading class 0x382f3788
Event: 0.125 loading class 0x382f3788 done
Event: 0.125 loading class 0x383bd170 done
Event: 0.127 loading class 0x382caa28
Event: 0.127 loading class 0x382caa28 done


Dynamic libraries:
0x00400000 - 0x0042f000     C:\windows\System32\java.exe
0x7c900000 - 0x7c9b0000     C:\windows\system32\ntdll.dll
0x7c800000 - 0x7c8f4000     C:\windows\system32\kernel32.dll
0x77dd0000 - 0x77e6b000     C:\windows\system32\ADVAPI32.dll
0x77e70000 - 0x77f01000     C:\windows\system32\RPCRT4.dll
0x77d40000 - 0x77dd0000     C:\windows\system32\USER32.dll
0x77f10000 - 0x77f56000     C:\windows\system32\GDI32.dll
0x773d0000 - 0x774d2000     C:\windows\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9\COMCTL32.dll
0x77c10000 - 0x77c68000     C:\windows\system32\msvcrt.dll
0x77f60000 - 0x77fd6000     C:\windows\system32\SHLWAPI.dll
0x76390000 - 0x763ad000     C:\windows\system32\IMM32.DLL
0x629c0000 - 0x629c9000     C:\windows\System32\LPK.DLL
0x74d90000 - 0x74dfb000     C:\windows\System32\USP10.dll
0x6d590000 - 0x6d596000     C:\PROGRA~1\KASPER~1\KASPER~1.0FO\kloehk.dll
0x6d020000 - 0x6d035000     C:\PROGRA~1\KASPER~1\KASPER~1.0FO\adialhk.dll
0x78aa0000 - 0x78b5e000     C:\Program Files\Java\jre7\bin\msvcr100.dll
0x6e750000 - 0x6ea9c000     C:\Program Files\Java\jre7\bin\client\jvm.dll
0x71ad0000 - 0x71ad9000     C:\windows\System32\WSOCK32.dll
0x71ab0000 - 0x71ac7000     C:\windows\System32\WS2_32.dll
0x71aa0000 - 0x71aa8000     C:\windows\System32\WS2HELP.dll
0x76b40000 - 0x76b6d000     C:\windows\System32\WINMM.dll
0x76bf0000 - 0x76bfb000     C:\windows\System32\PSAPI.DLL
0x6e6a0000 - 0x6e6ac000     C:\Program Files\Java\jre7\bin\verify.dll
0x6d480000 - 0x6d4a0000     C:\Program Files\Java\jre7\bin\java.dll
0x6e730000 - 0x6e743000     C:\Program Files\Java\jre7\bin\zip.dll
0x10000000 - 0x1000d000     C:\module\supporting\HelloWorld.dll
0x79000000 - 0x7904a000     C:\windows\system32\mscoree.dll
0x10200000 - 0x10320000     C:\windows\WinSxS\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_f75eb16c\MSVCR80D.dll
0x03110000 - 0x03212000     C:\windows\WinSxS\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_f75eb16c\msvcm80d.dll
0x774e0000 - 0x7761c000     C:\windows\system32\ole32.dll
0x603b0000 - 0x60416000     C:\windows\Microsoft.NET\Framework\v4.0.30319\mscoreei.dll
0x79e70000 - 0x7a400000     C:\windows\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll
0x78130000 - 0x781cb000     C:\windows\WinSxS\x86_Microsoft.VC80.CRT_1fc8b3b9a1e18e3b_8.0.50727.3053_x-ww_b80fa8ca\MSVCR80.dll
0x7c9c0000 - 0x7d1d4000     C:\windows\system32\shell32.dll
0x790c0000 - 0x79bb7000     C:\windows\assembly\NativeImages_v2.0.50727_32\mscorlib\9adb89fa22fd5b4ce433b5aca7fb1b07\mscorlib.ni.dll
0x79060000 - 0x790bb000     C:\windows\Microsoft.NET\Framework\v2.0.50727\mscorjit.dll
0x5e3a0000 - 0x5e42d000     C:\windows\Microsoft.NET\Framework\v2.0.50727\diasymreader.dll
0x0ffd0000 - 0x0fff8000     C:\windows\System32\rsaenh.dll
0x7a440000 - 0x7abc5000     C:\windows\assembly\NativeImages_v2.0.50727_32\System\aa7926460a336408c8041330ad90929d\System.ni.dll
0x59a60000 - 0x59b01000     C:\windows\System32\dbghelp.dll
0x77c00000 - 0x77c08000     C:\windows\system32\VERSION.dll

VM Arguments:
java_command: Test1
Launcher Type: SUN_STANDARD

Environment Variables:
JAVA_HOME=C:\Program Files\Java\jdk1.7.0_01
PATH=C:\windows\System32;C:\wamp\bin\php\php5.3.0;C:\wamp\bin\php\php5.3.0;C:\Program Files\Java\jdk1.6.0_18\bin;C:\windows\system32;C:\windows;C:\windows\system32\WBEM;C:\strawberry\c\bin;C:\strawberry\perl\site\bin;C:\strawberry\perl\bin;C:\wamp\bin\php\php5.3.0;C:\wamp\bin\php\php5.3.0;C:\Program Files\Java\jdk1.6.0_18\bin;C:\windows\system32;C:\windows;C:\windows\system32\WBEM;C:\strawberry\c\bin;C:\strawberry\perl\site\bin;C:\strawberry\perl\bin;C:\wamp\bin\php\php5.3.0;C:\wamp\bin\php\php5.3.0;C:\Program Files\Java\jdk1.6.0_18\bin;C:\windows\system32;C:\windows;C:\windows\system32\WBEM;C:\strawberry\c\bin;C:\strawberry\perl\site\bin;C:\strawberry\perl\bin;C:\wamp\bin\php\php5.3.0;C:\wamp\bin\php\php5.3.0;C:\Program Files\Java\jdk1.6.0_18\bin;C:\windows\system32;C:\windows;C:\windows\system32\WBEM;C:\strawberry\c\bin;C:\strawberry\perl\site\bin;C:\strawberry\perl\bin;C:\wamp\bin\php\php5.3.0;C:\wamp\bin\php\php5.3.0;C:\Program Files\Java\jdk1.6.0_18\bin;C:\windows\system32;C:\windows;C:\windows\system32\WBEM;C:\strawberry\c\bin;C:\strawberry\perl\site\bin;C:\strawberry\perl\bin;ATALINA_HOME\bin
USERNAME=sathishkumarkk
OS=Windows_NT
PROCESSOR_IDENTIFIER=x86 Family 6 Model 23 Stepping 6, GenuineIntel



---------------  S Y S T E M  ---------------

OS: Windows XP Build 2600 Service Pack 2

CPU:total 2 (2 cores per cpu, 1 threads per core) family 6 model 23 stepping 6, cmov, cx8, fxsr, mmx, sse, sse2, sse3, ssse3, sse4.1, tsc

Memory: 4k page, physical 2028972k(797208k free), swap 4915092k(3478568k free)

vm_info: Java HotSpot(TM) Client VM (23.3-b01) for windows-x86 JRE (1.7.0_07-b10), built on Aug 28 2012 19:49:22 by "java_re" with unknown MS VC++:1600

time: Thu Sep 06 09:45:18 2012
elapsed time: 0 seconds

This link I used to do a try JNI and it works but while I try to return either int or bool or String from the C#... the above fatal error occurs.

CSharpHelloWorld.cs

using System;
using System.Windows.Forms;

public class CSharpHelloWorld
{
    public CSharpHelloWorld() { }

    public int displayHelloWorld()
    {
       // MessageBox.Show("Hello Java, I'm C#!", "Sample");
       return 1;
    }
}

later I created CSharpHelloWorld.netmodule by

csc /t:module CSharpHelloWorld.cs

next I wrote Test1.java

public class Test1 { 
private native int displayHelloWorld();
    static {
        try{
            System.loadLibrary("HelloWorld");
            //System.load("D://HelloWorld.dll");
            //System.loadLibrary("CallApi");
            System.out.println("Loaded");
        }catch(UnsatisfiedLinkError e){
            //nothing to do
            System.out.println("Couldn't load");
            System.out.println(e.getMessage());
        }
    }




    public static void main (String[] args) {
        Test1 t = new Test1();
       System.out.println(t.displayHelloWorld());
    }
}

I compiled it and used the Test1.class file to get Test1.h by running

javah Test1

then I wrote HelloWorldC.h that is I just copied it from the tutorial I mentioned.

#using <mscorlib.dll>
#using "MCPP\CSharpHelloWorld.netmodule"

using namespace System;

public __gc class HelloWorldC
{
    public:
        // Provide .NET interop and garbage collecting to the pointer.
        CSharpHelloWorld __gc *t;
        HelloWorldC() {
            t = new CSharpHelloWorld();
            // Assign the reference a new instance of the object
        }

     // This inline function is called from the C++ Code
       int callCSharpHelloWorld() {
            int i=t->displayHelloWorld();
            printf(i);
            return i;
        }
};

finally I wrote HelloWorld.cpp

#include <jni.h>
#include "Java\Test1.h"

// The Managed C++ header containing the call to the C#
#include "MCPP\HelloWorldC.h"

// This is the JNI call to the Managed C++ Class
// NOTE: When the java header was created, the package name was not include in the JNI call.
// This naming convention was corrected by adding the 
// "helloworld" name following the following syntax: 
// Java_<package name>_<class name>_<method name>
JNIEXPORT jint JNICALL Java_Test1_displayHelloWorld  (JNIEnv *jn, jobject jobj) {

    // Instantiate the MC++ class.
    HelloWorldC* t = new HelloWorldC();


    // The actual call is made. 
    return t->callCSharpHelloWorld();
}

then I build this with Visual Studio 2005 and I got the dll file later I copied that dll and past it near to my java class and I ran Test1.class

java Test1

after running I get the fatal error that which I mention above.

Note: I have followed same structure mentioned in the tutorial that I mentioned.

c#
java
c++
java-native-interface
asked on Stack Overflow Sep 10, 2012 by Sathish Kumar k k • edited Sep 11, 2012 by Sathish Kumar k k

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0