codefans

导航

<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

统计

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

程序设计链接

搜索

最新评论

阅读排行榜

评论排行榜

我的一个JNI的例子。

例证一, 从c语言向JAVA传递类

/*
 * Class:     sms_SendSMS
 * Method:    ReceiveAllMessage
 * Signature: (Lsms/SM_PARAM;)I
 */
JNIEXPORT jobjectArray JNICALL Java_sms_SendSMS_ReceiveAllMessage
  (JNIEnv *env, jclass)
{
 jobjectArray array=NULL; //定义数组对象
 const int LEN = 30;
 SM_PARAM *sm_param_temp=new SM_PARAM[LEN];
 for(int j =0; j < LEN; j++)
 {
  sm_param_temp[j].TP_UD[0]='\0';
 }
 int temp=ReceiveAllMessage(sm_param_temp);
 if(temp <= 0)
 {
  return array;
 }
 
 jclass cls = (env)->FindClass("sms/SM_PARAM");
 array = env-> NewObjectArray(temp, cls, NULL);
 for(int i=0; i< temp; i++)//,sm_param_temp++
 {
  if (cls != 0)
  {
   jmethodID mid = env->GetMethodID(cls,NULL,"()V");
   if(mid != 0)
   {
    jobject obj = env->NewObject(cls,mid,0);

//   env->FindClass("java/lang/String")
//   jmethodID mid1 = env->GetMethodID(cls,"setContent","(Ljava/lang/String;)V");

    jstring jstr = 0;

 
    jfieldID TPA = env->GetFieldID(cls,"TPA","Ljava/lang/String;");
    jstr = env->NewStringUTF(sm_param_temp->TPA);
    env-> SetObjectField(obj, TPA, jstr);
    env->ReleaseStringUTFChars(jstr, sm_param_temp->TPA);
    
    jfieldID TP_SCTS = env->GetFieldID(cls,"TP_SCTS","Ljava/lang/String;");
    jstr = env->NewStringUTF(sm_param_temp->TP_SCTS);
    env-> SetObjectField(obj, TP_SCTS, jstr);
    env->ReleaseStringUTFChars(jstr, sm_param_temp->TP_SCTS);
    
    jfieldID TP_UD = env->GetFieldID(cls,"TP_UD","[B");    
    jbyteArray tpArray = env->NewByteArray(161);
    for(int k=0; k<160; k++) 
    {
     const signed char ch = sm_param_temp->TP_UD[k];
     if(ch == '\0'){
      break;
     }
     env->SetByteArrayRegion(tpArray, k, 1, &ch);
    }
    const signed char end = '\0';
    env->SetByteArrayRegion(tpArray, 160, 1, &end);

    env->SetObjectField(obj, TP_UD, tpArray);
//    env->ReleaseByteArrayElements(tpArray, sm_param_temp->TP_UD, mode);

    jchar ch = sm_param_temp->index;
    jfieldID index = env->GetFieldID(cls,"index","C");
    env-> SetCharField(obj, index, ch);

    env->SetObjectArrayElement(array, i, obj);
   }

  }

 }

 delete[] sm_param_temp;

 return array;
}


例证二, 从JAVA向c语言传递类

/*
 * Class:     sms_SendSMS
 * Method:    EncodePdu
 * Signature: (Lsms/SM_PARAM;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_sms_SendSMS_EncodePdu
  (JNIEnv *env, jclass, jobject src)
{
 SM_PARAM *sm_param_temp=new SM_PARAM;

 jclass cls = (env)->GetObjectClass(src);
 if (cls != 0)
 {
  jfieldID TPA = env->GetFieldID(cls,"TPA","Ljava/lang/String;");
  jstring jstr = (jstring) env->GetObjectField(src, TPA);
  const char *temp;
  jboolean copy='1';
  temp = env->GetStringUTFChars(jstr, &copy);
  strcpy(sm_param_temp->TPA, temp);  
//  env->ReleaseStringUTFChars(jstr, sm_param_temp->TPA);
  
  jfieldID TP_SCTS = env->GetFieldID(cls,"TP_SCTS","Ljava/lang/String;");
  jstr = (jstring)env-> GetObjectField(src, TP_SCTS);
  temp = env->GetStringUTFChars(jstr, &copy);
  strcpy(sm_param_temp->TP_SCTS, temp);  
  
  jfieldID SCA = env->GetFieldID(cls,"SCA","Ljava/lang/String;");
  jstr = (jstring)env-> GetObjectField(src, SCA);
  temp = env->GetStringUTFChars(jstr, &copy);
  strcpy(sm_param_temp->SCA, temp);  
  

  jfieldID TP_UD = env->GetFieldID(cls,"TP_UD","[B");    
  jbyteArray tpArray = (jbyteArray)env-> GetObjectField(src, TP_UD);
  if(tpArray != NULL)
  {
   for(int k=0; k<env->GetArrayLength(tpArray); k++) 
   {
    sm_param_temp->TP_UD[k]= *(env->GetByteArrayElements(tpArray, &copy));
   }
  }
  sm_param_temp->TP_UD[160] = '\0';
//    env->ReleaseByteArrayElements(tpArray, sm_param_temp->TP_UD, mode);  

  jchar ch = 0;
  jfieldID index = env->GetFieldID(cls,"index","C");
  sm_param_temp->index = (char)env->GetCharField(src, index);

  jfieldID TP_PID = env->GetFieldID(cls,"TP_PID","C");
  sm_param_temp->TP_PID = (char)env->GetCharField(src, TP_PID);

  jfieldID TP_DCS = env->GetFieldID(cls,"TP_DCS","C");
  sm_param_temp->TP_DCS = (char)env->GetCharField(src, TP_DCS);  
 }
 
 char *dst = new char[400];
 int rValue = EncodePdu(sm_param_temp, dst);

 jstring jstr = 0;
 jstr = env->NewStringUTF(dst);

 delete sm_param_temp;
 delete[] dst;
 
 return jstr;
}

posted on 2005-11-06 15:03 春雷的博客 阅读(847) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: