0

将托管字符串列表复制List<String^>^到 s 数组的最简单方法是wchar_t什么?

因此,List<String^>^ someList 我需要将其复制到一个新数组中,例如:

wchar_t *paramList = new wchar_t[sizeoflist];

当我们讨论这个话题时,有人可以指出一个很好的 C++/CLI 参考(书籍/在线文章)来解释这些方面吗?

4

1 回答 1

2

与其提出有关问题的问题(请参阅@HanPassant 和@ChristianRau 的问题评论),而是让您思考您真正想要的东西:

#include <msclr/marshal_cppstd.h>
#include <string>
#include <vector>
using namespace System;
using namespace std;

// preserves UTF-16LE encoding
vector<wstring> StringArrayToNative(array<String ^>^ arr)
{
    vector<wstring> v;
    v.reserve(arr->Length);
    for each (String^ s in arr)
    {
        // copies from the CLR GC heap to the C++ heap.
        v.push_back(msclr::interop::marshal_as<wstring>(s));
    }
    return v;
}

wstring当然,是由wchart_t元素组成的。wchar_t旨在用于可移植代码,并且应该包含本机大小的字符。它被标准大小的类型所取代,因为毕竟字符编码是标准的。不幸的是,char16_t没有内置到 C++/CLI 工具集中。尽管如此,在所有 MSVC 中,wchar_t大小为 2 个字节,并且在 Windows 上通常用于保存 Unicode 字符集的一个 UTF-16LE 代码单元。这使它成为用于 Win32 API 的东西。(顺便说一句——微软称 UTF-16LE,简称为“Unicode”。)

于 2013-07-24T11:39:15.037 回答