How to return a list in C# using P/Invoke?

前端 未结 1 622
梦谈多话
梦谈多话 2021-01-20 14:13

I am working on a small project where I use P/Invoke and want to return the following in C#:

public: class std::list &         


        
相关标签:
1条回答
  • 2021-01-20 15:00

    You cannot do this. Template types, AFAIK, don't work with P/Invoke possibly because the runtime cannot figure out the length of the list/how to allocate & deallocate the required amount of memory, etc.

    Your best bet will be to write a C++/CLI wrapper or change the return type in C++ to an int array or to pass in a parameter to the method that can be filled in the C++ side (an out parameter).

    You might think that the C# list and the std::list can be marshalled both ways, but that is not the case.

    Related question: Marshalling .NET generic types

    0 讨论(0)
提交回复
热议问题