c# - PInvoke ReadFile kernel32: Attempted to read or write protected memory. This is often an indication that other memory is corrupt -
i'm trying make kernel32's readfile work since native c# alternatives slow as...
the following code working couple of times, gives "attempted read or write protected memory. indication other memory corrupt."
i figured restart of server make work couple times before getting same error, seems wrong there, happens no matter now..
here's code:
using system.runtime.interopservices; private winfileio.winfileio _winfile; _winfile = new winfileio.winfileio(new byte[buffersize]); _winfile.openforreading(_fileinfo.fullname); public winfileio(array buffer) { pinbuffer(buffer); phandleread = intptr.zero; phandlewrite = intptr.zero; buffersize = buffer.getlength(0); } private void pinbuffer(array buffer) { unpinbuffer(); gchbuf = gchandle.alloc(buffer, gchandletype.pinned); intptr paddr = marshal.unsafeaddrofpinnedarrayelement(buffer, 0); // pbuffer pointer used of i/o functions in class. pbuffer = (void*)paddr.topointer(); } public void unpinbuffer() { if (gchbuf.isallocated) gchbuf.free(); } [system.runtime.interopservices.dllimport("kernel32", setlasterror = true)] static extern unsafe system.intptr createfile ( string filename, // file name uint desiredaccess, // access mode uint sharemode, // share mode uint securityattributes, // security attributes uint creationdisposition, // how create uint flagsandattributes, // file attributes int htemplatefile // handle template file ); [system.runtime.interopservices.dllimport("kernel32", setlasterror = true)] private unsafe static extern bool readfile ( int hfile, byte[] arraypbuffer, int numberofbytestoread, ref int lpnumberofbytestoread, int* ptr ); [system.runtime.interopservices.dllimport("kernel32", setlasterror = true)] static extern unsafe bool setfilepointer ( system.intptr hobject, int ldistancetomove, ref int lpdistancetomovehigh, emovemethod dwmovemethod ); public void openforreading(string filename) { close(true, false); phandleread = createfile(filename, 3, 3, 0, open_existing, 0, 0); if (phandleread == system.intptr.zero) { win32exception = new win32exception(); applicationexception ae = new applicationexception("winfileio:openforreading - not open file " + filename + " - " + we.message); throw ae; } } public void moveoffset(int movedistance, emovemethod movemethod) { if (phandleread != system.intptr.zero) { int movedistancehigh = 0; setfilepointer(phandleread, movedistance, ref movedistancehigh, movemethod); } } public unsafe tuple<int, byte[]> readchunk(int buffersize, int offset) { int bytesread = 0; byte[] bufbytes = new byte[buffersize]; moveoffset(offset, emovemethod.begin); int pointer = (int)marshal.ptrtostructure(phandleread, typeof(int)); if (readfile(pointer, bufbytes, buffersize, ref bytesread, null)) { byte[] outbytes = new byte[bytesread]; array.copy(bufbytes, outbytes, bytesread); return new tuple<int, byte[]>(bytesread, outbytes); } return null; }
i think that's relevant code.
i'm guessing has createfile , readfile-signatures not being compatible (intptr vs. int), marshal.ptrtostructure intptr not pointing should, or memory not being freed or something? fact didn't work couple tries after reboot confuses me though.
anyone spot obvious or have suggestions can into?
thanks
edit: might notice, kind of mishmash of different approaches, i'm not using pinned buffer anymore struggled contents of buffer read want to.
edit2: stacktrace says problem:
at system.runtime.interopservices.marshal.ptrtostructure(intptr ptr, type structuretype)
your pinvoke declarations bad, ones pinvoke.net
the 1st argument of readfile() handle, 1 got createfile(). intptr. dug hole trying convert intptr int, marshal.ptrtostructure() call not correct. , bomb ave, handle not pointer.
fix [dllimport] declaration , use intptr got createfile() directly. , don't forget system.io.filestream .net wrapper these winapi functions, ever need pinvoke createfile() if need open handle device instead of file. still use filestream constructor takes handle , use read() method call readfile().
Comments
Post a Comment