convert from std::string to String^

给你一囗甜甜゛ 提交于 2020-01-09 05:40:09

问题


I have a function in C++ that have a value in std::string type and would like to convert it to String^.

void(String ^outValue)
{
   std::string str("Hello World");
   outValue = str;
}

回答1:


Googling reveals marshal_as (untested):

// 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;
}

Also see Overview of Marshaling.




回答2:


From MSDN:

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

int main() {
   string str = "test";
   String^ newSystemString = gcnew String(str.c_str());
}

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



来源:https://stackoverflow.com/questions/13718188/convert-from-stdstring-to-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!