我有一个JAVA函数,通过JNI调用C函数。 我的Java函数:
char details= 'd';
char reg_code='r';
char[] reg_chal ={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x00,0x01,0x02,0x03,0x04,0x05,0x06} ;
char[] aid = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x00,0x01,0x02,0x03,0x04,0x05,0x06};
RegisterInfo regInfo = new RegisterInfo( reg_chal,aid);
res = nativeLib.reg(regInfo, details, reg_code);
现在我的C
文件包含JNI内容..实际上我正在向JNI发送registerInfo对象。 Register Info类包含一些char数组。我想将char数组的值赋给我的C
结构:reg_info_t:
#define DEBUG_TAG "NDK_NativeLib"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, __VA_ARGS__)
typedef struct{
unsigned char reg_chal[12];
unsigned char aid[12];
}reg_info_t;
JNIEXPORT jint JNICALL Java_com_marakana_NativeLib_reg(JNIEnv *env,jclass obj,jobject info ,jchar details,jchar reg_code)
{
reg_info_t regInfo;
jclass clazz;
jfieldID fid;
jmethodID mid;
jint status =0;
LOGD("NDK:LC: [%s]", "JNI Function call started");
clazz = (*env)->GetObjectClass(env, info);
if (0 == clazz)
{
printf("GetObjectClass returned 0\n");
return(-1);
}
jstring reg_chal= (*env)->NewStringUTF(env,regInfo.reg_chal);
fid = (*env)->GetFieldID(env,clazz,"reg_chal","Ljava/lang/String;");
(*env)->SetObjectField(env,info,fid,reg_chal);
LOGD("NDK:LC: [%s]", reg_chal);
return status;
}
RegisterInfo.java
public class RegisterInfo {
private char[] reg_chal ;
private char[] aid;
public RegisterInfo(char[] reg_chal,char[] aid) {
super();
this.reg_chal = reg_chal;
this.aid = aid;
}
}
现在,当我运行代码时,我收到错误:
09-14 10:14:19.536: WARN/dalvikvm(666): **JNI WARNING: illegal start byte 0xf0**
09-14 10:14:19.536: WARN/dalvikvm(666): string: '�ѯ$ԯ�'
09-14 10:14:19.536: WARN/dalvikvm(666): in Lcom/marakana/NativeLib;.reg (Lcom/marakana/RegisterInfo;CC)I (NewStringUTF)
我认为我在string
,char[]
和对象分配之间的转换中出现了一些错误。你能帮我么。提前谢谢。
答案 0 :(得分:1)
检查此链接是否为char []
jchar NewCharArray
GetCharArrayElements
GetCharArrayRegion/SetCharArrayRegion
ReleaseCharArrayElements
http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jnistring.html