【问题标题】:JNI passing a string from C to javaJNI将字符串从C传递到java
【发布时间】:2012-07-08 00:56:12
【问题描述】:

我正在尝试将一个字符串从 C 传递给 java,并且我正在重启并出现以下跟踪,有人可以帮助我了解如何解决这个问题吗?

PC: 2cf57c7c (__GI_strlen+0xc glibc-2.4/string/strlen.c:42) RA: 2cf202a0 (vfprintf+0x42c0 glibc-2.4/stdio-common/vfprintf.c:1549)

我的 JNI 代码如下所示:

JNIEXPORT jstring JNICALL xxx_nativeGetParentName
  (JNIEnv *env, jobject obj, jstring childName)
{
    log("nativeGetParentName entered\n");
    char *name; 
    Node* parentName = NULL;
    jstring jstr = NULL;

    name = (char *)(*env)->GetStringUTFChars(env, childName, NULL);
    if (name == NULL)
        return NULL;
    log("about to call mpe_hnGetParentName\n");
    int retCode = mpe_GetParentName(name,&parentName);  // Call to the C function which holds the implementation 
    (*env)->ReleaseStringUTFChars(env,childName,name);

    if (retCode != 0 ) {
       log("mpe_GetParentName called with return code=%d\n", retCode);
       return NULL;
    }

    if(parentName[0] != NULL) {
        jstr= (*env)->NewStringUTF(env, parentName[0]); // Hitting the reboot exactly here!
        log("getting ParentName Succeded=%s\n", jstr);
        free(parentUuid);
    }

    return jstr;
}

C 函数调用的原型如下:

i32 GetParentName(Node childName, Node **parentName);

节点本质上是一个字符数组:

typedef char[] Node;

我已成功从 C 方法获取 parentName,但是当我尝试映射到 JString 时,我正在重新启动。

提前致谢!

【问题讨论】:

    标签: java c java-native-interface type-conversion


    【解决方案1】:

    您的第二条日志消息很可能是问题所在:

    log("getting ParentName Succeded=%s\n", jstr);
    

    jstrjstring 类型,它是一个指向结构的指针。它不是一个可以作为%s 格式表达式的有效参数传递的字符串。

    【讨论】:

    • 非常感谢!!我太天真了..这就是原因..重启是固定的:)祝你有美好的一天!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    • 2011-09-08
    • 2012-10-06
    • 2012-10-06
    • 1970-01-01
    相关资源
    最近更新 更多