How to build a dynamic array in C++ and return it back to C#/.NET

前端 未结 2 1525
忘掉有多难
忘掉有多难 2021-01-15 05:24

I have to find the way to build the array of structs on C++ Win32 side. I don\'t have the initial number of items. It should be very fast to resize that array.

When

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-15 05:59

    Generally if your C++ dll compiled with CLI support you can simply use managed containers. If you don't want your dll to be compiled with CLI option, then you may write small C++/CLI wrapper dll which will call methods from native C++ dll and store objects in the managed container. Another possible solution is to change interface of the C++ lib to return objects by indexes, support insertion and/or removal.

    std::vector vec;
    
    void init() {
        //read data to vec
    }
    
    CFoo getIthElement(int i) {
        return vec[i];
    }
    
    int getElementCount() {
        return vec.size();
    }
    

    So you will use getIthElement and getElementCount functions on the C# side.

提交回复
热议问题