如何使用jni将java中的Arraylist转换为c中的lpwstr
How to convert Arraylist in java to lpwstr in c using jni
我正在使用JNI将ArrayList传递给c++。我想把它转换成LPWSTR*类型。但我收到的是作为工作对象的arraylist。如何转换?
让我们开始吧。我不确定其中的一些转换,希望其他的能有所帮助。
你有一个对象。获取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
相关文章:
- std::map, std::unordered_map - 缩小初始值设定项列表中的转换范围
- 类链中的转换
- 如何在我使用过的Arduino库之一中固定构造函数中的转换错误
- 直接初始化中的转换运算符
- C++中的转换无效
- 为什么不调用模板类中的转换构造函数?
- 缩小 C++11 中的转换范围:"actual value after conversion"是什么?
- Boost 1.49/1.50/1.51 ASIO with MinGW & GCC 4.7.0 中的转换错误
- C++中的转换构造函数
- std::accumulate中的转换无效
- std::remove函数中的转换错误
- 是否正在缩小诊断所需的非类型模板参数中的转换范围
- VS2013中的转换运算符错误C2678,适用于VS2008
- 为什么参数与printf未定义行为中的转换说明符不匹配
- 正在缩小C++中的转换
- pthread_create c++中的转换无效
- C++中的转换问题(应为字符串)
- 运行时类型在c++中的转换,如何
- 从类发射信号,如果 QStateMachine 中的转换成功
- vc++中的DWORD数据类型及其在Java中的转换