转自:http://www.cnblogs.com/liangwind/archive/2009/08/26/1925511.html
//jstring to char*
char* jstringTostring(JNIEnv* env, jstring jstr)
{
char* rtn = NULL;
jclass clsstring = env->FindClass("java/lang/String");
jstring strencode = env->NewStringUTF("utf-8");
jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");
jbyteArray barr= (jbyteArray)env->CallObjectMethod(jstr, mid, strencode);
jsize alen = env->GetArrayLength(barr);
jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE);
if (alen > 0)
{
rtn = (char*)malloc(alen + 1);
memcpy(rtn, ba, alen);
rtn[alen] = 0;
}
env->ReleaseByteArrayElements(barr, ba, 0);
return rtn;
}
//char* to jstring
jstring stoJstring(JNIEnv* env, const char* pat)
{
jclass strClass = env->FindClass("Ljava/lang/String;");
jmethodID ctorID = env->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V");
jbyteArray bytes = env->NewByteArray(strlen(pat));
env->SetByteArrayRegion(bytes, 0, strlen(pat), (jbyte*)pat);
jstring encoding = env->NewStringUTF("utf-8");
return (jstring)env->NewObject(strClass, ctorID, bytes, encoding);
}
//将jstring类型转换成windows类型
char* jstringToWindows( JNIEnv *env, jstring jstr )
{
int length = (env)->GetStringLength(jstr );
const jchar* jcstr = (env)->GetStringChars(jstr, 0 );
char* rtn = (char*)malloc( length*2+1 );
int size = 0;
size = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)jcstr, length, rtn,(length*2+1), NULL, NULL );
if( size <= 0 )
return NULL;
(env)->ReleaseStringChars(jstr, jcstr );
rtn[size] = 0;
return rtn;
}
//将windows类型转换成jstring类型
jstring WindowsTojstring( JNIEnv* env, char* str )
{
jstring rtn = 0;
int slen = strlen(str);
unsigned short * buffer = 0;
if( slen == 0 )
rtn = (env)->NewStringUTF(str );
else
{
int length = MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, NULL, 0 );
buffer = (unsigned short *)malloc( length*2 + 1 );
if( MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, (LPWSTR)buffer, length ) >0 )
rtn = (env)->NewString( (jchar*)buffer, length );
}
if( buffer )
free( buffer );
return rtn;
}
/*JNIEXPORT jstring JNICALL Java_test_cs_web_SWIFTAlianceCASmfTest_strcal
(JNIEnv *env, jclass obj, jstring jstr1, jstring jstr2)
{
jbyteArray bytes = 0;
jthrowable exc;
char *pszResult = NULL;
char *pszSTR1 = NULL;
char *pszSTR2 = NULL;
pszSTR1 = jstringTostring(env, jstr1);
pszSTR2 = jstringTostring(env, jstr2);
int nlen = sizeof(char)*(strlen(pszSTR1)+strlen(pszSTR2));
pszResult = (char*)malloc(nlen);
strcpy(pszResult, pszSTR1);
strcat(pszResult, pszSTR2);
jstring jstrRe = stoJstring(env, pszResult);
free(pszSTR1);
free(pszSTR2);
free(pszResult);
return(jstrRe);
}
*/
jni object的使用
每一个jni格式的dll中的object对应该java里面的一个类。
如下例有一个 ObjData类,类中有成员bData ,Len
public class ObjData {
public byte[] bData;
public int Len;
}
//------------------------jni获得传过来的Object类型的变量objDataIn--------
jclass clazz =(env)->FindClass("ObjData");
//从传进来的对象中取出byte[]
jfieldID byteData = (env)->GetFieldID(clazz,"bData","[B");
jbyteArray pDataIn = (jbyteArray) (env)->GetObjectField(objDataIn, byteData);
jsize theArrayLeng = env->GetArrayLength(pDataIn);
//byte[]转为BYTE[]
jbyte * arrayBody = env->GetByteArrayElements(pDataIn,0);
BYTE * jDataIn = (BYTE *)arrayBody;
//将BYTE数组转为jarray
jbyte* byte = (jbyte*)jDataOut;
jbyteArray jarray = env->NewByteArray(theArrayLeng);
env->SetByteArrayRegion(jarray, 0, theArrayLeng, byte);
//给每一个实例的变量付值
(env)->SetObjectField(objDataIn,byteData,jarray);
(env)->SetIntField(objDataIn,pDataInLen,jDataInLen);
(env)->ReleaseByteArrayElements(pDataIn, arrayBody, 0);