从通过JNI C ++获取Java数组 [英] Get java array from c++ via JNI

查看:185
本文介绍了从通过JNI C ++获取Java数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想从C获得resultNative ++
但resultNative是Java。
任何人可以请告诉我怎么做,通过JNI?我不是那么熟悉C ++,并已谷歌搜索了半天却没有找到任何答案。非常感谢。
该nativeResult这里是在Java中。

 公共字符串[] searchDatabase()
{
    字符串结果[] =新的String [6];
    字符串nativeResult [] =新的String [2];
    TNAME = this.getTargetName();    结果= da.SearchKorea(TNAME,CTX);
    Af - Ag型=结果[2];
    tTrans =导致[3];
    的TImage =结果[4];
    tFave =结果[5];    nativeResult [1] =的TImage + - + TNAME;
    nativeResult [2] = tTrans +[+ Af - Ag型+];
    返回nativeResult;
}


解决方案

首先,你必须去的类,它提供的方法的引用。比方说,你的类被称为MyClass的,它是在包页。你得到这样的参考类:

  //调用本机函数时,你得到的JNIEnv *指针。
JCLASS MyClass的= env->的findClass(P / MyClass的);

或者,如果你有到Java对象的引用,那么你也可以使用 GetObjectClass

  JCLASS MyClass的= env-> GetObjectClass(JAVAOBJECT);

然后,你必须让你想通过提供方法的名称和描述方法的签名字符串来调用方法的ID。

 ()【JAVA / LANG /字符串;描述的方法并不期待任何参数并且返回一个字符串数组。
jmethodID放在methodID = env->的GetMethodID(MyClass的searchDatabase,()【JAVA / LANG /字符串;);

然后,你必须调用与的JNIEnv :: CallObjectMethod 的方法,在这里你有中借鉴传递到Java对象。

  jobjectarray串= env-> CallObjectMethod(JAVAOBJECT,放在methodID);

然后你就可以得到数组的元素与 GetObjectArrayElement

  INT指数= 0;
的jstring字符串= env-> GetObjectArrayElement(字符串,指数);

然后你就可以从中获得本地字符串以不同的方式。

 为const char * nativeChars = env-> GetStringUTFChars(字符串,nullptr);

您可以找到有关JNI 这里了解更多信息以及有关JNI类型签名细节这里

So, I want to obtain resultNative from c++ but resultNative is in Java. can anybody please show me how to do that via JNI? I am not so familiar with c++ and have searched google for a long time but didn't find any answer. thank you so much. the nativeResult is here in Java.

public String[] searchDatabase()
{
    String result[]=new String[6];
    String nativeResult[]=new String[2];
    tName=this.getTargetName();

    result=da.SearchKorea(tName,ctx);
    tType=result[2];
    tTrans=result[3];
    tImage=result[4];
    tFave=result[5];

    nativeResult[1]= tImage+" - "+tName;
    nativeResult[2]= tTrans+" ["+tType+"]";
    return nativeResult;
}

解决方案

First you have to get a reference to the class providing the method. Let's say your class is called MyClass and it is in package p. You get the reference to the class like this:

// You get the JNIEnv* pointer when calling a native function.
jclass myClass = env->FindClass("p/MyClass");

Or if you have a reference to the java object then you can also use GetObjectClass:

jclass myClass = env->GetObjectClass(javaObject);

Then you have to get the ID of the method you'd like to call by providing the name of the method and a string describing the signature of the method.

"()[java/lang/String;" describes a method expecting no arguments and returning a string array.
jmethodID methodID = env->GetMethodID(myClass , "searchDatabase", "()[java/lang/String;");

Then you have to call the method with JNIEnv::CallObjectMethod, and here you have to pass the referenc to the java object.

jobjectarray strings = env->CallObjectMethod(javaObject, methodID);

Then you can get an element of the array with GetObjectArrayElement.

int index = 0;
jstring string = env->GetObjectArrayElement(strings, index);

And then you can get the native string from it in various ways.

const char* nativeChars = env->GetStringUTFChars(string, nullptr);

You can find more information about JNI here, and details about JNI type signatures here.

这篇关于从通过JNI C ++获取Java数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆