C ++ .NET converts System :: String to std :: string

How do you convert System :: String to std :: string in C ++ .NET?

+48
c ++ string marshalling
Aug 19 '09 at 15:28
source share
6 answers

The syntax is cleaner if you are using the latest version of .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; } 

It also gives you better cleanup before exceptions.

msdn article for various other conversions

+59
Aug 19 '09 at 15:51
source share
β€” -

And in response to the β€œsimpler way” in later versions of C ++ / CLI, you can do this without marshal_context. I know this works in Visual Studio 2010; not sure before that.




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



+27
Apr 23 '10 at 18:07
source share
 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; } 
+7
Aug 19 '09 at 15:40
source share

C # uses the UTF16 format for its lines.
Thus, in addition to type conversion, you should also be aware of the actual string format.

When compiling for a multibyte character set, Visual Studio and the Win API assume UTF8 (actually Windows Windows-28591 encoding ). When compiling Unicode for a character set, Visual Studio and the Win API assume UTF16.

So, you should convert the string from UTF16 to UTF8 format, and not just convert to std :: string.
This will become necessary when working with multi-character formats, such as some non-Latin languages.

The idea is to decide that std::wstring always represents UTF16 .
And std::string always represents UTF8 .

This is not done by the compiler, it is rather a good policy.

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

Or use a more compact syntax:

 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; } 
+6
Dec 25 '15 at 9:42
source share

I had too many ambiguous errors appearing with the answers above (yes, I am C ++ noob)

This worked for me to send a string from C # to C ++ CLI

FROM#

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



C ++ CLI

function:

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

which works from converting String ^ to 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; } 

IN FURTHER RESEARCH, it seems that it is cleaner and safer.

Instead, I switched to this method.

 std::string Utils::ToUnmanagedString(String^ stringIncoming) { std::string unmanagedString = marshal_as<std::string>(stringIncoming); return unmanagedString; } 
+4
Apr 17 '13 at 0:09
source share

Creating a Windows runtime component that you can use:

 String^ systemString = "Hello"; std::wstring ws1(systemString ->Data()); std::string standardString(ws1.begin(), ws1.end()); 
0
Dec 15 '15 at 11:50
source share



All Articles