Thomas Flinkow
Thomas Flinkow

Reputation: 5115

Marshal C++ string vector to C#

how would I return a C++ std::vector<std::string> to C# code? My methods declaration (C++ side) currently Looks like:

__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++.

Any help is greatly appreciated.

Upvotes: 1

Views: 1567

Answers (1)

Mr.C64
Mr.C64

Reputation: 42944

It's not trivial to marshal a vector of strings.

You may consider building a bridging layer between C++ and C# using C++/CLI.

If you don't want to use C++/CLI, a valid alternative is to use SAFEARRAYs. You can simplify safe array programming in C++ with ATL::CComSafeArray.

You may find this article on safe arrays on MSDN Magazine helpful.

You should also pay attention to the string encoding. If you use UTF-8 for you std::strings, then you should convert to UTF-16 for SAFEARRAY(BSTR) or for the .NET String class.

Upvotes: 4

Related Questions