[英]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
我想我犯了一个错误
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.要转换字符串数组,您首先需要访问数组,然后是单个字符串。
For array access, see http://java.sun.com/docs/books/jni/html/objtypes.html#5279 .有关数组访问,请参阅http://java.sun.com/docs/books/jni/html/objtypes.html#5279 。
For string access, see http://java.sun.com/docs/books/jni/html/objtypes.html#4001 .对于字符串访问,请参阅http://java.sun.com/docs/books/jni/html/objtypes.html#4001 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.