我在C++中有一个函数,它返回std::string类型的值,我想将其转换为String^。
void(String ^outValue)
{
std::string str("Hello World");
outValue = str;
}
我在C++中有一个函数,它返回std::string类型的值,我想将其转换为String^。
void(String ^outValue)
{
std::string str("Hello World");
outValue = str;
}
来自MSDN:
#include <string>
#include <iostream>
using namespace System;
using namespace std;
int main() {
string str = "test";
String^ newSystemString = gcnew String(str.c_str());
}
通过谷歌搜索发现 marshal_as(未经测试):
// marshal_as_test.cpp
// compile with: /clr
#include <stdlib.h>
#include <string>
#include <msclr\marshal_cppstd.h>
using namespace System;
using namespace msclr::interop;
int main() {
std::string message = "Test String to Marshal";
String^ result;
result = marshal_as<String^>( message );
return 0;
}
请参阅Marshaling概述。
System::String^ StdStringToUTF16(std::string s)
{
cli::array<System::Byte>^ a = gcnew cli::array<System::Byte>(s.length());
int i = s.length();
while (i-- > 0)
{
a[i] = s[i];
}
return System::Text::Encoding::UTF8->GetString(a);
}
String^
并不太有意义。 - juanchopanza