我有一个System::^ array结构数组,正在填充一个C# DLL。需要将该结构数组导出到C++ Dll (函数中),并在进一步的C++ DLL中使用该导出数组。
下面是填充数组结构( C# )的ReturningJSONArray代码。
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是一种system::Array类型的C#,如何将其转换为与C++ (或等效数据类型)兼容的东西。我尝试将其转换为结构向量,但似乎应该有一些转换(封送处理)可以有人详细说明这一点,或者还有其他解决办法。
发布于 2020-08-19 11:26:06
您的PJSONInfo结构不是闪电战,因为它包含字符串。因此,您需要声明相应的非托管类型并手动转换每个值。Double很容易转换,但请参阅将c#字符串转换为std::string关于如何转换字符串的内容。将转换后的值放置在c数组或std::vector中,这取决于您需要什么。
此外,您应该能够在c++/cli中使用泛型数组,即System::Array<PJSONInfo>^
https://stackoverflow.com/questions/63484490
复制相似问题