Assuming all of the characters in the System::String
are in the ASCII range, the most basic implementation would be something like:
void ConvertAndCopy(System::String^ ms, char* us)
{
for (int i(0); i < ms->Length; ++i)
us[i] = static_cast<char>(ms[i]);
us[ms->Length] = '\0';
}
// usage example:
System::String^ ms = "Hello world";
char us[12] = "";
ConvertAndCopy(ms, us);
Note that this performs no bounds-checking on the destination array and does not do any character set conversion.
Whether this performs any better than StringToHGlobalAnsi
or whether any performance gains are worth the significant increase in complexity (namely, managing your own memory and handling character set conversions), I have no idea.