⬆️ ⬇️

C # to Java (C # dll call from Java)

Good day!

Having tortured a little Habr, the search did not find such topics, in connection with which he created his own.



Some lyrics:

We have a Sharpe library, in which the methods necessary for the work lie. It is necessary to use these methods from the Java program.



Example one



First, I'll show you a simple example.

We have a primitive dll on sharpe, let's call it SharpClass:



public class CSharpHelloWorld { public CSharpHelloWorld() { } public void displayHelloWorld() { Console.WriteLine("Hello World From C#!"); } } 


')

We will assemble this class in the assembly:

Open the command line, and write this command (you must first go to the directory with our SharpClass.cs file): csc \ t: module SharpClass.cs . If the command does not work, then before launching it, you need to execute the bat file, I have it here - C: \ Program Files \ Microsoft Visual Studio 9 \ VC \ bin \ vcvars32.bat

After performing this procedure, we get the file SharpClass.netmodule, located in the same folder as the source.



For communication between .NET and JVM, a wrapper is needed (a wrapper for getting unmanaged code). Create a wrapper with c ++ using the java library jni.h and the Microsoft library mscorlib.dll

Create a wrapper with ++:



1.cpp

 #using <mscorlib.dll> #using "SharpClass.netmodule" using namespace System; __gc class HelloWorldC { public: CSharpHelloWorld __gc *t; HelloWorldC() { t = new CSharpHelloWorld(); } void method() { t -> displayHelloWorld(); } }; 




After writing this code, you must specify a link to mscorlib.dll. To do this, go to the project settings and first find General, where in the Common Language Runtime Support section we select Common Language Runtime Support, Old Syntax (/ clr: oldSyntax), then click References (links) and add a new one - in the .NET tab we will find the required dll (it was in VS2005).



2.cpp

 #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(); } 




1.h

 JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld (JNIEnv *, jobject); 




And the last header is generated using Java from the command line by running the command - javah -jni "class name without quotes and without .class" (it is naturally necessary to create it after compiling Java program, otherwise the class itself will not appear). In the files 1.h and 2.cpp, the code is also taken from the last header.



HelloWorld.h

 #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 




After that, the entire c ++ project is built in dll. Let's call it HelloWorld.dll.



Now go to Java. Here is a simple code to call our HelloWorld.dll:

 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(); } } 




That's all ready. Well, our SharpClass.netmodule also needs to be moved to the folder with the loaded sishna dll.



Example two



In the first example, a simple case was considered simply with text output to the screen. Now consider the case when a function in C # takes parameters and returns a value.

C # class:

 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; } } 




Java application using this class:

 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); } } 




After that we create a header for c ++ using the command described in the first example.

Next, create a wrapper with ++ that drags variables from one end to the other :). There will be 3 files - wrapper.cpp, CSharpToJava.cpp, Main.h (the latter is obtained by executing the javah -jni command on the java class)



wrapper.cpp

 #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)); } }; 




CSharpToJava.cpp

 #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; } 




Main.h

 /* 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 




Conclusion



Well, that's all. This code was written and tested 2 years ago, and here all the hands did not reach to write. When I dealt with this task, I tried to ask for help in one forum . While he asked for help, he figured out a little and wrote his decision.



useful links



Source: https://habr.com/ru/post/92924/



All Articles