[英]JNI passing a string from C to java
I'm trying to pass a string from C to java and I'm hitting a reboot with the following trace, can someone help me understand how to fix this? 我正在尝试将字符串从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)
My JNI code looks like this: 我的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;
}
The prototype of the C function calling looks like this: C函数调用的原型如下所示:
i32 GetParentName(Node childName, Node **parentName);
The node is essentially a character array: 该节点实质上是一个字符数组:
typedef char[] Node;
I'm successfully getting the parentName from the C method, but when I'm trying to map to JString I am hitting a reboot. 我已经从C方法成功获取了parentName,但是当我尝试映射到JString时,我遇到了重新启动的情况。
Thanks in advance! 提前致谢!
Your second log message is most likely the problem: 您的第二条日志消息很可能是该问题:
log("getting ParentName Succeded=%s\n", jstr);
jstr
is of type jstring
, which is a pointer to a struct. jstr
类型为jstring
,它是指向结构的指针。 It's not a string that you can pass as a valid argument for the %s
format expression. 您不能将其作为
%s
格式表达式的有效参数传递的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.