C# 调用C++ 代码 封装的DLL 的问题
本人现在尝试封装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#的类型对应关系有问题,请大家帮我看一下,谢谢!