C# 中的 std::string ? [英] std::string in C#?
问题描述
我认为问题出在我的 C++ 函数中,但我尝试了这个
I thought the problem is inside my C++ function,but I tried this
C++ dll 中的 C++ 函数:
C++ Function in C++ dll:
bool __declspec( dllexport ) OpenA(std::string file)
{
return true;
}
C# 代码:
[DllImport("pk2.dll")]
public static extern bool OpenA(string path);
if (OpenA(@"E:asdasd"))
我收到内存损坏的异常,为什么?
I get an exception that the memory is corrupt,why?
如果我删除 std::string 参数,它工作得很好,但使用 std::string 它不起作用.
If I remove the std::string parameter,it works great,but with std::string it doesnt work.
推荐答案
std::string 和 c# string 不兼容.据我所知,就互操作而言,c# 字符串对应于在 c++ 中传递 char*
或 wchar_t*
.
原因之一是 std::string 可以有许多不同的实现,而 c# 不能假设您正在使用任何特定的实现.
std::string and c# string are not compatible with each other. As far as I know the c# string corresponds to passing char*
or wchar_t*
in c++ as far as interop is concerned.
One of the reasons for this is that There can be many different implementations to std::string and c# can't assume that you're using any particular one.
这篇关于C# 中的 std::string ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!