C ++ .NET将System :: String转换为std :: string

如何将System :: String转换为C ++ .NET中的std :: string?

如果您使用最新版本的.net,则语法更清晰

#include "stdafx.h" #include <string> #include <msclr\marshal_cppstd.h> using namespace System; int main(array<System::String ^> ^args) { System::String^ managedString = "test"; msclr::interop::marshal_context context; std::string standardString = context.marshal_as<std::string>(managedString); return 0; } 

面对例外情况,这也使您能够更好地进行清理。

有一个MSDN文章的各种其他转换

为了响应更高版本的C ++ / CLI中的“简单方式”,您可以在不使用marshal_context的情况下执行此操作。 我知道这在Visual Studio 2010中有效; 不知道在那之前。


 #include "stdafx.h" #include <string> #include <msclr\marshal_cppstd.h> using namespace msclr::interop; int main(array<System::String ^> ^args) { System::String^ managedString = "test"; std::string standardString = marshal_as<std::string>(managedString); return 0; } 

 stdString = toss(systemString); static std::string toss( System::String ^ s ) { // convert .NET System::String to std::string const char* cstr = (const char*) (Marshal::StringToHGlobalAnsi(s)).ToPointer(); std::string sstr = cstr; Marshal::FreeHGlobal(System::IntPtr((void*)cstr)); return sstr; } 

C#为其string使用UTF16格式。
所以,除了转换types之外,还应该意识到string的实际格式。

编译多字节字符集时 Visual Studio和Win API采用UTF8(其实Windows编码是Windows-28591 )。
编译Unicode字符集时 Visual Studio和Win API假定为UTF16。

所以,你必须将string从UTF16转换为UTF8格式,而不是只转换为std :: string。
使用非拉丁语言等多字符格式时,这将变得必要。

这个想法是决定std::wstring 总是表示UTF16
std::string 总是表示UTF8

这不是编译器强制执行的,这是更好的策略。

 #include "stdafx.h" #include <string> #include <msclr\marshal_cppstd.h> using namespace System; int main(array<System::String ^> ^args) { System::String^ managedString = "test"; msclr::interop::marshal_context context; //Actual format is UTF16, so represent as wstring std::wstring utf16NativeString = context.marshal_as<std::wstring>(managedString); //C++11 format converter std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> convert; //convert to UTF8 and std::string std::string utf8NativeString = convert.to_bytes(utf16NativeString); return 0; } 

或者使用更紧凑的语法:

 int main(array<System::String ^> ^args) { System::String^ managedString = "test"; msclr::interop::marshal_context context; std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> convert; std::string utf8NativeString = convert.to_bytes(context.marshal_as<std::wstring>(managedString)); return 0; } 

我有太多模糊的错误出现与上述答案(是的,我是一个C + + noob)

这对于我从C#发送string到C ++ CLI的工作

C#

 bool result; result = mps.Import(mpsToolName); 

C ++ CLI

function:

 bool ManagedMPS::Import(System::String^ mpsToolNameTest) std::string mpsToolName; mpsToolName = toStandardString(mpsToolNameTest); 

函数从String ^转换为std :: string

 static std::string toStandardString(System::String^ string) { using System::Runtime::InteropServices::Marshal; System::IntPtr pointer = Marshal::StringToHGlobalAnsi(string); char* charPointer = reinterpret_cast<char*>(pointer.ToPointer()); std::string returnString(charPointer, string->Length); Marshal::FreeHGlobal(pointer); return returnString; } 

进一步的研究,看起来这是更清洁和更安全的。

我转而使用这种方法。

 std::string Utils::ToUnmanagedString(String^ stringIncoming) { std::string unmanagedString = marshal_as<std::string>(stringIncoming); return unmanagedString; } 

创build一个Windows运行时组件,你可以使用:

 String^ systemString = "Hello"; std::wstring ws1(systemString ->Data()); std::string standardString(ws1.begin(), ws1.end());