public class CSharpHelloWorld { public CSharpHelloWorld() { } public void displayHelloWorld() { Console.WriteLine("Hello World From C#!"); } }
#using <mscorlib.dll> #using "SharpClass.netmodule" using namespace System; __gc class HelloWorldC { public: CSharpHelloWorld __gc *t; HelloWorldC() { t = new CSharpHelloWorld(); } void method() { t -> displayHelloWorld(); } };
#include "C:\Program Files\Java\jdk1.6.0_02\include\jni.h" #include "HelloWorld.h" #include "1.cpp" JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld (JNIEnv *, jobject) { HelloWorldC* t = new HelloWorldC(); t->method(); }
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld (JNIEnv *, jobject);
#ifndef _Included_HelloWorld #define _Included_HelloWorld #ifdef __cplusplus extern "C" { #endif /* * Class: HelloWorld * Method: displayHelloWorld * Signature: ()V */ JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif
import java.io.*; class HelloWorld { public native void displayHelloWorld(); static { System.load("C:\\Java_C#\\JavaApplication1\\HelloWorld.dll"); } public static void main(String[] args) { new HelloWorld().displayHelloWorld(); } }
using System; using System.Collections.Generic; using System.Text; public class CSharpToJava { public CSharpToJava() { } public String returnValue(String value) { string ss = " cvb"; String answer = value+ss; return answer; } }
public class Main { public native String returnValue(String value); static { System.load("C:\\Java\\SharpToJava\\CSharpToJava.dll"); } public static void main(String[] args) { String value="Privet"; String val = new Main().returnValue(value); System.out.println(val); } }
#include #include <string> #using <mscorlib.dll> // native #using "CSharpClass.netmodule" // , using namespace std; using namespace System; // - native __gc class SendValue { public: CSharpToJava __gc *t; //CSharpToJava - . //t - ++ SendValue() { t = new CSharpToJava(); } String __gc* method(String __gc* value) { return (t -> returnValue(value)); } };
#include "C:\Program Files\Java\jdk1.6.0_02\include\jni.h" #include "Main.h" #include "wrapper.cpp" #include #include <string> using namespace System::Runtime::InteropServices; // Marshal class, String* const Char* using namespace std; //Main // // javah -jni " , .class" JNIEXPORT jstring JNICALL Java_Main_returnValue (JNIEnv* env, jobject, jstring jvalue) { // jvalue, value String __gc* String __gc* value=env->GetStringUTFChars(jvalue,0); // SendValue, wrapper.cpp SendValue* t = new SendValue(); // val String __gc* String __gc* val = t->method(value); // String* const char* char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(val); jstring jval; // const char* jstring, jval=env->NewStringUTF(str2); // return jval; }
/* DO NOT EDIT THIS FILE - it is machine generated */ /* Header for class Main */ #ifndef _Included_Main #define _Included_Main #ifdef __cplusplus extern "C" { #endif /* * Class: Main * Method: returnValue * Signature: (Ljava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_Main_returnValue (JNIEnv *, jobject, jstring); #ifdef __cplusplus } #endif #endif
Source: https://habr.com/ru/post/92924/