Convert C ++ / CLI String Array to String Vector

I have a parameter in C ++ / CLI as follows:

array<String^>^ list

I want to be able to convert this to a string vector.

How should I do it? Not as good with C ++ / CLI as I want to be.

+4
source share
1 answer

MSDN provides some information on how to marshal data. They also provide some standard implementation for msclr::marshal_aswrt std::string.

cli::arraya little more complicated, the key for the general case here is the pinarray at first (so we don’t have its movement behind our backs). In case of conversion String^ marshal_aswill be pin Stringappropriate.

The essence of the code:

vector<string> marshal_array(cli::array<String^>^ const& src)
{
    vector<std::string> result(src->Length);

    if (src->Length) {
        cli::pin_ptr<String^> pinned = &src[0]; // general case
        for (int i = 0; i < src->Length; ++i) {
            result[static_cast<size_t>(i)] = marshal_as<string>(src[i]);
        }
    }

    return result;
}
+4

All Articles