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:
Hellow... I've read the post at http://buiba.blogspot.com/2009/06/using-winapi-createfile-readfile.html[^] and I'm trying to read some bytes from my flash memory.. why it doesn't work.. what am i doing wrong?, it gives no errors just my buffer after ReadFile is empty my code is: a lot of thanks.........in advance.

 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);

uint size = GetFileSize(ptrFile,IntPtr.Zero);
byte[] buffer = new byte[512];
uint readed = 0;
uint a = SetFilePointer(ptrFile, 0, IntPtr.Zero, 0);
ReadFile(ptrFile, buffer,512 , out readed, IntPtr.Zero);


/* ---------------------------------------------------------
 * WINAPI STUFF
 * ------------------------------------------------------ */
        private void ThrowLastWin32Err()
        {
            Marshal.ThrowExceptionForHR(
             Marshal.GetHRForLastWin32Error());
        }
        [Flags]
        public enum DesiredAccess : uint
        {
            GENERIC_READ = 0x80000000,
            GENERIC_WRITE =0x40000000
        }
        [Flags]
        public enum ShareMode : uint
        {
            FILE_SHARE_NONE = 0x0,
            FILE_SHARE_READ = 0x1,
            FILE_SHARE_WRITE = 0x2,
            FILE_SHARE_READ_AND_WRITE = 0x3,            
            FILE_SHARE_DELETE = 0x4,
        }
        public enum MoveMethod : uint
        {
            FILE_BEGIN = 0,
            FILE_CURRENT = 1,
            FILE_END = 2
        }
        public enum CreationDisposition : uint
        {
            CREATE_NEW = 1,
            CREATE_ALWAYS = 2,
            OPEN_EXISTING = 3,
            OPEN_ALWAYS = 4,
            TRUNCATE_EXSTING = 5
        }
        [Flags]
        public enum FlagsAndAttributes : uint
        {
            FILE_ATTRIBUTES_ARCHIVE = 0x20,
            FILE_ATTRIBUTE_HIDDEN = 0x2,
            FILE_ATTRIBUTE_NORMAL = 0x80,
            FILE_ATTRIBUTE_OFFLINE = 0x1000,
            FILE_ATTRIBUTE_READONLY = 0x1,
            FILE_ATTRIBUTE_SYSTEM = 0x4,
            FILE_ATTRIBUTE_TEMPORARY = 0x100,
            FILE_FLAG_WRITE_THROUGH = 0x80000000,
            FILE_FLAG_OVERLAPPED = 0x40000000,
            FILE_FLAG_NO_BUFFERING = 0x20000000,
            FILE_FLAG_RANDOM_ACCESS = 0x10000000,
            FILE_FLAG_SEQUENTIAL_SCAN = 0x8000000,
            FILE_FLAG_DELETE_ON = 0x4000000,
            FILE_FLAG_POSIX_SEMANTICS = 0x1000000,
            FILE_FLAG_OPEN_REPARSE_POINT = 0x200000,
            FILE_FLAG_OPEN_NO_CALL = 0x100000
        }
        public const uint INVALID_HANDLE_VALUE = 0xFFFFFFFF;
        public const uint INVALID_SET_FILE_POINTER = 0xFFFFFFFF;

        // Use interop to call the CreateFile function.
        // For more information about CreateFile,
        // see the unmanaged MSDN reference library.
        [DllImport("kernel32.dll", SetLastError = true)]
        internal static extern SafeFileHandle CreateFile(
         string lpFileName,
         DesiredAccess dwDesiredAccess,
         ShareMode dwShareMode,
         IntPtr lpSecurityAttributes,
         CreationDisposition dwCreationDisposition,
         FlagsAndAttributes dwFlagsAndAttributes,
         IntPtr hTemplateFile);
        [DllImport("kernel32", SetLastError = true)]
        internal static extern bool ReadFile(
         SafeFileHandle hFile,
         Byte[] aBuffer,
         UInt32 cbToRead,
         out UInt32 cbThatWereRead,
         IntPtr pOverlapped);
Posted
Updated 29-Jun-11 4:29am
v4
Comments
Yuri Vital 29-Jun-11 10:34am    
What is the value of "readed" after ReadFiles ?
Why not read all file, you not use "size" var ?
Why not using "a" var ?
uint a = SetFilePointer(ptrFile, 0, IntPtr.Zero, 0); ?
glbrt.gds 1-Jul-11 14:17pm    
Yuri.. thanks for your post..i was just making some tests

it gives no errors
And yet your code is not testing for errors, but just assuming that every call succeeds. You should check the return values from each call to check whether it succeeded or not. For example, are you sure that you got a valid handle from CreateFile()?
 
Share this answer
 
Comments
glbrt.gds 30-Jun-11 8:56am    
Hello Richard..thanks for your comment....
How can i be sure of that? i've prove the same code with CreateFile but using "\\\\.\\A:" instead of "\\\\.\\u:" and I could see my floppy drive responding(physically)....drive u: is my flash memory plugged in an USB port.
Otherwise...the call to the GetFileSize funcion always return the same number value no matter if I'm calling CreateFile with "\\\\.\\u:" or "\\\\.\\A:"
Richard MacCutchan 30-Jun-11 9:44am    
I could see my floppy drive responding
That is not relevant to the issue. You must check the return codes from your API calls to see whether they succeeded or not. I suggest you run this code through the debugger and check the return values at each step to see what is happening.
glbrt.gds 30-Jun-11 10:32am    
Richard MacCutchan.......it's working !!!.. I checked it out and it is already working. thanks a lot..
Richard MacCutchan 30-Jun-11 12:01pm    
Congratulations; if there is a fix that would help other people maybe you code post it as a note into your question.
glbrt.gds 30-Jun-11 14:05pm    
Richard.. I just deleted the " uint size = GetFileSize(ptrFile,IntPtr.Zero);" line and wala..my buffer got filled like a glass of water....
but I've another problem, I'm trying to read some things like the number of bytes per cluster and others from File Allocation Table in my memory.. the problem is that I don't know how to convert the bytes that represent this nomber into decimal...how can i do this..
Unfortunately this is not as easy as it could be; if you take each byte by itself you can easily see what value it has, but if you take a group of bytes as a single number then you have to switch the order owing to the biglittle-endian format of words and double words in the intel architecture. For example
BYTE array[6] = { 12, 31, 44, 125, 7, 99 };
// is equivalent to
BYTE array[6] = { 0x0C, 0x1F, 0x2C, 0x7D, 0x07, 0x63 };
// so what is the decimal value of the last four bytes as an integer
// to make integer we need to reorder thus
int array2to5 = 0x63077D2C;
// which is the same as
int array2to5 = 1661435180;
// we can make this easier by using the printf() function like this:
printf("value = %d\n", *(int*)(array+2))


Apologies for getting my big and little ends the wrong way round.
 
Share this answer
 
v2
Comments
glbrt.gds 1-Jul-11 16:18pm    
but if the flash is formated using FAT32 it use little endian... I'm using c# not C..

My problem now is using SetFilePointer API function.. trying to move the pointer more than 2 gigabytes.
Richard MacCutchan 2-Jul-11 4:08am    
1. Sorry, I got it wrong above, it is, of course, little endian, but if you read the data as an array of bytes you will still need some method to access it as an integer. I'm not sure how we do this in C#.

2. Use SetFilePointerEx() for offsets larger than 2Gb.
glbrt.gds 5-Jul-11 12:24pm    
Hello Richar... I'm trying with SetFilePointerEx() with files it works fine but with my flash memory it doesn't move the pointer. it is always at the begining of the flash's bytes. a can't get for example an offset of 512 bytes ..it always begin with offset of 0

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