我有两个通信组件 - 一个托管,另一个非托管。托管需要从非托管实现中检索字符串(相同的字符串或只是一个副本)。我尝试了以下代码。
// Unmanaged code
const char* GetTestName(Test* test)
{
return test->getName();
}
// Managed wrapper
[DllImport(DllName, EntryPoint = "GetTestName")]
public static extern IntPtr GetTestName(IntPtr testObj);
// API Invocation
IntPtr testName = GetTestName(test);
string testStr = Marshal.PtrToStringAuto(testName);
但是, testStr 的值不是预期的。有谁知道我在这里做错了什么?任何建议都会非常有帮助。