<p>What is the approved way to convert from char* to System::string and back in C++/CLI? I found a few references to marshal_to&lt;> templated functions on Google, but it appears that this feature never made the cut for Visual Studio 2005 (and isn't in Visual Studio 2008 either, AFAIK). I have also seen some code on <a href="http://blogs.msdn.com/slippman/archive/2004/06/02/147090.aspx" rel="noreferrer">Stan Lippman's blog</a>, but it's from 2004. I have also seen Marshal::StringToHGlobalAnsi(). Is there a method that is considered "best practice"?</p>

[{'answer_id': 56605, 'author': 'Dan Blair', 'author_id': 1327, 'author_profile': 'https://Stackoverflow.com/users/1327', 'pm_score': 0, 'selected': False, 'text': "<p>What we did is made a C++\\CLI object that held the string in unmangaed code and would give out manged copies of the item.  The conversion code looks very much like what Stan has on his blog (I can't remember it exactly)(If you use his code make sure you update it to use delete[]) but we made sure that the destructor would handle releasing all the unmanged elements of the object.  This is a little overblown but we didn't have leaks when we tied into legacy C++ code modules. </p>\n"}, {'answer_id': 56694, 'author': 'Matthew Clendening', 'author_id': 5825, 'author_profile': 'https://Stackoverflow.com/users/5825', 'pm_score': 5, 'selected': True, 'text': '<p>There\'s a good overview here (this marshaling support added for VS2008):\n<a href="http://www.codeproject.com/KB/mcpp/OrcasMarshalAs.aspx" rel="noreferrer">http://www.codeproject.com/KB/mcpp/OrcasMarshalAs.aspx</a></p>\n'}, {'answer_id': 56779, 'author': 'Ben Straub', 'author_id': 1319, 'author_profile': 'https://Stackoverflow.com/users/1319', 'pm_score': 6, 'selected': False, 'text': '<p>System::String has a constructor that takes a char*:</p>\n\n<pre><code> using namespace system;\n const char* charstr = "Hello, world!";\n String^ clistr = gcnew String(charstr);\n Console::WriteLine(clistr);\n</code></pre>\n\n<p>Getting a char* back is a bit harder, but not too bad:</p>\n\n<pre><code> IntPtr p = Marshal::StringToHGlobalAnsi(clistr);\n char *pNewCharStr = static_cast&lt;char*&gt;(p.ToPointer());\n cout &lt;&lt; pNewCharStr &lt;&lt; endl;\n Marshal::FreeHGlobal(p);\n</code></pre>\n'}, {'answer_id': 6205115, 'author': 'dko', 'author_id': 704974, 'author_profile': 'https://Stackoverflow.com/users/704974', 'pm_score': 0, 'selected': False, 'text': '<p>I created a few helper methods. I needed to do this to move from an old Qt library to CLI String. If anyone can add onto this and tell me if it seems like I have a memory leak and what I can do to fix it, I would be most appreciative.</p>\n\n<pre><code>void MarshalString (  String ^ s, wstring&amp; os ) {\n    using namespace Runtime::InteropServices;\n    const wchar_t* char = (const wchar_t*)(Marshal::StringToHGlobalUni(s)).ToPointer();\n    os = char;\n}\nQString SystemStringToQt( System::String^ str)\n{\n    wstring t;\n    MarshalString(str, t);\n    QString r = QString::fromUcs2((const ushort*)t.c_str());\n    return r;\n}\n</code></pre>\n'}, {'answer_id': 16751886, 'author': 'Tadej Mali', 'author_id': 1010666, 'author_profile': 'https://Stackoverflow.com/users/1010666', 'pm_score': -1, 'selected': False, 'text': '<p>One additional link to a summary of possible ways:</p>\n\n<p><a href="http://support.microsoft.com/?kbid=311259" rel="nofollow">http://support.microsoft.com/?kbid=311259</a></p>\n'}]
['https://Stackoverflow.com/questions/56561', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3114/']