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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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