问题描述
我有一个结构数组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#代码段返回的结构数组。)
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>^