简体   繁体   English

JNI 将参数传递给 c++ 的方法

[英]JNI pass parameters to method of c++

I have a c++ file myCppTest.cpp which has method我有一个 c++ 文件 myCppTest.cpp 它有方法

int myFunction(int argv, char **argc) {
}

and a Java native method in myClass.java和 myClass.java 中的 Java 本机方法

public native int myFunction (int argv, char[][] argc);

After generate the header file using javah -jni myClass, i have the header使用 javah -jni myClass 生成 header 文件后,我有 header

JNIEXPORT jint JNICALL Java_JPTokenizer_init
  (JNIEnv *, jobject, jint, jobjectArray);

In my myClass.cpp, I defined在我的 myClass.cpp 中,我定义了

JNIEXPORT jint JNICALL Java_JPTokenizer_init
  (JNIEnv *env, jobject obj, jint argv, jobjectArray argc) {
        //need to call int myFunction(int argv, char **argc) in myCppTest.cpp 
}

How could I pass the arguments "jint argv, jobjectArray argc" to "int argv, char **argc", thanks.我怎么能将 arguments “jint argv,jobjectArray argc”传递给“int argv,char **argc”,谢谢。

EDIT:编辑:

I THINK I MADE A MISTAKE我想我犯了一个错误

The Java native method in myClass.java should be myClass.java 中的 Java 本机方法应该是

public native int init (int argv, char[][] argc);公共本机 int init (int argv, char[][] argc);

So there is所以有

JNIEXPORT jint JNICALL Java_myClass_init (JNIEnv *, jobject, jint, jobjectArray); JNIEXPORT jint JNICALL Java_myClass_init (JNIEnv *, jobject, jint, jobjectArray);

generated after javah. javah之后生成的。 And in myClass.cpp, i have在 myClass.cpp 中,我有

JNIEXPORT jint JNICALL Java_myClass_init (JNIEnv *env, jobject obj, jint argv, jobjectArray argc) { //need to call int myFunction(int argv, char **argc) in myCppTest.cpp } JNIEXPORT jint JNICALL Java_myClass_init (JNIEnv *env, jobject obj, jint argv, jobjectArray argc) { //需要在 myCppTest.cpp 中调用 int myFunction(int argv, char **argc) }

You can create an object of the class and invoke method just like any other C++ code.您可以创建 class 的 object 并像任何其他 C++ 代码一样调用方法。

JNIEXPORT jint JNICALL Java_JPTokenizer_init
    (JNIEnv *env, jobject obj, jint argv, jobjectArray argc) {
      myClass obj;  //create object of the class you want
      obj.myFunction((int) argv, (char *) &argc); //call the method from that object
}

There is no direct mapping between Java objects and C++ primitives, so you will have to convert the arguments that are passed by the Java runtime environment, and then call your function. There is no direct mapping between Java objects and C++ primitives, so you will have to convert the arguments that are passed by the Java runtime environment, and then call your function.

Java will call Java_JPTokenizer_init -- this is where you perform your conversion and invoke your "plain old" C++ function. Java 将调用Java_JPTokenizer_init - 这是您执行转换并调用“普通旧”C++ function 的地方。

To convert the array of strings, you will first need to access the array, then the individual strings.要转换字符串数组,您首先需要访问数组,然后是单个字符串。

Usually when I need to access a native library I use JNA instead of JNI.通常当我需要访问本机库时,我使用JNA而不是 JNI。 Normally JNA is much easier to set up than JNI so you might want to give it a try.通常,JNA 比 JNI 更容易设置,因此您可能想尝试一下。

Cheers, Felipe干杯,费利佩

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM