Solved

system::string to stl string

Posted on 2004-09-13
4
351 Views
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.  

Example

String str;
std::string str1;

I want to convert str to str1

thanks in advance,
 vbal  
0
Comment
Question by:vbal
  • 2
4 Comments
 
LVL 6

Assisted Solution

by:Svetlin_Panayotov
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 :)

Svetlin

0
 
LVL 19

Accepted Solution

by:
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;
   Marshal::FreeHGlobal(System::IntPtr((void*)chars));
}
0
 
LVL 6

Expert Comment

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

Author Comment

by:vbal
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.

vbal
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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