There is a c ++ function that returns a one-dimensional array. How to get this array in C ++ / CLI, so that you can later use it in C # (meaning what data types and operators to use)?

In CLI, an array is of type array<double> ^ , and in C ++ double* . How to write array from C ++ in CLI? My code is:

C ++:

  double *func1(....){ ......... } 

C ++ / CLI:

 array<double> ^func2(...){ //как здесь обратиться к указателю, который возвращает функция //func1, чтобы потом возвратить его из функции func2 для //дальнейшего приема в c# } 
  • one
    If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

No But you can easily convert by hand.

For example:

 vector<double> nativeVector; // ... auto nItems = nativeVector.size(); array<Double>^ managedArray = gcnew array<Double>(nItems); for (size_t i = 0; i < nItems; i++) managedArray[i] = nativeVector[i]; 

Documentation: How to: Use Arrays in C ++ / CLI .


However, in C ++ / CLI, you can use the native array if you want. You can convert, for example, only when transferring to another part of the program.


If you have a shared array, you need to know exactly how many elements there are. If you know its length, use it instead of nItems , the rest of the code is exactly the same.

If you do not know - oops, it is impossible to work with an array. Make it so that you know the length.

  • C ++: double * func1 (....) {......} C ++ / CLI: array <double> ^ func2 (...) {// how to access the pointer returned by the function // func1, then to return it from the func2 function for // further reception in c #} - boksts
  • @ user206138: The code in the comments is not readable, add it better to the question. // updated the answer - VladD
  • I don't know the length of the array ... - boksts
  • @ user206138: Then find out somehow. Nobody can recognize her except you. Without this information, nothing will work, sorry. - VladD
  • one
    @Grundy: ideone.com/h8sHM4 - VladD