我有一个C++ DLL具有返回一个C字符串的函数,我有一个C#程序调用此函数并将数据返回给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...
当我看着当地人它说变量“嗒嗒”等于“”。
数据发生了什么?
它与'stdString'一起被破坏。 – lapk
C#的'string'不等于'const char *'。 –
我会用什么来返回到C#字符串? – tr0yspradling