c#如何调用C++编写的动态连接库(dll)?
我想用C#调用CH375芯片官方提供的C++动态连接库,其主要的两个函数定义如下:
BOOLWINAPICH375ReadData( // 读取数据块
ULONGiIndex, // 指定CH375设备序号
PVOIDoBuffer, // 指向一个足够大的缓冲区,用于保存读取的数据
PULONGioLength ); // 指向长度单元,输入时为准备读取的长度,返回后为实际读取的长度
BOOLWINAPICH375WriteData( // 写出数据块
ULONGiIndex, // 指定CH375设备序号
PVOIDiBuffer, // 指向一个缓冲区,放置准备写出的数据
PULONGioLength ); // 指向长度单元,输入时为准备写出的长度,返回后为实际写出的长度
我在网上找了相关的资料,知道C#调用C++动态连接库方法
[DllImport("CH375DLL.DLL", EntryPoint = "CH375ReadData", ExactSpelling = false, SetLastError = true)]
public static extern bool CH375ReadData(?, ?, ?);//读单片机缓存
[DllImport("CH375DLL.DLL", EntryPoint = "CH375WriteData", ExactSpelling = false, SetLastError = true)]
public static extern bool CH375WriteData(?, ?, ?); //写单片机缓存
问题1: C++中函数中的这些参数, 我在C#中以什么参数类型与其对应?
问题2: 能否给个简单的列子,如:发送一段字符串指令
望哪位朋友帮帮忙,这问题捆扰很长时间了,先谢谢了!!!
[解决办法]
[DllImport("CH375DLL.DLL", EntryPoint = "CH375ReadData", ExactSpelling = false, SetLastError = true)]
public static extern bool CH375ReadData(ulong iIndex, byte[] oBuffer, ref ulong ioLength);//读单片机缓存
[DllImport("CH375DLL.DLL", EntryPoint = "CH375WriteData", ExactSpelling = false, SetLastError = true)]
public static extern bool CH375WriteData(ulong iIndex, byte[] iBuffer, ref ulong ioLength); //写单片机缓存
string msg = "hello world";
byte[] buffer = Encoding.Default.GetBytes(msg);
public static extern bool CH375ReadData(uint iIndex, System.IntPtr oBuffer, ref uint ioLength) ;
public static extern bool CH375WriteData(uint iIndex, System.IntPtr iBuffer, ref uint ioLength) ;
uint index = ...; //uint
uint length = 1024; //uint
byte[] buffer = new byte[length];
CH375ReadData(index, buffer, ref length);