如何解决如何将System :: ^ array从C#函数转换为C ++中的等效数据类型
我有一个结构数组System::^ array
,将其填充到C# DLL
中。需要将该结构数组导出到C++ Dll
(在函数中),并在我的下一个C++ DLL
中使用该导出的数组。
以下是填充数组(ReturningJSONArray
)的C#代码。
namespace JSON
{
public struct PJSONInfo
{
string sSetting;
double dMinVal;
double dMaxVal;
string sUnits;
}
public interface iCalFunction
{ PJSONInfo[] ReadJSON(); }
public class JSONReaderClass : iCalFunction
{
public PJSONInfo[] ReadJSON()
{
ILIST<PJSONInfo> JSONList = new List<PJSONInfo>();
..
..//(Fill the Ilist of PJSONInfo)
..
PJSONInfo[] ReturningJSONArray = JSONList.ToArray();
return ReturningJSONArray ;
}
}
}
( ReturningJSONArray 是从C#代码段返回的结构数组。)
以下是C#函数作为DLL链接到的C ++代码。
class CReadingClass
{
public :msclr::auto_gcroot<JSONReaderClass ^> pJSONClass;
}
int ReadJSONFromJSONReader()
{
CReadingClass* pReadClass;
pReadClass = new CReadingClass();
pReadClass ->pJSONClass =gcnew JSONReaderClass ();
System::Array^ JSONSetting = pReadClass -> pJSONClass -> ReadJSON();
//**JSONSetting** is a system::Array type of C#,return 1;
}
JSONSetting 是C#的system::Array
类型,如何将其转换为与C ++(或等效数据类型)兼容的东西。
我尝试将其转换为结构的向量,但似乎应该进行一些转换(封送处理),以便有人可以对此进行详细说明或有其他解决方法。
解决方法
您的PJSONInfo
结构不是blittable,因为它包含字符串。因此,您需要声明一个相应的非托管类型并手动转换每个值。 Double转换起来很简单,但请参见converting c# strings to std::string有关如何转换字符串的信息。根据需要将转换后的值放在c数组或std :: vector中。
此外,您应该能够在c ++ / cli中使用通用数组,即System::Array<PJSONInfo>^
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。