[英]Converting array<System::string^> to std::string*
I am new to C++/Cli, actually in the C# project I have string[] which I converted to array, Now I need to pass this unmanaged array into the native CPP file, ie I want to convert this "array" to std::string*.我是 C++/Cli 的新手,实际上在 C# 项目中我有我转换为数组的字符串 [],现在我需要将此非托管数组传递到本机 CPP 文件中,即我想将此“数组”转换为 std: :细绳*。 How I can do that.
我怎么能做到这一点。 I tried this below:
我在下面试过这个:
void functionA(cli::array^varA){无效函数A(cli::array^varA){
cli::pin_ptr<System::String^>varA_value = &varA[0];
std::string* varA_value_final = varA_value;
} }
But it gives error saying: a value of type cli::pin_ptr cannot be used to initialize entity of type std::string*但它给出了错误提示:cli::pin_ptr 类型的值不能用于初始化 std::string* 类型的实体
The managed class System::String
is completely different from the unmanaged class std::string
.托管 class
System::String
与非托管 class std::string
完全不同。 The classes are completely different, and store completely different data.类是完全不同的,存储完全不同的数据。 You cannot get a pointer to one and pretend it's the other.
你不能得到一个指针并假装它是另一个。
Iterate over the cli::array<System::String^>
, convert each element, and stick the results in a std::vector<std::string>
or other unmanaged container.遍历
cli::array<System::String^>
,转换每个元素,并将结果粘贴到std::vector<std::string>
或其他非托管容器中。 Grab a pointer to the first element.获取指向第一个元素的指针。 Use
marshal_as<std::string>()
to convert each element.使用
marshal_as<std::string>()
转换每个元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.