You need to know that you allocate an unmanaged block of memory in your C ++ function, so it is not possible to pass a managed String or Array object from C # code to a "hold" char array.
One approach is to define the "Delete" function in your own DLL and call it to free memory. On the managed side, you can use IntPtr to temporarily hold a pointer to a C ++ char array.
// c++ function (modified) void __cdecl FillAndReturnString(char ** someString) { *someString = new char[5]; strcpy_s(*someString, "test", 5); // use safe strcpy } void __cdecl DeleteString(char* someString) { delete[] someString } // c
}
source share