【发布时间】:2012-01-14 14:54:13
【问题描述】:
我有一个 C++ dll,它有一个返回 c 字符串的函数,我有一个 C# 程序调用此函数并将数据返回到 C# 字符串。这就是我的意思
__declspec(dllexport) const char* function (const char* str) {
std::string stdString( str );
std::cout << stdString.c_str() << std::endl; // this prints fine, no data loss
return stdString.c_str();
}
这是 C# 代码
[DllImport("MyDLL.dll")]
public static extern string function(string data);
string blah = function("blah");
Console.WriteLine(blah); // doesn't print anything...
当我查看当地人时,它说变量 'blah' 等于 ""。
数据发生了什么变化?
【问题讨论】:
-
和
stdString一起被销毁了。 -
C# 的
string不等于const char*。 -
我将使用什么来返回 C# 字符串?