How to marshal a list in C#

落花浮王杯 提交于 2020-01-03 16:43:35

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!