0

I have an array of structures System::^ array that is getting filled in a C# DLL. Need to export that array of structures to C++ Dll (in a function) and should use that exported array in my further C++ DLL.

The Following is the C# code where the structure of Array gets filled (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 is an array of structures that is been returned from the C# snippet.)

The Following is C++ code to which the C# function is linked as a DLL.

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 is a system::Array type of C# , How do I convert that into something that is compatible with C++ (or equivalent Datatype). I tried converting that into a vector of structure, but seems like there should be some conversions done (marshaling) can someone elaborate on this or is there any other workaround.

Hadi Mirzaei
  • 222
  • 2
  • 16
Bharath V
  • 1
  • 2
  • Pin and copy to `PJSONInfo*`. Search about `pin_ptr`. –  Aug 19 '20 at 10:22
  • The "C++" code looks more like C++/CX (produces native assembly for WinRT) or C++/CLI (produces MSIL for .NET VM/CLR). C++/CX replaced WRL; C++/CX is replaced by C++/WinRT (standard C++17 and a template library by Kenny Kerr). – Eljay Aug 19 '20 at 11:24

1 Answers1

0

Your PJSONInfo struct is not blittable since it contains strings. So you would need to declare a corresponding unmanaged type and convert each value manually. Double is trivial to convert, but see converting c# strings to std::string on how to convert the strings. Place the converted values in a c array or std::vector, depending on what you need.

Also, you should be able to use generic arrays in c++/cli, i.e. System::Array<PJSONInfo>^

JonasH
  • 28,608
  • 2
  • 10
  • 23