我想用C#调用CH375芯片官方提供的C++动态连接库,其主要的两个函数定义如下:BOOL WINAPI CH375ReadData(  // 读取数据块
ULONG iIndex,         // 指定CH375设备序号
PVOID oBuffer,        // 指向一个足够大的缓冲区,用于保存读取的数据
PULONG ioLength );     // 指向长度单元,输入时为准备读取的长度,返回后为实际读取的长度BOOL WINAPI CH375WriteData( // 写出数据块
ULONG iIndex,         // 指定CH375设备序号
PVOID iBuffer,        // 指向一个缓冲区,放置准备写出的数据
PULONG ioLength );     // 指向长度单元,输入时为准备写出的长度,返回后为实际写出的长度我在网上找了相关的资料,知道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: 能否给个简单的列子,如:发送一段字符串指令望哪位朋友帮帮忙,这问题捆扰很长时间了,先谢谢了!!!

解决方案 »

  1.   

    [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); //写单片机缓存oBuffer和iBuffer传入之前,请事先在托管这一级开辟好内存.至于想写string,可把string转成byte[]再写.string msg = "hello world";
    byte[] buffer = Encoding.Default.GetBytes(msg);
      

  2.   

    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) ;第二个问题得看看那个dll的接口说明,或者是dll的demo测试程序。
    像我一般拿到这个类,都附带接口说明,以及test程序的
      

  3.   

    支持2楼的做法。
    int iLen = 2048;
    IntPtr pointer = Marshal.AllocHGlobal(iLen);
    CH375ReadData(iIndex, pointer, ref iLen) ;
      

  4.   

    使用完了记得释放内存:Marshal.FreeHGlobal(pointer);
      

  5.   

    倒也没有必要AllocHGlobal和FreeHGlobal,可以直接传byte[]。
    不过1楼bloodish朋友的代码中ref ulong要改为ref uint。
    uint index = ...;                      //uint
    uint length = 1024;                    //uint
    byte[] buffer = new byte[length];CH375ReadData(index, buffer, ref length);