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

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

public: class std::list<int,class std::allocator<int> > const * __thiscall TransactionsModule_t::GetTransactionList(void)const

      

And now I'm confused:

[DllImport("TransactionManager.dll", EntryPoint = "...", CallingConvention = CallingConvention.ThisCall)]
public static extern ??? GetTransactionList(
    IntPtr interfacePtr);

      

I don't know how to start looking as I can't directly see what the return type is, but obviously it's kind of a list. As I understand it, but is it a nested list? Perhaps a dictionary Dictionary<int,List<int>>

?

+3


source to share


1 answer


You cannot do this. Template types, AFAIK, don't work with P / Invoke, perhaps because the runtime can't figure out the length of the list / how to allocate and free the amount of memory it needs, etc.

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



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

Related question: Routing generic .NET types

+3


source







All Articles