extern "C"{
char* jstringTostring(JNIEnv* env, jstring jstr)
{
char* rtn = NULL;
if(!jstr)
return 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;
}
};
int mtAndroidJni::getCompanyName(char* pcDirName, int* piNameLen) {
JniMethodInfo methodInfo;
jstring pJstrDir;
XL_JNI_START(mtAndroidJni::getCompanyName);
if(JniHelper::getStaticMethodInfo(methodInfo,CLASS_NAME,"GetCompanyName", "()Ljava/lang/String;"))
{
pJstrDir = (jstring)methodInfo.env->CallStaticObjectMethod(methodInfo.classID,methodInfo.methodID);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
}
else
{
Log.e("mtAndroidJni::getVTNAME error");
return 0;
}
//判断JAVA端传入的数据长度是否大于数组
int iTempNum = methodInfo.env->GetStringLength(pJstrDir);
if (iTempNum > *piNameLen) {
*piNameLen = iTempNum;
methodInfo.env->DeleteLocalRef((jobject) pJstrDir);
return 0;
}
char* pcTempStr = jstringTostring(methodInfo.env, pJstrDir);
//const char* pcTempStr = methodInfo.env->GetStringUTFChars(pJstrDir, NULL);
strncpy(pcDirName, pcTempStr, *piNameLen);
char name[128] = {0};
UTF8ToGBK(pcDirName, strlen(pcDirName), name, sizeof(name));
Log.i("111111%s", name);
free(pcTempStr);
//methodInfo.env->ReleaseStringUTFChars(pJstrDir, pcTempStr);
methodInfo.env->DeleteLocalRef((jobject) pJstrDir);
XL_JNI_END(mtAndroidJni::getCompanyName);
return 1;
}
char* jstringTostring(JNIEnv* env, jstring jstr)
{
char* rtn = NULL;
if(!jstr)
return 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;
}
};
int mtAndroidJni::getCompanyName(char* pcDirName, int* piNameLen) {
JniMethodInfo methodInfo;
jstring pJstrDir;
XL_JNI_START(mtAndroidJni::getCompanyName);
if(JniHelper::getStaticMethodInfo(methodInfo,CLASS_NAME,"GetCompanyName", "()Ljava/lang/String;"))
{
pJstrDir = (jstring)methodInfo.env->CallStaticObjectMethod(methodInfo.classID,methodInfo.methodID);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
}
else
{
Log.e("mtAndroidJni::getVTNAME error");
return 0;
}
//判断JAVA端传入的数据长度是否大于数组
int iTempNum = methodInfo.env->GetStringLength(pJstrDir);
if (iTempNum > *piNameLen) {
*piNameLen = iTempNum;
methodInfo.env->DeleteLocalRef((jobject) pJstrDir);
return 0;
}
char* pcTempStr = jstringTostring(methodInfo.env, pJstrDir);
//const char* pcTempStr = methodInfo.env->GetStringUTFChars(pJstrDir, NULL);
strncpy(pcDirName, pcTempStr, *piNameLen);
char name[128] = {0};
UTF8ToGBK(pcDirName, strlen(pcDirName), name, sizeof(name));
Log.i("111111%s", name);
free(pcTempStr);
//methodInfo.env->ReleaseStringUTFChars(pJstrDir, pcTempStr);
methodInfo.env->DeleteLocalRef((jobject) pJstrDir);
XL_JNI_END(mtAndroidJni::getCompanyName);
return 1;
}