"Explicit String Marshalling Example (CPP Side)"

// C++
 
extern "C" __declspec(dllexport) uint32_t GetErrorStrLen() {
    return static_cast<uint32_t>(strlen(lastErrorString));
}
 
extern "C" __declspec(dllexport) void GetErrorStr(char16_t* const strSpace) {
    strcpy(strSpace, lastErrorString); // Consider using strcpy_s or similar in real code!
}


Code snippet taken from "P/Invoke Tips".