日期:2014-05-20  浏览次数:20759 次

JNI的一些问题、
我现在需要用java去调用现存的DLL里的几个函数。
函数原型如下“
unsigned char __stdcall piccrequest(unsigned char *serial)

unsigned char __stdcall piccreadex(unsigned char ctrlword,unsigned char *serial,unsigned char area,unsigned char keyA1B0,unsigned char *picckey,unsigned char *piccdata0_2)

下面是我JNI的声明:
	/**
 * @param serial
 * @return返回卡序列號
 */
public native String getCardSerial(char[] serial);

/**
 * @return讀取0~2塊數據
 */
public native String getCardData(char ctrlWord, char[] serial, char area,
char key, char[] picckey, char[] piccdata0_2);


对应的C端的代码:
typedef    unsigned char  (__stdcall * getCardNum)(unsigned char[]);
typedef    unsigned char  (__stdcall * getDeviceNum)(unsigned char[]);
typedef    unsigned char  (__stdcall * getCardData)(unsigned char,unsigned char[],unsigned char,unsigned char,unsigned char[],unsigned char[]);
HINSTANCE hModule=NULL;
getCardNum gcnum;
getDeviceNum gdnum;
getCardData gdata;/*
读取卡序列号
*/
JNIEXPORT jstring JNICALL Java_com_szzc_jni_JniApi_getCardSerial
(JNIEnv * env, jobject obj, jcharArray js){
hModule=LoadLibrary("OUR_MIFARE.dll"); 
   gcnum = (getCardNum)GetProcAddress(hModule, "piccrequest");
    //.....
    unsigned char * str = (unsigned char *)env->GetCharArrayElements(js,NULL);
    unsigned char cardNum = gcnum(str);
printf("the return value of func is %d\n",cardNum); 
printf("卡列号:\n");
unsigned char* p = str;
while(*p!='\0'){
    printf("%x\n",*p);
    p++;
}
return (jstring)cardNum;
}

JNIEXPORT jstring JNICALL Java_com_szzc_jni_JniApi_getCardData
(JNIEnv * env, jobject obj, jchar jc, jcharArray ja1, jchar jc2, jchar jc3, jcharArray ja2, jcharArray ja3){
hModule=LoadLibrary("OUR_MIFARE.dll"); 
gdata = (getCardData)GetProcAddress(hModule, "piccreadex");
unsigned char ctrlWord = (unsigned char)jc;
unsigned char area = (unsigned char)jc2;
unsigned char key=(unsigned char)jc3;
unsigned char * serial= (unsigned char *)env->GetCharArrayElements(ja1,NULL);
unsigned char * picckey= (unsigned char *)env->GetCharArrayElements(ja2,NULL);
unsigned char * piccdata= (unsigned char *)env->GetCharArrayElements(ja3,NULL);
unsigned char status = gdata(ctrlWord,serial,area,key,picckey,piccdata);
printf("%d\n",status);
cout << jc << endl;
cout << jc2 <<endl;
cout<< jc3 <<endl;
cout << "****" <<endl;
unsigned char* p = serial;
while(*p!='\0'){
    printf("%x\n",*p);
    p++;

jstring j=(jstring)"test";