Java JNI Call C # method __java

Source: Internet
Author: User
Tags mscorlib

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.