AL Palma
asked on
How to convert std::string to System::String
Hi EE,
I'm new to C++ and I have a problem figuring out this code, it won't work:
I'm using Microsoft Visual C++ Express 2005
========================== ==========
string str;
ifstream myfile;
myfile.open("c:\\test.txt" ,std::ios: :in);
getline(myfile,str);
MessageBox::Show(str);
myfile.close();
========================== ==========
when you run this, it has error to MessageBox simply because it use System::string.
thanks
I'm new to C++ and I have a problem figuring out this code, it won't work:
I'm using Microsoft Visual C++ Express 2005
==========================
string str;
ifstream myfile;
myfile.open("c:\\test.txt"
getline(myfile,str);
MessageBox::Show(str);
myfile.close();
==========================
when you run this, it has error to MessageBox simply because it use System::string.
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
std::string is a standard C++ type created on the unmanaged heap and System::String is a CLR (.NET) type created on the managed heap. You cannot use a managed object in native C++ code or an unmanaged object in managed C++ code without special considerations (usually involves marshaling data from one space to the other or pinning pointers). That's why your original attempt to pass the std::string as a parameter to the .NET MessageBox::Show method failed. The System::String constructor can take a standard C++ char array (or wide char array) and it copies the string to managed memory.
ASKER
It works, how was that happen? I give you the points.
Thanks.