3

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.

1 Answer 1

1

check this link for char[]

jchar   NewCharArray
        GetCharArrayElements
        GetCharArrayRegion/SetCharArrayRegion
        ReleaseCharArrayElements

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

Sign up to request clarification or add additional context in comments.

3 Comments

Can you please provide some code snippets for the above mentioned hints wrt my example.
examples are given in that link
Ok Thanks. I want to know that if the same example(given link) would work if i'm sending char array from JAVA? Also any help on assigning values to struct in C is welcome. Thanks again for your time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.