【发布时间】:2011-07-11 09:58:55
【问题描述】:
假设我在 C++ 中有 x.dll,看起来像这样
MYDLLEXPORT
const char* f1()
{
return "Hello";
}
MYDLLEXPORT
const char* f2()
{
char* p = new char[20];
strcpy(p, "Hello");
return p;
}
现在,假设我想在 C# 中使用它
[DllImport("x.dll")]
public static extern string f1();
[DllImport("x.dll")]
public static extern string f2();
有没有办法告诉 CLR 对从 f2 返回的字符串而不是 f1 获得强所有权?事实是,从 f1 返回的字符串最终将被 GC 释放、删除或其他任何事情,而从 f2 返回的字符串不会。希望问题很清楚。提前致谢
【问题讨论】:
标签: c# c++ interop marshalling