In writing a Java application, you can use C or CPP to write DLL functions, and then invoke in Java to implement a call to the local API, which, for business needs, invokes C # functions in Java for two days. So I studied, and found that we can use manage C + + to implement the call to C # functions. The specific methods are as follows:
The first is to write Java, using Javah to generate CPP header files. And then it's like the normal JNI writing process. Then in JNI's excuse function out calls the Manage C + + method. However, you first write the C # method and then generate the netmodule file. Select New Class Library, write the C # class and method, use console command
Csc/debug/t:module "Yourcsfilename.cs" generates netmodule files, in Manage C + +, we want to use this file to invoke the method.
Then write manage C + + files, where you need to reference <mscorlib.dll> library. As follows:
#using <mscorlib.dll>
#using "Yourfilename.netmodule"
using namespace System;
...
...
Here's a snippet of my Code:
#using <mscorlib.dll>
#using "Managewindow.netmodule"
using namespace System;
Public __gc class setwindowc{
Public
Cssetwindow::managewindow __gc *mw;//keyword __gc garbage collection
SETWINDOWC () {
Mw=new Cssetwindow::managewindow ();
}
The void Callsetwindow () {//inline function calls the C # Setwindow method.
Mw->setwindow ();
}
};
Then call the Manage C + + method in the interface function of JNI.
Jniexport void Jnicall java_com_efreda_sweet_jni_windowcontrol_setwindowontop (jnienv *env, Jobject obj) {
setwindowc* sw=new SETWINDOWC ();
Sw->callsetwindow ();
}
It should be noted that A/clr error may occur at compile time.
If it appears, the project properties, general, common language Runtime support selection for/clr:oldsyntax, note that must be oldsyntax. Otherwise the compilation is not available (because the __gc keyword is used).
Personal feeling there is little need to invoke the C # method with Java JNI. But since there is a legal call, then learn it.