Вам не нужно создавать экземпляр кодировщика, вы можете использовать статические экземпляры.
Если вызываемая функция не ожидает указатель накуча HGlobal, вы можете просто использовать простое выделение памяти C / C ++ (new или malloc) для буфера.
В вашем примере функция не становится владельцем, поэтому вам не нужнокопию, просто прикрепите буфер.
Что-то вроде:
// Encode the text as UTF8
array<Byte>^ encodedBytes = Encoding::UTF8->GetBytes(unicodeString);
// prevent GC moving the bytes around while this variable is on the stack
pin_ptr<Byte> pinnedBytes = &encodedBytes[0];
// Call the function, typecast from byte* -> char* is required
MyTest(reinterpret_cast<char*>(pinnedBytes), encodedBytes->Length);
Или, если вам нужна строка с нулевым символом в конце, как и большинство функций C (включаяНапример, в OP) тогда вы, вероятно, должны добавить нулевой байт.
// Encode the text as UTF8, making sure the array is zero terminated
array<Byte>^ encodedBytes = Encoding::UTF8->GetBytes(unicodeString + "\0");
// prevent GC moving the bytes around while this variable is on the stack
pin_ptr<Byte> pinnedBytes = &encodedBytes[0];
// Call the function, typecast from byte* -> char* is required
MyTest(reinterpret_cast<char*>(pinnedBytes));