1.编写Java类
public class UserPwdInfo {
public int m_wPwdIndex;
public String m_strPwd;
}
public class UserPwdList {
public int m_wPwdTotal;
public UserPwdInfo[] m_PwdList;
}
2.C++ 结构体定义
typedef struct _tagUserPwd
{
WORD m_wPwdIndex;
char m_strPwd[200];//chentr 20130604 密钥从50改到200
}USERPWD, *PUSERPWD;
typedef struct _tagUserPwdList
{
WORD m_wPwdTotal;
_tagUserPwd m_PwdList[256];
}USERPWDLIST, *PUSERPWDLIST;
3.java api
public static native void passJava2Native(UserPwdList userpwdList);
4.C++ 实现接口
/*
* Class: com_TiprayAPI_cpp_TiprayAPI
* Method: passJava2Native
* Signature: (Lcom/TiprayAPI/cpp/UserPwdList;)V
*/
JNIEXPORT void JNICALL Java_com_TiprayAPI_cpp_TiprayAPI_passJava2Native
(JNIEnv* env, jclass clazz, jobject obj)
{
CString szMSG="";
szMSG.Format("[LdTiprayAPI]call passJava2Native!");
OutputDebugString(szMSG);
jclass jcRec = (env)->FindClass("com/TiprayAPI/cpp/UserPwdList");
jfieldID jfPwdTotal = (env)->GetFieldID( jcRec, "m_wPwdTotal", "I");
//USERPWDLIST m_PwdList[]
jfieldID jfUserPwdarrays = env->GetFieldID(jcRec, "m_PwdList", "[Lcom/TiprayAPI/cpp/UserPwdInfo;");
int m_wPwdTotal = (env)->GetIntField(obj, jfPwdTotal);
szMSG.Format("[LdTiprayAPI]m_wPwdTotal: %d",m_wPwdTotal);
OutputDebugString(szMSG);
jobjectArray jobj_arr = (jobjectArray)env->GetObjectField(obj,jfUserPwdarrays);
for(int i=0;i< m_wPwdTotal; i++)
{
jobject jobj = env->GetObjectArrayElement(jobj_arr,i);
jclass clazz_father = env->GetObjectClass(jobj);
jfieldID jfPwdIndex = (env)->GetFieldID( clazz_father, "m_wPwdIndex", "I");
int m_wPwdIndex = (env)->GetIntField(jobj, jfPwdIndex);
szMSG.Format("[LdTiprayAPI]m_wPwdIndex: %d",m_wPwdIndex);
OutputDebugString(szMSG);
jfieldID jfStrpwd = (env)->GetFieldID(clazz_father, "m_strPwd","Ljava/lang/String;");
jstring m_strPwd = (jstring)(env)->GetObjectField( jobj, jfStrpwd);
//convert jstring to char
char* charPwd = (char*) (env)->GetStringUTFChars(m_strPwd, 0);
szMSG.Format("[LdTiprayAPI]m_strPwd: %s m_strPwd size : %d",charPwd,strlen(charPwd));
OutputDebugString(szMSG);
}
}