lib包装成dll导入的函数访问内存错误 [英] lib wrapped into dll imported function access memory error
问题描述
我有一个需要在c#中使用的lib文件,我把它包装到dll中,但是当我调用其中一个函数时,它总是显示尝试读取或写入受保护的内存的错误。
lib头文件中的函数如下所示
LONG WINAPI mdRandREx(LONG,LONG,LONG,LPVOID ,LPVOID,LONG);
在c ++中我把它包装成如下
long MmdRandREx(long path,long netno,long stno,long dev [],short * buf,long bufsize)
{
buf =(short *)malloc(bufsize);
返回mdRandREx(path,netno,stno,dev,buf,bufsize);
}
in c#I导入它
[DllImport(@dllname,CallingConvention = CallingConvention.Cdecl)]
静态extern long MmdRandREx(长路径,长netno, long stno,long [] dev,out short buf,long bufsize);
并将其称为
短buf;
长retval = MmdRandREx(路径,1,stanoPLC,dev1,out buf,bufsize);
我需要使用这个函数来读取PLC输入和另一个写PLC访问PCI卡的功能。
任何人都可以帮我这个吗?
提前谢谢。
I have a lib file need to use in c#, I have wrapped it into dll, but when I call one of the function, it always show me the error of "attempted to read or write protected memory".
the function in lib header file is as the following
LONG WINAPI mdRandREx( LONG, LONG, LONG, LPVOID, LPVOID, LONG );
In c++ I wrapped it as the following
long MmdRandREx(long path,long netno,long stno,long dev[],short *buf,long bufsize)
{
buf=(short *)malloc(bufsize);
return mdRandREx(path,netno,stno,dev,buf,bufsize);
}
in c# I import it
[DllImport(@dllname, CallingConvention = CallingConvention.Cdecl)]
static extern long MmdRandREx(long path, long netno, long stno, long[] dev, out short buf, long bufsize);
and call it
short buf;
long retval=MmdRandREx(path,1, stanoPLC, dev1,out buf, bufsize);
I need to use this function to read PLC input and another function to write PLC access a PCI card.
can any one help me on this?
thanks in advance.
推荐答案
我启用了allow unsafe。我试图将buf改为short [],但结果是一样的。
感谢您的回复
I have enabled allow unsafe. and I have tried to change buf to short[], but the result is same.
thanks for your reply
这篇关于lib包装成dll导入的函数访问内存错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!