I've got a fairly large MFC application that has just been migrated from VS6.0 to VS2008. It was a pretty painful process, but now I'd like to explore any managed-code options that may be available. I was able to successfully build the project using the /clr switch which seems to give me access to managed types.
I'd like to know if conversion between System::String and CString is automatic or not. The MSDN documentation that I've found suggests that this conversion isn't automatic but I haven't found this to be the case. All three of the examples below work and both 1 and 2 are documented by the MSDN. What I'm curious about is example 3 which also works but I don't know why. The CurrentDirectory property is returning a pointer into the managed heap, String^, but why am I able to assign it to a CString? Exactly what is example 3 doing and what are the memory management implications?
Example 1)
marshal_context ^ context = gcnew marshal_context(); String ^env = System::Environment::CurrentDirectory; const char* env2 = context->marshal_as(env); AfxMessageBox(env2); delete context;
Example 2)
CString s(System::Environment::CurrentDirectory); AfxMessageBox(s);
Example 3)
CString s = System::Environment::CurrentDirectory; AfxMessageBox(s);