Solved

system::string to stl string

Posted on 2004-09-13
4
356 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
[X]
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
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: 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++ (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…
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