问题
I have to send a list from C# to C++.The C# list is List<string>MyList
and the C++ code accepts it as list<wstring>cppList
.How to use marshalas for this.
Thanks
回答1:
It is always wiser not to use complex type marshaling between native code and managed code.
In case of List
, these type totally differ from each other as they have different memory layout for each item.
So the best way is to write a utility function in a native dll that accepts array of string(char*) and manually build your native List
and ultimately call the desired method. It is easy for your to create wrapper of that utility function.
回答2:
C# cannot P/Invoke complex C++ types. You will have to use C++/CLI, they might have a method for marshalling it across. Else, you will have to marshal each string across individually.
回答3:
strings
in C# are wstrings
(2 byte unicode strings), so if what you say is true, then no special conversions are necessary.
来源:https://stackoverflow.com/questions/6532923/how-to-marshal-a-list-in-c-sharp