C# 调用C++DLL的问题“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”

2014-09-01 17:10匿名  分类:C/C++ | 浏览 697 次  悬赏:15
 C#调用C++ 的DLL
错误为:“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”

[DllImport("dmailnewclient.dll", EntryPoint = "?dmail_create@@YAHAAIAAUehInformation@@AAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z", CharSet = CharSet.Ansi,SetLastError = true, CallingConvention = CallingConvention.StdCall)]
public static extern int Dmail_Creat(uint handle, ehInformation ehInfor, string errdesc, UInt16 flag = 0);
调用:
string errdesc ="111";
int temp = dmailnewclient.Dmail_Creat(t,ehinfo, errdesc);
两边string不是同一个类型,不能通用。

C#无法正确使用C++的string类型。
C++那边应该改为const char*或者const wchar_t*
如果用前者,C#那边CharSet要改为ANSI
posted on 2015-05-18 15:33  梅罗  阅读(392)  评论(0编辑  收藏  举报