I've tried to follow the examples which I found here, but I am unable to get it to work. This is my Java class
package jniTester;
public class JNITester {
static {
System.load("D:\\\\VisualStudio_Cpp_2017\\SkriptumTeil5\\Debug\\HelloWorldJNI.dll");
}
public static native String welcome(String name);
}
From this I've created with javah the jniTester.h file
This is my C# class
namespace HelloWorldJNI
{
public static class HelloWorldJNI
{
public static String Welcome(String name)
{
return "Hello " + name + "! This is your C# buddy.";
}
}
}
From this I've created HelloWorldJNI.netmodule
Here is my cpp class
#include "stdafx.h"
#include <jni.h>
#include <string>
#include "jniTester.h"
#using "D:\VisualStudio_C#_2017\SkriptumTeil5\HelloWorldJNI\HelloWorldJNI.netmodule"
using namespace std;
JNIEXPORT jstring JNICALL Java_jniTester_JNITester_welcome(JNIEnv *env, jclass thisclass, jstring inJNIStr) {
// Step 1: Convert the JNI String (jstring) into C-String (char*)
const char *inCStr = env->GetStringUTFChars(inJNIStr, NULL);
if (NULL == inCStr) return NULL;
// Step 2: Convert the C++ string to C-string, then to JNI String (jstring) and return
//string outCppStr = "Hello " + std::string(inCStr) + ". Greetings from your C++ buddy";
//env->ReleaseStringUTFChars(inJNIStr, inCStr); // release resources
//return env->NewStringUTF(outCppStr.c_str());
//// Alternate Step 2:
System::String^ outStr = HelloWorldJNI::HelloWorldJNI::Welcome(gcnew System::String(inCStr));
env->ReleaseStringUTFChars(inJNIStr, inCStr); // release resources
char* converted = static_cast<char*>((System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(outStr)).ToPointer());
return env->NewStringUTF(converted);
}
The code under Step 2 works. However this is not calling my C# method. The implementation under Alternate Step 2 fails with
# A fatal error has been detected by the Java Runtime Environment:
#
# Internal Error (0xe0434352), pid=37224, tid=0x00003350
I am no cpp expert, so I am totally in the dark. What is wrong here?
export "C" { /* my JNI functions */ }around of your JNI functions implementations. Since you are using C++ to wrap the C#. Also check - how to call managed code from the native codeexport "C"stuff. First it isexternnotexport. Second it must be in the header already, otherwise you wouldn't be able to call the function at all. Have you tried calling a C# function without arguments? Have you tried in a standalone C++ executable? This is unlikely a JNI problem.