从std :: string转换为String ^

Mot*_*oti 3 string managed-c++

我在C++中有一个函数,它具有std :: string类型的值,并希望将其转换为String ^.

void(String ^outValue)
{
   std::string str("Hello World");
   outValue = str;
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*nen 7

谷歌搜索显示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;
}
Run Code Online (Sandbox Code Playgroud)

另请参阅封送概述.


Teo*_*aru 6

来自MSDN:

#include <string>
#include <iostream>
using namespace System;
using namespace std;

int main() {
   string str = "test";
   String^ newSystemString = gcnew String(str.c_str());
}
Run Code Online (Sandbox Code Playgroud)

http://msdn.microsoft.com/en-us/library/ms235219.aspx