2
我使用JNI将ArrayList传递给C++。我想将其转换为LPWSTR *类型。但我收到arraylist作为jobject。我该如何转换?如何使用jni将Java中的Arraylist转换为c中的lpwstr
我使用JNI将ArrayList传递给C++。我想将其转换为LPWSTR *类型。但我收到arraylist作为jobject。我该如何转换?如何使用jni将Java中的Arraylist转换为c中的lpwstr
让我们开始吧。我不确定一些转换,希望其他人能够提供帮助。
您有一个对象。获取JNI的方法并调用它们。这很简单。
以下代码示例可能有助于开始。
// parameter
jobject YourJObjectRepresentingArrayList;
// I suppose you have the JNIEnv somehow
JNIEnv* env;
// use the Array list
ArrayList_class = env->FindClass("java/util/ArrayList");
// to conver jobject to jstring
jmethodID caster = env->GetMethodID(ArrayList_class, "toString", "()Ljava/lang/String;");
// get two methods
Get_method = env->GetMethodID(ArrayList_class, "get", "(I)Ljava/lang/Object");
Size_method = env->GetMethodID(ArrayList_class, "size", "()I");
// call java.lang.ArrayList.get()
int NumElts = env->CallIntMethod(YourJObjectRepresentingArrayList, ArrayList_class, Size_method);
// allocate output array
LPWSTR* Out = new LPWSTR[NumElts];
// fetch all the items
for(int i = 0 ; i < NumElts ; i++)
{
// call java.lang.ArrayList.get(int index) method
// Not sure about the parameter passing here
jobject Tmp = env->CallObjectMethod(YourJObjectRepresentingArrayList, Get_method, i);
jstring Str = (jstring)env->CallObjectMethod(Tmp, caster);
// get the length
int StrLen = env->GetStringLength(env, Str);
Out[i] = new wchar_t[StrLen];
const char* SourceUTF = env->GetStringChars(env, Str);
// store the string - not sure about UTF-16/UTF-8 here. It is OS-dependant.
// MultiByteToWideChar or iconv on POSIX
ConvertUTF8ToWChar(Out[i], SourceUTF);
env->ReleaseStringUTFChars(s, SourceUTF);
}
// done
ArrayList是什么?字符串?你想如何将它们转换成LPWSTR?我的意思是,逻辑上。连接还是别的? – 2012-07-26 08:58:20
如果您确实想将其转换为LPWSTR *,请参阅Victor Latypov的上述评论。 如果您的意思是LPWSTR **(指向字符串数组的指针),那么只需将ArrayList转换为标准的Strings(String [])数组即可。 – 2012-07-26 09:08:59
我有字符串arraylist。我希望它被转换为LPWSTR *。如果我在arraylist中有3个字符串,在C++中,我希望它具有3个值的LPWSTR数组或LPWSTR * – 2012-07-26 09:15:14