本人调用DLL中的API,调用其他按值传递单个参数时没有问题。但是传递指针时出现错误。
dll中函数声明
写函数声明:
BOOL WINAPI CH375ReadData( // 读取数据块
ULONG iIndex, // 指定CH375设备序号,必须是0
PVOID oBuffer, // 指向一个足够大的缓冲区,用于保存读取的数据
PULONG ioLength ) // 指向长度单元,输入时为准备读取的长度,返回后为实际读取的长度
读函数声明:
BOOL WINAPI CH375WriteData( // 写出数据块
ULONG iIndex, // 指定CH375设备序号,必须是0
PVOID iBuffer, // 指向一个缓冲区,放置准备写出的数据
PULONG ioLength )
我使用的情况:[DllImport("CH375DLL.DLL", EntryPoint = "CH375ReadData",ExactSpelling=false, SetLastError=true)]
public static extern bool CH375ReadData(Int16 iIndex, [MarshalAs(UnmanagedType.LPArray)] byte[] oBuffer, [MarshalAs(UnmanagedType.LPArray)] Int32[] ioLength);
[DllImport("CH375DLL.DLL", EntryPoint = "CH375WriteData", ExactSpelling = false, SetLastError = true)]
public static extern bool CH375WriteData(Int16 iIndex, [MarshalAs(UnmanagedType.LPArray)]byte[] iBuffer, [MarshalAs(UnmanagedType.LPArray)] Int32[] ioLength);
调用:byte[] sendBuffer=new byte[64]; //USB发送缓冲区
byte[] receiveBuffer=new byte[64]; //USB接收缓冲区Int32[] len = new Int32[1];
len[0] = 2;
sendBuffer[0] = 6;
CH375WriteData(0,sendBuffer,len);
CH375ReadData(0,receiveBuffer,len);结果发现接收缓冲区无法得到数据。初步断定是数据封装的问题。但是不知道怎么改。希望有人指点。多谢!
dll中函数声明
写函数声明:
BOOL WINAPI CH375ReadData( // 读取数据块
ULONG iIndex, // 指定CH375设备序号,必须是0
PVOID oBuffer, // 指向一个足够大的缓冲区,用于保存读取的数据
PULONG ioLength ) // 指向长度单元,输入时为准备读取的长度,返回后为实际读取的长度
读函数声明:
BOOL WINAPI CH375WriteData( // 写出数据块
ULONG iIndex, // 指定CH375设备序号,必须是0
PVOID iBuffer, // 指向一个缓冲区,放置准备写出的数据
PULONG ioLength )
我使用的情况:[DllImport("CH375DLL.DLL", EntryPoint = "CH375ReadData",ExactSpelling=false, SetLastError=true)]
public static extern bool CH375ReadData(Int16 iIndex, [MarshalAs(UnmanagedType.LPArray)] byte[] oBuffer, [MarshalAs(UnmanagedType.LPArray)] Int32[] ioLength);
[DllImport("CH375DLL.DLL", EntryPoint = "CH375WriteData", ExactSpelling = false, SetLastError = true)]
public static extern bool CH375WriteData(Int16 iIndex, [MarshalAs(UnmanagedType.LPArray)]byte[] iBuffer, [MarshalAs(UnmanagedType.LPArray)] Int32[] ioLength);
调用:byte[] sendBuffer=new byte[64]; //USB发送缓冲区
byte[] receiveBuffer=new byte[64]; //USB接收缓冲区Int32[] len = new Int32[1];
len[0] = 2;
sendBuffer[0] = 6;
CH375WriteData(0,sendBuffer,len);
CH375ReadData(0,receiveBuffer,len);结果发现接收缓冲区无法得到数据。初步断定是数据封装的问题。但是不知道怎么改。希望有人指点。多谢!
public static extern bool CH375ReadData(uint iIndex, [Out, MarshalAs(UnmanagedType.LPArray)] byte[] oBuffer, ref uint ioLength );
[DllImport("CH375DLL.DLL", EntryPoint = "CH375ReadData",ExactSpelling=false, SetLastError=true)]
public static extern bool CH375ReadData(Int16 iIndex, System.IntPtr buffer, ref ulong length );
[DllImport("CH375DLL.DLL", EntryPoint = "CH375WriteData", ExactSpelling = false, SetLastError = true)]
public static extern bool CH375WriteData(Int16 iIndex, [MarshalAs(UnmanagedType.LPArray)]byte[] iBuffer, [MarshalAs(UnmanagedType.LPArray)] Int32[] ioLength);
调用:byte[] sendBuffer=new byte[64]; //USB发送缓冲区
byte[] receiveBuffer=new byte[64]; //USB接收缓冲区ulong len = 2;
sendBuffer[0] = 6;
System.IntPtr buffer = System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement( receiveBuffer ,0 );
CH375WriteData(0,sendBuffer,len);
CH375ReadData(0,receiveBuffer, ref len);
public static extern bool CH375ReadData(ref uint iIndex, [Out, MarshalAs(UnmanagedType.LPArray)] byte[] oBuffer, out uint ioLength );
我试了你的方案。
但是有好几处都是实参和形参类型不一致,没办法通过编译。
请检查一下,多谢!
又试过之后,原来能写进去数,现在写不进去了。
总读不出数据。真是郁闷
我做的是用上位机通过USB,MCU控制一个继电器。写入数据后继电器发出响声,说明数据已经写入了。但是读取采集到的数据时,缓冲区里为空。
DLL是用C写的。
C下定义的ULONG应该是16位吧,对应的应该是C#下的short吧。
如何传递数组指针参数呢?
是数据封装的问题么?
public static extern bool CH375ReadData(uint iIndex, [MarshalAs(UnmanagedType.LPArray)] byte[] oBuffer, [MarshalAs(UnmanagedType.LPArray)] uint[] ioLength);
[DllImport("CH375DLL.DLL", EntryPoint = "CH375WriteData")]
public static extern bool CH375WriteData(uint iIndex, [MarshalAs(UnmanagedType.LPArray)]byte[] iBuffer, [MarshalAs(UnmanagedType.LPArray)] uint[] ioLength);
调用:byte[] sendBuffer=new byte[64]; //USB发送缓冲区
byte[] receiveBuffer=new byte[64]; //USB接收缓冲区uint[] len = new uint[1];
len[0] = 2;
sendBuffer[0] = 6;
CH375WriteData(0,sendBuffer,len);
CH375ReadData(0,receiveBuffer,len);
http://community.csdn.net/Expert/topic/4790/4790049.xml?temp=.518963
但是我这里的问题仍然没有解决。
我觉得你说的挺有道理,跟我找过的资料一致。
但是关于C和C#的类型对应,我这里有个资料。
http://msdn.microsoft.com/msdnmag/issues/03/07/NET/default.aspx?fig=true按照你上边的建议我做了修改,修改后仍然是只能写入,无法读出。关于下位机的程序我可以保证没有问题,我用别的程序试过没有问题。
希望能帮忙想想是否有办法。多谢了!
你说的这种情况令人费解,可能还有什么我没想的地方吧,我只能给你提个建议:
用vc写个程序试试,如果还不行,那只有在汇编级调试了,要调试进入dll里看看了
dll没有问题,以为我之前一直在用DELPHI编写的程序调用,没有问题。如果不加MarshalAs(UnmanagedType.LPArray,我这里根本无法写入数据。
加上之后好在能写数据了,但是读数组不行。
经过尝试,我已经将问题解决了。
主要是读数时应该给读数个数参数,这个参数和MCU设定的参数不一致,所以一直无法读数。多谢各位了。尤其是xqlez。