- 源文件:
public class Info { public String Name; public String Time; }
public class Action { public native byte[] genReq(int hash_algorithm, byte[] data); public native boolean verifyRes(int hash_algorithm, byte[] resp); public native Info getInfo(byte[] resp); public static Action getInstance() { System.loadLibrary("the_dll"); Action a = new Action(); return a; } }
- 頭文件:
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class Action */ #ifndef _Included_Action #define _Included_Action #ifdef __cplusplus extern "C" { #endif /* * Class: Action * Method: genReq * Signature: (I)[B */ JNIEXPORT jbyteArray JNICALL Java_Action_genReq (JNIEnv *, jobject, jint); /* * Class: Action * Method: verifyRes * Signature: (I[B)Z */ JNIEXPORT jboolean JNICALL Java_Action_verifyRes (JNIEnv *, jobject, jint, jbyteArray); /* * Class: Action * Method: getInfo * Signature: ([B)LInfo; */ JNIEXPORT jobject JNICALL Java_Action_getInfo (JNIEnv *, jobject, jbyteArray); #ifdef __cplusplus } #endif #endif
- C++文件:
#include "Action.h" #include "jni.h" #include <stdio.h> #include <stdlib.h> #include <string.h> JNIEXPORT jbyteArray JNICALL Java_Action_genReq (JNIEnv * env, jobject obj, jint hash_algorithm, jbyteArray data) { jbyteArray result= NULL; void *TS_handle; int error_code; unsigned char *request; unsigned int request_len; unsigned char* raw_data = (unsigned char*)env->GetByteArrayElements(data, 0); unsigned int raw_data_len =env->GetArrayLength(data); ...... if ( !( request = (unsigned char *)malloc(request_len) ) ) { ...... } ...... result = env->NewByteArray(request_len); env->SetByteArrayRegion(result,0, request_len, (jbyte*)request); ...... return result; } JNIEXPORT jboolean JNICALL Java_Action_verifyRes (JNIEnv * env, jobject obj, jint hash_algorithm, jbyteArray resp) { jboolean result = 0; void *TS_handle; int error_code; unsigned int status_code, info_code_flag, info_code; ...... unsigned char* response_ASN1_encode = (unsigned char*)env->GetByteArrayElements(resp, 0); unsigned int response_ASN1_encode_len =env->GetArrayLength(resp); ...... result = 1; ...... return result; } JNIEXPORT jobject JNICALL Java_Action_getInfo (JNIEnv * env, jobject obj, jbyteArray resp) { jobject result= NULL; jclass info = env->FindClass("the/path/Info"); jobject ainfo = env->AllocObject(info); void *A_handle; int error_code; unsigned int name_len, time_len; unsigned char *name, *time; ...... unsigned char* response_ASN1_encode = (unsigned char*)env->GetByteArrayElements(resp, 0); unsigned int response_ASN1_encode_len =env->GetArrayLength(resp); ...... if (name_len) { if ( !(name=(unsigned char *)malloc(name_len)) ) { ...... } ...... jclass strClass = env->FindClass("Ljava/lang/String;"); jmethodID ctorID = env->GetMethodID(strClass, "<init>", "([B)V"); jbyteArray bytes = env->NewByteArray(name_len); env->SetByteArrayRegion(bytes, 0, name_len, (jbyte*)name); jstring encoding = env->NewStringUTF("utf-8"); jobject str = env->NewObject(strClass, ctorID, bytes, encoding); jfieldID fld_name = env->GetFieldID(info, "Name", "Ljava/lang/String;"); env->SetObjectField(info, fld_name, str); free(name); } else{ } if (time_len) { if ( !(time=(unsigned char *)malloc(time_len)) ) { ...... } ...... jclass strClass = env->FindClass("Ljava/lang/String;"); jmethodID ctorID = env->GetMethodID(strClass, "<init>", "([B)V"); jbyteArray bytes = env->NewByteArray(time_len); env->SetByteArrayRegion(bytes, 0, time_len, (jbyte*)time); jstring encoding = env->NewStringUTF("utf-8"); jobject str = env->NewObject(strClass, ctorID, bytes, encoding); jfieldID fld_time = env->GetFieldID(info, "Time", "Ljava/lang/String;"); env->SetObjectField(info, fld_time, str); free(time); } else{ } result = info; ...... return result; }
- 調用方式:
...... Action a = Action.getInstance(); req = a.genReq(hash_algorithm, input.getBytes("utf-8")); boolean isOK = a.verifyRes(hash_algorithm, resp); Info Info = a.getInfo(resp); ......
JNI的使用
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.