如何将字符串数组从 C# 函数返回到调用它的C++函数
How do I return a String Array from a C# function to a C++ function calling it?
我正在使用JNI,我有用C#编写的.net库,我想使用C++和JNI作为中介将值从中传递到Java。
我已经有一段时间没有和C++合作了。这是我对 C# 方法所拥有的,我需要将返回值传递给 C++ 方法:
public string[] getSystemFingerPrint(){ //<---- This method is called from a C++ class.
/* Code Body */
return FingerPrint._fingerprint;
}
该方法是从我用C++编写的本机库中调用的。唯一的问题是,我不知道如何将字符串数组从 .net 转换为C++可读的内容。这是我拥有的C++方法,该方法负责返回从 C# .net 库调用的值:
FingerPrintC __gc *t;
FingerPrintC() {
t = new FingerPrint();
}
.
.
.
char[] getSystemFingerPrint(){ //<----I know that's the wrong return value.
return t->getSystemFingerPrint(); //<----What will .net return the string array as here?
}
所以我想问题是:当 C++.net 调用 .net 函数时,.net 返回一个字符串数组C++是什么?
编辑 1 :
当我解决其他一些错误时,我能够得到一些线索:当我尝试编译返回 char * 的值时,它给了我这个错误:
error C2440: 'return' : cannot convert from 'System::String __gc * __gc[]' to 'char *'
这有点帮助,但是当我尝试将其用作返回值时,好吧,它不太喜欢它......所以现在的问题是:如何使System::String __gc * __gc[]
成为C++喜欢的返回类型?
编辑2:
更多进展:所以格式化函数头的正确方法是System::String __gc *<FunctionName>()[]
。不幸的是,C++不是很宽容,它不会简单地让你将该值作为jobjectArray返回。所以我的下一步是从数组中获取值并将它们放入新的 jobjectArray 中......
编辑3:
所以现在我在这里:
JNIEXPORT jobjectArray JNICALL Java_RegistrationControl_GetSystemFingerPrint (JNIEnv *jn, jobject jobj){
FingerPrintC* t = new FingerPrintC();
System::String __gc * t2[] = t->GetSystemFingerPrint();
jobjectArray ret =
(jobjectArray)jn->NewObjectArray(
6,
jn->FindClass("java/lang/String"),
jn->NewStringUTF("")
);
for (int c = 0; c < 6; c++) jn->SetObjectArrayElement(ret, c, jn->NewStringUTF(t2[c])); //<---- This line fails.
return ret;
}
我得到的错误是:JNIEnv_::NewStringUTF' : cannot convert parameter 1 from 'System::String __gc *' to 'const char *
所以现在我需要弄清楚如何将System::String __gc *
转换为常量char *
。
经过漫长而艰苦的跋涉,我们得出了答案。因此,能够通过 JNI 将字符串数组从 C# 函数传递到 Java 的解决方案是:
使用命名空间 System::Runtime::InteropServices;//<-----NameSpace 使用我们需要的方法...
JNIEXPORT jobjectArray JNICALL Java_RegistrationControl_GetSystemFingerPrint (JNIEnv *jn, jobject jobj){ //<----- The Native Function to be called by Java
System::String __gc * t2[] = FingerPrint::GetSystemFingerPrint(); //<----- The method called from the C# library.
jobjectArray ret = (jobjectArray)jn->NewObjectArray( //<-----Define the jobjectArray
6, //<-----Must know exactly how many elements are in the array. This works for me.
jn->FindClass("java/lang/String"), //<<-----Type of the array.
jn->NewStringUTF("") //<-----Initialize each array value, I guess?
);
for (int c = 0; c < 6; c++){
const char *i = (char*)(void*)Marshal::StringToHGlobalAnsi(t2[c]); //<-----Transform the "string" into something that the JNI method can tolerate.
jn->SetObjectArrayElement(ret, c, jn->NewStringUTF(i)); //<-----jn->NewStringUTF(i) transforms the string into something Java can tolerate. This line transforms the transformed string and adds it to the array to be returned.
}
return ret;
}
我只能希望将来有人遇到这种情况并能够利用它。
- 从函数返回const char*数组
- 检查函数返回类型是否与STL容器类型值相同
- 从 c 或 cpp 系列子函数返回到主函数
- 从函数返回任意简单类型的数据
- 警告:在函数返回类型 [-Wignore 限定符] 时忽略类型限定符
- 从 C++ 中的函数返回数组地址问题
- VirtDisk.h QueryChangesVirtualDisk() 函数返回 RangeCount 为 0
- 为什么glGetSubroutineIndex为不同的函数返回相同的值?
- 在 c++ 中将函数返回类型指定为模板参数
- 从封装在对象中的函数 C++ 返回时为空的列表
- 为什么向量内部的指针在从函数返回时会发生变化?
- 函数返回的 rvalue 引用(表达式)是 xvalue - 但没有标识?
- 程序中的布尔函数返回输入的范围无论如何都是无效的
- 寿命延长从函数返回引用
- 程序不向函数返回值
- 字符串函数返回奇怪的值
- 类的大问题,以及从空函数c++返回
- 为什么在尝试测量函数返回所需的时间时,我的运行时编号是错误的?
- C++二叉搜索树模板从函数返回节点
- NVCC 错误:string_view.h:constexpr 函数返回是非常量