Tuesday, October 27, 2009

How to convert 'System::String' to 'char*'?

In VC++ 8:

Example:-


using namespace System::Runtime::InteropServices; // for class Marshal

void PrintMessage(System::String^ str)
{
const char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(str);
printf(str2);
Marshal::FreeHGlobal((System::IntPtr)(void*)str2);
}

PrintMessage("Method 2");

No comments: