我有一個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