Click here to Skip to main content
15,888,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello again...I've a problems trying to move the pointer forward.. I'm trying to read my flash memory as array of bytes... Using SetFilePointerEx API function I've already did it, but, the problems is that it always read begening at 0 byte, I can't begin with offset of 512 bytes for example... why is this.....Note: it works pretty well seeking a file like .txt file. Some help.. please....I really need some help..

My code:

C#
SafeFileHandle ptrFile = CreateFile("\\\\.\\u:", DesiredAccess.GENERIC_READ | DesiredAccess.GENERIC_WRITE, ShareMode.FILE_SHARE_READ_AND_WRITE, IntPtr.Zero, CreationDisposition.OPEN_EXISTING, FlagsAndAttributes.FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);

        byte[] buffer = new byte[512];

        uint readed = 0;
        try
        {
           bool retor = SetFilePointerEx(ptrFile, 512, IntPtr.Zero,      MoveMethod.FILE_BEGIN);
        }
        catch (Exception)
        {
           throw new Win32Exception(Marshal.GetLastWin32Error());
        }

        bool b = ReadFile(ptrFile, buffer,512 , out readed, IntPtr.Zero);
Posted
Updated 5-Jul-11 6:39am
v2

1 solution

In the above code what is the value of retor when you return from the SetFilePointerEx() function? If it is false then you need to call GetLastError() to find out why.

You must check your return codes to see if your function calls succeed.
 
Share this answer
 
Comments
glbrt.gds 6-Jul-11 13:16pm    
Richard......... thanks for comment..someone in this forum http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/cb8364ad-46ec-4af5-8630-36804d7de473 helped me up
Richard MacCutchan 6-Jul-11 13:45pm    
Interesting, but I cannot quite see what is different. Maybe you could post your working version here as I would be interested in checking it for myself.

glbrt.gds 7-Jul-11 14:19pm    
Richard my code is really the same but when i was trying read i was using a lot of numbers(bytes) to read(testing) not a multiple of 512 bytes............
My problem now is writing into the memory. for example: if i want to write a number of bytes begining at the position 43 (offset), how could I do this when I only can move the pointer(using SetFilePointer function) a multiple of 512 bytes......
Richard MacCutchan 7-Jul-11 15:19pm    
I don't think you can do that. You are accessing the data at the device level and are constrained by the sector size of 512, so you must read and write in multiples of 512 bytes in all cases, as far as I am aware.
If you wish to write odd numbers of bytes then you will need to add code to read the relevant sector(s), modify the data and write the sector(s) back to the device.

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