system::string to stl string

Posted on 2004-09-13
Last Modified: 2010-04-24
I have an unmanaged class that accepts string arguments of std::string (STL).  I want to convert a managed string to a STL string, so that it can be passed to my unmanaged class.  


String str;
std::string str1;

I want to convert str to str1

thanks in advance,
Question by:vbal
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Assisted Solution

Svetlin_Panayotov earned 125 total points
ID: 12051487
     std::string x1;
      x1 = "std:string going managed :)";
      System::String* x2 = new System::String(x1.c_str());      

Something like this should do the work, I believe :)


LVL 19

Accepted Solution

drichards earned 125 total points
ID: 12051579
Here's going from managed String  to unmanaged std::string:

void MarshalString ( System::String* s, std::string& os )
   using namespace System::Runtime::InteropServices;
   const char* chars =
      (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
   os = chars;

Expert Comment

ID: 12051907
Oh sorry for my previous post - it converts std::string to was 7 in the morning here when I was reading your question :)

Author Comment

ID: 12056235
Thankyou, Dr. Richards, your solution works quite well.  Also Svetlin, even though you solution is the reverse, I found that I needed that as well.  So I will increase the total points to 250 and split the point allocation.


Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question