C#中的byte[]对应C++中的什么,
public static extern bool Read(Byte[] InBuf, int InBufSize, Byte[] OutBuf, int OutBufSize, uint ReCVLen);C++ dll;bool Read(BYTE* InBuf,int InBufSize,BYTE OutBuf,int OutBufSize,DWORD ReCVLen)
这样子编译没问题,运行后说尝试读取或写入受保护的内存。这通常指示其他内存已损坏。是不是参数写错了,该怎么写? 反正在vc中写read(inbuf,inbufsize,outbuf,outbufsize,&recvlen)
inbuf 和outbuf是byte数组, 在vc中是正确的我用过, 所有想做成dll,在c#中调用..
public static extern bool Read(Byte[] InBuf, int InBufSize, Byte[] OutBuf, int OutBufSize, uint ReCVLen);C++ dll;bool Read(BYTE* InBuf,int InBufSize,BYTE OutBuf,int OutBufSize,DWORD ReCVLen)
这样子编译没问题,运行后说尝试读取或写入受保护的内存。这通常指示其他内存已损坏。是不是参数写错了,该怎么写? 反正在vc中写read(inbuf,inbufsize,outbuf,outbufsize,&recvlen)
inbuf 和outbuf是byte数组, 在vc中是正确的我用过, 所有想做成dll,在c#中调用..
public static extern bool Read(Byte[] InBuf, int InBufSize, out Byte[] OutBuf, out int OutBufSize, uint ReCVLen);
public static extern bool Read(Byte[] InBuf, int InBufSize, out Byte[] OutBuf, out int OutBufSize,out uint ReCVLen);
看花了,不好意思 有out了
第三个参数是不是写错了?
恩,我第3个写byte*
是不是像你这样写就对了?
c#是type【】XXX;
而c++ 是type XXX【】;
这么写是对的,不用什么ref和out.
出错可能是因为你分配的内存不够
bool Read(IntPtr InBuf, int InBufSize, IntPtr OutBuf, int OutBufSize, uint32 RecvLen);
都是一个字节。
[DllImport("XLow.dll", EntryPoint = "Read")] public static extern bool Read(Byte[] InBuf, int InBufSize, out Byte[] OutBuf, int OutBufSize, uint ReCVLen);调用:Read(InBuf, 6, out OutBuf,13 , RecvLen))
bool Read(Byte[] InBuf, int InBufSize, Byte[] OutBuf, int OutBufSize, uint ReCVLen);c++
bool Read(Char[] InBuf, int InBufsize, Char[] OutBuf, int OutBufSize, unsigned int ReCVLen);
bool Read(Char[] InBuf, int InBufsize, Char[] OutBuf, int OutBufSize, unsigned int ReCVLen); error C2146: syntax error : missing ')' before identifier 'InBuf'error C2146: syntax error : missing ';' before identifier 'InBuf'fatal error C1004: unexpected end of file found
{
// DWORD tt= pXLow->readx(InBuf,InBufSize,OutBuf,OutBufSize,&RecvLen);
// return true;
}注释掉就没异常了。
所以想写DLL2,在DLL2中调用DLL1,然后我在C#中调用DLL2.
这样可以么?关键就用到DLL1中的一个函数Read(要写入数据的缓存,写入的长度,读出数据的缓存,读出的长度,实际返回的长度);所以在DLL2中调用Read,最后在C#中调用DLL2,你说,数据会返回在OutBuf数组中吗?
bool Read(BYTE* InBuf,int InBufSize,BYTE* OutBuf,int OutBufSize,DWORD* ReCVLen)
public static extern bool Read(Byte[] InBuf, int InBufSize, Byte[] OutBuf, int OutBufSize, ref uint ReCVLen);记住,调用前OutBuf要定义得足够大