Keeping changes after WriteFile, why it doesn' happen

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gilberto Gil
    New Member
    • Jun 2011
    • 14

    Keeping changes after WriteFile, why it doesn' happen

    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.
    My code:
    [code=c#]

    SafeFileHandle ptrFile = CreateFile("\\\ \.\\u:", DesiredAccess.G ENERIC_READ | DesiredAccess.G ENERIC_WRITE, ShareMode.FILE_ SHARE_READ_AND_ WRITE, IntPtr.Zero, CreationDisposi tion.OPEN_EXIST ING, FlagsAndAttribu tes.FILE_ATTRIB UTE_NORMAL, IntPtr.Zero);
    byte[] buffer = new byte[512];
    uint cantleidos = 0;

    try
    {
    bool retor = SetFilePointerE x(ptrFile, 5, IntPtr.Zero, MoveMethod.FILE _BEGIN);
    bool b = ReadFile(ptrFil e, buffer,512 , out cantleidos, IntPtr.Zero);
    }
    catch (Exception)
    {
    throw new Win32Exception( Marshal.GetLast Win32Error());
    }

    Buffer.BlockCop y(System.Text.E ncoding.UTF8.Ge tBytes("SOMETEX T".ToCharArray( )), 0, buffer,3,"SOMET EXT".ToCharArra y().Length);

    uint wrote = 0;
    bool c = WriteFile(ptrFi le, buffer, (uint)buffer.Le ngth, ref wrote, IntPtr.Zero);
    int closed = CloseHandle(ptr File);
    [/code]
    Last edited by Plater; Jul 11 '11, 01:53 PM. Reason: code tags
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You probably need to do some sort of FLUSH mechanism?

    Comment

    • Christian Binder
      Recognized Expert New Member
      • Jan 2008
      • 218

      #3
      Is the content of wrote-variable after line 19 correct? (should be 512, the same as buffer.Length)

      you could print the buffer using string.Join("," , buffer), comparing it with the results in your hex-editor (maybe the file's bytes are already changed ... just a thought)

      you could also try to use a file on your local hard drive to see if their is a difference.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        wrote is passed by reference, meaning the function is changing it. It would be worthwhile to check the results of that value and value of c

        Comment

        Working...