问题很简单,想从托管 C# 代码中读取一个字符串到我的非托管 C++ 代码中WCHAR* []
。C函数是:
extern "C" __declspec(dllexport) int __cdecl myfunc(int argc, WCHAR* argv[])
在 C# 中我导入了 DLL:
[DllImport("mydll.dll", CharSet = CharSet.Auto, SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int myfunc(int argc, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder str);
我跑了,但是当我尝试读取 C++ 代码中的字符串时,我得到了AccessViolationException : Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
执行此操作的正确方法是什么,反之亦然(即,将字符串从非托管 C++ 传递到 C# 托管代码)?
帮助赞赏。谢谢