So this is the error I get on Eclipse:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# Internal Error (0xe0434352), pid=7696, tid=8632
#
# JRE version: Java(TM) SE Runtime Environment (8.0_45-b15) (build 1.8.0_45-b15)
# Java VM: Java HotSpot(TM) Client VM (25.45-b02 mixed mode, sharing windows-x86 )
# Problematic frame:
# C [KERNELBASE.dll+0xc42d]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
This is the C# dll:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StringAPI
{
public class Test
{
public string Something(string username)
{
return username;
}
}
}
This is the Java code:
import java.util.Scanner;
public class String_Displayer
{
static
{
System.loadLibrary("StringAPIWrapper");
}
public native String something(String username);
public static void main(String[] args)
{
String_Displayer sd = new String_Displayer();
System.out.println("Please enter your name:");
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
String username = scan.next();
String s = sd.something(username);
System.out.print(sd.something(username));
}
And this is the C++/CLI Wrapper dll:
#include "stdafx.h"
#include "com_StringDisplayer_String_Displayer.h"
#include "StringAPIWrapper.h"
#include <vcclr.h>
#include <iostream>
using namespace std;
using namespace System;
using namespace StringAPI;
JNIEXPORT jstring JNICALL Java_com_StringDisplayer_String_1Displayer_something (JNIEnv *env, jclass jc, jstring js)
{
const jchar* temp = (env)->GetStringChars(js, 0);
if (temp == NULL)
{
return NULL;
}
String^ managedString = gcnew System::String((const wchar_t*)temp);
env->ReleaseStringChars(js, temp);
Test^ test1 = gcnew Test();
String^ result = test1->Something(managedString);
pin_ptr<const wchar_t> resultChars = PtrToStringChars(result);
return env->NewString((jchar*)resultChars, result->Length);
}
What I'm sure of is the function that calls the C# function is working properly. I've also tried to just write it directly from C++/CLI using System::Console::Writeline without converting to a jstring but it didn't work. I know that using Writeline does actually work: If I do System::Console::WriteLine("test"); and comment out the call to C#, "test" is written to the java console.