[英]Marshal C++ string vector to C#
how would I return a C++ std::vector<std::string>
to C# code? 我如何将C ++
std::vector<std::string>
到C#代码? My methods declaration (C++ side) currently Looks like: 我的方法声明(C ++端)当前如下所示:
__declspec(dllexport) std::vector<std::string> __cdecl Initialize(std::vector<std::string> array = {})
But I have no idea about how to get the vector
or how to send it to C++. 但是我不知道如何获取
vector
或如何将其发送到C ++。
Any help is greatly appreciated. 任何帮助是极大的赞赏。
It's not trivial to marshal a vector
of strings. 封送字符串
vector
并非易事。
You may consider building a bridging layer between C++ and C# using C++/CLI. 您可以考虑使用C ++ / CLI在C ++和C#之间构建桥接层。
If you don't want to use C++/CLI, a valid alternative is to use SAFEARRAY
s. 如果您不想使用C ++ / CLI,则有效的替代方法是使用
SAFEARRAY
。 You can simplify safe array programming in C++ with ATL::CComSafeArray
. 您可以使用
ATL::CComSafeArray
简化C ++中的安全数组编程。
You may find this article on safe arrays on MSDN Magazine helpful. 您可能会在MSDN Magazine上发现有关安全阵列的这篇文章很有帮助。
You should also pay attention to the string encoding. 您还应该注意字符串编码。 If you use UTF-8 for you
std::string
s, then you should convert to UTF-16 for SAFEARRAY(BSTR)
or for the .NET String
class. 如果您对
std::string
使用UTF-8,那么对于SAFEARRAY(BSTR)
或.NET String
类,应将其转换为UTF-16。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.