[英]How to marshal a list in C#
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. I have to send a list from C# to C++.The C# list is
List<string>MyList
MyList and the C++ code accepts it as list<wstring>cppList
.How to use marshalas for this.
Thanks谢谢
C# cannot P/Invoke complex C++ types. C# 不能 P/Invoke 复杂的 C++ 类型。 You will have to use C++/CLI, they might have a method for marshalling it across.
您将不得不使用 C++/CLI,他们可能有一种方法来编组它。 Else, you will have to marshal each string across individually.
否则,您将不得不单独编组每个字符串。
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.在
List
的情况下,这些类型完全不同,因为它们对每个项目都有不同的 memory 布局。
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.因此,最好的方法是在本机 dll 中编写一个实用程序 function ,它接受字符串数组(char*)并手动构建您的本机
List
并最终调用所需的方法。 It is easy for your to create wrapper of that utility function.您可以轻松创建该实用程序 function 的包装器。
strings
in C# are wstrings
(2 byte unicode strings), so if what you say is true, then no special conversions are necessary. C# 中的
strings
是wstrings
(2 字节 unicode 字符串),所以如果你说的是真的,那么不需要特殊的转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.