开发者

How to send a JAVA object to C through JNi and assign the java object values to C Structure?

I have a JAVA function which makes a call to C function through JNI . My Java Function :

  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);

Now my C File which contains JNI stuff .. Actually i'm sending registerInfo object to JNI. Register Info class contains some char arrays. I want to assign the value of the char arrays to my C structure : 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;
   }
}

Now when i run the Code i get the error :

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)

I think i am making some error in conversions between string,char[] and objects assigning. Can you please help me. Thanks in advance.


check this link for char[]

jchar   NewCharArray
        GetCharArrayElements
        GetCharArrayRegion/SetCharArrayRegion
        ReleaseCharArrayElements

http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jnistring.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜