When I first started working with forms in Microsoft Visual Studio I had some problems getting to grips with their managed class. One of the more fustrating points was that text boxes returned text as type System::String and all my functions were expecting std::strings.

Converting from std:string to System::String was quite simple:

Code:

std::string myString = "Hello World"
 
System::String out = gcnew System::String(myString.c_str());

However, converting System::String to std::string was a little more tricky:

Code:

System::String myString = "Hello World";
 
std::string out = (const char*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(myString )).ToPointer();

Trackback address for this post

This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)

My name is Kieren and this is my blog. I use it mostly to document any solutions to problems I come across in computing.

Search

XML Feeds