2
Answers

lib wrapped into dll imported function access memory error

Sunny Wang

Sunny Wang

9y
656
1
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.

Answers (2)