我以为问题出在我的 C++ 函数中,但我试过了
C++ dll 中的 C++ 函数:
bool __declspec( dllexport ) OpenA(std::string file)
{
return true;
}
C#代码:
[DllImport("pk2.dll")]
public static extern bool OpenA(string path);
if (OpenA(@"E:\asdasd\"))
我得到一个内存损坏的异常,为什么?
如果我删除 std::string 参数,它工作得很好,但是对于 std::string 它不起作用。
最佳答案
std::string 和 c# string 彼此不兼容。据我所知,就互操作而言,c# 字符串对应于在 c++ 中传递 char*
或 wchar_t*
。
原因之一是 std::string 可以有许多不同的实现,而 c# 不能假定您使用的是任何特定的实现。
关于c# - C# 中的 std::string?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/874551/