Click here to Skip to main content
15,888,259 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to Write to my flash memory,, I've read the firts 512 bytes, modified that array changing some bytes for others and then using WriteFile API function, wrote (or tried) again those 512 first bytes into my memory again.. this WriteFile API function return 1 , after that I use CloseHandle API function closing the handle I used to interact with the memory...but, when I see my memory using WinHex(hex editor) the bytes there are the same before my program tries to write. why is this if I am even closing the handle and after close it my buffer(array of bytes read from my memory) seems modified.

The number of bytes written are actually 512.. this is my code ... this is just testing code.

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 cantleidos = 0;
    try
    {      
   bool retor = SetFilePointerEx(ptrFile, 5, IntPtr.Zero, MoveMethod.FILE_BEGIN);
     bool b = ReadFile(ptrFile, buffer,512 , out cantleidos, IntPtr.Zero);
    }
    catch (Exception)
    {
   throw new Win32Exception(Marshal.GetLastWin32Error());
    }
      Buffer.BlockCopy(System.Text.Encoding.UTF8.GetBytes("SOMETEXT".ToCharArray()), 0, buffer,3,"SOMETEXT".ToCharArray().Length);
     
  uint wrote = 0;
  bool c = WriteFile(ptrFile, buffer, (uint)buffer.Length, ref wrote, IntPtr.Zero);
    int closed = CloseHandle(ptrFile);
Posted
Updated 8-Jul-11 8:07am
v2

My first guess would be that there is a cache involved somewhere that you may not be aware of.

Did you try reading the data back using ReadFile instead of WinHex?

If ReadFile gives you back the data you expect, but WinHex doesn't, then you are probably doing things correctly

Is WinHex reading the actual flash directly or is it re-reading a cached value?

If WinHex is reading the flash directly, then maybe the write is actually being cached by the operating system (a fairly typical thing) and even though you've written the data and closed the handle, the OS hasn't gotten around to actually writing it out yet. (The OS will do the right thing and give you the cached value if you ReadFile, but it won't if WinHex is directly accessing the sectors.)

If that's the case, you can restart windows and it will write out the cache before it exits and when you restart you'll see the changes with WinHex. That's a drastic way of forcing it to write through to the flash, but it's a good test to see if that's what's actually happening. If that's the case, then you can poke around and see if there is a cache setting somewhere or something you can do to force a write through (assuming you need to do that).
 
Share this answer
 
Comments
glbrt.gds 8-Jul-11 17:10pm    
I restarted my computer and WinHex show the same, my code is not changing my memory... I've not proved reading after write.. I'm gonna do ..
Thanks for your comment..
Take a look at the CreateFile documentation:

http://msdn.microsoft.com/en-us/library/aa363858(v=vs.85).aspx[^]

In particular look at the FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH flags which you probably want to use.

Also look at the sections on caching behavior and physical devices and volumes.

If caching isn't the issue, it may be a permission thing. In particular, CreateFile references:

http://support.microsoft.com/kb/942448[^]

which lists a number of conditions that have to be met on Vista (or later) in order to be able to write to a physical device/volume.

I don't know if what you are doing satisfies those conditions. And it isn't clear what happens if these conditions aren't met. I would have expected CreateFile or WriteFile to fail and return an error. But they might also just fail silently.
 
Share this answer
 
Comments
glbrt.gds 11-Jul-11 14:36pm    
Thanks everybody,i've other task to do , but when i finish i'll begin again with my memory io project...
TRK3 11-Jul-11 14:38pm    
Good luck. Post a comment when you get back to it and finally figure out what the problem was -- I'd like to know what it turns out to be.

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