C# 调用C++ 代码 封装的DLL 的问题

Nickiven007 2017-12-11 01:39:28
本人现在尝试封装C++代码,将生成的DLL供C#调用。但是在调试的过程中,不知道哪里出了什么问题,只要一执行到抽取的方法语句时,就会自动跳出Debug。请大家帮我看一下:

代码如下:

待封装的方法,来自方法类:

Void Func::Read (
HANDLE hDevice,
BYTE Address,
WORD BufferLength1,
BYTE *pOutBuffer1,
WORD BufferLength2,
BYTE *pInBuffer2
){......}

C++封装方法

extern "C" __declspec(dllexport) int __stdcall Read(
char Address,
short BufferLength1,
char *pOutBuffer1,
short BufferLength2,
char *pInBuffer2
);
__declspec(dllexport) int __stdcall Read(
char Address,
short BufferLength1,
char *pOutBuffer1,
short BufferLength2,
char *pInBuffer2
) {
BYTE c1 = *pOutBuffer1, c2 = *pInBuffer2;
func.Read(hDevice, Address, BufferLength1, &c1, BufferLength2, &c2);
*pOutBuffer1 = c1;
*pInBuffer2 = c2;
}

C#抽取方法 调用

[DllImport("C:\\Users\\......\\Test.dll")]
public static extern unsafe void Read(byte Address, int BufferLength1, ref byte[] inbuffer, int BufferLength2, ref byte[] outbuffer);

byte[] inBuffer = new byte[2];
byte[] OutBuffer = new byte[128];
inBuffer[0] = ((0 & 0xFF00) >> 8); // Form the start address
inBuffer[1] = (0 & 0x00FF);

Read((byte)0x50, 2, ref inBuffer, 128, ref OutBuffer);

不确定是不是C,C++,C#的类型对应关系有问题,请大家帮我看一下,谢谢!
...全文
104 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

7,765

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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