Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Comments
Andy411 9-Dec-14 3:49am    
Are you sure, that buf is 16 bit long? I would rather expect a byte array as buffer. Same with the first LPVOID parameter. Realy long or better an array of type byte?
Also I guess that buff has to be an array, so short[] buf instead of short buf.
And maybe you should set "enable unsafe code" in the build properties of your project.

1 solution

I have enabled allow unsafe. and I have tried to change buf to short[], but the result is same.
thanks for your reply
 
Share this answer
 
Comments
TheRealSteveJudge 9-Dec-14 10:07am    
Thou shalt not answer your own questions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900