3
我有一個JAVA函數,它通過JNI調用C函數。 我的Java功能:它包含JNI東西如何通過JNi將JAVA對象發送給C並將Java對象值分配給C結構?
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
文件..其實我送registerInfo對象JNI。 Register Info類包含一些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[]
與對象賦值之間的轉換中出錯。你能幫我麼。提前致謝。
能否請您提供一些代碼段用於上述提示WRT我的榜樣。 –
示例在該鏈接中給出 – Pratik
好的,謝謝。我想知道如果相同的例子(給定的鏈接)將工作,如果我從JAVA發送字符數組?也歡迎任何幫助將值分配給C中的struct。感謝你的寶貴時間。 –