Skip to content

Commit 15d8cc2

Browse files
committed
返回值的获取
1 parent c855ec8 commit 15d8cc2

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

callc/CallC.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.durongze.jni;
22

33
public class CallC{
4-
public native void CInterface(String[] name, int[] age, float[] height, int num);
4+
public native String[] CInterface(String[] name, int[] age, float[] height, int num);
55
static {
66
System.loadLibrary("CLibrary");
77
}
@@ -11,11 +11,14 @@ public static void main(String[] args){
1111
String[] name = new String[num]; // ("durongze", "duyongze");
1212
int[] age = new int[num]; // (28, 30);
1313
float[] height = new float[num]; // (177, 188);
14-
for (; idx < num; ++idx){
15-
name[idx] = Integer.toString(idx);
16-
age[idx] = idx;
17-
height[idx] = idx + num;
14+
for (idx = 0; idx < num; ++idx){
15+
name[idx] = Integer.toString(100 + idx);
16+
age[idx] = 200 + idx;
17+
height[idx] = 300 + idx;
18+
}
19+
String[] vals = new CallC().CInterface(name, age, height, num);
20+
for (idx = 0; idx < vals.length; ++idx){
21+
System.out.println(vals[idx]);
1822
}
19-
new CallC().CInterface(name, age, height, num);
2023
}
2124
};

callc/com_durongze_jni_CallC.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,30 @@ extern "C" {
1212
/*
1313
* Class: com_durongze_jni_CallC
1414
* Method: CInterface
15-
* Signature: ([Ljava/lang/String;[I[FI)V
15+
* Signature: ([Ljava/lang/String;[I[FI)[Ljava/lang/String;
1616
*/
17-
JNIEXPORT void JNICALL Java_com_durongze_jni_CallC_CInterface
17+
JNIEXPORT jobjectArray JNICALL Java_com_durongze_jni_CallC_CInterface
1818
(JNIEnv *env, jobject, jobjectArray names, jintArray ages, jfloatArray heights, jint num)
1919
{
20+
jobjectArray result;
21+
jclass intArrCls = env->FindClass("java/lang/String");
22+
result = env->NewObjectArray(num, intArrCls, NULL);
2023
int idx = 0;
2124
jint *as = env->GetIntArrayElements(ages, 0);
25+
int asLen = env->GetArrayLength(ages);
2226
jfloat *hs = env->GetFloatArrayElements(heights, 0);
23-
for (; idx < num; ++idx) {
27+
int hsLen = env->GetArrayLength(heights);
28+
for (idx = 0; idx < num; ++idx) {
2429
jstring ns = static_cast<jstring>(env->GetObjectArrayElement(names, idx));
2530
const char* pns = env->GetStringUTFChars(ns,0);
2631
printf("%s: names[%d]:%s, ages[%d]:%d, heights[%d]:%lf\n",
2732
__FUNCTION__, idx, pns, idx, as[idx], idx, hs[idx]);
2833
env->ReleaseStringUTFChars(ns, 0);
34+
env->SetObjectArrayElement(result, idx, ns);
2935
}
3036
env->ReleaseIntArrayElements(ages, as, 0);
3137
env->ReleaseFloatArrayElements(heights, hs, 0);
38+
return result;
3239
}
3340

3441
#ifdef __cplusplus

callc/com_durongze_jni_CallC.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)