So, i have visual studio consloe project that compile to .dll file.
I've created simple windows form System::Windows::Forms::Form
I've created a .java file:
import java.io.Serializable;
public class MyBean implements Serializable{
/**
*
*/
static{
System.loadLibrary("MyBean");
}
private static final long serialVersionUID = 1L;
private static native String getDateCpp();
public String getDate(){
return getDateCpp();
}
}
compiled it and generate a .h file by javah:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class MyBean */
#ifndef _Included_MyBean
#define _Included_MyBean
#ifdef __cplusplus
extern "C" {
#endif
#undef MyBean_serialVersionUID
#define MyBean_serialVersionUID 1i64
/*
* Class: MyBean
* Method: getDateCpp
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_MyBean_getDateCpp
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
and implemented a .cpp file that shows Form:
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "Form1.h"
#include "MyBean.h"
#include <string>
#include <vcclr.h>
using namespace std;
bool To_string( String^ source, string &target )
{
pin_ptr<const wchar_t> wch = PtrToStringChars( source );
int len = (( source->Length+1) * 2);
char *ch = new char[ len ];
bool result = wcstombs( ch, wch, len ) != -1;
target = ch;
delete ch;
return result;
}
JNIEXPORT jstring JNICALL Java_MyBean_getDateCpp
(JNIEnv * env, jclass jcl){
Form1^ form = gcnew Form1();
form->Show();
String^ text = form->text;
string stdString = "";
if(To_string(text,stdString))
return (*env).NewStringUTF(stdString.c_str());
else
return (*env).NewStringUTF("blad");
}
after compilling succesfully i'm getting error form java after i try to call c++ function:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000007f9324f811c, pid=4404, tid=4800
#
# JRE version: Java(TM) SE Runtime Environment (7.0_45-b18) (build 1.7.0_45-b18)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (24.45-b08 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C [KERNELBASE.dll+0x3811c]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# D:\studia\semestr 7\java\lab05\hs_err_pid4404.log
#
# 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.
#
and log is long, i'll not post it here.
What i'm doing wrong?