C# 调用C++导出Dll 参数问题。

北风大王 2020-04-21 11:09:41
如题,最近项目做一个调用C++方法实现功能,遇到其中一个方法参数问题一直没法解决。方法如下
ZCOMMON_EXPORT int __stdcall Send2comAndRead( int  tinyCmdType, const char* pData, char *pOutData, int iOutLen, int iOutTimeMillions);

C# 代码中调用如下
        [DllImport(dllName, EntryPoint = "Send2comAndRead", ExactSpelling = false, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern int Send2comAndRead(int tinyCmdType, string info, ref string ReceiveMsg, int len, int outTimes);

在调用时出现错误,尝试读取或写入受保护内存。求大神解答下这里的参数需怎么处理。


...全文
59 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
北风大王 2020-04-21
  • 打赏
  • 举报
回复
改了一下,用数组接收了。
        [DllImport(dllName, EntryPoint = "Send2comAndRead", ExactSpelling = false, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern int Send2comAndRead(int tinyCmdType, string info, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] byte[] Msgs, int len, int outTimes);
qq_36786068 2020-04-21
  • 打赏
  • 举报
回复
const char* pData,-----------------------string char *pOutData-------------------------stringBuild --直接stringbuild即可,不用加ref

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧